From 9a45382c6ee4ebe0fc8de8ed3fcd1031ce7b1a38 Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Mon, 24 Aug 2020 21:46:25 +0200 Subject: [PATCH] finishing config --- .../com/flemmli97/flan/config/Config.java | 48 ++++++++--- .../com/flemmli97/flan/config/LangConfig.java | 79 ++++++++++++------- .../flan/event/ItemInteractEvents.java | 17 +++- 3 files changed, 101 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/flemmli97/flan/config/Config.java b/src/main/java/com/flemmli97/flan/config/Config.java index 653b352..66f1434 100644 --- a/src/main/java/com/flemmli97/flan/config/Config.java +++ b/src/main/java/com/flemmli97/flan/config/Config.java @@ -5,16 +5,19 @@ import com.google.gson.JsonObject; import net.minecraft.item.Item; import net.minecraft.item.Items; import net.minecraft.server.MinecraftServer; +import net.minecraft.util.Identifier; import net.minecraft.util.WorldSavePath; import net.minecraft.util.registry.Registry; +import org.lwjgl.system.CallbackI; import java.io.File; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Config { - private final File configDir; + private File config; public int startingBlocks = 500; public int maxClaimBlocks = 5000; @@ -31,29 +34,50 @@ public class Config { public int claimDisplayTime = 1000; public Config(MinecraftServer server) { - this.configDir = server.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile(); + File configDir = server.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile(); try { - if(!this.configDir.exists()) - this.configDir.mkdirs(); - File file = new File(this.configDir, "flan_config.json"); - if(!file.exists()) - file.createNewFile(); - this.save(file); + if(!configDir.exists()) + configDir.mkdirs(); + this.config = new File(configDir, "flan_config.json"); + if(!this.config.exists()) { + this.config.createNewFile(); + this.save(); + } } catch (IOException e) { e.printStackTrace(); } } - public void load() { - + try { + FileReader reader = new FileReader(this.config); + JsonObject obj = ConfigHandler.GSON.fromJson(reader, JsonObject.class); + reader.close(); + this.startingBlocks = obj.get("startingBlocks").getAsInt(); + this.maxClaimBlocks = obj.get("maxClaimBlocks").getAsInt(); + this.ticksForNextBlock = obj.get("ticksForNextBlock").getAsInt(); + this.minClaimsize = obj.get("minClaimsize").getAsInt(); + this.defaultClaimDepth = obj.get("defaultClaimDepth").getAsInt(); + JsonArray arr = obj.getAsJsonArray("blacklistedWorlds"); + this.blacklistedWorlds = new String[arr.size()]; + for(int i = 0; i < arr.size(); i ++) + this.blacklistedWorlds[i] = arr.get(i).getAsString(); + this.worldWhitelist = obj.get("worldWhitelist").getAsBoolean(); + this.claimingItem = Registry.ITEM.get(new Identifier((obj.get("claimingItem").getAsString()))); + this.inspectionItem = Registry.ITEM.get(new Identifier((obj.get("inspectionItem").getAsString()))); + this.claimDisplayTime = obj.get("claimDisplayTime").getAsInt(); + } catch (IOException e) { + e.printStackTrace(); + } } - private void save(File file) { + private void save() { JsonObject obj = new JsonObject(); obj.addProperty("startingBlocks", this.startingBlocks); obj.addProperty("maxClaimBlocks", this.maxClaimBlocks); obj.addProperty("ticksForNextBlock", this.ticksForNextBlock); + obj.addProperty("minClaimsize", this.minClaimsize); + obj.addProperty("defaultClaimDepth", this.defaultClaimDepth); JsonArray arr = new JsonArray(); obj.add("blacklistedWorlds", arr); obj.addProperty("worldWhitelist", this.worldWhitelist); @@ -61,7 +85,7 @@ public class Config { obj.addProperty("inspectionItem", Registry.ITEM.getId(this.inspectionItem).toString()); obj.addProperty("claimDisplayTime", this.claimDisplayTime); try { - FileWriter writer = new FileWriter(file); + FileWriter writer = new FileWriter(this.config); ConfigHandler.GSON.toJson(obj, writer); writer.close(); } catch (IOException e) { diff --git a/src/main/java/com/flemmli97/flan/config/LangConfig.java b/src/main/java/com/flemmli97/flan/config/LangConfig.java index 5dbf430..2c9fcdb 100644 --- a/src/main/java/com/flemmli97/flan/config/LangConfig.java +++ b/src/main/java/com/flemmli97/flan/config/LangConfig.java @@ -6,33 +6,47 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.util.WorldSavePath; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.lang.reflect.Field; public class LangConfig { - private final File configDir; + private File config; public String noClaim = "There is no claim here."; - public String noPermission = "You don't have the required permissions to do that here!"; - public String noPermissionSimple = "Sorry you can't do that here!"; - public String configReload = "Configs reloaded"; public String inspectBlockOwner = "This is %1$s's claim"; public String inspectNoClaim = "Nobody owns this block"; - public String cantClaimHere = "Sorry you cant claim here"; public String claimBlocksFormat = "Claim Blocks: %1$d + (Bonus) %2$d; Used: %3$d"; public String listClaims = "Listing all claims:"; + + public String noPermission = "You don't have the required permissions to do that here!"; + public String noPermissionSimple = "Sorry you can't do that here!"; + + public String configReload = "Configs reloaded"; + + public String cantClaimHere = "Sorry you cant claim here"; + public String minClaimSize = "This is too small. Minimum claimsize is %d"; + public String landClaimDisabledWorld = "Claiming is disabled in this world"; + public String editMode = "Editing mode set to %s"; + public String notEnoughBlocks = "Not enough claim blocks"; + public String conflictOther = "Claim would overlap other claims"; + public String wrongMode = "Wrong claim mode. You are in %s-mode"; + public String stringScreenReturn = "Click on paper to go back"; + public String groupAdd = "Added group %s"; public String groupRemove = "Removed group %s"; public String groupExist = "Group already exist"; public String playerModify = "Modified permission group for following players to %1$s: %2$s"; public String playerModifyNo = "Couldn't set permission group for the players. Probably cause they already belong to a group"; + public String playerGroupAddFail = "Couldn't add that player to the group either cause the player " + + "is already in a group or no player matching the name was found"; public String resizeClaim = "Resizing claim"; public String resizeSuccess = "Resized Claims"; public String claimCreateSuccess = "Created a new claim"; public String subClaimCreateSuccess = "Created a new subclaim"; - - public String minClaimSize = "This is too small. Minimum claimsize is %d"; public String deleteClaim = "Claim deleted"; public String deleteAllClaimConfirm = "Are you sure you want to delete all claims? Type it again to confirm"; public String deleteAllClaim = "All claims deleted"; @@ -40,19 +54,12 @@ public class LangConfig { public String deleteSubClaim = "Subclaim deleted"; public String deleteSubClaimAll = "All Subclaims from this claim deleted"; + public String adminMode = "Adminmode (Ignore Claims) set to: %s"; public String adminDeleteAll = "Deleted all claims for following players: %s"; public String setAdminClaim = "Claim changed to an Adminclaim"; public String readGriefpreventionData = "Reading data from GriefPrevention"; public String readGriefpreventionDataSuccess = "Successfully read data"; public String giveClaimBlocks = "Gave following players %2$d claimblocks: %1$s"; - public String adminMode = "Adminmode (Ignore Claims) set to: %s"; - public String editMode = "Editing mode set to %s"; - public String notEnoughBlocks = "Not enough claim blocks"; - public String conflictOther = "Claim would overlap other claims"; - public String stringScreenReturn = "Click on paper to go back"; - public String wrongMode = "Wrong claim mode. You are in %s-mode"; - public String playerGroupAddFail = "Couldn't add that player to the group either cause the player " + - "is already in a group or no player matching the name was found"; public String claimBasicInfo = "Owner: %1$s, from: [x=%2$d,z=%3$d] to [x=%4$d,z=%5$d]; Subclaim-amount: %6$d"; public String claimInfoPerms = "Permissions: %s"; @@ -60,34 +67,48 @@ public class LangConfig { public String claimGroupPerms = " Permissions: %s"; public String claimGroupPlayers = " Players: %s"; - public String landClaimDisabledWorld = "Claiming is disabled in this world"; - public LangConfig(MinecraftServer server) { - this.configDir = server.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile(); + File configDir = server.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile(); try { - if(!this.configDir.exists()) - this.configDir.mkdirs(); - File file = new File(this.configDir, "flan_lang.json"); - if(!file.exists()) - file.createNewFile(); - this.save(file); + if(!configDir.exists()) + configDir.mkdirs(); + this.config = new File(configDir, "flan_lang.json"); + if(!this.config.exists()) { + this.config.createNewFile(); + this.save(); + } } catch (IOException e) { e.printStackTrace(); } } public void load() { - + try { + FileReader reader = new FileReader(this.config); + JsonObject obj = ConfigHandler.GSON.fromJson(reader, JsonObject.class); + reader.close(); + for(Field field : this.getClass().getDeclaredFields()){ + if(field.getDeclaringClass().equals(String.class)){ + field.set(this, obj.get(field.getName()).getAsString()); + } + } + } catch (IOException | IllegalAccessException e) { + e.printStackTrace(); + } } - private void save(File file){ + private void save(){ JsonObject obj = new JsonObject(); - try { - FileWriter writer = new FileWriter(file); + for(Field field : this.getClass().getDeclaredFields()){ + if(field.getType().equals(String.class)){ + obj.addProperty(field.getName(), (String) field.get(this)); + } + } + FileWriter writer = new FileWriter(this.config); ConfigHandler.GSON.toJson(obj, writer); writer.close(); - } catch (IOException e) { + } catch (IOException | IllegalAccessException e) { e.printStackTrace(); } } diff --git a/src/main/java/com/flemmli97/flan/event/ItemInteractEvents.java b/src/main/java/com/flemmli97/flan/event/ItemInteractEvents.java index 79f009c..7d8da68 100644 --- a/src/main/java/com/flemmli97/flan/event/ItemInteractEvents.java +++ b/src/main/java/com/flemmli97/flan/event/ItemInteractEvents.java @@ -59,13 +59,26 @@ public class ItemInteractEvents { return TypedActionResult.pass(stack); } - public static void claimLandHandling(ServerPlayerEntity player, BlockPos target){ + private static boolean cantClaimInWorld(ServerWorld world){ for(String s : ConfigHandler.config.blacklistedWorlds){ - if(s.equals(player.getServerWorld().getRegistryKey().getValue().toString())) { + if(s.equals(world.getRegistryKey().getValue().toString())) { + return true; + } + } + return false; + } + + public static void claimLandHandling(ServerPlayerEntity player, BlockPos target){ + if(ConfigHandler.config.worldWhitelist){ + if(!cantClaimInWorld(player.getServerWorld())) { player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.landClaimDisabledWorld, Formatting.DARK_RED), false); return; } } + else if(cantClaimInWorld(player.getServerWorld())) { + player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.landClaimDisabledWorld, Formatting.DARK_RED), false); + return; + } ClaimStorage storage = ClaimStorage.get(player.getServerWorld()); Claim claim = storage.getClaimAt(target); PlayerClaimData data = PlayerClaimData.get(player);