diff --git a/Changelog.txt b/Changelog.txt index fd106db..8dcfeb0 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -12,6 +12,7 @@ Flan 1.5.3 flan:used_claimblocks: Tracks the used claimblocks of players flan:free_claimblocks: Tracks the amount a player can use to claim flan:claim_number: Tracks the number of claims a player has in total +- Add option to only display part of a claims info with claimInfo Flan 1.5.2 ====================== diff --git a/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java b/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java index 7b1bd2c..3571c0f 100644 --- a/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java +++ b/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java @@ -677,7 +677,7 @@ public class Claim implements IPermissionContainer { return String.format("%s:[x=%d,z=%d] - [x=%d,z=%d]", this.claimName, this.minX, this.minZ, this.maxX, this.maxZ); } - public List infoString(ServerPlayerEntity player) { + public List infoString(ServerPlayerEntity player, InfoType infoType) { boolean perms = this.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos()); List l = new ArrayList<>(); l.add(PermHelper.simpleColoredText("=============================================", Formatting.GREEN)); @@ -695,25 +695,35 @@ public class Claim implements IPermissionContainer { l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBasicInfoSubNamed, ownerName, this.minX, this.minZ, this.maxX, this.maxZ, this.claimName), Formatting.GOLD)); } if (perms) { - l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimInfoPerms, this.globalPerm), Formatting.RED)); - l.add(PermHelper.simpleColoredText(ConfigHandler.lang.claimGroupInfoHeader, Formatting.RED)); - Map> nameToGroup = new HashMap<>(); - for (Map.Entry e : this.playersGroups.entrySet()) { - GameProfile pgroup = player.getServer().getUserCache().getByUuid(e.getKey()); - if (prof != null) { - nameToGroup.merge(e.getValue(), Lists.newArrayList(pgroup.getName()), (old, val) -> { - old.add(pgroup.getName()); - return old; - }); + if (infoType == InfoType.ALL || infoType == InfoType.GLOBAL) + l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimInfoPerms, this.globalPerm), Formatting.RED)); + if (infoType == InfoType.ALL || infoType == InfoType.GROUP) { + l.add(PermHelper.simpleColoredText(ConfigHandler.lang.claimGroupInfoHeader, Formatting.RED)); + Map> nameToGroup = new HashMap<>(); + for (Map.Entry e : this.playersGroups.entrySet()) { + GameProfile pgroup = player.getServer().getUserCache().getByUuid(e.getKey()); + if (prof != null) { + nameToGroup.merge(e.getValue(), Lists.newArrayList(pgroup.getName()), (old, val) -> { + old.add(pgroup.getName()); + return old; + }); + } + } + for (Map.Entry> e : this.permissions.entrySet()) { + l.add(PermHelper.simpleColoredText(String.format(" %s:", e.getKey()), Formatting.DARK_RED)); + l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimGroupPerms, e.getValue()), Formatting.RED)); + l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimGroupPlayers, nameToGroup.getOrDefault(e.getKey(), new ArrayList<>())), Formatting.RED)); } - } - for (Map.Entry> e : this.permissions.entrySet()) { - l.add(PermHelper.simpleColoredText(String.format(" %s:", e.getKey()), Formatting.DARK_RED)); - l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimGroupPerms, e.getValue()), Formatting.RED)); - l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimGroupPlayers, nameToGroup.getOrDefault(e.getKey(), new ArrayList<>())), Formatting.RED)); } } l.add(PermHelper.simpleColoredText("=============================================", Formatting.GREEN)); return l; } + + public enum InfoType { + ALL, + SIMPLE, + GLOBAL, + GROUP + } } diff --git a/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java b/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java index a74a8be..5c3c003 100644 --- a/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java +++ b/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java @@ -9,9 +9,6 @@ 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 com.mojang.brigadier.exceptions.SimpleCommandExceptionType; -import com.mojang.brigadier.suggestion.Suggestions; -import com.mojang.brigadier.suggestion.SuggestionsBuilder; import io.github.flemmli97.flan.api.ClaimPermission; import io.github.flemmli97.flan.api.IPlayerData; import io.github.flemmli97.flan.api.PermissionRegistry; @@ -46,9 +43,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.regex.Pattern; -import java.util.stream.Collectors; public class CommandClaim { @@ -63,7 +57,8 @@ public class CommandClaim { .then(CommandManager.literal("unlockDrops").executes(CommandClaim::unlockDrops) .then(CommandManager.argument("players", GameProfileArgumentType.gameProfile()).requires(src -> CommandPermission.perm(src, CommandPermission.cmdUnlockAll, true)).executes(CommandClaim::unlockDropsPlayers))) .then(CommandManager.literal("personalGroups").requires(src -> CommandPermission.perm(src, CommandPermission.cmdPGroup)).executes(CommandClaim::openPersonalGroups)) - .then(CommandManager.literal("claimInfo").requires(src -> CommandPermission.perm(src, CommandPermission.cmdInfo)).executes(CommandClaim::claimInfo)) + .then(CommandManager.literal("claimInfo").requires(src -> CommandPermission.perm(src, CommandPermission.cmdInfo)).executes(ctx -> CommandClaim.claimInfo(ctx, Claim.InfoType.ALL)) + .then(CommandManager.argument("type", StringArgumentType.word()).suggests((src, b) -> CommandHelpers.enumSuggestion(Claim.InfoType.class, b)).executes(CommandClaim::claimInfo))) .then(CommandManager.literal("transferClaim").requires(src -> CommandPermission.perm(src, CommandPermission.cmdTransfer)).then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).executes(CommandClaim::transferClaim))) .then(CommandManager.literal("delete").requires(src -> CommandPermission.perm(src, CommandPermission.cmdTransfer)).executes(CommandClaim::deleteClaim)) .then(CommandManager.literal("deleteAll").requires(src -> CommandPermission.perm(src, CommandPermission.cmdTransfer)).executes(CommandClaim::deleteAllClaim)) @@ -88,12 +83,12 @@ public class CommandClaim { .then(CommandManager.literal("group").requires(src -> CommandPermission.perm(src, CommandPermission.cmdGroup)) .then(CommandManager.literal("add").then(CommandManager.argument("group", StringArgumentType.string()).executes(CommandClaim::addGroup))) .then(CommandManager.literal("remove").then(CommandManager.argument("group", StringArgumentType.string()) - .suggests(CommandClaim::groupSuggestion).executes(CommandClaim::removeGroup))) + .suggests(CommandHelpers::groupSuggestion).executes(CommandClaim::removeGroup))) .then(CommandManager.literal("players") - .then(CommandManager.literal("add").then(CommandManager.argument("group", StringArgumentType.word()).suggests(CommandClaim::groupSuggestion) + .then(CommandManager.literal("add").then(CommandManager.argument("group", StringArgumentType.word()).suggests(CommandHelpers::groupSuggestion) .then(CommandManager.argument("players", GameProfileArgumentType.gameProfile()).executes(CommandClaim::addPlayer) .then(CommandManager.literal("overwrite").executes(CommandClaim::forceAddPlayer))))) - .then(CommandManager.literal("remove").then(CommandManager.argument("group", StringArgumentType.word()).suggests(CommandClaim::groupSuggestion) + .then(CommandManager.literal("remove").then(CommandManager.argument("group", StringArgumentType.word()).suggests(CommandHelpers::groupSuggestion) .then(CommandManager.argument("players", GameProfileArgumentType.gameProfile()).suggests((context, build) -> { ServerPlayerEntity player = context.getSource().getPlayer(); List list = new ArrayList<>(); @@ -106,21 +101,21 @@ public class CommandClaim { return CommandSource.suggestMatching(list, build); }).executes(CommandClaim::removePlayer)))))) .then(CommandManager.literal("teleport").requires(src -> CommandPermission.perm(src, CommandPermission.cmdTeleport)) - .then(CommandManager.literal("self").then(CommandManager.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandClaim.claimSuggestions(ctx, b, ctx.getSource().getPlayer().getUuid())) + .then(CommandManager.literal("self").then(CommandManager.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandHelpers.claimSuggestions(ctx, b, ctx.getSource().getPlayer().getUuid())) .executes(CommandClaim::teleport))) - .then(CommandManager.literal("admin").then(CommandManager.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandClaim.claimSuggestions(ctx, b, null)) + .then(CommandManager.literal("admin").then(CommandManager.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandHelpers.claimSuggestions(ctx, b, null)) .executes(CommandClaim::teleportAdminClaims))) - .then(CommandManager.literal("other").then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).then(CommandManager.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandClaim.claimSuggestions(ctx, b, CommandClaim.singleProfile(ctx, "player").getId())) - .executes(src -> CommandClaim.teleport(src, CommandClaim.singleProfile(src, "player").getId())))))) + .then(CommandManager.literal("other").then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).then(CommandManager.argument("claim", StringArgumentType.string()).suggests((ctx, b) -> CommandHelpers.claimSuggestions(ctx, b, CommandHelpers.singleProfile(ctx, "player").getId())) + .executes(src -> CommandClaim.teleport(src, CommandHelpers.singleProfile(src, "player").getId())))))) .then(CommandManager.literal("permission").requires(src -> CommandPermission.perm(src, CommandPermission.cmdPermission)) - .then(CommandManager.literal("personal").then(CommandManager.argument("group", StringArgumentType.string()).suggests(CommandClaim::personalGroupSuggestion) - .then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, true)) + .then(CommandManager.literal("personal").then(CommandManager.argument("group", StringArgumentType.string()).suggests(CommandHelpers::personalGroupSuggestion) + .then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> CommandHelpers.permSuggestions(ctx, b, true)) .then(CommandManager.argument("toggle", StringArgumentType.word()) .suggests((ctx, b) -> CommandSource.suggestMatching(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editPersonalPerm))))) - .then(CommandManager.literal("global").then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, false)) + .then(CommandManager.literal("global").then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> CommandHelpers.permSuggestions(ctx, b, false)) .then(CommandManager.argument("toggle", StringArgumentType.word()).suggests((ctx, b) -> CommandSource.suggestMatching(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGlobalPerm)))) - .then(CommandManager.literal("group").then(CommandManager.argument("group", StringArgumentType.string()).suggests(CommandClaim::groupSuggestion) - .then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, true)) + .then(CommandManager.literal("group").then(CommandManager.argument("group", StringArgumentType.string()).suggests(CommandHelpers::groupSuggestion) + .then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> CommandHelpers.permSuggestions(ctx, b, true)) .then(CommandManager.argument("toggle", StringArgumentType.word()) .suggests((ctx, b) -> CommandSource.suggestMatching(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGroupPerm)))))); builder.then(CommandManager.literal("help").executes(ctx -> CommandHelp.helpMessage(ctx, 0, builder.getArguments())) @@ -289,6 +284,10 @@ public class CommandClaim { } private static int claimInfo(CommandContext context) throws CommandSyntaxException { + return claimInfo(context, CommandHelpers.parseEnum(Claim.InfoType.class, StringArgumentType.getString(context, "type"), Claim.InfoType.ALL)); + } + + private static int claimInfo(CommandContext context, Claim.InfoType infoType) throws CommandSyntaxException { ServerPlayerEntity player = context.getSource().getPlayer(); Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos()); PlayerClaimData data = PlayerClaimData.get(player); @@ -299,14 +298,14 @@ public class CommandClaim { if (data.getEditMode() == EnumEditMode.SUBCLAIM) { Claim sub = claim.getSubClaim(player.getBlockPos()); if (sub != null) { - List info = sub.infoString(player); + List info = sub.infoString(player, infoType); player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.claimSubHeader, Formatting.AQUA), false); for (Text text : info) player.sendMessage(text, false); return Command.SINGLE_SUCCESS; } } - List info = claim.infoString(player); + List info = claim.infoString(player, infoType); for (Text text : info) player.sendMessage(text, false); return Command.SINGLE_SUCCESS; @@ -528,39 +527,8 @@ public class CommandClaim { return Command.SINGLE_SUCCESS; } - private static final Pattern allowed = Pattern.compile("[a-zA-Z0-9_+.-]+"); - - private static CompletableFuture groupSuggestion(CommandContext context, SuggestionsBuilder build) throws CommandSyntaxException { - ServerPlayerEntity player = context.getSource().getPlayer(); - List list = new ArrayList<>(); - ClaimStorage storage = ClaimStorage.get(player.getServerWorld()); - Claim claim = storage.getClaimAt(player.getBlockPos()); - if (claim != null && claim.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos())) { - list = claim.groups(); - } - for (int i = 0; i < list.size(); i++) { - if (allowed.matcher(list.get(i)).matches()) - continue; - list.set(i, '\"' + list.get(i) + '\"'); - } - return CommandSource.suggestMatching(list, build); - } - - private static CompletableFuture personalGroupSuggestion(CommandContext context, SuggestionsBuilder build) throws CommandSyntaxException { - ServerPlayerEntity player = context.getSource().getPlayer(); - List list = new ArrayList<>(PlayerClaimData.get(player).playerDefaultGroups().keySet()); - list.sort(null); - for (int i = 0; i < list.size(); i++) { - if (allowed.matcher(list.get(i)).matches()) - continue; - list.set(i, '\"' + list.get(i) + '\"'); - } - return CommandSource.suggestMatching(list, build); - } - private static int addGroup(CommandContext context) throws CommandSyntaxException { return modifyGroup(context, false); - } private static int removeGroup(CommandContext context) throws CommandSyntaxException { @@ -635,21 +603,6 @@ public class CommandClaim { return Command.SINGLE_SUCCESS; } - private static CompletableFuture permSuggestions(CommandContext context, SuggestionsBuilder build, boolean group) { - ServerWorld world = context.getSource().getWorld(); - Claim claim = ClaimStorage.get(world).getClaimAt(new BlockPos(context.getSource().getPosition())); - boolean admin = claim != null && claim.isAdminClaim(); - List allowedPerms = new ArrayList<>(); - for (ClaimPermission perm : PermissionRegistry.getPerms()) { - if (!admin && ConfigHandler.config.globallyDefined(world, perm)) { - continue; - } - if (!group || !PermissionRegistry.globalPerms().contains(perm)) - allowedPerms.add(perm.id); - } - return CommandSource.suggestMatching(allowedPerms, build); - } - private static int editGlobalPerm(CommandContext context) throws CommandSyntaxException { int mode = 0; switch (StringArgumentType.getString(context, "toggle")) { @@ -791,17 +744,4 @@ public class CommandClaim { return 0; }).orElse(0); } - - public static CompletableFuture claimSuggestions(CommandContext context, SuggestionsBuilder build, UUID owner) { - return CommandSource.suggestMatching(ClaimStorage.get(context.getSource().getWorld()).allClaimsFromPlayer(owner) - .stream().map(claim -> claim.getClaimName().isEmpty() ? claim.getClaimID().toString() : claim.getClaimName()).collect(Collectors.toList()), build); - } - - public static GameProfile singleProfile(CommandContext context, String arg) throws CommandSyntaxException { - Collection profs = GameProfileArgumentType.getProfileArgument(context, arg); - if (profs.size() != 1) { - throw new SimpleCommandExceptionType(() -> ConfigHandler.lang.onlyOnePlayer).create(); - } - return profs.stream().findFirst().get(); - } } diff --git a/common/src/main/java/io/github/flemmli97/flan/commands/CommandHelpers.java b/common/src/main/java/io/github/flemmli97/flan/commands/CommandHelpers.java new file mode 100644 index 0000000..1021ada --- /dev/null +++ b/common/src/main/java/io/github/flemmli97/flan/commands/CommandHelpers.java @@ -0,0 +1,102 @@ +package io.github.flemmli97.flan.commands; + +import com.mojang.authlib.GameProfile; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import io.github.flemmli97.flan.api.ClaimPermission; +import io.github.flemmli97.flan.api.PermissionRegistry; +import io.github.flemmli97.flan.claim.Claim; +import io.github.flemmli97.flan.claim.ClaimStorage; +import io.github.flemmli97.flan.config.ConfigHandler; +import io.github.flemmli97.flan.player.PlayerClaimData; +import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.GameProfileArgumentType; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class CommandHelpers { + + private static final Pattern allowed = Pattern.compile("[a-zA-Z0-9_+.-]+"); + + public static CompletableFuture claimSuggestions(CommandContext context, SuggestionsBuilder build, UUID owner) { + return CommandSource.suggestMatching(ClaimStorage.get(context.getSource().getWorld()).allClaimsFromPlayer(owner) + .stream().map(claim -> claim.getClaimName().isEmpty() ? claim.getClaimID().toString() : claim.getClaimName()).collect(Collectors.toList()), build); + } + + public static GameProfile singleProfile(CommandContext context, String arg) throws CommandSyntaxException { + Collection profs = GameProfileArgumentType.getProfileArgument(context, arg); + if (profs.size() != 1) { + throw new SimpleCommandExceptionType(() -> ConfigHandler.lang.onlyOnePlayer).create(); + } + return profs.stream().findFirst().get(); + } + + public static CompletableFuture permSuggestions(CommandContext context, SuggestionsBuilder build, boolean group) { + ServerWorld world = context.getSource().getWorld(); + Claim claim = ClaimStorage.get(world).getClaimAt(new BlockPos(context.getSource().getPosition())); + boolean admin = claim != null && claim.isAdminClaim(); + List allowedPerms = new ArrayList<>(); + for (ClaimPermission perm : PermissionRegistry.getPerms()) { + if (!admin && ConfigHandler.config.globallyDefined(world, perm)) { + continue; + } + if (!group || !PermissionRegistry.globalPerms().contains(perm)) + allowedPerms.add(perm.id); + } + return CommandSource.suggestMatching(allowedPerms, build); + } + + public static CompletableFuture groupSuggestion(CommandContext context, SuggestionsBuilder build) throws CommandSyntaxException { + ServerPlayerEntity player = context.getSource().getPlayer(); + List list = new ArrayList<>(); + ClaimStorage storage = ClaimStorage.get(player.getServerWorld()); + Claim claim = storage.getClaimAt(player.getBlockPos()); + if (claim != null && claim.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos())) { + list = claim.groups(); + } + for (int i = 0; i < list.size(); i++) { + if (allowed.matcher(list.get(i)).matches()) + continue; + list.set(i, '\"' + list.get(i) + '\"'); + } + return CommandSource.suggestMatching(list, build); + } + + public static CompletableFuture personalGroupSuggestion(CommandContext context, SuggestionsBuilder build) throws CommandSyntaxException { + ServerPlayerEntity player = context.getSource().getPlayer(); + List list = new ArrayList<>(PlayerClaimData.get(player).playerDefaultGroups().keySet()); + list.sort(null); + for (int i = 0; i < list.size(); i++) { + if (allowed.matcher(list.get(i)).matches()) + continue; + list.set(i, '\"' + list.get(i) + '\"'); + } + return CommandSource.suggestMatching(list, build); + } + + public static > T parseEnum(Class clss, String name, T fallback) { + try { + return Enum.valueOf(clss, name); + } catch (IllegalArgumentException e) { + return fallback; + } + } + + public static > CompletableFuture enumSuggestion(Class clss, SuggestionsBuilder build) { + return CommandSource.suggestMatching(Stream.of(clss.getEnumConstants()).map(Object::toString), build); + } +}