From 0f8c813c712548a1a1dd3764848490f1543898bf Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Wed, 2 Sep 2020 15:19:31 +0200 Subject: [PATCH] read set perms from griefpreventiondata, add ability to list other peoples claims --- .../java/com/flemmli97/flan/claim/Claim.java | 83 +++++---- .../flemmli97/flan/claim/ClaimStorage.java | 167 ++++++++++++++---- .../com/flemmli97/flan/claim/PermHelper.java | 3 +- .../flemmli97/flan/commands/CommandClaim.java | 70 +++++--- .../com/flemmli97/flan/config/LangConfig.java | 4 +- .../flan/player/OfflinePlayerData.java | 53 ++++++ .../flan/player/PlayerClaimData.java | 16 +- 7 files changed, 298 insertions(+), 98 deletions(-) create mode 100644 src/main/java/com/flemmli97/flan/player/OfflinePlayerData.java diff --git a/src/main/java/com/flemmli97/flan/claim/Claim.java b/src/main/java/com/flemmli97/flan/claim/Claim.java index 57605a8..0d2a66e 100644 --- a/src/main/java/com/flemmli97/flan/claim/Claim.java +++ b/src/main/java/com/flemmli97/flan/claim/Claim.java @@ -30,7 +30,6 @@ public class Claim { private int minX, minZ, maxX, maxZ, minY; private UUID owner; - private boolean adminClaim; private UUID claimID; private final EnumMap globalPerm = Maps.newEnumMap(EnumPermission.class); @@ -66,7 +65,7 @@ public class Claim { this.minY = Math.max(0, minY); this.owner = creator; this.world = world; - this.setDirty(); + this.setDirty(true); } public static Claim fromJson(JsonObject obj, UUID owner, ServerWorld world) { @@ -77,12 +76,12 @@ public class Claim { public void setClaimID(UUID uuid) { this.claimID = uuid; - this.setDirty(); + this.setDirty(true); } public void extendDownwards(BlockPos pos){ this.minY = pos.getY(); - this.setDirty(); + this.setDirty(true); } public UUID getClaimID() { @@ -110,17 +109,27 @@ public class Claim { this.maxZ = claim.maxZ; this.minY = claim.minY; this.removed = false; - this.setDirty(); + this.setDirty(true); } - public void toggleAdminClaim(boolean flag) { - this.adminClaim = flag; - this.subClaims.forEach(claim->claim.adminClaim = flag); - this.setDirty(); + public void toggleAdminClaim(ServerPlayerEntity player, boolean flag) { + if(!flag) + this.transferOwner(player); + else { + this.owner = null; + this.subClaims.forEach(claim -> claim.owner = null); + } + this.setDirty(true); } public boolean isAdminClaim(){ - return this.adminClaim; + return this.owner==null; + } + + public void transferOwner(ServerPlayerEntity player){ + this.owner = player.getUuid(); + this.subClaims.forEach(claim->claim.owner = player.getUuid()); + this.setDirty(true); } public int getPlane() { @@ -175,7 +184,7 @@ public class Claim { if (player.getUuid().equals(this.owner)) return true; PlayerClaimData data = PlayerClaimData.get(player); - if ((this.adminClaim && player.hasPermissionLevel(2)) || data.isAdminIgnoreClaim()) + if ((this.isAdminClaim() && player.hasPermissionLevel(2)) || data.isAdminIgnoreClaim()) return true; for (Claim claim : this.subClaims) { if (claim.insideClaim(pos)) { @@ -238,7 +247,7 @@ public class Claim { sub.parent = this.claimID; sub.parentClaim = this; this.subClaims.add(sub); - this.setDirty(); + this.setDirty(true); } return conflicts; } @@ -248,7 +257,7 @@ public class Claim { claim.parent = this.claimID; claim.parentClaim = this; this.subClaims.add(claim); - this.setDirty(); + this.setDirty(true); } public Claim getSubClaim(BlockPos pos) { @@ -260,7 +269,7 @@ public class Claim { public boolean deleteSubClaim(Claim claim) { claim.remove(); - this.setDirty(); + this.setDirty(true); return this.subClaims.remove(claim); } @@ -278,22 +287,22 @@ public class Claim { conflicts.add(other); if (conflicts.isEmpty()) { claim.copySizes(newClaim); - this.setDirty(); + this.setDirty(true); } return conflicts; } public boolean setPlayerGroup(UUID player, String group, boolean force) { - if (this.owner.equals(player)) + if (player.equals(this.owner)) return false; if (group == null) { this.playersGroups.remove(player); - this.setDirty(); + this.setDirty(true); return true; } if (!this.playersGroups.containsKey(player) || force) { this.playersGroups.put(player, group); - this.setDirty(); + this.setDirty(true); return true; } return false; @@ -322,17 +331,22 @@ public class Claim { this.globalPerm.remove(toggle); else this.globalPerm.put(toggle, mode == 1); - this.setDirty(); + this.setDirty(true); } + public boolean editPerms(ServerPlayerEntity player, String group, EnumPermission perm, int mode) { + return this.editPerms(player, group, perm, mode, false); + } /** * Edit the permissions for a group. If not defined for the group creates a new default permission map for that group * * @param mode -1 = makes it resort to the global perm, 0 = deny perm, 1 = allow perm * @return If editing was successful or not */ - public boolean editPerms(ServerPlayerEntity player, String group, EnumPermission perm, int mode) { - if (this.canInteract(player, EnumPermission.EDITPERMS, player.getBlockPos())) { + public boolean editPerms(ServerPlayerEntity player, String group, EnumPermission perm, int mode, boolean griefPrevention) { + if(perm.isAlwaysGlobalPerm()) + return false; + if (griefPrevention || this.canInteract(player, EnumPermission.EDITPERMS, player.getBlockPos())) { if (mode > 1) mode = -1; boolean has = this.permissions.containsKey(group); @@ -343,7 +357,7 @@ public class Claim { perms.put(perm, mode == 1); if (!has) this.permissions.put(group, perms); - this.setDirty(); + this.setDirty(true); return true; } return false; @@ -358,7 +372,7 @@ public class Claim { toRemove.add(uuid); }); toRemove.forEach(this.playersGroups::remove); - this.setDirty(); + this.setDirty(true); return true; } return false; @@ -379,11 +393,11 @@ public class Claim { /** * Only marks non sub claims */ - public void setDirty() { + public void setDirty(boolean flag) { if(this.parentClaim()!=null) - this.parentClaim().setDirty(); + this.parentClaim().setDirty(flag); else - this.dirty = true; + this.dirty = flag; } public boolean isDirty() { @@ -391,15 +405,17 @@ public class Claim { } public void readJson(JsonObject obj, UUID uuid) { + this.claimID = UUID.fromString(obj.get("ID").getAsString()); JsonArray pos = obj.getAsJsonArray("PosxXzZY"); this.minX = pos.get(0).getAsInt(); this.maxX = pos.get(1).getAsInt(); this.minZ = pos.get(2).getAsInt(); this.maxZ = pos.get(3).getAsInt(); this.minY = pos.get(4).getAsInt(); - this.owner = uuid; - this.adminClaim = obj.has("AdminClaim")?obj.get("AdminClaim").getAsBoolean():false; - this.claimID = UUID.fromString(obj.get("ID").getAsString()); + if(obj.has("AdminClaim")?obj.get("AdminClaim").getAsBoolean():false) + this.owner = null; + else + this.owner = uuid; this.globalPerm.clear(); this.permissions.clear(); this.subClaims.clear(); @@ -431,6 +447,7 @@ public class Claim { } public JsonObject toJson(JsonObject obj) { + obj.addProperty("ID", this.claimID.toString()); JsonArray pos = new JsonArray(); pos.add(this.minX); pos.add(this.maxX); @@ -438,8 +455,6 @@ public class Claim { pos.add(this.maxZ); pos.add(this.minY); obj.add("PosxXzZY", pos); - obj.addProperty("ID", this.claimID.toString()); - obj.addProperty("AdminClaim", this.adminClaim); if (this.parent != null) obj.addProperty("Parent", this.parent.toString()); if (!this.globalPerm.isEmpty()) { @@ -499,7 +514,7 @@ public class Claim { @Override public String toString() { - return String.format("Claim:[ID=%s, Owner=%s, from: x=%d; z=%d, to: x=%d, z=%d", this.claimID != null ? this.claimID.toString() : "null", this.owner.toString(), this.minX, this.minZ, this.maxX, this.maxZ); + return String.format("Claim:[ID=%s, Owner=%s, from: x=%d; z=%d, to: x=%d, z=%d", this.claimID != null ? this.claimID.toString() : "null", this.owner!=null?this.owner.toString():"Admin", this.minX, this.minZ, this.maxX, this.maxZ); } public String formattedClaim() { @@ -510,8 +525,8 @@ public class Claim { boolean perms = this.canInteract(player, EnumPermission.EDITPERMS, player.getBlockPos()); List l = Lists.newArrayList(); l.add(PermHelper.simpleColoredText("=============================================", Formatting.GREEN)); - GameProfile prof = player.getServer().getUserCache().getByUuid(this.owner); - String ownerName = this.adminClaim ? "Admin" : prof != null ? prof.getName() : ""; + GameProfile prof = this.owner!=null?player.getServer().getUserCache().getByUuid(this.owner):null; + String ownerName = this.isAdminClaim() ? "Admin" : prof != null ? prof.getName() : ""; if(this.parent==null) l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBasicInfo, ownerName, this.minX, this.minZ, this.maxX, this.maxZ, this.subClaims.size()), Formatting.GOLD)); else diff --git a/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java b/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java index cf54d48..4adad36 100644 --- a/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java +++ b/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java @@ -1,5 +1,6 @@ package com.flemmli97.flan.claim; +import com.flemmli97.flan.Flan; import com.flemmli97.flan.IClaimData; import com.flemmli97.flan.config.ConfigHandler; import com.flemmli97.flan.player.EnumDisplayType; @@ -14,8 +15,17 @@ import com.google.gson.JsonObject; import com.ibm.icu.impl.Pair; import it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.LocateBiomeCommand; +import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; +import net.minecraft.text.BaseText; +import net.minecraft.text.ClickEvent; +import net.minecraft.text.HoverEvent; +import net.minecraft.text.LiteralText; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; import net.minecraft.util.WorldSavePath; import net.minecraft.util.math.BlockPos; @@ -30,18 +40,21 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Collection; +import java.util.EnumMap; import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.logging.Level; public class ClaimStorage { + public static final String adminClaimString = "!AdminClaims"; private final Long2ObjectArrayMap> claims = new Long2ObjectArrayMap<>(); private final Map claimUUIDMap = Maps.newHashMap(); private final Map> playerClaimMap = Maps.newHashMap(); - + private final Set dirty = Sets.newHashSet(); public static ClaimStorage get(ServerWorld world) { return (ClaimStorage) ((IClaimData) world).getClaimData(); } @@ -118,9 +131,16 @@ public class ClaimStorage { }); } this.playerClaimMap.getOrDefault(claim.getOwner(), Sets.newHashSet()).remove(claim); + this.dirty.add(claim.getOwner()); return this.claimUUIDMap.remove(claim.getClaimID()) != null; } + public void toggleAdminClaim(ServerPlayerEntity player, Claim claim, boolean toggle){ + this.deleteClaim(claim, false, EnumEditMode.DEFAULT, player.getServerWorld()); + claim.toggleAdminClaim(player, toggle); + this.addClaim(claim); + } + public boolean resizeClaim(Claim claim, BlockPos from, BlockPos to, ServerPlayerEntity player) { int[] dims = claim.getDimensions(); BlockPos opposite = new BlockPos(dims[0] == from.getX() ? dims[1] : dims[0], dims[4], dims[2] == from.getZ() ? dims[3] : dims[2]); @@ -181,7 +201,7 @@ public class ClaimStorage { } public Collection getAdminClaims(){ - return ImmutableSet.copyOf(this.claimUUIDMap.values().stream().filter(claim->claim.isAdminClaim()).iterator()); + return ImmutableSet.copyOf(this.playerClaimMap.get(null)); } public static int[] getChunkPos(Claim claim) { @@ -201,7 +221,8 @@ public class ClaimStorage { for (File file : dir.listFiles()) { if (!file.getName().endsWith(".json")) continue; - UUID uuid = UUID.fromString(file.getName().replace(".json", "")); + String realName = file.getName().replace(".json", ""); + UUID uuid = realName.equals(adminClaimString)?null:UUID.fromString(file.getName().replace(".json", "")); FileReader reader = new FileReader(file); JsonArray arr = ConfigHandler.GSON.fromJson(reader, JsonArray.class); if (arr == null) @@ -225,8 +246,8 @@ public class ClaimStorage { dir.mkdir(); try { for (Map.Entry> e : this.playerClaimMap.entrySet()) { - - File file = new File(dir, e.getKey().toString() + ".json"); + String owner = e.getKey()==null?adminClaimString:e.getKey().toString(); + File file = new File(dir, owner + ".json"); boolean dirty = false; if (!file.exists()) { if (e.getValue().isEmpty()) @@ -234,11 +255,17 @@ public class ClaimStorage { file.createNewFile(); dirty = true; } else { - for (Claim claim : e.getValue()) - if (claim.isDirty()) { - dirty = true; - break; - } + if(this.dirty.contains(owner.equals(adminClaimString)?null:e.getKey())) { + dirty = true; + this.dirty.clear(); + } + else { + for (Claim claim : e.getValue()) + if (claim.isDirty()) { + dirty = true; + claim.setDirty(false); + } + } } if (dirty) { JsonArray arr = new JsonArray(); @@ -253,11 +280,11 @@ public class ClaimStorage { } } - public static Set readGriefPreventionData(MinecraftServer server) { + public static void readGriefPreventionData(MinecraftServer server, ServerCommandSource src) { Yaml yml = new Yaml(); File griefPrevention = server.getSavePath(WorldSavePath.ROOT).resolve("plugins/GriefPreventionData/ClaimData").toFile(); if (!griefPrevention.exists()) - return null; + return; Map> subClaimMap = Maps.newHashMap(); Map intFileMap = Maps.newHashMap(); @@ -273,14 +300,14 @@ public class ClaimStorage { EnumPermission.LECTERNTAKE, EnumPermission.ENDCRYSTALPLACE, EnumPermission.PROJECTILES, EnumPermission.TRAMPLE, EnumPermission.RAID, EnumPermission.BUCKET, EnumPermission.ANIMALINTERACT, EnumPermission.HURTANIMAL, EnumPermission.TRADING, EnumPermission.ARMORSTAND, EnumPermission.BREAKNONLIVING)); - System.out.println(managers); - System.out.println(builders); - System.out.println(containers); - System.out.println(accessors); + Map> perms = Maps.newHashMap(); + perms.put("managers", managers); + perms.put("builders", builders); + perms.put("containers", containers); + perms.put("accessors", accessors); try { //Get all parent claims - Set failedClaimsFile = Sets.newHashSet(); for (File f : griefPrevention.listFiles()) { if (f.getName().endsWith(".yml")) { FileReader reader = new FileReader(f); @@ -290,7 +317,7 @@ public class ClaimStorage { intFileMap.put(Integer.valueOf(f.getName().replace(".yml", "")), f); } catch (NumberFormatException e){ - failedClaimsFile.add(f.getName()); + src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.errorFile, f.getName(), Formatting.RED)), false); } } } @@ -310,42 +337,114 @@ public class ClaimStorage { } } for (File parent : intFileMap.values()) { - Pair parentClaim = parseFromYaml(parent, yml, server); - List childs = subClaimMap.get(parent); - if (childs != null && !childs.isEmpty()) { - for (File childF : childs) - parentClaim.second.addSubClaimGriefprevention(parseFromYaml(childF, yml, server).second); + try { + Pair parentClaim = parseFromYaml(parent, yml, server, perms); + List childs = subClaimMap.get(parent); + if (childs != null && !childs.isEmpty()) { + for (File childF : childs) + parentClaim.second.addSubClaimGriefprevention(parseFromYaml(childF, yml, server, perms).second); + } + ClaimStorage storage = ClaimStorage.get(parentClaim.first); + Set conflicts = storage.conflicts(parentClaim.second, null); + if (conflicts.isEmpty()) { + parentClaim.second.setClaimID(storage.generateUUID()); + storage.addClaim(parentClaim.second); + } else { + src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.readConflict, parent.getName(), conflicts), Formatting.DARK_RED), false); + for(Claim claim : conflicts){ + int[] dim = claim.getDimensions(); + MutableText text = PermHelper.simpleColoredText(String.format("@[x=%d;z=%d]", dim[0], dim[2]), Formatting.RED); + text.setStyle(text.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/tp @s " + dim[0] + " ~ " + dim[2])).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableText("chat.coordinates.tooltip")))); + src.sendFeedback(text, false); + } + } } - ClaimStorage storage = ClaimStorage.get(parentClaim.first); - if (storage.conflicts(parentClaim.second, null).isEmpty()) { - parentClaim.second.setClaimID(storage.generateUUID()); - storage.addClaim(parentClaim.second); + catch (Exception e){ + src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.errorFile, parent.getName(), Formatting.RED)), false); + e.printStackTrace(); } - else - failedClaimsFile.add(parent.getName()); } - if (!failedClaimsFile.isEmpty()) - return failedClaimsFile; } catch (IOException e) { e.printStackTrace(); } - return null; } - private static Pair parseFromYaml(File file, Yaml yml, MinecraftServer server) throws IOException { + private static Pair parseFromYaml(File file, Yaml yml, MinecraftServer server, + Map> perms) throws IOException { FileReader reader = new FileReader(file); Map values = yml.load(reader); reader.close(); - UUID owner = UUID.fromString(values.get("Owner").toString()); + String ownerString = (String) values.get("Owner"); + + UUID owner = ownerString.isEmpty()?null:UUID.fromString(ownerString); + List builders = readList(values, "Builders"); + List managers = readList(values, "Managers"); + List containers = readList(values, "Containers"); + List accessors = readList(values, "Accessors"); String[] lesserCorner = values.get("Lesser Boundary Corner").toString().split(";"); String[] greaterCorner = values.get("Greater Boundary Corner").toString().split(";"); ServerWorld world = server.getWorld(worldRegFromString(lesserCorner[0])); Claim claim = new Claim(Integer.parseInt(lesserCorner[1]), Integer.parseInt(greaterCorner[1]), Integer.parseInt(lesserCorner[3]), Integer.parseInt(greaterCorner[3]), ConfigHandler.config.defaultClaimDepth == 255?0: Integer.parseInt(lesserCorner[2]), owner, world); + if(!builders.isEmpty() && !builders.contains(ownerString)) { + if(builders.contains("public")){ + perms.get("builders").forEach(perm -> { + if(!perm.isAlwaysGlobalPerm()) + claim.editGlobalPerms(perm, 1); + }); + } + else { + perms.get("builders").forEach(perm -> claim.editPerms(null, "Builders", perm, 1, true)); + builders.forEach(s -> claim.setPlayerGroup(UUID.fromString(s), "Builders", true)); + } + } + if(!managers.isEmpty() && !managers.contains(ownerString)) { + if(managers.contains("public")){ + perms.get("managers").forEach(perm -> { + if(!perm.isAlwaysGlobalPerm()) + claim.editGlobalPerms(perm, 1); + }); + } + else { + perms.get("managers").forEach(perm -> claim.editPerms(null, "Managers", perm, 1, true)); + managers.forEach(s -> claim.setPlayerGroup(UUID.fromString(s), "Managers", true)); + } + } + if(!containers.isEmpty() && !containers.contains(ownerString)) { + if(containers.contains("public")){ + perms.get("containers").forEach(perm -> { + if(!perm.isAlwaysGlobalPerm()) + claim.editGlobalPerms(perm, 1); + }); + } + else { + perms.get("containers").forEach(perm -> claim.editPerms(null, "Containers", perm, 1, true)); + containers.forEach(s -> claim.setPlayerGroup(UUID.fromString(s), "Containers", true)); + } + } + if(!accessors.isEmpty() && !accessors.contains(ownerString)) { + if(accessors.contains("public")){ + perms.get("accessors").forEach(perm -> { + if(!perm.isAlwaysGlobalPerm()) + claim.editGlobalPerms(perm, 1); + }); + } + else { + perms.get("accessors").forEach(perm -> claim.editPerms(null, "Accessors", perm, 1, true)); + accessors.forEach(s -> claim.setPlayerGroup(UUID.fromString(s), "Accessors", true)); + } + } return Pair.of(world, claim); } + private static List readList(Map values, String key){ + Object obj = values.get(key); + if(obj instanceof List) + return (List) obj; + return Lists.newArrayList(); + } + public static RegistryKey worldRegFromString(String spigot) { if (spigot.equals("world_the_end")) return World.END; diff --git a/src/main/java/com/flemmli97/flan/claim/PermHelper.java b/src/main/java/com/flemmli97/flan/claim/PermHelper.java index c9786ae..89d674f 100644 --- a/src/main/java/com/flemmli97/flan/claim/PermHelper.java +++ b/src/main/java/com/flemmli97/flan/claim/PermHelper.java @@ -3,6 +3,7 @@ package com.flemmli97.flan.claim; import com.flemmli97.flan.config.ConfigHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import net.minecraft.text.MutableText; import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.util.Formatting; @@ -42,7 +43,7 @@ public class PermHelper { }); } - public static Text simpleColoredText(String text, Formatting... formatting) { + public static MutableText simpleColoredText(String text, Formatting... formatting) { return new LiteralText(text).setStyle(formatting != null ? Style.EMPTY.withFormatting(formatting) : Style.EMPTY); } } diff --git a/src/main/java/com/flemmli97/flan/commands/CommandClaim.java b/src/main/java/com/flemmli97/flan/commands/CommandClaim.java index 2f4fa48..5372ed9 100644 --- a/src/main/java/com/flemmli97/flan/commands/CommandClaim.java +++ b/src/main/java/com/flemmli97/flan/commands/CommandClaim.java @@ -8,6 +8,7 @@ import com.flemmli97.flan.config.ConfigHandler; import com.flemmli97.flan.gui.ClaimMenuScreenHandler; import com.flemmli97.flan.player.EnumDisplayType; import com.flemmli97.flan.player.EnumEditMode; +import com.flemmli97.flan.player.OfflinePlayerData; import com.flemmli97.flan.player.PlayerClaimData; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -25,6 +26,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.command.argument.GameProfileArgumentType; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandSource; import net.minecraft.server.command.ServerCommandSource; @@ -39,6 +41,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.concurrent.CompletableFuture; public class CommandClaim { @@ -52,7 +55,7 @@ public class CommandClaim { CommandManager.literal("deleteAll").executes(CommandClaim::deleteAllClaim), CommandManager.literal("deleteSubClaim").executes(CommandClaim::deleteSubClaim), CommandManager.literal("deleteAllSubClaims").executes(CommandClaim::deleteAllSubClaim), - CommandManager.literal("list").executes(CommandClaim::listClaims), + CommandManager.literal("list").executes(CommandClaim::listClaims).then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).requires(src -> src.hasPermissionLevel(2)).executes(cmd->listClaims(cmd, GameProfileArgumentType.getProfileArgument(cmd, "player")))), CommandManager.literal("switchMode").executes(CommandClaim::switchClaimMode), CommandManager.literal("adminMode").requires(src -> src.hasPermissionLevel(2)).executes(CommandClaim::switchAdminMode), CommandManager.literal("readGriefPrevention").requires(src -> src.hasPermissionLevel(2)).executes(CommandClaim::readGriefPreventionData), @@ -218,18 +221,42 @@ public class CommandClaim { private static int listClaims(CommandContext context) throws CommandSyntaxException { ServerPlayerEntity player = context.getSource().getPlayer(); - Map> claims = Maps.newHashMap(); - for (ServerWorld world : player.getServer().getWorlds()) { - ClaimStorage storage = ClaimStorage.get(world); - claims.put(world, storage.allClaimsFromPlayer(player.getUuid())); + return listClaimsFromUUID(context, null); + } + + private static int listClaims(CommandContext context, Collection profs) throws CommandSyntaxException { + if(profs.size()!=1) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.onlyOnePlayer, Formatting.RED), false); + return 0; } - PlayerClaimData data = PlayerClaimData.get(player); - player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBlocksFormat, - data.getClaimBlocks(), data.getAdditionalClaims(), data.usedClaimBlocks()), Formatting.GOLD), false); - player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.listClaims, Formatting.GOLD), false); + GameProfile prof = profs.iterator().next(); + if(prof == null || prof.getId()==null) + return 0; + return listClaimsFromUUID(context, prof.getId()); + } + + private static int listClaimsFromUUID(CommandContext context, UUID of) throws CommandSyntaxException { + MinecraftServer server = context.getSource().getMinecraftServer(); + ServerPlayerEntity player = of==null?context.getSource().getPlayer():server.getPlayerManager().getPlayer(of); + Map> claims = Maps.newHashMap(); + for (ServerWorld world : server.getWorlds()) { + ClaimStorage storage = ClaimStorage.get(world); + claims.put(world, storage.allClaimsFromPlayer(player!=null?player.getUuid():of)); + } + if(player!=null) { + PlayerClaimData data = PlayerClaimData.get(player); + context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBlocksFormat, + data.getClaimBlocks(), data.getAdditionalClaims(), data.usedClaimBlocks()), Formatting.GOLD), false); + } + else { + OfflinePlayerData data = new OfflinePlayerData(server, of); + context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBlocksFormat, + data.claimBlocks, data.additionalClaimBlocks, data.getUsedClaimBlocks(server)), Formatting.GOLD), false); + } + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.listClaims, Formatting.GOLD), false); for (Map.Entry> entry : claims.entrySet()) for (Claim claim : entry.getValue()) - player.sendMessage(PermHelper.simpleColoredText( + context.getSource().sendFeedback(PermHelper.simpleColoredText( entry.getKey().getRegistryKey().getValue().toString() + " # " + claim.formattedClaim(), Formatting.YELLOW), false); return Command.SINGLE_SUCCESS; } @@ -286,16 +313,16 @@ public class CommandClaim { return Command.SINGLE_SUCCESS; } - private static int toggleAdminClaim(CommandContext context) { - ServerCommandSource src = context.getSource(); - ClaimStorage storage = ClaimStorage.get(src.getWorld()); - Claim claim = storage.getClaimAt(new BlockPos(src.getPosition())); + private static int toggleAdminClaim(CommandContext context) throws CommandSyntaxException { + ServerPlayerEntity player = context.getSource().getPlayer(); + ClaimStorage storage = ClaimStorage.get(player.getServerWorld()); + Claim claim = storage.getClaimAt(player.getBlockPos()); if (claim == null) { - src.sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, Formatting.RED), false); + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, Formatting.RED), false); return 0; } - claim.toggleAdminClaim(BoolArgumentType.getBool(context, "toggle")); - src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.setAdminClaim, claim.isAdminClaim()), Formatting.GOLD), true); + storage.toggleAdminClaim(player, claim, BoolArgumentType.getBool(context, "toggle")); + context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.setAdminClaim, claim.isAdminClaim()), Formatting.GOLD), true); return Command.SINGLE_SUCCESS; } @@ -311,12 +338,9 @@ public class CommandClaim { private static int readGriefPreventionData(CommandContext context) { ServerCommandSource src = context.getSource(); src.sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionData, Formatting.GOLD), true); - Set errors = ClaimStorage.readGriefPreventionData(src.getMinecraftServer()); - PlayerClaimData.readGriefPreventionPlayerData(src.getMinecraftServer()); - if (errors == null) - src.sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionDataSuccess, Formatting.GOLD), true); - else - src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.readGriefpreventionDataFail, errors), Formatting.RED), true); + ClaimStorage.readGriefPreventionData(src.getMinecraftServer(), src); + PlayerClaimData.readGriefPreventionPlayerData(src.getMinecraftServer(), src); + src.sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionDataSuccess, Formatting.GOLD), true); return Command.SINGLE_SUCCESS; } diff --git a/src/main/java/com/flemmli97/flan/config/LangConfig.java b/src/main/java/com/flemmli97/flan/config/LangConfig.java index c1a42ba..4b90cc8 100644 --- a/src/main/java/com/flemmli97/flan/config/LangConfig.java +++ b/src/main/java/com/flemmli97/flan/config/LangConfig.java @@ -20,6 +20,7 @@ public class LangConfig { public String claimBlocksFormat = "Claim Blocks: %1$d + (Bonus) %2$d; Used: %3$d"; public String listClaims = "Listing all claims:"; public String listAdminClaims = "Listing all admin-claims in %1:"; + public String onlyOnePlayer = "Only one player can be used as argument"; public String noPermission = "You don't have the required permissions to do that here!"; public String noPermissionSimple = "Sorry you can't do that here!"; @@ -58,7 +59,8 @@ public class LangConfig { public String setAdminClaim = "Adminclaim of this claim now: %s"; public String readGriefpreventionData = "Reading data from GriefPrevention"; public String readGriefpreventionDataSuccess = "Successfully read data"; - public String readGriefpreventionDataFail = "Failed reading data for following claim files (Check the logs!): %s"; + public String errorFile = "Error reading file %s"; + public String readConflict = "%1$s conflicts with existing claims. Not added to world! Conflicts:"; public String giveClaimBlocks = "Gave following players %2$d claimblocks: %1$s"; public String claimBasicInfo = "Owner: %1$s, from: [x=%2$d,z=%3$d] to [x=%4$d,z=%5$d]; Subclaim-amount: %6$d"; diff --git a/src/main/java/com/flemmli97/flan/player/OfflinePlayerData.java b/src/main/java/com/flemmli97/flan/player/OfflinePlayerData.java new file mode 100644 index 0000000..9ebe178 --- /dev/null +++ b/src/main/java/com/flemmli97/flan/player/OfflinePlayerData.java @@ -0,0 +1,53 @@ +package com.flemmli97.flan.player; + +import com.flemmli97.flan.claim.Claim; +import com.flemmli97.flan.claim.ClaimStorage; +import com.flemmli97.flan.config.ConfigHandler; +import com.google.gson.JsonObject; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.WorldSavePath; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collection; +import java.util.UUID; + +public class OfflinePlayerData { + + public final int claimBlocks, additionalClaimBlocks; + public final UUID owner; + public OfflinePlayerData(MinecraftServer server, UUID uuid){ + File dir = new File(server.getSavePath(WorldSavePath.PLAYERDATA).toFile(), "/claimData/"); + int claim = ConfigHandler.config.startingBlocks; + int add = 0; + this.owner = uuid; + if (dir.exists()) { + try { + File file = new File(dir, uuid + ".json"); + if (file.exists()) { + FileReader reader = new FileReader(file); + JsonObject obj = ConfigHandler.GSON.fromJson(reader, JsonObject.class); + claim = obj.get("ClaimBlocks").getAsInt(); + add = obj.get("AdditionalBlocks").getAsInt(); + reader.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + this.claimBlocks = claim; + this.additionalClaimBlocks = add; + } + + public int getUsedClaimBlocks(MinecraftServer server){ + int usedClaimsBlocks = 0; + for (ServerWorld world : server.getWorlds()) { + Collection claims = ClaimStorage.get(world).allClaimsFromPlayer(this.owner); + if (claims != null) + usedClaimsBlocks += claims.stream().filter(claim->!claim.isAdminClaim()).mapToInt(Claim::getPlane).sum(); + } + return usedClaimsBlocks; + } +} diff --git a/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java b/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java index 0cb1c42..0b4978c 100644 --- a/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java +++ b/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java @@ -4,6 +4,7 @@ import com.flemmli97.flan.IClaimData; import com.flemmli97.flan.claim.Claim; import com.flemmli97.flan.claim.ClaimStorage; import com.flemmli97.flan.claim.ParticleIndicators; +import com.flemmli97.flan.claim.PermHelper; import com.flemmli97.flan.config.ConfigHandler; import com.google.common.collect.Sets; import com.google.gson.JsonObject; @@ -11,8 +12,10 @@ import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.packet.s2c.play.ParticleS2CPacket; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Formatting; import net.minecraft.util.WorldSavePath; import net.minecraft.util.math.BlockPos; @@ -267,12 +270,14 @@ public class PlayerClaimData { return usedClaimsBlocks; } - public static void readGriefPreventionPlayerData(MinecraftServer server) { + public static void readGriefPreventionPlayerData(MinecraftServer server, ServerCommandSource src) { File griefPrevention = server.getSavePath(WorldSavePath.ROOT).resolve("plugins/GriefPreventionData/PlayerData").toFile(); if (!griefPrevention.exists()) return; - try { - for (File f : griefPrevention.listFiles()) { + for (File f : griefPrevention.listFiles()) { + try { + if (f.getName().contains(".")) + continue; if (f.getName().startsWith("$")) { } else { @@ -301,8 +306,9 @@ public class PlayerClaimData { reader.close(); } } - } catch (IOException e) { - e.printStackTrace(); + catch (Exception e){ + src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.errorFile, f.getName(), Formatting.RED)), false); + } } } }