Skip to content

Commit

Permalink
Refactor del command to cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Mar 2, 2024
1 parent a4aa510 commit c880d6b
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.wiefferink.areashop.commands.util.AreaShopCommandException;
import me.wiefferink.areashop.commands.util.WorldFlagUtil;
import me.wiefferink.areashop.commands.util.WorldGuardRegionParser;
import me.wiefferink.areashop.commands.util.WorldSelection;
import me.wiefferink.areashop.events.ask.AddingRegionEvent;
import me.wiefferink.areashop.events.ask.BuyingRegionEvent;
import me.wiefferink.areashop.events.ask.RentingRegionEvent;
Expand Down Expand Up @@ -104,7 +105,7 @@ private void handleCommand(CommandContext<Player> context) {
regions = new HashMap<>();
regions.put(inputRegion.get().getId(), inputRegion.get());
} else {
WorldSelection selection = getWorldSelectionFromContext(context);
WorldSelection selection = WorldSelection.fromPlayer(context.sender(), this.worldEditInterface);
regions = Utils.getWorldEditRegionsInSelection(selection.selection()).stream()
.collect(Collectors.toMap(ProtectedRegion::getId, region -> region));
}
Expand All @@ -122,16 +123,6 @@ private void handleCommand(CommandContext<Player> context) {
);
}

private WorldSelection getWorldSelectionFromContext(CommandContext<Player> context) throws ParserException {
Player player = context.sender();
WorldEditSelection selection = worldEditInterface.getPlayerSelection(player);
if (selection == null) {
throw new AreaShopCommandException("cmd-noSelection");
}
World world = selection.getWorld();
return new WorldSelection(world, selection);
}

private AddTaskState createState(
@NonNull Player player,
GeneralRegion.@NonNull RegionType regionType,
Expand Down Expand Up @@ -428,10 +419,6 @@ public String getHelp(CommandSender target) {
return CommandProperties.of("add");
}

private record WorldSelection(@NonNull World world, @NonNull WorldEditSelection selection) {

}

private record AddTaskState(
@NonNull Player sender,
GeneralRegion.@NonNull RegionType regionType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import me.wiefferink.areashop.MessageBridge;
import me.wiefferink.areashop.commands.util.GeneralRegionFlagUtil;
import me.wiefferink.areashop.commands.util.RegionFlagUtil;
import me.wiefferink.areashop.managers.IFileManager;
import me.wiefferink.areashop.regions.BuyRegion;
import me.wiefferink.areashop.regions.GeneralRegion;
Expand Down Expand Up @@ -36,7 +36,7 @@ public AddFriendCloudCommand(
) {
this.messageBridge = messageBridge;
this.plugin = plugin;
this.regionFlag = GeneralRegionFlagUtil.createDefault(fileManager);
this.regionFlag = RegionFlagUtil.createDefault(fileManager);
}

public String getHelp(CommandSender target) {
Expand Down Expand Up @@ -66,7 +66,7 @@ private void handleCommand(CommandContext<Player> context) {
this.messageBridge.message(sender, "addfriend-noPermission");
return;
}
GeneralRegion region = GeneralRegionFlagUtil.createOrParseRegion(context, this.regionFlag);
GeneralRegion region = RegionFlagUtil.createOrParseRegion(context, this.regionFlag);
OfflinePlayer friend = context.get(KEY_FRIEND);
if (sender.hasPermission("areashop.addfriendall") && ((region instanceof RentRegion rentRegion && !rentRegion.isRented())
|| (region instanceof BuyRegion buyRegion && !buyRegion.isSold()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import me.wiefferink.areashop.MessageBridge;
import me.wiefferink.areashop.commands.util.GeneralRegionFlagUtil;
import me.wiefferink.areashop.commands.util.RegionFlagUtil;
import me.wiefferink.areashop.commands.util.SignProfileUtil;
import me.wiefferink.areashop.features.signs.RegionSign;
import me.wiefferink.areashop.features.signs.SignManager;
Expand Down Expand Up @@ -45,7 +45,7 @@ public AddSignCloudCommand(
this.messageBridge = messageBridge;
this.signManager = signManager;
this.plugin = plugin;
this.regionFlag = GeneralRegionFlagUtil.createDefault(fileManager);
this.regionFlag = RegionFlagUtil.createDefault(fileManager);
}

@Override
Expand Down Expand Up @@ -79,7 +79,7 @@ private void handleCommand(@NonNull CommandContext<Player> context) {
return;
}

GeneralRegion region = GeneralRegionFlagUtil.createOrParseRegion(context, this.regionFlag);
GeneralRegion region = RegionFlagUtil.createOrParseRegion(context, this.regionFlag);
String profile = SignProfileUtil.getOrParseProfile(context, this.plugin);
Optional<RegionSign> optionalRegionSign = this.signManager.signFromLocation(block.getLocation());
if(optionalRegionSign.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class AreashopCloudCommands {
AddFriendCloudCommand.class,
AddSignCloudCommand.class,
BuyCloudCommand.class,
DelCloudCommand.class,
RentCloudCommand.class
);

Expand Down
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;
}

}










This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

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.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.context.CommandContext;
Expand All @@ -18,6 +22,35 @@ private RegionFlagUtil() {
throw new IllegalStateException("Cannot instantiate static utility class");
}

@NonNull
public static CommandFlag<GeneralRegion> createDefault(@NonNull IFileManager fileManager) {
return CommandFlag.builder("region")
.withComponent(ParserDescriptor.of(new GeneralRegionParser<>(fileManager), GeneralRegion.class))
.build();
}

@NonNull
public static <C extends Entity & CommandSender> GeneralRegion createOrParseRegion(
@NonNull CommandContext<C> context,
@NonNull CommandFlag<GeneralRegion> flag
) throws AreaShopCommandException {
GeneralRegion region = context.flags().get(flag);
if (region != null) {
return region;
}
Location location = context.sender().getLocation();
List<GeneralRegion> regions = Utils.getImportantRegions(location);
String errorMessageKey;
if (regions.isEmpty()) {
errorMessageKey = "cmd-noRegionsAtLocation";
} else if (regions.size() > 1) {
errorMessageKey = "cmd-moreRegionsAtLocation";
} else {
return regions.get(0);
}
throw new AreaShopCommandException(errorMessageKey);
}

public static CommandFlag<BuyRegion> createDefaultBuy(@NonNull IFileManager fileManager) {
return CommandFlag.builder("region")
.withComponent(ParserDescriptor.of(new BuyRegionParser<>(fileManager), BuyRegion.class))
Expand Down
Loading

0 comments on commit c880d6b

Please sign in to comment.