From 4a2862281b8c77cf4a5dd1593123c888610d2a62 Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 17 Oct 2024 12:14:25 +0300 Subject: [PATCH] Better block ownership saving Better feedback on ownership manual removal --- src/main/java/com/gamingmesh/jobs/Jobs.java | 2 +- .../jobs/commands/list/clearownership.java | 7 +- .../jobs/config/GeneralConfigManager.java | 7 +- .../jobs/config/LanguageManager.java | 2 +- .../blockOwnerShip/BlockOwnerShip.java | 129 ++++++++---------- .../container/blockOwnerShip/BlockTypes.java | 46 ++++--- 6 files changed, 98 insertions(+), 95 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index f8c348df..382fb83a 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -936,7 +936,7 @@ public final class Jobs extends JavaPlugin { if (dao != null && Jobs.getGeneralConfigManager().ExploreSaveIntoDatabase) dao.saveExplore(); - blockOwnerShipsMaterial.values().forEach(BlockOwnerShip::save); + BlockOwnerShip.save(blockOwnerShipsMaterial); if (saveTask != null) saveTask.shutdown(); diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/clearownership.java b/src/main/java/com/gamingmesh/jobs/commands/list/clearownership.java index 541b906d..720f5bba 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/clearownership.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/clearownership.java @@ -4,11 +4,13 @@ import java.util.Map; import java.util.UUID; import java.util.WeakHashMap; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.commands.Cmd; +import com.gamingmesh.jobs.commands.JobsCommands; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; import com.gamingmesh.jobs.i18n.Language; @@ -65,8 +67,11 @@ public class clearownership implements Cmd { } } - Language.sendMessage(sender,"command.clearownership.output.cleared", "[furnaces]", amounts.getOrDefault(BlockTypes.FURNACE, 0), "[brewing]", amounts.getOrDefault( + Language.sendMessage(sender, "command.clearownership.output.cleared", "[furnaces]", amounts.getOrDefault(BlockTypes.FURNACE, 0), "[brewing]", amounts.getOrDefault( BlockTypes.BREWING_STAND, 0), "[smoker]", amounts.getOrDefault(BlockTypes.SMOKER, 0), "[blast]", amounts.getOrDefault(BlockTypes.BLAST_FURNACE, 0)); + + Bukkit.dispatchCommand(sender, JobsCommands.LABEL + " " + ownedblocks.class.getSimpleName() + (sender.getName().equals(jPlayer.getName()) ? "" : jPlayer.getName())); + return true; } } diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index 7d050f73..274dea03 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -870,17 +870,18 @@ public class GeneralConfigManager { FurnacesMaxDefault = c.get("ExploitProtections.Furnaces.MaxDefaultAvailable", 20); if (Version.isCurrentEqualOrHigher(Version.v1_14_R1)) { - BlastFurnacesReassign = c.get("ExploitProtections.BlastFurnaces.Reassign", false); + BlastFurnacesReassign = c.get("ExploitProtections.BlastFurnaces.Reassign", true); BlastFurnacesMaxDefault = c.get("ExploitProtections.BlastFurnaces.MaxDefaultAvailable", 15); - SmokerReassign = c.get("ExploitProtections.Smokers.Reassign", false); + SmokerReassign = c.get("ExploitProtections.Smokers.Reassign", true); SmokersMaxDefault = c.get("ExploitProtections.Smokers.MaxDefaultAvailable", 15); - } + } c.addComment("ExploitProtections.BrewingStands.Reassign", "When enabled, players interacted brewing stands will be saved into file and will be reassigned after restart to keep giving out money", "Players will no longer need to click on brewing stand to get paid from it after server restart"); BrewingStandsReassign = c.get("ExploitProtections.BrewingStands.Reassign", true); + c.addComment("ExploitProtections.BrewingStands.MaxDefaultAvailable", "Defines max available brewing stands each player can have to get paid from", "Set to 0 if you want to disable this limitation", diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index 06d5f275..4d820e86 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -495,7 +495,7 @@ public class LanguageManager { c.get("command.clearownership.help.info", "Clear block ownership"); c.get("command.clearownership.help.args", "[playername]"); Jobs.getGCManager().getCommandArgs().put("clearownership", Arrays.asList("[playername]")); - c.get("command.clearownership.output.cleared", "&2Removed &7[furnaces] &2furnaces, &7[brewing] &2brewing stands, &7[smoker]&2 smokers and &7[blast]&2 blast furnaces."); + c.get("command.clearownership.output.cleared", "&2Removed &7[furnaces] &2furnaces &7[brewing] &2brewing &7[smoker]&2 smokers &7[blast]&2 blast"); c.get("command.clearownership.output.lost", "&cLost ownership of &7[type] &cat [location]"); c.get("command.skipquest.help.info", "Skip defined quest and get new one"); diff --git a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java index 9dbad2d2..3febcabb 100644 --- a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java +++ b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java @@ -9,7 +9,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.UUID; -import com.gamingmesh.jobs.api.JobsBlockOwnershipRegisterEvent; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; @@ -19,12 +18,15 @@ import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.MetadataValue; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.api.JobsBlockOwnershipRegisterEvent; import com.gamingmesh.jobs.config.YmlMaker; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.stuff.blockLoc; import net.Zrips.CMILib.Container.CMILocation; +import net.Zrips.CMILib.FileHandler.ConfigReader; import net.Zrips.CMILib.Items.CMIMaterial; +import net.Zrips.CMILib.Logs.CMIDebug; import net.Zrips.CMILib.Messages.CMIMessages; public class BlockOwnerShip { @@ -127,10 +129,10 @@ public class BlockOwnerShip { if (ownerUUID != null && !ownerUUID.equals(player.getUniqueId())) { if (Jobs.getGCManager().blockOwnershipTakeOver) { - + if (Jobs.getPermissionManager().hasPermission(jPlayer, "jobs.noownershiptakeover")) return ownershipFeedback.invalid; - + // Removing ownership to record new player this.remove(ownerUUID, CMILocation.toString(block.getLocation(), ":", true, true)); block.removeMetadata(metadataName, plugin); @@ -290,8 +292,9 @@ public class BlockOwnerShip { public int remove(UUID uuid, String location) { HashMap ls = blockOwnerShips.get(uuid); + int ret = 0; if (ls == null) - return 0; + return ret; for (Entry one : new HashMap(ls).entrySet()) { @@ -303,11 +306,13 @@ public class BlockOwnerShip { ls.remove(one.getKey()); Map oldRecord = ownerMapByLocation.get(one.getValue().getWorldName()); - if (oldRecord != null) + if (oldRecord != null) { oldRecord.remove(one.getValue().toVectorString()); + ret++; + } } - return 1; + return ret; } public List getBlockMetadatas(Block block) { @@ -320,34 +325,12 @@ public class BlockOwnerShip { } public void load() { - YmlMaker f = new YmlMaker(Jobs.getFolder(), "furnaceBrewingStands.yml"); - YmlMaker f2 = new YmlMaker(Jobs.getFolder(), "blockOwnerShips.yml"); - if (!f.exists() && !f2.exists()) + + YmlMaker f = new YmlMaker(Jobs.getFolder(), "blockOwnerShips.yml"); + if (!f.exists()) return; - if (f.exists()) { - f.getConfigFile().renameTo(f2.getConfigFile()); - } - - f = f2; - - String path = ""; - switch (type) { - case BLAST_FURNACE: - path = "BlastFurnace"; - break; - case BREWING_STAND: - path = "Brewing"; - break; - case FURNACE: - path = "Furnace"; - break; - case SMOKER: - path = "Smoker"; - break; - default: - break; - } + String path = type.getPath(); if (isReassignDisabled()) return; @@ -375,17 +358,24 @@ public class BlockOwnerShip { } HashMap blist = new HashMap(); + boolean informed = false; for (String oneL : ls) { - blockLoc bl = new blockLoc(oneL); - CMILocation cmil = CMILocation.fromString(oneL, ":"); + try { + blockLoc bl = new blockLoc(oneL); - blist.put(CMILocation.toString(cmil, ":", true, true), bl); + blist.put(oneL, bl); - Map oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap()); - oldRecord.put(bl.toVectorString(), uuid); - ownerMapByLocation.put(bl.getWorldName(), oldRecord); + Map oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap()); + oldRecord.put(bl.toVectorString(), uuid); + ownerMapByLocation.put(bl.getWorldName(), oldRecord); - total++; + total++; + + } catch (Throwable e) { + if (!informed) + e.printStackTrace(); + informed = true; + } } if (!blist.isEmpty()) { @@ -398,46 +388,45 @@ public class BlockOwnerShip { } } - public void save() { - YmlMaker f = new YmlMaker(Jobs.getFolder(), "furnaceBrewingStands.yml"); - if (f.exists()) { - f.getConfigFile().renameTo(new File(Jobs.getFolder(), "blockOwnerShips.yml")); + public static void save(HashMap blockOwnerShipsMaterial) { + + File f = new File(Jobs.getInstance().getDataFolder(), "blockOwnerShips.yml"); + + ConfigReader cfg = null; + try { + cfg = new ConfigReader(f); + } catch (Exception e) { + e.printStackTrace(); } - f = new YmlMaker(Jobs.getFolder(), "blockOwnerShips.yml"); - - if (blockOwnerShips.isEmpty() && f.getConfigFile().length() == 0L) { - f.getConfigFile().delete(); + if (cfg == null) return; - } - f.createNewFile(); - f.saveDefaultConfig(); + cfg.getC().options().copyDefaults(true); - if (isReassignDisabled()) { - return; - } - - String path = (type == BlockTypes.FURNACE ? "Furnace" - : type == BlockTypes.BLAST_FURNACE ? "BlastFurnace" - : type == BlockTypes.BREWING_STAND ? "Brewing" : type == BlockTypes.SMOKER ? "Smoker" : ""); - f.getConfig().set(path, null); - - for (Entry> one : blockOwnerShips.entrySet()) { - StringBuilder full = new StringBuilder(); - - for (String oneL : one.getValue().keySet()) { - if (!full.toString().isEmpty()) - full.append(";"); - - full.append(oneL); + for (BlockOwnerShip ownership : blockOwnerShipsMaterial.values()) { + if (ownership.isReassignDisabled()) { + return; } + String path = ownership.getType().getPath(); - if (!full.toString().isEmpty()) - f.getConfig().set(path + "." + one.getKey().toString(), full.toString()); + cfg.getC().set(path, null); + + for (Entry> one : ownership.blockOwnerShips.entrySet()) { + StringBuilder full = new StringBuilder(); + + for (String oneL : one.getValue().keySet()) { + if (!full.toString().isEmpty()) + full.append(";"); + full.append(oneL); + } + + if (!full.toString().isEmpty()) + cfg.get(path + "." + one.getKey().toString(), full.toString()); + } } - f.saveConfig(); + cfg.save(); } public boolean isReassignDisabled() { diff --git a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java index 3d669d8e..517fc9fe 100644 --- a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java +++ b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java @@ -6,39 +6,47 @@ import net.Zrips.CMILib.Items.CMIMaterial; public enum BlockTypes { - BREWING_STAND("BREWING_STAND", "LEGACY_BREWING_STAND"), - FURNACE("FURNACE", "LEGACY_BURNING_FURNACE"), - SMOKER, - BLAST_FURNACE; + BREWING_STAND("Brewing", "BREWING_STAND", "LEGACY_BREWING_STAND"), + FURNACE("Furnace", "FURNACE", "LEGACY_BURNING_FURNACE"), + SMOKER("Smoker"), + BLAST_FURNACE("BlastFurnace"); private String[] names; + private String path = "_"; private static HashMap cache = new HashMap<>(); + static { - for (CMIMaterial one : CMIMaterial.values()) { - for (BlockTypes b : values()) { - for (String name : b.names) { - if (name.equals(one.toString())) { - cache.put(one, b); - } - } - } - } + for (CMIMaterial one : CMIMaterial.values()) { + for (BlockTypes b : values()) { + for (String name : b.names) { + if (name.equals(one.toString())) { + cache.put(one, b); + } + } + } + } } - BlockTypes() { - names = new String[] { toString() }; + BlockTypes(String path) { + this.path = path; + names = new String[] { toString() }; } - BlockTypes(String... names) { - this.names = names; + BlockTypes(String path, String... names) { + this.path = path; + this.names = names; } public String[] getNames() { - return names; + return names; } public static BlockTypes getFromCMIMaterial(CMIMaterial type) { - return cache.get(type); + return cache.get(type); + } + + public String getPath() { + return path; } }