From 3059833910e88c903e5b25b76de976ed53b1ec1c Mon Sep 17 00:00:00 2001 From: montlikadani Date: Mon, 12 Oct 2020 12:42:50 +0200 Subject: [PATCH] Rework how the brewing stand, smoker... getting registered As of new blocks like smoker, blast furnace this owner ship registration should be reworked to make more readable and less code. In this change it has so many deprecations, but its commented out. --- src/main/java/com/gamingmesh/jobs/Jobs.java | 67 ++++- .../jobs/Placeholders/Placeholder.java | 6 +- .../jobs/commands/list/clearownership.java | 18 +- .../jobs/config/GeneralConfigManager.java | 3 +- .../jobs/config/LanguageManager.java | 12 +- .../com/gamingmesh/jobs/config/YmlMaker.java | 9 +- .../gamingmesh/jobs/container/JobsPlayer.java | 50 +++- .../blockOwnerShip/BlockOwnerShip.java | 283 ++++++++++++++++++ .../container/blockOwnerShip/BlockTypes.java | 35 +++ .../jobs/listeners/JobsPaymentListener.java | 113 +++---- .../jobs/stuff/FurnaceBrewingHandling.java | 8 +- 11 files changed, 497 insertions(+), 107 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java create mode 100644 src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index c67d36ae..0b03c0d7 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -22,6 +22,7 @@ import com.gamingmesh.jobs.CMILib.RawMessage; import com.gamingmesh.jobs.CMILib.Version; import com.gamingmesh.jobs.CMILib.ActionBarManager; import com.gamingmesh.jobs.CMILib.CMIChatColor; +import com.gamingmesh.jobs.CMILib.CMIMaterial; import com.gamingmesh.jobs.CMILib.CMIReflections; import com.gamingmesh.jobs.CMILib.VersionChecker; import com.gamingmesh.jobs.Gui.GuiManager; @@ -34,6 +35,8 @@ import com.gamingmesh.jobs.api.JobsPrePaymentEvent; import com.gamingmesh.jobs.commands.JobsCommands; import com.gamingmesh.jobs.config.*; import com.gamingmesh.jobs.container.*; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.dao.JobsManager; @@ -83,6 +86,8 @@ public class Jobs extends JavaPlugin { private static BlockProtectionManager bpManager; private static JobsManager dbManager; + private final Set blockOwnerShips = new HashSet<>(); + private static PistonProtectionListener pistonProtectionListener; private static ConfigManager configManager; @@ -116,6 +121,50 @@ public class Jobs extends JavaPlugin { private static PointsData pointsDatabase; + public Optional getBlockOwnerShip(CMIMaterial type) { + return getBlockOwnerShip(type, true); + } + + public Optional getBlockOwnerShip(CMIMaterial type, boolean addNew) { + if (((type == CMIMaterial.FURNACE || type == CMIMaterial.LEGACY_BURNING_FURNACE) && !gConfigManager.isFurnacesReassign()) + || (type == CMIMaterial.BLAST_FURNACE && !gConfigManager.BlastFurnacesReassign) + || ((type == CMIMaterial.BREWING_STAND || type == CMIMaterial.LEGACY_BREWING_STAND) && !gConfigManager.isBrewingStandsReassign()) + || (type == CMIMaterial.SMOKER && !gConfigManager.SmokerReassign)) { + return Optional.empty(); + } + + BlockOwnerShip b = null; + for (BlockOwnerShip ship : blockOwnerShips) { + if (ship.getMaterial() == type) { + b = ship; + break; + } + } + + if (addNew && b == null) { + b = new BlockOwnerShip(type); + blockOwnerShips.add(b); + } + + return Optional.ofNullable(b); + } + + public Optional getBlockOwnerShip(BlockTypes type) { + BlockOwnerShip b = null; + for (BlockOwnerShip ship : blockOwnerShips) { + if (ship.getType() == type) { + b = ship; + break; + } + } + + return Optional.ofNullable(b); + } + + public Set getBlockOwnerShips() { + return blockOwnerShips; + } + public static PistonProtectionListener getPistonProtectionListener() { if (pistonProtectionListener == null) pistonProtectionListener = new PistonProtectionListener(); @@ -736,7 +785,17 @@ public class Jobs extends JavaPlugin { getDBManager().getDB().loadAllJobsWorlds(); getDBManager().getDB().loadAllJobsNames(); - FurnaceBrewingHandling.load(); + instance.getBlockOwnerShip(CMIMaterial.FURNACE).ifPresent(BlockOwnerShip::load); + instance.getBlockOwnerShip(CMIMaterial.BREWING_STAND).ifPresent(BlockOwnerShip::load); + if (Version.isCurrentEqualOrLower(Version.v1_13_R1)) { + instance.getBlockOwnerShip(CMIMaterial.LEGACY_BREWING_STAND).ifPresent(BlockOwnerShip::load); + instance.getBlockOwnerShip(CMIMaterial.LEGACY_BURNING_FURNACE).ifPresent(BlockOwnerShip::load); + } + if (Version.isCurrentEqualOrHigher(Version.v1_14_R1)) { + instance.getBlockOwnerShip(CMIMaterial.BLAST_FURNACE).ifPresent(BlockOwnerShip::load); + instance.getBlockOwnerShip(CMIMaterial.SMOKER).ifPresent(BlockOwnerShip::load); + } + ToggleBarHandling.load(); usedSlots.clear(); for (Job job : jobs) { @@ -773,12 +832,10 @@ public class Jobs extends JavaPlugin { HandlerList.unregisterAll(instance); -// GUIManager.CloseInventories(); -// shopManager.CloseInventories(); dao.saveExplore(); - getBpManager().saveCache(); - FurnaceBrewingHandling.save(); + + blockOwnerShips.forEach(BlockOwnerShip::save); ToggleBarHandling.save(); shutdown(); diff --git a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java index e104963f..27b2ee5f 100644 --- a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java +++ b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java @@ -15,13 +15,13 @@ import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.CMILib.CMIMaterial; import com.gamingmesh.jobs.container.CurrencyType; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.Title; import com.gamingmesh.jobs.container.TopList; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; import com.gamingmesh.jobs.stuff.TimeManage; public class Placeholder { @@ -405,11 +405,11 @@ public class Placeholder { case user_bstandcount: return Integer.toString(user.getBrewingStandCount()); case user_maxbstandcount: - return Integer.toString(user.getMaxBrewingStandsAllowed()); + return Integer.toString(user.getMaxOwnerShipAllowed(BlockTypes.BREWING_STAND)); case user_furncount: return Integer.toString(user.getFurnaceCount()); case user_maxfurncount: - return Integer.toString(user.getMaxFurnacesAllowed(CMIMaterial.FURNACE)); + return Integer.toString(user.getMaxOwnerShipAllowed(BlockTypes.FURNACE)); case user_doneq: return Integer.toString(user.getDoneQuests()); case user_seen: 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 5ac3a847..386be5b4 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/clearownership.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/clearownership.java @@ -4,10 +4,12 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.CMILib.Version; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.JobsPlayer; -import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; public class clearownership implements Cmd { @@ -30,10 +32,18 @@ public class clearownership implements Cmd { return true; } - int furnace = FurnaceBrewingHandling.clearFurnaces(jPlayer.getUniqueId()); - int brewing = FurnaceBrewingHandling.clearBrewingStands(jPlayer.getUniqueId()); + BlockOwnerShip furnaceOwnerShip = plugin.getBlockOwnerShip(BlockTypes.FURNACE).orElse(null), + brewingOwnerShip = plugin.getBlockOwnerShip(BlockTypes.BREWING_STAND).orElse(null), + smokerOwnerShip = plugin.getBlockOwnerShip(BlockTypes.SMOKER).orElse(null), + blastFurnaceOwnerShip = plugin.getBlockOwnerShip(BlockTypes.BLAST_FURNACE).orElse(null); + int furnace = furnaceOwnerShip != null ? furnaceOwnerShip.clear(jPlayer.getUniqueId()) : 0, + brewing = brewingOwnerShip != null ? brewingOwnerShip.clear(jPlayer.getUniqueId()) : 0, + smoker = smokerOwnerShip != null ? smokerOwnerShip.clear(jPlayer.getUniqueId()) : 0, + blast = blastFurnaceOwnerShip != null ? blastFurnaceOwnerShip.clear(jPlayer.getUniqueId()) : 0; - sender.sendMessage(Jobs.getLanguage().getMessage("command.clearownership.output.cleared", "[furnaces]", furnace, "[brewing]", brewing)); + sender.sendMessage(Jobs.getLanguage().getMessage("command.clearownership.output.cleared", "[furnaces]", furnace, + "[brewing]", brewing, "[smoker]", Version.isCurrentEqualOrHigher(Version.v1_14_R1) ? smoker : "", + "[blast]", Version.isCurrentEqualOrHigher(Version.v1_14_R1) ? blast : "")); 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 e1d6510b..c01a519f 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -102,7 +102,8 @@ public class GeneralConfigManager { hideJobsInfoWithoutPermission, UseTaxes, TransferToServerAccount, TakeFromPlayersPayment, AutoJobJoinUse, AllowDelevel, BossBarEnabled, BossBarShowOnEachAction, BossBarsMessageByDefault, ExploreCompact, DBCleaningJobsUse, DBCleaningUsersUse, DisabledWorldsUse, UseAsWhiteListWorldList, PaymentMethodsMoney, PaymentMethodsPoints, PaymentMethodsExp, MythicMobsEnabled, - LoggingUse, payForCombiningItems, BlastFurnacesReassign, SmokerReassign, payForStackedEntities, payForEachVTradeItem; + LoggingUse, payForCombiningItems, BlastFurnacesReassign = false, SmokerReassign = false, payForStackedEntities, + payForEachVTradeItem; public ItemStack guiBackButton, guiNextButton, guiFiller; diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index e5c1508f..700ac33f 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -81,6 +81,10 @@ public class LanguageManager { c.get("general.info.invalidPage", "&cInvalid page"); c.get("general.info.true", "&2True"); c.get("general.info.false", "&cFalse"); + c.get("general.info.blocks.furnace", "Furnace"); + c.get("general.info.blocks.smoker", "Smoker"); + c.get("general.info.blocks.blastfurnace", "Blast furnace"); + c.get("general.info.blocks.brewingstand", "Brewing stand"); c.get("general.admin.error", "&cThere was an error in the command."); c.get("general.admin.success", "&eYour command has been performed."); c.get("general.error.noHelpPage", "&cThere is no help page by this number!"); @@ -94,10 +98,8 @@ public class LanguageManager { c.get("general.error.fromconsole", "&cYou can use this command only from console!"); c.get("general.error.worldisdisabled", "&cYou can't use command in this world!"); - c.get("general.error.newFurnaceRegistration", "&eRegistered new ownership for furnace &7[current]&e/&f[max]"); - c.get("general.error.newBrewingRegistration", "&eRegistered new ownership for brewing stand &7[current]&e/&f[max]"); - c.get("general.error.noFurnaceRegistration", "&cYou've reached max furnace count!"); - c.get("general.error.noBrewingRegistration", "&cYou've reached max brewing stand count!"); + c.get("general.error.newRegistration", "&eRegistered new ownership for [block] &7[current]&e/&f[max]"); + c.get("general.error.noRegistration", "&cYou've reached max [block] count!"); c.get("command.help.output.info", "Type /jobs [cmd] ? for more information about a command."); c.get("command.help.output.cmdUsage", "&2Usage: &7[command]"); @@ -458,7 +460,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 and &7[brewing] &2brewing stands"); + 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.skipquest.help.info", "Skip defined quest and get new one"); c.get("command.skipquest.help.args", "[jobname] [questname] (playerName)"); diff --git a/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java b/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java index 45c6e800..26acf5bf 100644 --- a/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java +++ b/src/main/java/com/gamingmesh/jobs/config/YmlMaker.java @@ -12,6 +12,8 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.java.JavaPlugin; +import com.gamingmesh.jobs.Jobs; + public class YmlMaker { public String fileName; private JavaPlugin plugin; @@ -25,10 +27,7 @@ public class YmlMaker { this.plugin = plugin; this.fileName = fileName; - File dataFolder = plugin.getDataFolder(); - if (!dataFolder.exists()) - dataFolder.mkdirs(); - ConfigFile = new File(dataFolder, fileName); + ConfigFile = new File(Jobs.getFolder(), fileName); } public void reloadConfig() { @@ -56,7 +55,7 @@ public class YmlMaker { public File getConfigFile() { if (ConfigFile == null) - ConfigFile = new File(plugin.getDataFolder(), fileName); + ConfigFile = new File(Jobs.getFolder(), fileName); return ConfigFile; } diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index 4656f241..7aff4351 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -36,10 +36,10 @@ import com.gamingmesh.jobs.CMILib.ActionBarManager; import com.gamingmesh.jobs.CMILib.CMIChatColor; import com.gamingmesh.jobs.CMILib.CMIMaterial; import com.gamingmesh.jobs.Signs.SignTopType; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.economy.PaymentData; import com.gamingmesh.jobs.resources.jfep.Parser; -import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; import com.gamingmesh.jobs.stuff.TimeManage; public class JobsPlayer { @@ -1190,14 +1190,31 @@ public class JobsPlayer { } } + /** + * @deprecated {@link Jobs#getBlockOwnerShip(BlockTypes)} + * @return the furnace count + */ + @Deprecated public int getFurnaceCount() { - return FurnaceBrewingHandling.getTotalFurnaces(getUniqueId()); + return !Jobs.getInstance().getBlockOwnerShip(BlockTypes.FURNACE).isPresent() ? 0 : + Jobs.getInstance().getBlockOwnerShip(BlockTypes.FURNACE).get().getTotal(getUniqueId()); } + /** + * @deprecated {@link Jobs#getBlockOwnerShip(BlockTypes)} + * @return the brewing stand count + */ + @Deprecated public int getBrewingStandCount() { - return FurnaceBrewingHandling.getTotalBrewingStands(getUniqueId()); + return !Jobs.getInstance().getBlockOwnerShip(BlockTypes.BREWING_STAND).isPresent() ? 0 : + Jobs.getInstance().getBlockOwnerShip(BlockTypes.BREWING_STAND).get().getTotal(getUniqueId()); } + /** + * @deprecated use {@link #getMaxOwnerShipAllowed(CMIMaterial)} + * @return max allowed brewing stands + */ + @Deprecated public int getMaxBrewingStandsAllowed() { Double maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxbrewingstands"); @@ -1208,31 +1225,40 @@ public class JobsPlayer { } /** - * @deprecated use {@link #getMaxFurnacesAllowed(CMIMaterial)} - * @return + * @deprecated use {@link #getMaxOwnerShipAllowed(CMIMaterial)} + * @return the max allowed furnaces */ @Deprecated public int getMaxFurnacesAllowed() { - return getMaxFurnacesAllowed(CMIMaterial.FURNACE); + return getMaxOwnerShipAllowed(BlockTypes.FURNACE); } - public int getMaxFurnacesAllowed(CMIMaterial type) { - String perm = "jobs.max" + (type == CMIMaterial.FURNACE || type == CMIMaterial.LEGACY_BURNING_FURNACE - ? "furnaces" : type == CMIMaterial.BLAST_FURNACE ? "blastfurnaces" : type == CMIMaterial.SMOKER ? "smokers" : ""); + /** + * Returns the max allowed owner ship for the given block type. + * @param type {@link BlockTypes} + * @return max allowed owner ship + */ + public int getMaxOwnerShipAllowed(BlockTypes type) { + String perm = "jobs.max" + (type == BlockTypes.FURNACE + ? "furnaces" : type == BlockTypes.BLAST_FURNACE ? "blastfurnaces" : type == BlockTypes.SMOKER ? "smokers" : + type == BlockTypes.BREWING_STAND ? "brewingstands" : ""); if (perm.isEmpty()) return 0; Double maxV = Jobs.getPermissionManager().getMaxPermission(this, perm); - if (maxV == 0D && (type == CMIMaterial.FURNACE || type == CMIMaterial.LEGACY_BURNING_FURNACE)) + if (maxV == 0D && type == BlockTypes.FURNACE) maxV = (double) Jobs.getGCManager().getFurnacesMaxDefault(); - if (maxV == 0D && type == CMIMaterial.BLAST_FURNACE) + if (maxV == 0D && type == BlockTypes.BLAST_FURNACE) maxV = (double) Jobs.getGCManager().BlastFurnacesMaxDefault; - if (maxV == 0D && type == CMIMaterial.SMOKER) + if (maxV == 0D && type == BlockTypes.SMOKER) maxV = (double) Jobs.getGCManager().SmokersMaxDefault; + if (maxV == 0 && type == BlockTypes.BREWING_STAND) + maxV = (double) Jobs.getGCManager().getBrewingStandsMaxDefault(); + return maxV.intValue(); } diff --git a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java new file mode 100644 index 00000000..2ff26076 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java @@ -0,0 +1,283 @@ +package com.gamingmesh.jobs.container.blockOwnerShip; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; +import java.util.Map.Entry; + +import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; + +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.CMILib.CMIMaterial; +import com.gamingmesh.jobs.config.YmlMaker; +import com.gamingmesh.jobs.container.JobsPlayer; +import com.gamingmesh.jobs.stuff.blockLoc; + +public class BlockOwnerShip { + + private CMIMaterial material; + private BlockTypes type; + private String metadataName = ""; + + private final HashMap> blockOwnerShips = new HashMap<>(); + + public BlockOwnerShip(CMIMaterial type) { + // Type should be any type of furnace, smoker or brewing stand + if (type != CMIMaterial.FURNACE && type != CMIMaterial.LEGACY_BURNING_FURNACE + && type != CMIMaterial.BLAST_FURNACE && type != CMIMaterial.SMOKER && type != CMIMaterial.BREWING_STAND + && type != CMIMaterial.LEGACY_BREWING_STAND) { + throw new IllegalArgumentException("Material types should be any type of furnace, smoker or brewing stand"); + } + + material = type; + this.type = BlockTypes.getFromCMIMaterial(type); + + switch (this.type) { + case BLAST_FURNACE: + metadataName = "jobsBlastFurnaceOwner"; + break; + case BREWING_STAND: + metadataName = "jobsBrewingOwner"; + break; + case FURNACE: + metadataName = "jobsFurnaceOwner"; + break; + case SMOKER: + metadataName = "jobsSmokerOwner"; + break; + default: + break; + } + } + + public BlockTypes getType() { + return type; + } + + public CMIMaterial getMaterial() { + return material; + } + + public String getMetadataName() { + return metadataName; + } + + public HashMap> getBlockOwnerShips() { + return blockOwnerShips; + } + + public ownershipFeedback register(Player player, Block block) { + CMIMaterial mat = CMIMaterial.get(block); + if (type != BlockTypes.getFromCMIMaterial(mat)) { + return ownershipFeedback.invalid; + } + + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player); + if (jPlayer == null) { + return ownershipFeedback.invalid; + } + + int max = jPlayer.getMaxOwnerShipAllowed(type); + int have = getTotal(jPlayer.getUniqueId()); + + boolean owner = false; + List data = getBlockMetadatas(block); + if (!data.isEmpty()) { + // only care about first + MetadataValue value = data.get(0); + String uuid = value.asString(); + + if (!uuid.equals(player.getUniqueId().toString())) { + return ownershipFeedback.notOwn; + } + + if (have > max && max > 0) { + remove(block); + } + + owner = true; + } + + if (owner) + return ownershipFeedback.old; + + if (have >= max && max > 0) + return ownershipFeedback.tooMany; + + block.setMetadata(metadataName, new FixedMetadataValue(Jobs.getInstance(), player.getUniqueId().toString())); + + if (!Jobs.getGCManager().isBrewingStandsReassign() && !Jobs.getGCManager().isFurnacesReassign() + && !Jobs.getGCManager().BlastFurnacesReassign && !Jobs.getGCManager().SmokerReassign) { + return ownershipFeedback.newReg; + } + + List ls = blockOwnerShips.getOrDefault(player.getUniqueId(), new ArrayList<>()); + ls.add(new blockLoc(block.getLocation())); + blockOwnerShips.put(player.getUniqueId(), ls); + return ownershipFeedback.newReg; + } + + public boolean remove(Block block) { + UUID uuid = null; + List data = getBlockMetadatas(block); + if (!data.isEmpty()) { + // only care about first + MetadataValue value = data.get(0); + uuid = UUID.fromString(value.asString()); + } + + if (uuid == null) { + return false; + } + + List ls = blockOwnerShips.getOrDefault(uuid, new ArrayList<>()); + for (blockLoc one : ls) { + if (one.getLocation().equals(block.getLocation())) { + block.removeMetadata(metadataName, Jobs.getInstance()); + ls.remove(one); + return true; + } + } + + return false; + } + + public int clear(UUID uuid) { + List ls = blockOwnerShips.remove(uuid); + if (ls == null) + return 0; + + for (blockLoc one : ls) { + one.getBlock().removeMetadata(metadataName, Jobs.getInstance()); + } + + return ls.size(); + } + + public List getBlockMetadatas(Block block) { + return block.getMetadata(metadataName); + } + + public int getTotal(UUID uuid) { + return blockOwnerShips.getOrDefault(uuid, new ArrayList<>()).size(); + } + + public void load() { + YmlMaker f = new YmlMaker(Jobs.getInstance(), "furnaceBrewingStands.yml"); + YmlMaker f2 = new YmlMaker(Jobs.getInstance(), "blockOwnerShips.yml"); + if (!f.exists() && !f2.exists()) + return; + + if (f.exists()) { + f.getConfigFile().renameTo(f2.getConfigFile()); + } + + f = f2; + + int total = 0; + + FileConfiguration config = f.getConfig(); + + String path = (type == BlockTypes.FURNACE ? "Furnace" + : type == BlockTypes.BLAST_FURNACE ? "BlastFurnace" + : type == BlockTypes.BREWING_STAND ? "Brewing" : type == BlockTypes.SMOKER ? "Smoker" : ""); + + if (isReassignDisabled() || !config.isConfigurationSection(path)) + return; + + ConfigurationSection section = config.getConfigurationSection(path); + for (String one : section.getKeys(false)) { + String value = section.getString(one); + List ls = new ArrayList<>(); + if (value.contains(";")) + ls.addAll(Arrays.asList(value.split(";"))); + else + ls.add(value); + + UUID uuid = UUID.fromString(one); + if (uuid == null) + continue; + + List blist = new ArrayList<>(); + for (String oneL : ls) { + blockLoc bl = new blockLoc(oneL); + Block block = bl.getBlock(); + if (block == null) + continue; + + block.removeMetadata(metadataName, Jobs.getInstance()); + block.setMetadata(metadataName, new FixedMetadataValue(Jobs.getInstance(), one)); + + blist.add(bl); + total++; + } + + if (!blist.isEmpty()) { + blockOwnerShips.put(uuid, blist); + } + } + + if (total > 0) { + Jobs.consoleMsg("&e[Jobs] Loaded " + total + " " + path.toLowerCase() + " for reassigning."); + } + } + + public void save() { + YmlMaker f = new YmlMaker(Jobs.getInstance(), "furnaceBrewingStands.yml"); + if (f.exists()) { + f.getConfigFile().renameTo(new File(Jobs.getFolder(), "blockOwnerShips.yml")); + } + + f = new YmlMaker(Jobs.getInstance(), "blockOwnerShips.yml"); + if (!f.exists()) + f.createNewFile(); + + f.saveDefaultConfig(); + + FileConfiguration config = f.getConfig(); + + if (isReassignDisabled()) { + return; + } + + String path = (type == BlockTypes.FURNACE ? "Furnace" + : type == BlockTypes.BLAST_FURNACE ? "BlastFurnace" + : type == BlockTypes.BREWING_STAND ? "Brewing" : type == BlockTypes.SMOKER ? "Smoker" : ""); + config.set(path, null); + + for (Entry> one : blockOwnerShips.entrySet()) { + String full = ""; + + for (blockLoc oneL : one.getValue()) { + if (!full.isEmpty()) + full += ";"; + + full += oneL.toString(); + } + + if (!full.isEmpty()) + config.set(path + "." + one.getKey().toString(), full); + } + + f.saveConfig(); + } + + public boolean isReassignDisabled() { + return (type == BlockTypes.FURNACE && !Jobs.getGCManager().isFurnacesReassign()) + || (type == BlockTypes.BLAST_FURNACE && !Jobs.getGCManager().BlastFurnacesReassign) + || (type == BlockTypes.BREWING_STAND && !Jobs.getGCManager().isBrewingStandsReassign()) + || (type == BlockTypes.SMOKER && !Jobs.getGCManager().SmokerReassign); + } + + public enum ownershipFeedback { + invalid, tooMany, newReg, old, notOwn + } +} diff --git a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java new file mode 100644 index 00000000..db2975a1 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockTypes.java @@ -0,0 +1,35 @@ +package com.gamingmesh.jobs.container.blockOwnerShip; + +import com.gamingmesh.jobs.CMILib.CMIMaterial; + +public enum BlockTypes { + + BREWING_STAND("BREWING_STAND", "LEGACY_BREWING_STAND"), FURNACE("FURNACE", "LEGACY_BURNING_FURNACE"), SMOKER, + BLAST_FURNACE; + + private String[] names; + + BlockTypes() { + names = new String[] { toString() }; + } + + BlockTypes(String... names) { + this.names = names; + } + + public String[] getNames() { + return names; + } + + public static BlockTypes getFromCMIMaterial(CMIMaterial type) { + for (BlockTypes b : values()) { + for (String name : b.names) { + if (name.equals(type.name())) { + return b; + } + } + } + + return null; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 09a5dcee..8cee8a9d 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -23,9 +23,9 @@ import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.actions.*; import com.gamingmesh.jobs.api.JobsChunkChangeEvent; import com.gamingmesh.jobs.container.*; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip; +import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip.ownershipFeedback; import com.gamingmesh.jobs.hooks.HookManager; -import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; -import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling.ownershipFeedback; import com.google.common.base.Objects; import org.bukkit.Bukkit; @@ -100,8 +100,13 @@ public class JobsPaymentListener implements Listener { private Jobs plugin; - public static final String furnaceOwnerMetadata = "jobsFurnaceOwner", - brewingOwnerMetadata = "jobsBrewingOwner", VegyMetadata = "VegyTimer"; + /** + * @deprecated Use {@link Jobs#getBlockOwnerShip(CMIMaterial)} + */ + @Deprecated + public static final String furnaceOwnerMetadata = "jobsFurnaceOwner", blastFurnaceOwnerMetadata = "jobsBlastFurnaceOwner", + brewingOwnerMetadata = "jobsBrewingOwner", smokerOwnerMetadata = "jobsSmokerOwner"; + public static final String VegyMetadata = "VegyTimer"; private final String BlockMetadata = "BlockOwner", CowMetadata = "CowTimer", entityDamageByPlayer = "JobsEntityDamagePlayer"; @@ -350,7 +355,7 @@ public class JobsPaymentListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { - Block block = event.getBlock(); + final Block block = event.getBlock(); //disabling plugin in world if (!Jobs.getGCManager().canPerformActionInWorld(block.getWorld())) return; @@ -367,13 +372,8 @@ public class JobsPaymentListener implements Listener { if (Jobs.getGCManager().disablePaymentIfRiding && player.isInsideVehicle()) return; - CMIMaterial cmat = CMIMaterial.get(block); - if (cmat == CMIMaterial.FURNACE || cmat == CMIMaterial.SMOKER - || cmat == CMIMaterial.BLAST_FURNACE && block.hasMetadata(furnaceOwnerMetadata)) - FurnaceBrewingHandling.removeFurnace(block); - else if (cmat == CMIMaterial.BREWING_STAND || cmat == CMIMaterial.LEGACY_BREWING_STAND - && block.hasMetadata(brewingOwnerMetadata)) - FurnaceBrewingHandling.removeBrewing(block); + // Remove block owner ships + plugin.getBlockOwnerShips().forEach(os -> os.remove(block)); if (!Jobs.getPermissionHandler().hasWorldPermission(player, player.getLocation().getWorld().getName())) return; @@ -998,8 +998,8 @@ public class JobsPaymentListener implements Listener { if (!Jobs.getGCManager().canPerformActionInWorld(block.getWorld())) return; - if (block.hasMetadata(furnaceOwnerMetadata)) - FurnaceBrewingHandling.removeFurnace(block); + final Block finalBlock = block; + plugin.getBlockOwnerShip(CMIMaterial.get(finalBlock)).ifPresent(os -> os.remove(finalBlock)); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -1007,20 +1007,13 @@ public class JobsPaymentListener implements Listener { if (event.getDestination().getType() != InventoryType.BREWING) return; - if (!Jobs.getGCManager().PreventBrewingStandFillUps) + if (!Jobs.getGCManager().PreventBrewingStandFillUps || event.getItem().getType() == Material.AIR) return; - if (event.getItem().getType() == Material.AIR) - return; + final BrewingStand stand = (BrewingStand) event.getDestination().getHolder(); - BrewingStand stand = (BrewingStand) event.getDestination().getHolder(); - //disabling plugin in world - if (!Jobs.getGCManager().canPerformActionInWorld(stand.getWorld())) - return; - - Block block = stand.getBlock(); - if (block.hasMetadata(brewingOwnerMetadata)) - FurnaceBrewingHandling.removeBrewing(block); + if (Jobs.getGCManager().canPerformActionInWorld(stand.getWorld())) + plugin.getBlockOwnerShip(CMIMaterial.get(stand.getBlock())).ifPresent(os -> os.remove(stand.getBlock())); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -1030,10 +1023,12 @@ public class JobsPaymentListener implements Listener { if (!Jobs.getGCManager().canPerformActionInWorld(block.getWorld())) return; - if (!block.hasMetadata(furnaceOwnerMetadata)) + BlockOwnerShip bos = plugin.getBlockOwnerShip(CMIMaterial.get(block), false).orElse(null); + if (bos == null || !block.hasMetadata(bos.getMetadataName())) { return; + } - List data = block.getMetadata(furnaceOwnerMetadata); + List data = bos.getBlockMetadatas(block); if (data.isEmpty()) return; @@ -1559,18 +1554,11 @@ public class JobsPaymentListener implements Listener { } } - for (Block block : event.blockList()) { + for (final Block block : event.blockList()) { if (block == null) continue; - CMIMaterial cmat = CMIMaterial.get(block); - - if (cmat == CMIMaterial.FURNACE || cmat == CMIMaterial.SMOKER - || cmat == CMIMaterial.BLAST_FURNACE && block.hasMetadata(furnaceOwnerMetadata)) - FurnaceBrewingHandling.removeFurnace(block); - else if (cmat == CMIMaterial.BREWING_STAND || cmat == CMIMaterial.LEGACY_BREWING_STAND - && block.hasMetadata(brewingOwnerMetadata)) - FurnaceBrewingHandling.removeBrewing(block); + plugin.getBlockOwnerShips().forEach(b -> b.remove(block)); if (Jobs.getGCManager().useBlockProtection && block.getState().hasMetadata(BlockMetadata)) return; @@ -1620,40 +1608,23 @@ public class JobsPaymentListener implements Listener { } } - if (cmat == CMIMaterial.FURNACE || cmat == CMIMaterial.LEGACY_BURNING_FURNACE - || cmat == CMIMaterial.SMOKER || cmat == CMIMaterial.BLAST_FURNACE) { - ownershipFeedback done = FurnaceBrewingHandling.registerFurnaces(p, block); - if (done == ownershipFeedback.tooMany) { - boolean report = false; - - if (block.hasMetadata(furnaceOwnerMetadata)) { - List data = block.getMetadata(furnaceOwnerMetadata); - if (data.isEmpty()) - return; - - // only care about first - MetadataValue value = data.get(0); - String uuid = value.asString(); - - if (!uuid.equals(p.getUniqueId().toString())) - report = true; - } else - report = true; - - if (report) - ActionBarManager.send(p, Jobs.getLanguage().getMessage("general.error.noFurnaceRegistration")); - } else if (done == ownershipFeedback.newReg && jPlayer != null) { - ActionBarManager.send(p, Jobs.getLanguage().getMessage("general.error.newFurnaceRegistration", - "[current]", jPlayer.getFurnaceCount(), - "[max]", jPlayer.getMaxFurnacesAllowed(cmat) == 0 ? "-" : jPlayer.getMaxFurnacesAllowed(cmat))); + boolean isBrewingStand = cmat == CMIMaterial.BREWING_STAND || cmat == CMIMaterial.LEGACY_BREWING_STAND; + boolean isFurnace = cmat == CMIMaterial.FURNACE || cmat == CMIMaterial.LEGACY_BURNING_FURNACE; + if (isFurnace || cmat == CMIMaterial.SMOKER || cmat == CMIMaterial.BLAST_FURNACE || isBrewingStand) { + BlockOwnerShip blockOwner = plugin.getBlockOwnerShip(CMIMaterial.get(block)).orElse(null); + if (blockOwner == null) { + return; } - } else if (cmat == CMIMaterial.BREWING_STAND || cmat == CMIMaterial.LEGACY_BREWING_STAND) { - ownershipFeedback done = FurnaceBrewingHandling.registerBrewingStand(p, block); + + String name = isBrewingStand ? Jobs.getLanguage().getMessage("general.info.blocks.brewingstand") + : isFurnace ? Jobs.getLanguage().getMessage("general.info.blocks.furnace") + : cmat == CMIMaterial.SMOKER ? Jobs.getLanguage().getMessage("general.info.blocks.smoker") + : cmat == CMIMaterial.BLAST_FURNACE ? Jobs.getLanguage().getMessage("general.info.blocks.blastfurnace") : ""; + ownershipFeedback done = blockOwner.register(p, block); if (done == ownershipFeedback.tooMany) { boolean report = false; - - if (block.hasMetadata(brewingOwnerMetadata)) { - List data = block.getMetadata(brewingOwnerMetadata); + if (block.hasMetadata(blockOwner.getMetadataName())) { + List data = blockOwner.getBlockMetadatas(block); if (data.isEmpty()) return; @@ -1667,11 +1638,11 @@ public class JobsPaymentListener implements Listener { report = true; if (report) - ActionBarManager.send(p, Jobs.getLanguage().getMessage("general.error.noBrewingRegistration")); + ActionBarManager.send(p, Jobs.getLanguage().getMessage("general.error.noRegistration", "[block]", name)); } else if (done == ownershipFeedback.newReg && jPlayer != null) { - ActionBarManager.send(p, Jobs.getLanguage().getMessage("general.error.newBrewingRegistration", - "[current]", jPlayer.getBrewingStandCount(), - "[max]", jPlayer.getMaxBrewingStandsAllowed() == 0 ? "-" : jPlayer.getMaxBrewingStandsAllowed())); + ActionBarManager.send(p, Jobs.getLanguage().getMessage("general.error.newRegistration", "[block]", name, + "[current]", blockOwner.getTotal(jPlayer.getUniqueId()), + "[max]", jPlayer.getMaxOwnerShipAllowed(blockOwner.getType()) == 0 ? "-" : jPlayer.getMaxOwnerShipAllowed(blockOwner.getType()))); } } else if (Version.isCurrentEqualOrHigher(Version.v1_13_R1) && block.getType().toString().startsWith("STRIPPED_") && diff --git a/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java b/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java index 2bca8d1e..2e755a92 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java @@ -20,6 +20,12 @@ import com.gamingmesh.jobs.config.YmlMaker; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.listeners.JobsPaymentListener; +/** + * @deprecated As of new blocks (smoker, blast furnace) this has been deprecated and + * marked as "removeable". In the future this class will get removed + * and not used anymore by anyone. Instead use {@link Jobs#getBlockOwnerShips()} + */ +@Deprecated public class FurnaceBrewingHandling { static HashMap> furnaceMap = new HashMap<>(); @@ -239,7 +245,7 @@ public class FurnaceBrewingHandling { return ownershipFeedback.invalid; JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player); - int max = jPlayer.getMaxFurnacesAllowed(cmat); + int max = jPlayer.getMaxFurnacesAllowed(); int have = jPlayer.getFurnaceCount(); boolean owner = false;