flan/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java

805 lines
52 KiB
Java
Raw Normal View History

package io.github.flemmli97.flan.commands;
2020-08-23 12:52:36 +00:00
2021-06-08 21:06:01 +00:00
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.flemmli97.flan.api.data.IPlayerData;
import io.github.flemmli97.flan.api.permission.ClaimPermission;
import io.github.flemmli97.flan.api.permission.PermissionRegistry;
import io.github.flemmli97.flan.claim.Claim;
import io.github.flemmli97.flan.claim.ClaimStorage;
import io.github.flemmli97.flan.claim.PermHelper;
import io.github.flemmli97.flan.config.ConfigHandler;
import io.github.flemmli97.flan.gui.ClaimMenuScreenHandler;
import io.github.flemmli97.flan.gui.PersonalGroupScreenHandler;
2021-07-30 16:20:35 +00:00
import io.github.flemmli97.flan.integration.currency.CommandCurrency;
2021-07-20 22:27:08 +00:00
import io.github.flemmli97.flan.integration.permissions.PermissionNodeHandler;
import io.github.flemmli97.flan.player.EnumDisplayType;
import io.github.flemmli97.flan.player.EnumEditMode;
import io.github.flemmli97.flan.player.OfflinePlayerData;
import io.github.flemmli97.flan.player.PlayerClaimData;
2021-12-03 21:37:35 +00:00
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.ComponentArgument;
import net.minecraft.commands.arguments.GameProfileArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.MinecraftServer;
2021-12-03 21:37:35 +00:00
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
2020-08-23 12:52:36 +00:00
import java.util.ArrayList;
import java.util.Collection;
2021-05-02 16:06:56 +00:00
import java.util.HashMap;
2020-08-23 12:52:36 +00:00
import java.util.List;
import java.util.Map;
import java.util.UUID;
2020-08-23 12:52:36 +00:00
public class CommandClaim {
2021-12-03 21:37:35 +00:00
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, boolean dedicated) {
LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal("flan")
.then(Commands.literal("reload").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdReload, true)).executes(CommandClaim::reloadConfig))
.then(Commands.literal("addClaim").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.claimCreate))
.then(Commands.argument("from", BlockPosArgument.blockPos()).then(Commands.argument("to", BlockPosArgument.blockPos()).executes(CommandClaim::addClaim)))
.then(Commands.literal("all").executes(CommandClaim::addClaimAll))
.then(Commands.literal("rect").then(Commands.argument("x", IntegerArgumentType.integer()).then(Commands.argument("z", IntegerArgumentType.integer()).executes(ctx -> CommandClaim.addClaimRect(ctx, IntegerArgumentType.getInteger(ctx, "x"), IntegerArgumentType.getInteger(ctx, "z")))))))
.then(Commands.literal("menu").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdMenu)).executes(CommandClaim::openMenu))
.then(Commands.literal("setHome").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdHome)).executes(CommandClaim::setClaimHome))
.then(Commands.literal("trapped").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTrapped)).executes(CommandClaim::trapped))
.then(Commands.literal("name").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdName)).then(Commands.argument("name", StringArgumentType.string()).executes(CommandClaim::nameClaim)))
.then(Commands.literal("unlockDrops").executes(CommandClaim::unlockDrops)
.then(Commands.argument("players", GameProfileArgument.gameProfile()).requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdUnlockAll, true)).executes(CommandClaim::unlockDropsPlayers)))
.then(Commands.literal("personalGroups").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdPGroup)).executes(CommandClaim::openPersonalGroups))
.then(Commands.literal("claimInfo").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdInfo)).executes(ctx -> CommandClaim.claimInfo(ctx, Claim.InfoType.ALL))
.then(Commands.argument("type", StringArgumentType.word()).suggests((src, b) -> CommandHelpers.enumSuggestion(Claim.InfoType.class, b)).executes(CommandClaim::claimInfo)))
.then(Commands.literal("transferClaim").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTransfer)).then(Commands.argument("player", GameProfileArgument.gameProfile()).executes(CommandClaim::transferClaim)))
.then(Commands.literal("delete").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTransfer)).executes(CommandClaim::deleteClaim))
.then(Commands.literal("deleteAll").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTransfer)).executes(CommandClaim::deleteAllClaim))
.then(Commands.literal("deleteSubClaim").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTransfer)).executes(CommandClaim::deleteSubClaim))
.then(Commands.literal("deleteAllSubClaims").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTransfer)).executes(CommandClaim::deleteAllSubClaim))
.then(Commands.literal("list").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdList)).executes(CommandClaim::listClaims).then(Commands.argument("player", GameProfileArgument.gameProfile()).requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdListAll, true))
.executes(cmd -> listClaims(cmd, GameProfileArgument.getGameProfiles(cmd, "player")))))
.then(Commands.literal("switchMode").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdClaimMode)).executes(CommandClaim::switchClaimMode))
.then(Commands.literal("adminMode").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdAdminMode, true)).executes(CommandClaim::switchAdminMode))
.then(Commands.literal("readGriefPrevention").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdGriefPrevention, true)).executes(CommandClaim::readGriefPreventionData))
.then(Commands.literal("setAdminClaim").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdAdminSet, true)).then(Commands.argument("toggle", BoolArgumentType.bool()).executes(CommandClaim::toggleAdminClaim)))
.then(Commands.literal("listAdminClaims").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdAdminList, true)).executes(CommandClaim::listAdminClaims))
.then(Commands.literal("adminDelete").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdAdminDelete, true)).executes(CommandClaim::adminDelete)
.then(Commands.literal("all").then(Commands.argument("players", GameProfileArgument.gameProfile())
2020-10-30 14:38:24 +00:00
.executes(CommandClaim::adminDeleteAll))))
2021-12-03 21:37:35 +00:00
.then(Commands.literal("giveClaimBlocks").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdAdminGive, true)).then(Commands.argument("players", GameProfileArgument.gameProfile())
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandClaim::giveClaimBlocks))))
.then(Commands.literal("buyBlocks").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdBuy, false))
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandCurrency::buyClaimBlocks)))
.then(Commands.literal("sellBlocks").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdSell, false))
.then(Commands.argument("amount", IntegerArgumentType.integer()).executes(CommandCurrency::sellClaimBlocks)))
.then(Commands.literal("claimMessage").then(Commands.argument("type", StringArgumentType.word()).suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"enter", "leave"}, b))
.then(Commands.argument("title", StringArgumentType.word()).suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"title", "subtitle"}, b))
.then(Commands.literal("text").then(Commands.argument("component", ComponentArgument.textComponent()).executes(ctx -> CommandClaim.editClaimMessages(ctx, ComponentArgument.getComponent(ctx, "component")))))
.then(Commands.literal("string").then(Commands.argument("message", StringArgumentType.string()).executes(CommandClaim::editClaimMessages))))))
.then(Commands.literal("group").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdGroup))
.then(Commands.literal("add").then(Commands.argument("group", StringArgumentType.string()).executes(CommandClaim::addGroup)))
.then(Commands.literal("remove").then(Commands.argument("group", StringArgumentType.string())
2021-07-03 18:26:56 +00:00
.suggests(CommandHelpers::groupSuggestion).executes(CommandClaim::removeGroup)))
2021-12-03 21:37:35 +00:00
.then(Commands.literal("players")
.then(Commands.literal("add").then(Commands.argument("group", StringArgumentType.word()).suggests(CommandHelpers::groupSuggestion)
.then(Commands.argument("players", GameProfileArgument.gameProfile()).executes(CommandClaim::addPlayer)
.then(Commands.literal("overwrite").executes(CommandClaim::forceAddPlayer)))))
.then(Commands.literal("remove").then(Commands.argument("group", StringArgumentType.word()).suggests(CommandHelpers::groupSuggestion)
.then(Commands.argument("players", GameProfileArgument.gameProfile()).suggests((context, build) -> {
ServerPlayer player = context.getSource().getPlayerOrException();
2021-05-02 16:06:56 +00:00
List<String> list = new ArrayList<>();
2021-12-03 21:37:35 +00:00
CommandSourceStack src = context.getSource();
ClaimStorage storage = ClaimStorage.get(src.getLevel());
Claim claim = storage.getClaimAt(src.getPlayerOrException().blockPosition());
if (claim != null && claim.canInteract(src.getPlayerOrException(), PermissionRegistry.EDITPERMS, src.getPlayerOrException().blockPosition())) {
2020-08-25 17:43:52 +00:00
list = claim.playersFromGroup(player.getServer(), "");
}
2021-12-03 21:37:35 +00:00
return SharedSuggestionProvider.suggest(list, build);
2020-10-30 14:38:24 +00:00
}).executes(CommandClaim::removePlayer))))))
2021-12-03 21:37:35 +00:00
.then(Commands.literal("teleport").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdTeleport))
.then(Commands.literal("self").then(Commands.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandHelpers.claimSuggestions(ctx, b, ctx.getSource().getPlayerOrException().getUUID()))
.executes(CommandClaim::teleport)))
2021-12-03 21:37:35 +00:00
.then(Commands.literal("admin").then(Commands.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandHelpers.claimSuggestions(ctx, b, null))
2021-06-13 17:46:57 +00:00
.executes(CommandClaim::teleportAdminClaims)))
2021-12-03 21:37:35 +00:00
.then(Commands.literal("other").then(Commands.argument("player", GameProfileArgument.gameProfile()).then(Commands.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandHelpers.claimSuggestions(ctx, b, CommandHelpers.singleProfile(ctx, "player").getId()))
2021-07-03 18:26:56 +00:00
.executes(src -> CommandClaim.teleport(src, CommandHelpers.singleProfile(src, "player").getId()))))))
2021-12-03 21:37:35 +00:00
.then(Commands.literal("permission").requires(src -> PermissionNodeHandler.perm(src, PermissionNodeHandler.cmdPermission))
.then(Commands.literal("personal").then(Commands.argument("group", StringArgumentType.string()).suggests(CommandHelpers::personalGroupSuggestion)
.then(Commands.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> CommandHelpers.permSuggestions(ctx, b, true))
.then(Commands.argument("toggle", StringArgumentType.word())
.suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editPersonalPerm)))))
.then(Commands.literal("global").then(Commands.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> CommandHelpers.permSuggestions(ctx, b, false))
.then(Commands.argument("toggle", StringArgumentType.word()).suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGlobalPerm))))
.then(Commands.literal("group").then(Commands.argument("group", StringArgumentType.string()).suggests(CommandHelpers::groupSuggestion)
.then(Commands.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> CommandHelpers.permSuggestions(ctx, b, true))
.then(Commands.argument("toggle", StringArgumentType.word())
.suggests((ctx, b) -> SharedSuggestionProvider.suggest(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGroupPerm))))));
builder.then(Commands.literal("help").executes(ctx -> CommandHelp.helpMessage(ctx, 0, builder.getArguments()))
.then(Commands.argument("page", IntegerArgumentType.integer()).executes(ctx -> CommandHelp.helpMessage(ctx, builder.getArguments())))
.then(Commands.literal("cmd").then(Commands.argument("command", StringArgumentType.word()).suggests((ctx, sb) -> SharedSuggestionProvider.suggest(CommandHelp.registeredCommands(ctx, builder.getArguments()), sb)).executes(CommandHelp::helpCmd))));
builder.then(Commands.literal("?").executes(ctx -> CommandHelp.helpCmd(ctx, "help")));
dispatcher.register(builder);
2020-08-23 12:52:36 +00:00
}
2021-12-03 21:37:35 +00:00
private static int reloadConfig(CommandContext<CommandSourceStack> context) {
ConfigHandler.reloadConfigs(context.getSource().getServer());
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.configReload), true);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int addClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
ClaimStorage storage = ClaimStorage.get(player.getLevel());
BlockPos from = BlockPosArgument.getLoadedBlockPos(context, "from");
BlockPos to = BlockPosArgument.getLoadedBlockPos(context, "to");
2020-09-02 22:00:44 +00:00
storage.createClaim(from, to, player);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int addClaimAll(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
PlayerClaimData data = PlayerClaimData.get(player);
int usable = data.getClaimBlocks() + data.getAdditionalClaims() - data.usedClaimBlocks();
int size = (int) Math.floor(Math.sqrt(usable));
return addClaimRect(context, size, size);
}
2021-12-03 21:37:35 +00:00
private static int addClaimRect(CommandContext<CommandSourceStack> context, int x, int z) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
ClaimStorage storage = ClaimStorage.get(player.getLevel());
boolean evenX = x % 2 == 0;
boolean evenZ = z % 2 == 0;
2021-12-03 21:37:35 +00:00
BlockPos from = player.blockPosition().offset(evenX ? -(int) ((x - 1) * 0.5) : -(int) (x * 0.5), -5, evenZ ? -(int) ((z - 1) * 0.5) : -(int) (z * 0.5));
BlockPos to = player.blockPosition().offset((int) (x * 0.5), -5, (int) (z * 0.5));
storage.createClaim(from, to, player);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int transferClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Collection<GameProfile> profs = GameProfileArgument.getGameProfiles(context, "player");
2020-09-03 16:00:37 +00:00
if (profs.size() != 1) {
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.onlyOnePlayer, ChatFormatting.RED), false);
2020-09-03 16:00:37 +00:00
return 0;
}
GameProfile prof = profs.iterator().next();
2021-12-03 21:37:35 +00:00
ClaimStorage storage = ClaimStorage.get(player.getLevel());
Claim claim = storage.getClaimAt(player.blockPosition());
2020-09-03 16:00:37 +00:00
if (claim == null) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, ChatFormatting.RED), false);
2020-09-03 16:00:37 +00:00
return 0;
}
PlayerClaimData data = PlayerClaimData.get(player);
boolean enoughBlocks = true;
if (!data.isAdminIgnoreClaim()) {
2021-12-03 21:37:35 +00:00
MinecraftServer server = context.getSource().getServer();
ServerPlayer newOwner = server.getPlayerList().getPlayer(prof.getId());
IPlayerData newData = newOwner != null ? PlayerClaimData.get(newOwner) : new OfflinePlayerData(server, prof.getId());
enoughBlocks = newData.canUseClaimBlocks(claim.getPlane());
}
if (!enoughBlocks) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.ownerTransferNoBlocks, ChatFormatting.RED), false);
2021-07-20 22:27:08 +00:00
if (PermissionNodeHandler.perm(context.getSource(), PermissionNodeHandler.cmdAdminMode, true))
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.ownerTransferNoBlocksAdmin, ChatFormatting.RED), false);
return 0;
}
2020-09-03 16:00:37 +00:00
if (!storage.transferOwner(claim, player, prof.getId())) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.ownerTransferFail, ChatFormatting.RED), false);
2020-09-03 16:00:37 +00:00
return 0;
}
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.ownerTransferSuccess, prof.getName()), ChatFormatting.GOLD), false);
2020-09-03 16:00:37 +00:00
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int openMenu(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
2021-06-13 17:37:21 +00:00
PlayerClaimData data = PlayerClaimData.get(player);
2021-12-03 21:37:35 +00:00
Claim claim = ClaimStorage.get(player.getLevel()).getClaimAt(player.blockPosition());
2021-06-13 17:37:21 +00:00
if (claim == null) {
PermHelper.noClaimMessage(player);
return 0;
}
if (data.getEditMode() == EnumEditMode.DEFAULT) {
ClaimMenuScreenHandler.openClaimMenu(player, claim);
2021-12-03 21:37:35 +00:00
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.blockPosition().getY());
2021-06-13 17:37:21 +00:00
} else {
2021-12-03 21:37:35 +00:00
Claim sub = claim.getSubClaim(player.blockPosition());
2021-06-13 17:37:21 +00:00
if (sub != null)
ClaimMenuScreenHandler.openClaimMenu(player, sub);
else
2020-08-24 19:03:06 +00:00
ClaimMenuScreenHandler.openClaimMenu(player, claim);
}
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int trapped(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
2021-06-08 20:33:40 +00:00
PlayerClaimData data = PlayerClaimData.get(player);
if (data.setTrappedRescue()) {
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.trappedRescue, ChatFormatting.GOLD), false);
2021-06-08 20:33:40 +00:00
return Command.SINGLE_SUCCESS;
} else {
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.trappedFail, ChatFormatting.RED), false);
2021-06-08 20:33:40 +00:00
}
return 0;
}
2021-12-03 21:37:35 +00:00
private static int nameClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
2021-06-10 15:29:30 +00:00
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getEditMode() == EnumEditMode.DEFAULT) {
Claim claim = PermHelper.checkReturn(player, PermissionRegistry.EDITPERMS, PermHelper.genericNoPermMessage(player));
if (claim == null)
return 0;
2021-12-03 21:37:35 +00:00
boolean nameUsed = ClaimStorage.get(player.getLevel()).allClaimsFromPlayer(claim.getOwner())
2021-06-10 15:29:30 +00:00
.stream().map(Claim::getClaimName).anyMatch(name -> name.equals(StringArgumentType.getString(context, "name")));
if (!nameUsed) {
String name = StringArgumentType.getString(context, "name");
claim.setClaimName(name);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimNameSet, name), ChatFormatting.GOLD), false);
2021-06-10 15:29:30 +00:00
} else {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.claimNameUsed, ChatFormatting.DARK_RED), false);
2021-06-10 15:29:30 +00:00
}
} else {
2021-12-03 21:37:35 +00:00
Claim claim = ClaimStorage.get(player.getLevel()).getClaimAt(player.blockPosition());
Claim sub = claim.getSubClaim(player.blockPosition());
if (sub != null && (claim.canInteract(player, PermissionRegistry.EDITPERMS, player.blockPosition()) || sub.canInteract(player, PermissionRegistry.EDITPERMS, player.blockPosition()))) {
2021-06-10 15:29:30 +00:00
boolean nameUsed = claim.getAllSubclaims()
.stream().map(Claim::getClaimName).anyMatch(name -> name.equals(StringArgumentType.getString(context, "name")));
if (!nameUsed) {
String name = StringArgumentType.getString(context, "name");
sub.setClaimName(name);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimNameSet, name), ChatFormatting.GOLD), false);
2021-06-10 15:29:30 +00:00
} else {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.claimNameUsedSub, ChatFormatting.DARK_RED), false);
2021-06-10 15:29:30 +00:00
}
2021-12-03 21:37:35 +00:00
} else if (claim.canInteract(player, PermissionRegistry.EDITPERMS, player.blockPosition())) {
boolean nameUsed = ClaimStorage.get(player.getLevel()).allClaimsFromPlayer(claim.getOwner())
2021-06-10 15:29:30 +00:00
.stream().map(Claim::getClaimName).anyMatch(name -> name.equals(StringArgumentType.getString(context, "name")));
if (!nameUsed) {
String name = StringArgumentType.getString(context, "name");
claim.setClaimName(name);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimNameSet, name), ChatFormatting.GOLD), false);
2021-06-10 15:29:30 +00:00
} else {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.claimNameUsed, ChatFormatting.DARK_RED), false);
2021-06-10 15:29:30 +00:00
}
} else
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermission, ChatFormatting.DARK_RED), false);
2021-06-10 15:29:30 +00:00
}
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int unlockDrops(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
2021-06-09 10:49:19 +00:00
PlayerClaimData data = PlayerClaimData.get(player);
data.unlockDeathItems();
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.unlockDrops, ConfigHandler.config.dropTicks), ChatFormatting.GOLD), false);
2021-06-09 10:49:19 +00:00
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int unlockDropsPlayers(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Collection<GameProfile> profs = GameProfileArgument.getGameProfiles(context, "players");
2021-06-09 12:45:08 +00:00
List<String> success = new ArrayList<>();
for (GameProfile prof : profs) {
2021-12-03 21:37:35 +00:00
ServerPlayer player = context.getSource().getServer().getPlayerList().getPlayer(prof.getId());
2021-06-09 12:45:08 +00:00
if (player != null) {
PlayerClaimData data = PlayerClaimData.get(player);
data.unlockDeathItems();
success.add(prof.getName());
}
}
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.unlockDropsMulti, success), ChatFormatting.GOLD), false);
2021-06-09 12:45:08 +00:00
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int openPersonalGroups(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
PersonalGroupScreenHandler.openGroupMenu(player);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int claimInfo(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
2021-07-03 18:26:56 +00:00
return claimInfo(context, CommandHelpers.parseEnum(Claim.InfoType.class, StringArgumentType.getString(context, "type"), Claim.InfoType.ALL));
}
2021-12-03 21:37:35 +00:00
private static int claimInfo(CommandContext<CommandSourceStack> context, Claim.InfoType infoType) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Claim claim = ClaimStorage.get(player.getLevel()).getClaimAt(player.blockPosition());
2020-08-24 19:03:06 +00:00
PlayerClaimData data = PlayerClaimData.get(player);
2020-09-03 16:00:37 +00:00
if (claim == null) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, ChatFormatting.RED), false);
return 0;
2020-09-03 16:00:37 +00:00
}
2020-08-25 17:43:52 +00:00
if (data.getEditMode() == EnumEditMode.SUBCLAIM) {
2021-12-03 21:37:35 +00:00
Claim sub = claim.getSubClaim(player.blockPosition());
2020-08-25 17:43:52 +00:00
if (sub != null) {
2021-12-03 21:37:35 +00:00
List<Component> info = sub.infoString(player, infoType);
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.claimSubHeader, ChatFormatting.AQUA), false);
for (Component text : info)
player.displayClientMessage(text, false);
2020-08-24 19:03:06 +00:00
return Command.SINGLE_SUCCESS;
}
}
2021-12-03 21:37:35 +00:00
List<Component> info = claim.infoString(player, infoType);
for (Component text : info)
player.displayClientMessage(text, false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int deleteClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
ClaimStorage storage = ClaimStorage.get(player.getLevel());
Claim claim = storage.getClaimAt(player.blockPosition());
boolean check = PermHelper.check(player, player.blockPosition(), claim, PermissionRegistry.EDITCLAIM, b -> {
2020-08-25 17:43:52 +00:00
if (!b.isPresent())
2020-08-24 19:03:06 +00:00
PermHelper.noClaimMessage(player);
2020-08-25 17:43:52 +00:00
else if (!b.get())
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteClaimError, ChatFormatting.DARK_RED), false);
else
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteClaim, ChatFormatting.RED), false);
});
if (!check)
return 0;
2021-12-03 21:37:35 +00:00
storage.deleteClaim(claim, true, PlayerClaimData.get(player).getEditMode(), player.getLevel());
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int deleteAllClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
PlayerClaimData data = PlayerClaimData.get(player);
if (data.confirmedDeleteAll()) {
2021-12-03 21:37:35 +00:00
for (ServerLevel world : player.getServer().getAllLevels()) {
ClaimStorage storage = ClaimStorage.get(world);
2021-12-03 21:37:35 +00:00
storage.allClaimsFromPlayer(player.getUUID()).forEach((claim) -> storage.deleteClaim(claim, true, PlayerClaimData.get(player).getEditMode(), player.getLevel()));
}
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteAllClaim, ChatFormatting.GOLD), false);
data.setConfirmDeleteAll(false);
} else {
data.setConfirmDeleteAll(true);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteAllClaimConfirm, ChatFormatting.DARK_RED), false);
2020-08-24 19:03:06 +00:00
}
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int deleteSubClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
ClaimStorage storage = ClaimStorage.get(player.getLevel());
Claim claim = storage.getClaimAt(player.blockPosition());
2020-08-25 17:43:52 +00:00
if (claim == null) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, ChatFormatting.RED), false);
2020-08-24 19:03:06 +00:00
return 0;
}
2021-12-03 21:37:35 +00:00
Claim sub = claim.getSubClaim(player.blockPosition());
2020-08-25 17:43:52 +00:00
if (sub == null) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, ChatFormatting.RED), false);
2020-08-24 19:03:06 +00:00
return 0;
}
2021-12-03 21:37:35 +00:00
boolean check = PermHelper.check(player, player.blockPosition(), claim, PermissionRegistry.EDITCLAIM, b -> {
2020-08-25 17:43:52 +00:00
if (!b.isPresent())
2020-08-24 19:03:06 +00:00
PermHelper.noClaimMessage(player);
2020-08-25 17:43:52 +00:00
else if (!b.get())
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteClaimError, ChatFormatting.DARK_RED), false);
2020-08-24 19:03:06 +00:00
else
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteSubClaim, ChatFormatting.DARK_RED), false);
2020-08-24 19:03:06 +00:00
});
if (!check)
return 0;
claim.deleteSubClaim(sub);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int deleteAllSubClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Claim claim = PermHelper.checkReturn(player, PermissionRegistry.EDITCLAIM, PermHelper.genericNoPermMessage(player));
2020-08-25 17:43:52 +00:00
if (claim == null)
2020-08-24 19:03:06 +00:00
return 0;
List<Claim> subs = claim.getAllSubclaims();
subs.forEach(claim::deleteSubClaim);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteSubClaimAll, ChatFormatting.DARK_RED), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int listClaims(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
return listClaimsFromUUID(context, null);
}
2021-12-03 21:37:35 +00:00
private static int listClaims(CommandContext<CommandSourceStack> context, Collection<GameProfile> profs) throws CommandSyntaxException {
2020-09-02 13:36:58 +00:00
if (profs.size() != 1) {
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.onlyOnePlayer, ChatFormatting.RED), false);
return 0;
}
GameProfile prof = profs.iterator().next();
2020-09-02 13:36:58 +00:00
if (prof == null || prof.getId() == null)
return 0;
return listClaimsFromUUID(context, prof.getId());
}
2021-12-03 21:37:35 +00:00
private static int listClaimsFromUUID(CommandContext<CommandSourceStack> context, UUID of) throws CommandSyntaxException {
MinecraftServer server = context.getSource().getServer();
ServerPlayer player = of == null ? context.getSource().getPlayerOrException() : server.getPlayerList().getPlayer(of);
Map<Level, Collection<Claim>> claims = new HashMap<>();
for (ServerLevel world : server.getAllLevels()) {
ClaimStorage storage = ClaimStorage.get(world);
2021-12-03 21:37:35 +00:00
claims.put(world, storage.allClaimsFromPlayer(player != null ? player.getUUID() : of));
}
if (ConfigHandler.config.maxClaimBlocks != -1) {
if (player != null) {
PlayerClaimData data = PlayerClaimData.get(player);
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBlocksFormat,
data.getClaimBlocks(), data.getAdditionalClaims(), data.usedClaimBlocks()), ChatFormatting.GOLD), false);
} else {
OfflinePlayerData data = new OfflinePlayerData(server, of);
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBlocksFormat,
data.claimBlocks, data.getAdditionalClaims(), data.usedClaimBlocks()), ChatFormatting.GOLD), false);
}
}
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.listClaims, ChatFormatting.GOLD), false);
for (Map.Entry<Level, Collection<Claim>> entry : claims.entrySet())
for (Claim claim : entry.getValue())
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(
entry.getKey().dimension().location().toString() + " # " + claim.formattedClaim(), ChatFormatting.YELLOW), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int switchClaimMode(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
PlayerClaimData data = PlayerClaimData.get(player);
data.setEditMode(data.getEditMode() == EnumEditMode.DEFAULT ? EnumEditMode.SUBCLAIM : EnumEditMode.DEFAULT);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editMode, data.getEditMode()), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int switchAdminMode(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
PlayerClaimData data = PlayerClaimData.get(player);
data.setAdminIgnoreClaim(!data.isAdminIgnoreClaim());
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.adminMode, data.isAdminIgnoreClaim()), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int adminDelete(CommandContext<CommandSourceStack> context) {
CommandSourceStack src = context.getSource();
ClaimStorage storage = ClaimStorage.get(src.getLevel());
Claim claim = storage.getClaimAt(new BlockPos(src.getPosition()));
if (claim == null) {
2021-12-03 21:37:35 +00:00
src.sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, ChatFormatting.RED), false);
return 0;
}
2021-12-03 21:37:35 +00:00
storage.deleteClaim(claim, true, EnumEditMode.DEFAULT, src.getLevel());
src.sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.deleteClaim, ChatFormatting.RED), true);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int adminDeleteAll(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSourceStack src = context.getSource();
if (src.getEntity() instanceof ServerPlayer player) {
PlayerClaimData data = PlayerClaimData.get(player);
2020-08-25 17:43:52 +00:00
if (!data.confirmedDeleteAll()) {
data.setConfirmDeleteAll(true);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.deleteAllClaimConfirm, ChatFormatting.DARK_RED), false);
return Command.SINGLE_SUCCESS;
}
}
2021-05-02 16:06:56 +00:00
List<String> players = new ArrayList<>();
2021-12-03 21:37:35 +00:00
for (GameProfile prof : GameProfileArgument.getGameProfiles(context, "players")) {
for (ServerLevel world : src.getLevel().getServer().getAllLevels()) {
ClaimStorage storage = ClaimStorage.get(world);
2020-08-25 17:43:52 +00:00
storage.allClaimsFromPlayer(prof.getId()).forEach((claim) -> storage.deleteClaim(claim, true, EnumEditMode.DEFAULT, world));
}
players.add(prof.getName());
}
2021-12-03 21:37:35 +00:00
src.sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.adminDeleteAll, players), ChatFormatting.GOLD), true);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int toggleAdminClaim(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
ClaimStorage storage = ClaimStorage.get(player.getLevel());
Claim claim = storage.getClaimAt(player.blockPosition());
if (claim == null) {
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, ChatFormatting.RED), false);
return 0;
}
storage.toggleAdminClaim(player, claim, BoolArgumentType.getBool(context, "toggle"));
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.setAdminClaim, claim.isAdminClaim()), ChatFormatting.GOLD), true);
2020-08-24 19:03:06 +00:00
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int listAdminClaims(CommandContext<CommandSourceStack> context) {
CommandSourceStack src = context.getSource();
Collection<Claim> claims = ClaimStorage.get(src.getLevel()).getAdminClaims();
src.sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.listAdminClaims, src.getLevel().dimension().location()), ChatFormatting.GOLD), false);
2020-08-25 19:06:36 +00:00
for (Claim claim : claims)
2021-12-03 21:37:35 +00:00
src.sendSuccess(PermHelper.simpleColoredText(claim.formattedClaim(), ChatFormatting.YELLOW), false);
2020-08-25 19:06:36 +00:00
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int readGriefPreventionData(CommandContext<CommandSourceStack> context) {
CommandSourceStack src = context.getSource();
src.sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionData, ChatFormatting.GOLD), true);
if (ClaimStorage.readGriefPreventionData(src.getServer(), src))
src.sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionClaimDataSuccess, ChatFormatting.GOLD), true);
if (PlayerClaimData.readGriefPreventionPlayerData(src.getServer(), src))
src.sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionPlayerDataSuccess, ChatFormatting.GOLD), true);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int giveClaimBlocks(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSourceStack src = context.getSource();
2021-05-02 16:06:56 +00:00
List<String> players = new ArrayList<>();
int amount = IntegerArgumentType.getInteger(context, "amount");
2021-12-03 21:37:35 +00:00
for (GameProfile prof : GameProfileArgument.getGameProfiles(context, "players")) {
ServerPlayer player = src.getServer().getPlayerList().getPlayer(prof.getId());
if (player != null) {
PlayerClaimData data = PlayerClaimData.get(player);
data.setAdditionalClaims(data.getAdditionalClaims() + amount);
} else
2021-12-03 21:37:35 +00:00
PlayerClaimData.editForOfflinePlayer(src.getServer(), prof.getId(), amount);
players.add(prof.getName());
}
2021-12-03 21:37:35 +00:00
src.sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.giveClaimBlocks, players, amount), ChatFormatting.GOLD), true);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int addGroup(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
return modifyGroup(context, false);
}
2021-12-03 21:37:35 +00:00
private static int removeGroup(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
return modifyGroup(context, true);
}
2021-12-03 21:37:35 +00:00
private static int modifyGroup(CommandContext<CommandSourceStack> context, boolean remove) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
String group = StringArgumentType.getString(context, "group");
2021-12-03 21:37:35 +00:00
ClaimStorage storage = ClaimStorage.get(player.getLevel());
Claim claim = storage.getClaimAt(player.blockPosition());
if (claim == null) {
2020-08-24 19:03:06 +00:00
PermHelper.noClaimMessage(player);
return 0;
}
2020-08-25 17:43:52 +00:00
if (remove) {
if (claim.removePermGroup(player, group))
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupRemove, group), ChatFormatting.GOLD), false);
else {
2020-08-24 19:03:06 +00:00
PermHelper.genericNoPermMessage(player);
return 0;
}
2020-08-25 17:43:52 +00:00
} else {
if (claim.groups().contains(group)) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupExist, group), ChatFormatting.RED), false);
return 0;
} else if (claim.editPerms(player, group, PermissionRegistry.EDITPERMS, -1))
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupAdd, group), ChatFormatting.GOLD), false);
else {
2020-08-24 19:03:06 +00:00
PermHelper.genericNoPermMessage(player);
return 0;
}
}
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int forceAddPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
String group = StringArgumentType.getString(context, "group");
return modifyPlayer(context, group, true);
}
2021-12-03 21:37:35 +00:00
private static int addPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
String group = StringArgumentType.getString(context, "group");
return modifyPlayer(context, group, false);
}
2021-12-03 21:37:35 +00:00
private static int removePlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
return modifyPlayer(context, null, false);
}
2021-12-03 21:37:35 +00:00
private static int modifyPlayer(CommandContext<CommandSourceStack> context, String group, boolean force) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
ClaimStorage storage = ClaimStorage.get(player.getLevel());
Claim claim = storage.getClaimAt(player.blockPosition());
if (claim == null) {
2020-08-24 19:03:06 +00:00
PermHelper.noClaimMessage(player);
return 0;
}
2021-12-03 21:37:35 +00:00
if (!claim.canInteract(player, PermissionRegistry.EDITPERMS, player.blockPosition())) {
2020-08-24 19:03:06 +00:00
PermHelper.genericNoPermMessage(player);
return 0;
}
2021-05-02 16:06:56 +00:00
List<String> modified = new ArrayList<>();
2021-12-03 21:37:35 +00:00
for (GameProfile prof : GameProfileArgument.getGameProfiles(context, "players")) {
2020-08-25 17:43:52 +00:00
if (claim.setPlayerGroup(prof.getId(), group, force))
modified.add(prof.getName());
}
2020-08-25 17:43:52 +00:00
if (!modified.isEmpty())
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.playerModify, group, modified), ChatFormatting.GOLD), false);
else
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.playerModifyNo, group, modified), ChatFormatting.RED), false);
return Command.SINGLE_SUCCESS;
}
2020-09-03 16:00:37 +00:00
2021-12-03 21:37:35 +00:00
private static int editGlobalPerm(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
int mode = switch (StringArgumentType.getString(context, "toggle")) {
case "true" -> 1;
case "false" -> 0;
case "default" -> -1;
default -> 0;
};
2020-09-03 16:00:37 +00:00
return editPerms(context, null, mode);
}
2021-12-03 21:37:35 +00:00
private static int editGroupPerm(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
int mode = switch (StringArgumentType.getString(context, "toggle")) {
case "true" -> 1;
case "false" -> 0;
case "default" -> -1;
default -> 0;
};
2020-09-03 16:00:37 +00:00
return editPerms(context, StringArgumentType.getString(context, "group"), mode);
}
2021-12-03 21:37:35 +00:00
private static int editPerms(CommandContext<CommandSourceStack> context, String group, int mode) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Claim claim = ClaimStorage.get(player.getLevel()).getClaimAt(player.blockPosition());
2020-09-03 16:00:37 +00:00
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getEditMode() == EnumEditMode.SUBCLAIM) {
2021-12-03 21:37:35 +00:00
Claim sub = claim.getSubClaim(player.blockPosition());
2020-09-03 16:00:37 +00:00
if (sub != null)
claim = sub;
}
if (claim == null) {
PermHelper.noClaimMessage(player);
return 0;
}
2021-12-03 21:37:35 +00:00
if (!claim.canInteract(player, PermissionRegistry.EDITPERMS, player.blockPosition())) {
player.displayClientMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermission, ChatFormatting.DARK_RED), false);
2020-09-03 16:00:37 +00:00
return 0;
}
ClaimPermission perm;
2020-09-03 16:00:37 +00:00
String p = StringArgumentType.getString(context, "permission");
try {
perm = PermissionRegistry.get(p);
if (group != null && PermissionRegistry.globalPerms().contains(perm))
2020-09-03 16:00:37 +00:00
throw new IllegalArgumentException();
} catch (NullPointerException e) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.noSuchPerm, p), ChatFormatting.DARK_RED), false);
2020-09-03 16:00:37 +00:00
return 0;
}
String setPerm = mode == 1 ? "true" : mode == 0 ? "false" : "default";
if (group == null) {
2020-10-30 14:09:57 +00:00
claim.editGlobalPerms(player, perm, mode);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editPerm, perm, setPerm), ChatFormatting.GOLD), false);
2020-09-03 16:00:37 +00:00
} else {
claim.editPerms(player, group, perm, mode);
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editPermGroup, perm, group, setPerm), ChatFormatting.GOLD), false);
2020-09-03 16:00:37 +00:00
}
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
private static int editPersonalPerm(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
String group = StringArgumentType.getString(context, "group");
int mode = switch (StringArgumentType.getString(context, "toggle")) {
case "true" -> 1;
case "false" -> 0;
case "default" -> -1;
default -> 0;
};
ClaimPermission perm;
String p = StringArgumentType.getString(context, "permission");
try {
perm = PermissionRegistry.get(p);
if (PermissionRegistry.globalPerms().contains(perm))
throw new IllegalArgumentException();
} catch (NullPointerException e) {
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.noSuchPerm, p), ChatFormatting.DARK_RED), false);
return 0;
}
String setPerm = mode == 1 ? "true" : mode == 0 ? "false" : "default";
if (PlayerClaimData.get(player).editDefaultPerms(group, perm, mode))
2021-12-03 21:37:35 +00:00
player.displayClientMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editPersonalGroup, group, perm, setPerm), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
public static int setClaimHome(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Claim claim = PermHelper.checkReturn(player, PermissionRegistry.EDITCLAIM, PermHelper.genericNoPermMessage(player));
if (claim == null)
return 0;
2021-12-03 21:37:35 +00:00
claim.setHomePos(player.blockPosition());
context.getSource().sendSuccess(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.setHome, player.blockPosition().getX(), player.blockPosition().getY(), player.blockPosition().getZ()), ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
public static int teleport(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
return teleport(context, context.getSource().getPlayerOrException().getUUID());
}
2021-12-03 21:37:35 +00:00
public static int teleportAdminClaims(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
2021-06-13 17:46:57 +00:00
return teleport(context, null);
}
2021-12-03 21:37:35 +00:00
public static int teleport(CommandContext<CommandSourceStack> context, UUID owner) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
String name = StringArgumentType.getString(context, "claim");
2021-12-03 21:37:35 +00:00
return ClaimStorage.get(player.getLevel()).allClaimsFromPlayer(owner)
.stream().filter(claim -> {
if (claim.getClaimName().isEmpty())
return claim.getClaimID().toString().equals(name);
return claim.getClaimName().equals(name);
}).findFirst().map(claim -> {
BlockPos pos = claim.getHomePos();
if (claim.canInteract(player, PermissionRegistry.TELEPORT, pos, false)) {
PlayerClaimData data = PlayerClaimData.get(player);
if (data.setTeleportTo(pos)) {
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.teleportHome, ChatFormatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.teleportHomeFail, ChatFormatting.RED), false);
} else
2021-12-03 21:37:35 +00:00
context.getSource().sendSuccess(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, ChatFormatting.DARK_RED), false);
return 0;
}).orElse(0);
}
2021-09-14 15:26:41 +00:00
2021-12-03 21:37:35 +00:00
public static int editClaimMessages(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
return editClaimMessages(context, new TextComponent(StringArgumentType.getString(context, "message")));
2021-09-14 15:26:41 +00:00
}
2021-12-03 21:37:35 +00:00
public static int editClaimMessages(CommandContext<CommandSourceStack> context, Component text) throws CommandSyntaxException {
if (text instanceof MutableComponent) {
2021-09-14 15:26:41 +00:00
Style style = text.getStyle();
if (style.isEmpty())
2021-12-03 21:37:35 +00:00
style = style.applyFormat(ChatFormatting.WHITE);
2021-09-14 15:26:41 +00:00
if (!style.isItalic())
style = style.withItalic(false);
2021-12-03 21:37:35 +00:00
((MutableComponent) text).setStyle(style);
2021-09-14 15:26:41 +00:00
}
2021-12-03 21:37:35 +00:00
ServerPlayer player = context.getSource().getPlayerOrException();
2021-09-14 15:26:41 +00:00
Claim claim = PermHelper.checkReturn(player, PermissionRegistry.EDITPERMS, PermHelper.genericNoPermMessage(player));
if (claim == null)
return 0;
boolean sub = StringArgumentType.getString(context, "title").equals("subtitle");
boolean enter = StringArgumentType.getString(context, "type").equals("enter");
String feedback;
if (enter) {
if (sub) {
claim.setEnterTitle(claim.enterTitle, text);
feedback = ConfigHandler.lang.setEnterSubMessage;
} else {
claim.setEnterTitle(text, claim.enterSubtitle);
feedback = ConfigHandler.lang.setEnterMessage;
}
} else {
if (sub) {
claim.setLeaveTitle(claim.leaveTitle, text);
feedback = ConfigHandler.lang.setLeaveSubMessage;
} else {
claim.setLeaveTitle(text, claim.leaveSubtitle);
feedback = ConfigHandler.lang.setLeaveMessage;
}
}
String[] unf = feedback.split("%s", 2);
2021-12-03 21:37:35 +00:00
MutableComponent cmdFeed = new TextComponent(unf[0]).withStyle(ChatFormatting.GOLD)
2021-09-14 15:26:41 +00:00
.append(text);
if (unf.length > 1)
2021-12-03 21:37:35 +00:00
cmdFeed.append(new TextComponent(unf[1])).withStyle(ChatFormatting.GOLD);
context.getSource().sendSuccess(cmdFeed, false);
2021-09-14 15:26:41 +00:00
return Command.SINGLE_SUCCESS;
}
2020-08-23 12:52:36 +00:00
}