From a18ba89dedb91779115e26310db80d7f7498e561 Mon Sep 17 00:00:00 2001 From: Zrips Date: Tue, 14 Sep 2021 15:22:13 +0300 Subject: [PATCH] Fixing block ownership after server restarts --- .../blockOwnerShip/BlockOwnerShip.java | 74 ++++++++++++++++--- .../jobs/listeners/JobsPaymentListener.java | 48 +++++++----- .../com/gamingmesh/jobs/stuff/blockLoc.java | 5 ++ 3 files changed, 99 insertions(+), 28 deletions(-) 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 ee3db399..10330765 100644 --- a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java +++ b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java @@ -9,6 +9,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.UUID; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; @@ -22,6 +23,7 @@ import com.gamingmesh.jobs.stuff.blockLoc; import net.Zrips.CMILib.Container.CMILocation; import net.Zrips.CMILib.Items.CMIMaterial; +import net.Zrips.CMILib.Logs.CMIDebug; public class BlockOwnerShip { @@ -31,6 +33,8 @@ public class BlockOwnerShip { private final Map> blockOwnerShips = new HashMap<>(); + private final Map> ownerMapByLocation = new HashMap<>(); + private final Jobs plugin = org.bukkit.plugin.java.JavaPlugin.getPlugin(Jobs.class); public BlockOwnerShip(CMIMaterial type) { @@ -87,6 +91,15 @@ public class BlockOwnerShip { return ownershipFeedback.invalid; } + HashMap oldRecords = getBlockOwnerShips().get(player.getUniqueId()); + + if (oldRecords != null) { + blockLoc existing = oldRecords.get(CMILocation.toString(block.getLocation(), ":", true, true)); + if (existing != null) { + return ownershipFeedback.old; + } + } + int max = jPlayer.getMaxOwnerShipAllowed(type); int have = getTotal(jPlayer.getUniqueId()); @@ -124,8 +137,15 @@ public class BlockOwnerShip { if (ls.containsKey(locString)) return ownershipFeedback.old; - ls.put(locString, new blockLoc(block.getLocation())); + blockLoc bloc = new blockLoc(block.getLocation()); + + ls.put(locString, bloc); blockOwnerShips.put(jPlayer.getUniqueId(), ls); + + Map oldRecord = ownerMapByLocation.getOrDefault(block.getLocation().getWorld().getName(), new HashMap()); + oldRecord.put(bloc.toVectorString(), jPlayer.getUniqueId()); + ownerMapByLocation.put(block.getLocation().getWorld().getName(), oldRecord); + return ownershipFeedback.newReg; } @@ -150,11 +170,26 @@ public class BlockOwnerShip { com.gamingmesh.jobs.stuff.blockLoc removed = ls.remove(blockLoc); if (removed != null) { block.removeMetadata(metadataName, plugin); + + Map oldRecord = ownerMapByLocation.get(block.getLocation().getWorld().getName()); + if (oldRecord != null) + oldRecord.remove(block.getLocation().getBlockX() + ":" + block.getLocation().getBlockY() + ":" + block.getLocation().getBlockZ()); + } return removed != null; } + public UUID getOwnerByLocation(Location loc) { + blockLoc bl = new blockLoc(loc); + Map record = ownerMapByLocation.get(bl.getWorldName()); + if (record == null) { + CMIDebug.d("first null"); + return null; + } + return record.get(bl.toVectorString()); + } + public int clear(UUID uuid) { HashMap ls = blockOwnerShips.remove(uuid); if (ls == null) @@ -162,6 +197,11 @@ public class BlockOwnerShip { for (blockLoc one : ls.values()) { one.getBlock().removeMetadata(metadataName, plugin); + + Map oldRecord = ownerMapByLocation.get(one.getWorldName()); + if (oldRecord != null) + oldRecord.remove(one.toVectorString()); + } return ls.size(); @@ -188,9 +228,23 @@ public class BlockOwnerShip { f = f2; - String path = (type == BlockTypes.FURNACE ? "Furnace" - : type == BlockTypes.BLAST_FURNACE ? "BlastFurnace" - : type == BlockTypes.BREWING_STAND ? "Brewing" : type == BlockTypes.SMOKER ? "Smoker" : ""); + 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; + } if (isReassignDisabled()) return; @@ -221,15 +275,13 @@ public class BlockOwnerShip { for (String oneL : ls) { blockLoc bl = new blockLoc(oneL); CMILocation cmil = CMILocation.fromString(oneL, ":"); - // Do we seriously need to re apply this to all blocks? -// Block block = bl.getBlock(); -// if (block == null) -// continue; -// -// block.removeMetadata(metadataName, plugin); -// block.setMetadata(metadataName, new FixedMetadataValue(plugin, one)); blist.put(CMILocation.toString(cmil, ":", true, true), bl); + + Map oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap()); + oldRecord.put(bl.toVectorString(), uuid); + ownerMapByLocation.put(bl.getWorldName(), oldRecord); + total++; } diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index bb6f02e9..58cbd84f 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -367,18 +367,25 @@ public final class JobsPaymentListener implements Listener { if (ownerShip == null) return; - List data = block.getMetadata(ownerShip.getMetadataName()); - if (data.isEmpty()) - return; + UUID uuid = null; + + List data = ownerShip.getBlockMetadatas(block); + if (data.isEmpty()) { + uuid = ownerShip.getOwnerByLocation(block.getLocation()); + if (uuid == null) + return; + } // only care about first - MetadataValue value = data.get(0); - UUID uuid; - try { - uuid = UUID.fromString(value.asString()); - } catch (IllegalArgumentException e) { + if (uuid == null && !data.isEmpty()) { + MetadataValue value = data.get(0); + try { + uuid = UUID.fromString(value.asString()); + } catch (IllegalArgumentException e) { + return; + } + } else return; - } JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(uuid); if (jPlayer == null) @@ -1075,18 +1082,25 @@ public final class JobsPaymentListener implements Listener { return; } + UUID uuid = null; + List data = bos.getBlockMetadatas(block); - if (data.isEmpty()) - return; + if (data.isEmpty()) { + uuid = bos.getOwnerByLocation(block.getLocation()); + if (uuid == null) + return; + } // only care about first - MetadataValue value = data.get(0); - UUID uuid; - try { - uuid = UUID.fromString(value.asString()); - } catch (IllegalArgumentException e) { + if (uuid == null && !data.isEmpty()) { + MetadataValue value = data.get(0); + try { + uuid = UUID.fromString(value.asString()); + } catch (IllegalArgumentException e) { + return; + } + } else return; - } Player player = Bukkit.getPlayer(uuid); if (player == null || !player.isOnline()) diff --git a/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java b/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java index 8a2feff3..7ff8e3ea 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java @@ -49,6 +49,10 @@ public class blockLoc { return (w == null ? worldName : w.getName()) + ":" + x + ":" + y + ":" + z; } + public String toVectorString() { + return x + ":" + y + ":" + z; + } + public boolean fromString(String loc) { String[] split = loc.split(":", 4); if (split.length == 0) { @@ -59,6 +63,7 @@ public class blockLoc { if (w == null) return false; this.w = w; + this.worldName = w.getName(); if (split.length < 4) { return false;