forked from NLthijs48/AreaShop
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
255 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
AreaShop/src/main/java/me/wiefferink/areashop/commands/cloud/DelCloudCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package me.wiefferink.areashop.commands.cloud; | ||
|
||
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
import me.wiefferink.areashop.MessageBridge; | ||
import me.wiefferink.areashop.commands.util.AreaShopCommandException; | ||
import me.wiefferink.areashop.commands.util.GeneralRegionParser; | ||
import me.wiefferink.areashop.commands.util.WorldSelection; | ||
import me.wiefferink.areashop.events.ask.DeletingRegionEvent; | ||
import me.wiefferink.areashop.interfaces.WorldEditInterface; | ||
import me.wiefferink.areashop.managers.IFileManager; | ||
import me.wiefferink.areashop.regions.BuyRegion; | ||
import me.wiefferink.areashop.regions.GeneralRegion; | ||
import me.wiefferink.areashop.regions.RentRegion; | ||
import me.wiefferink.areashop.tools.Utils; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.incendo.cloud.Command; | ||
import org.incendo.cloud.bean.CommandProperties; | ||
import org.incendo.cloud.context.CommandContext; | ||
import org.incendo.cloud.key.CloudKey; | ||
import org.incendo.cloud.parser.ParserDescriptor; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
@Singleton | ||
public class DelCloudCommand extends CloudCommandBean { | ||
|
||
private static final CloudKey<GeneralRegion> KEY_REGION = CloudKey.of("region", GeneralRegion.class); | ||
private final WorldEditInterface worldEditInterface; | ||
private final IFileManager fileManager; | ||
private final MessageBridge messageBridge; | ||
|
||
@Inject | ||
public DelCloudCommand( | ||
@Nonnull MessageBridge messageBridge, | ||
@Nonnull WorldEditInterface worldEditInterface, | ||
@Nonnull IFileManager fileManager | ||
) { | ||
this.messageBridge = messageBridge; | ||
this.worldEditInterface = worldEditInterface; | ||
this.fileManager = fileManager; | ||
} | ||
|
||
@Override | ||
public String stringDescription() { | ||
return "Allows you to delete regions"; | ||
} | ||
|
||
@Override | ||
protected @NonNull CommandProperties properties() { | ||
return CommandProperties.of("delete", "del"); | ||
} | ||
|
||
@Override | ||
protected Command.@NonNull Builder<? extends CommandSender> configureCommand(Command.@NonNull Builder<CommandSender> builder) { | ||
ParserDescriptor<CommandSender, GeneralRegion> regionParser = ParserDescriptor.of( | ||
new GeneralRegionParser<>(this.fileManager), | ||
GeneralRegion.class | ||
); | ||
return builder.literal("delete", "del") | ||
.optional(KEY_REGION, regionParser) | ||
.handler(this::handleCommand); | ||
} | ||
|
||
public void handleCommand(@NonNull CommandContext<CommandSender> context) { | ||
CommandSender sender = context.sender(); | ||
if (!sender.hasPermission("areashop.destroybuy") | ||
&& !sender.hasPermission("areashop.destroybuy.landlord") | ||
&& !sender.hasPermission("areashop.destroyrent") | ||
&& !sender.hasPermission("areashop.destroyrent.landlord")) { | ||
throw new AreaShopCommandException("del-noPermission"); | ||
} | ||
Optional<GeneralRegion> inputRegion = context.optional(KEY_REGION); | ||
if (inputRegion.isPresent()) { | ||
handleSingleDeletion(sender, inputRegion.get()); | ||
return; | ||
} | ||
List<GeneralRegion> regions; | ||
if (sender instanceof Player player) { | ||
WorldSelection selection = WorldSelection.fromPlayer(player, this.worldEditInterface); | ||
regions = Utils.getWorldEditRegionsInSelection(selection.selection()).stream() | ||
.map(ProtectedRegion::getId) | ||
.map(this.fileManager::getRegion) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} else { | ||
throw new AreaShopCommandException("cmd-weOnlyByPlayer"); | ||
} | ||
if (regions.isEmpty()) { | ||
throw new AreaShopCommandException("cmd-noWERegionsFound"); | ||
} | ||
handleMassDeletion(player, regions); | ||
} | ||
|
||
private void handleSingleDeletion(@NonNull CommandSender sender, GeneralRegion region) { | ||
boolean isLandlord = sender instanceof Player player && region.isLandlord(player.getUniqueId()); | ||
if (region instanceof RentRegion) { | ||
// Remove the rent if the player has permission | ||
if (sender.hasPermission("areashop.destroyrent") || (isLandlord && sender.hasPermission( | ||
"areashop.destroyrent.landlord"))) { | ||
DeletingRegionEvent event = fileManager.deleteRegion(region, true); | ||
if (event.isCancelled()) { | ||
this.messageBridge.message(sender, "general-cancelled", event.getReason()); | ||
} else { | ||
this.messageBridge.message(sender, "destroy-successRent", region); | ||
} | ||
} else { | ||
this.messageBridge.message(sender, "destroy-noPermissionRent", region); | ||
} | ||
} else if (region instanceof BuyRegion) { | ||
// Remove the buy if the player has permission | ||
if (sender.hasPermission("areashop.destroybuy") || (isLandlord && sender.hasPermission( | ||
"areashop.destroybuy.landlord"))) { | ||
DeletingRegionEvent event = fileManager.deleteRegion(region, true); | ||
if (event.isCancelled()) { | ||
messageBridge.message(sender, "general-cancelled", event.getReason()); | ||
} else { | ||
messageBridge.message(sender, "destroy-successBuy", region); | ||
} | ||
} else { | ||
messageBridge.message(sender, "destroy-noPermissionBuy", region); | ||
} | ||
} | ||
} | ||
|
||
private void handleMassDeletion(@NonNull Player sender, @NonNull List<GeneralRegion> regions) { | ||
List<String> namesSuccess = new ArrayList<>(); | ||
Set<GeneralRegion> regionsFailed = new TreeSet<>(); | ||
Set<GeneralRegion> regionsCancelled = new TreeSet<>(); | ||
for (GeneralRegion region : regions) { | ||
if (cannotDelete(sender, region)) { | ||
regionsFailed.add(region); | ||
continue; | ||
} | ||
|
||
DeletingRegionEvent event = this.fileManager.deleteRegion(region, true); | ||
if (event.isCancelled()) { | ||
regionsCancelled.add(region); | ||
} else { | ||
namesSuccess.add(region.getName()); | ||
} | ||
} | ||
|
||
// Send messages | ||
if (!namesSuccess.isEmpty()) { | ||
this.messageBridge.message(sender, "del-success", Utils.createCommaSeparatedList(namesSuccess)); | ||
} | ||
if (!regionsFailed.isEmpty()) { | ||
this.messageBridge.message(sender, "del-failed", Utils.combinedMessage(regionsFailed, "region")); | ||
} | ||
if (!regionsCancelled.isEmpty()) { | ||
this.messageBridge.message(sender, "del-cancelled", Utils.combinedMessage(regionsCancelled, "region")); | ||
} | ||
} | ||
|
||
private boolean cannotDelete(@NonNull Player sender, @NonNull GeneralRegion region) { | ||
boolean isLandlord = region.isLandlord(sender.getUniqueId()); | ||
if (region instanceof RentRegion | ||
&& (!sender.hasPermission("areashop.destroyrent") && !(isLandlord && sender.hasPermission( | ||
"areashop.destroyrent.landlord")))) { | ||
return true; | ||
|
||
} | ||
if (region instanceof BuyRegion | ||
&& (!sender.hasPermission("areashop.destroybuy") && !(isLandlord && sender.hasPermission( | ||
"areashop.destroybuy.landlord")))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
51 changes: 0 additions & 51 deletions
51
AreaShop/src/main/java/me/wiefferink/areashop/commands/util/GeneralRegionFlagUtil.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.