1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Fixing block ownership after server restarts

This commit is contained in:
Zrips 2021-09-14 15:22:13 +03:00
parent 7822f9a194
commit a18ba89ded
3 changed files with 99 additions and 28 deletions

View File

@ -9,6 +9,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; 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.Container.CMILocation;
import net.Zrips.CMILib.Items.CMIMaterial; import net.Zrips.CMILib.Items.CMIMaterial;
import net.Zrips.CMILib.Logs.CMIDebug;
public class BlockOwnerShip { public class BlockOwnerShip {
@ -31,6 +33,8 @@ public class BlockOwnerShip {
private final Map<UUID, HashMap<String, blockLoc>> blockOwnerShips = new HashMap<>(); private final Map<UUID, HashMap<String, blockLoc>> blockOwnerShips = new HashMap<>();
private final Map<String, Map<String, UUID>> ownerMapByLocation = new HashMap<>();
private final Jobs plugin = org.bukkit.plugin.java.JavaPlugin.getPlugin(Jobs.class); private final Jobs plugin = org.bukkit.plugin.java.JavaPlugin.getPlugin(Jobs.class);
public BlockOwnerShip(CMIMaterial type) { public BlockOwnerShip(CMIMaterial type) {
@ -87,6 +91,15 @@ public class BlockOwnerShip {
return ownershipFeedback.invalid; return ownershipFeedback.invalid;
} }
HashMap<String, blockLoc> 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 max = jPlayer.getMaxOwnerShipAllowed(type);
int have = getTotal(jPlayer.getUniqueId()); int have = getTotal(jPlayer.getUniqueId());
@ -124,8 +137,15 @@ public class BlockOwnerShip {
if (ls.containsKey(locString)) if (ls.containsKey(locString))
return ownershipFeedback.old; 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); blockOwnerShips.put(jPlayer.getUniqueId(), ls);
Map<String, UUID> oldRecord = ownerMapByLocation.getOrDefault(block.getLocation().getWorld().getName(), new HashMap<String, UUID>());
oldRecord.put(bloc.toVectorString(), jPlayer.getUniqueId());
ownerMapByLocation.put(block.getLocation().getWorld().getName(), oldRecord);
return ownershipFeedback.newReg; return ownershipFeedback.newReg;
} }
@ -150,11 +170,26 @@ public class BlockOwnerShip {
com.gamingmesh.jobs.stuff.blockLoc removed = ls.remove(blockLoc); com.gamingmesh.jobs.stuff.blockLoc removed = ls.remove(blockLoc);
if (removed != null) { if (removed != null) {
block.removeMetadata(metadataName, plugin); block.removeMetadata(metadataName, plugin);
Map<String, UUID> oldRecord = ownerMapByLocation.get(block.getLocation().getWorld().getName());
if (oldRecord != null)
oldRecord.remove(block.getLocation().getBlockX() + ":" + block.getLocation().getBlockY() + ":" + block.getLocation().getBlockZ());
} }
return removed != null; return removed != null;
} }
public UUID getOwnerByLocation(Location loc) {
blockLoc bl = new blockLoc(loc);
Map<String, UUID> record = ownerMapByLocation.get(bl.getWorldName());
if (record == null) {
CMIDebug.d("first null");
return null;
}
return record.get(bl.toVectorString());
}
public int clear(UUID uuid) { public int clear(UUID uuid) {
HashMap<String, blockLoc> ls = blockOwnerShips.remove(uuid); HashMap<String, blockLoc> ls = blockOwnerShips.remove(uuid);
if (ls == null) if (ls == null)
@ -162,6 +197,11 @@ public class BlockOwnerShip {
for (blockLoc one : ls.values()) { for (blockLoc one : ls.values()) {
one.getBlock().removeMetadata(metadataName, plugin); one.getBlock().removeMetadata(metadataName, plugin);
Map<String, UUID> oldRecord = ownerMapByLocation.get(one.getWorldName());
if (oldRecord != null)
oldRecord.remove(one.toVectorString());
} }
return ls.size(); return ls.size();
@ -188,9 +228,23 @@ public class BlockOwnerShip {
f = f2; f = f2;
String path = (type == BlockTypes.FURNACE ? "Furnace" String path = "";
: type == BlockTypes.BLAST_FURNACE ? "BlastFurnace" switch (type) {
: type == BlockTypes.BREWING_STAND ? "Brewing" : type == BlockTypes.SMOKER ? "Smoker" : ""); 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()) if (isReassignDisabled())
return; return;
@ -221,15 +275,13 @@ public class BlockOwnerShip {
for (String oneL : ls) { for (String oneL : ls) {
blockLoc bl = new blockLoc(oneL); blockLoc bl = new blockLoc(oneL);
CMILocation cmil = CMILocation.fromString(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); blist.put(CMILocation.toString(cmil, ":", true, true), bl);
Map<String, UUID> oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap<String, UUID>());
oldRecord.put(bl.toVectorString(), uuid);
ownerMapByLocation.put(bl.getWorldName(), oldRecord);
total++; total++;
} }

View File

@ -367,18 +367,25 @@ public final class JobsPaymentListener implements Listener {
if (ownerShip == null) if (ownerShip == null)
return; return;
List<MetadataValue> data = block.getMetadata(ownerShip.getMetadataName()); UUID uuid = null;
if (data.isEmpty())
return; List<MetadataValue> data = ownerShip.getBlockMetadatas(block);
if (data.isEmpty()) {
uuid = ownerShip.getOwnerByLocation(block.getLocation());
if (uuid == null)
return;
}
// only care about first // only care about first
MetadataValue value = data.get(0); if (uuid == null && !data.isEmpty()) {
UUID uuid; MetadataValue value = data.get(0);
try { try {
uuid = UUID.fromString(value.asString()); uuid = UUID.fromString(value.asString());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return;
}
} else
return; return;
}
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(uuid); JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(uuid);
if (jPlayer == null) if (jPlayer == null)
@ -1075,18 +1082,25 @@ public final class JobsPaymentListener implements Listener {
return; return;
} }
UUID uuid = null;
List<MetadataValue> data = bos.getBlockMetadatas(block); List<MetadataValue> data = bos.getBlockMetadatas(block);
if (data.isEmpty()) if (data.isEmpty()) {
return; uuid = bos.getOwnerByLocation(block.getLocation());
if (uuid == null)
return;
}
// only care about first // only care about first
MetadataValue value = data.get(0); if (uuid == null && !data.isEmpty()) {
UUID uuid; MetadataValue value = data.get(0);
try { try {
uuid = UUID.fromString(value.asString()); uuid = UUID.fromString(value.asString());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return;
}
} else
return; return;
}
Player player = Bukkit.getPlayer(uuid); Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) if (player == null || !player.isOnline())

View File

@ -49,6 +49,10 @@ public class blockLoc {
return (w == null ? worldName : w.getName()) + ":" + x + ":" + y + ":" + z; return (w == null ? worldName : w.getName()) + ":" + x + ":" + y + ":" + z;
} }
public String toVectorString() {
return x + ":" + y + ":" + z;
}
public boolean fromString(String loc) { public boolean fromString(String loc) {
String[] split = loc.split(":", 4); String[] split = loc.split(":", 4);
if (split.length == 0) { if (split.length == 0) {
@ -59,6 +63,7 @@ public class blockLoc {
if (w == null) if (w == null)
return false; return false;
this.w = w; this.w = w;
this.worldName = w.getName();
if (split.length < 4) { if (split.length < 4) {
return false; return false;