1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-26 04:25:15 +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.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<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);
public BlockOwnerShip(CMIMaterial type) {
@ -87,6 +91,15 @@ public class BlockOwnerShip {
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 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<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;
}
@ -150,11 +170,26 @@ public class BlockOwnerShip {
com.gamingmesh.jobs.stuff.blockLoc removed = ls.remove(blockLoc);
if (removed != null) {
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;
}
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) {
HashMap<String, blockLoc> 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<String, UUID> 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<String, UUID> oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap<String, UUID>());
oldRecord.put(bl.toVectorString(), uuid);
ownerMapByLocation.put(bl.getWorldName(), oldRecord);
total++;
}

View File

@ -367,18 +367,25 @@ public final class JobsPaymentListener implements Listener {
if (ownerShip == null)
return;
List<MetadataValue> data = block.getMetadata(ownerShip.getMetadataName());
if (data.isEmpty())
return;
UUID uuid = null;
List<MetadataValue> 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<MetadataValue> 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())

View File

@ -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;