Islands are now updated correctly across servers.

This build has a lot of debug in it!
This commit is contained in:
tastybento 2024-04-08 20:03:50 -07:00
parent f81185a6ab
commit c3142f68fd
3 changed files with 143 additions and 36 deletions

View File

@ -41,6 +41,7 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.adapters.Adapter; import world.bentobox.bentobox.database.objects.adapters.Adapter;
import world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter; import world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Pair; import world.bentobox.bentobox.util.Pair;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@ -290,6 +291,7 @@ public class Island implements DataObject, MetaDataAble {
this.world = island.getWorld(); this.world = island.getWorld();
this.bonusRanges.addAll(island.getBonusRanges()); this.bonusRanges.addAll(island.getBonusRanges());
this.primaries.addAll(island.getPrimaries()); this.primaries.addAll(island.getPrimaries());
BentoBox.getInstance().logDebug("Constructor copy");
this.setChanged(); this.setChanged();
} }
@ -306,6 +308,7 @@ public class Island implements DataObject, MetaDataAble {
public void addMember(@NonNull UUID playerUUID) { public void addMember(@NonNull UUID playerUUID) {
if (getRank(playerUUID) != RanksManager.MEMBER_RANK) { if (getRank(playerUUID) != RanksManager.MEMBER_RANK) {
setRank(playerUUID, RanksManager.MEMBER_RANK); setRank(playerUUID, RanksManager.MEMBER_RANK);
BentoBox.getInstance().logDebug("Add member");
setChanged(); setChanged();
} }
} }
@ -326,6 +329,7 @@ public class Island implements DataObject, MetaDataAble {
setRank(target, RanksManager.BANNED_RANK); setRank(target, RanksManager.BANNED_RANK);
log(new LogEntry.Builder("BAN").data("player", target.toString()).data("issuer", issuer.toString()) log(new LogEntry.Builder("BAN").data("player", target.toString()).data("issuer", issuer.toString())
.build()); .build());
BentoBox.getInstance().logDebug("Ban");
setChanged(); setChanged();
} }
return true; return true;
@ -615,6 +619,7 @@ public class Island implements DataObject, MetaDataAble {
public int getMaxEverProtectionRange() { public int getMaxEverProtectionRange() {
if (maxEverProtectionRange > this.getRange()) { if (maxEverProtectionRange > this.getRange()) {
maxEverProtectionRange = this.getRange(); maxEverProtectionRange = this.getRange();
BentoBox.getInstance().logDebug("get max ever protect");
setChanged(); setChanged();
} }
return Math.max(this.getProtectionRange(), maxEverProtectionRange); return Math.max(this.getProtectionRange(), maxEverProtectionRange);
@ -634,6 +639,7 @@ public class Island implements DataObject, MetaDataAble {
if (maxEverProtectionRange > this.range) { if (maxEverProtectionRange > this.range) {
this.maxEverProtectionRange = this.range; this.maxEverProtectionRange = this.range;
} }
BentoBox.getInstance().logDebug("setMaxEverProtectionRange");
setChanged(); setChanged();
} }
@ -1011,6 +1017,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void removeMember(UUID playerUUID) { public void removeMember(UUID playerUUID) {
if (members.remove(playerUUID) != null) { if (members.remove(playerUUID) != null) {
BentoBox.getInstance().logDebug("removeMember");
setChanged(); setChanged();
} }
} }
@ -1022,6 +1029,7 @@ public class Island implements DataObject, MetaDataAble {
if (!center.getWorld().equals(this.center.getWorld()) || !center.equals(this.center)) { if (!center.getWorld().equals(this.center.getWorld()) || !center.equals(this.center)) {
this.world = center.getWorld(); this.world = center.getWorld();
this.center = center; this.center = center;
BentoBox.getInstance().logDebug("setCenter");
setChanged(); setChanged();
} }
} }
@ -1032,6 +1040,7 @@ public class Island implements DataObject, MetaDataAble {
public void setCreatedDate(long createdDate) { public void setCreatedDate(long createdDate) {
if (this.createdDate != createdDate) { if (this.createdDate != createdDate) {
this.createdDate = createdDate; this.createdDate = createdDate;
BentoBox.getInstance().logDebug("setCreatedDate");
setChanged(); setChanged();
} }
} }
@ -1058,6 +1067,7 @@ public class Island implements DataObject, MetaDataAble {
public void setFlag(Flag flag, int value, boolean doSubflags) { public void setFlag(Flag flag, int value, boolean doSubflags) {
if (flags.containsKey(flag.getID()) && flags.get(flag.getID()) != value) { if (flags.containsKey(flag.getID()) && flags.get(flag.getID()) != value) {
flags.put(flag.getID(), value); flags.put(flag.getID(), value);
BentoBox.getInstance().logDebug("setFlag " + flag);
setChanged(); setChanged();
} }
// Subflag support // Subflag support
@ -1072,6 +1082,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setFlags(Map<String, Integer> flags) { public void setFlags(Map<String, Integer> flags) {
this.flags = flags; this.flags = flags;
BentoBox.getInstance().logDebug("setFlags ");
setChanged(); setChanged();
} }
@ -1090,7 +1101,6 @@ public class Island implements DataObject, MetaDataAble {
.forEach(f -> result.put(f.getID(), .forEach(f -> result.put(f.getID(),
plugin.getIWM().getDefaultIslandSettings(world).getOrDefault(f, f.getDefaultRank()))); plugin.getIWM().getDefaultIslandSettings(world).getOrDefault(f, f.getDefaultRank())));
this.setFlags(result); this.setFlags(result);
setChanged();
} }
/** /**
@ -1098,6 +1108,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setMembers(Map<UUID, Integer> members) { public void setMembers(Map<UUID, Integer> members) {
this.members = members; this.members = members;
BentoBox.getInstance().logDebug("setMembers");
setChanged(); setChanged();
} }
@ -1111,6 +1122,7 @@ public class Island implements DataObject, MetaDataAble {
public void setName(String name) { public void setName(String name) {
if (name == null || !name.equals(this.name)) { if (name == null || !name.equals(this.name)) {
this.name = (name != null && !name.equals("")) ? name : null; this.name = (name != null && !name.equals("")) ? name : null;
BentoBox.getInstance().logDebug("setName");
setChanged(); setChanged();
} }
} }
@ -1137,6 +1149,7 @@ public class Island implements DataObject, MetaDataAble {
} }
} }
setRank(owner, RanksManager.OWNER_RANK); setRank(owner, RanksManager.OWNER_RANK);
BentoBox.getInstance().logDebug("setOwner");
setChanged(); setChanged();
} }
@ -1147,6 +1160,7 @@ public class Island implements DataObject, MetaDataAble {
if (this.protectionRange != protectionRange) { if (this.protectionRange != protectionRange) {
this.protectionRange = protectionRange; this.protectionRange = protectionRange;
this.updateMaxEverProtectionRange(); this.updateMaxEverProtectionRange();
BentoBox.getInstance().logDebug("setProtectionRange");
setChanged(); setChanged();
} }
} }
@ -1182,6 +1196,7 @@ public class Island implements DataObject, MetaDataAble {
public void setPurgeProtected(boolean purgeProtected) { public void setPurgeProtected(boolean purgeProtected) {
if (this.purgeProtected != purgeProtected) { if (this.purgeProtected != purgeProtected) {
this.purgeProtected = purgeProtected; this.purgeProtected = purgeProtected;
BentoBox.getInstance().logDebug("setPurgeProtected");
setChanged(); setChanged();
} }
} }
@ -1199,6 +1214,7 @@ public class Island implements DataObject, MetaDataAble {
public void setRange(int range) { public void setRange(int range) {
if (this.range != range) { if (this.range != range) {
this.range = range; this.range = range;
BentoBox.getInstance().logDebug("setRange");
setChanged(); setChanged();
} }
} }
@ -1240,6 +1256,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setRanks(Map<UUID, Integer> ranks) { public void setRanks(Map<UUID, Integer> ranks) {
members = ranks; members = ranks;
BentoBox.getInstance().logDebug("setRanks");
setChanged(); setChanged();
} }
@ -1264,6 +1281,7 @@ public class Island implements DataObject, MetaDataAble {
setFlag(Flags.LOCK, RanksManager.VISITOR_RANK); setFlag(Flags.LOCK, RanksManager.VISITOR_RANK);
} }
log(new LogEntry.Builder("SPAWN").data("value", String.valueOf(isSpawn)).build()); log(new LogEntry.Builder("SPAWN").data("value", String.valueOf(isSpawn)).build());
BentoBox.getInstance().logDebug("setSpawn");
setChanged(); setChanged();
} }
@ -1285,6 +1303,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setSpawnPoint(Map<Environment, Location> spawnPoint) { public void setSpawnPoint(Map<Environment, Location> spawnPoint) {
this.spawnPoint = spawnPoint; this.spawnPoint = spawnPoint;
BentoBox.getInstance().logDebug("setSpawnPoint");
setChanged(); setChanged();
} }
@ -1305,6 +1324,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setWorld(World world) { public void setWorld(World world) {
this.world = world; this.world = world;
BentoBox.getInstance().logDebug("setWorld");
setChanged(); setChanged();
} }
@ -1329,6 +1349,7 @@ public class Island implements DataObject, MetaDataAble {
if (flag.getType().equals(Flag.Type.SETTING) || flag.getType().equals(Flag.Type.WORLD_SETTING)) { if (flag.getType().equals(Flag.Type.SETTING) || flag.getType().equals(Flag.Type.WORLD_SETTING)) {
setSettingsFlag(flag, newToggleValue, doSubflags); setSettingsFlag(flag, newToggleValue, doSubflags);
} }
BentoBox.getInstance().logDebug("toggleFlag " + flag);
setChanged(); setChanged();
} }
@ -1360,6 +1381,7 @@ public class Island implements DataObject, MetaDataAble {
flag.getSubflags().forEach(subflag -> setSettingsFlag(subflag, state, true)); flag.getSubflags().forEach(subflag -> setSettingsFlag(subflag, state, true));
} }
} }
BentoBox.getInstance().logDebug("setSettingsFlag " + flag);
setChanged(); setChanged();
} }
@ -1397,6 +1419,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void removeRank(Integer rank) { public void removeRank(Integer rank) {
if (members.values().removeIf(rank::equals)) { if (members.values().removeIf(rank::equals)) {
BentoBox.getInstance().logDebug("removeRank");
setChanged(); setChanged();
} }
} }
@ -1417,6 +1440,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void log(LogEntry logEntry) { public void log(LogEntry logEntry) {
history.add(logEntry); history.add(logEntry);
BentoBox.getInstance().logDebug("log");
setChanged(); setChanged();
} }
@ -1427,6 +1451,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setHistory(List<LogEntry> history) { public void setHistory(List<LogEntry> history) {
this.history = history; this.history = history;
BentoBox.getInstance().logDebug("setHistory");
setChanged(); setChanged();
} }
@ -1442,6 +1467,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setDoNotLoad(boolean doNotLoad) { public void setDoNotLoad(boolean doNotLoad) {
this.doNotLoad = doNotLoad; this.doNotLoad = doNotLoad;
BentoBox.getInstance().logDebug("setDoNotLoad");
setChanged(); setChanged();
} }
@ -1457,6 +1483,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setDeleted(boolean deleted) { public void setDeleted(boolean deleted) {
this.deleted = deleted; this.deleted = deleted;
BentoBox.getInstance().logDebug("setDeleted");
setChanged(); setChanged();
} }
@ -1546,8 +1573,10 @@ public class Island implements DataObject, MetaDataAble {
if (cooldowns.containsKey(flag.getID()) && cooldowns.get(flag.getID()) > System.currentTimeMillis()) { if (cooldowns.containsKey(flag.getID()) && cooldowns.get(flag.getID()) > System.currentTimeMillis()) {
return true; return true;
} }
cooldowns.remove(flag.getID()); if (cooldowns.remove(flag.getID()) != null) {
setChanged(); BentoBox.getInstance().logDebug("isCooldown");
setChanged();
}
return false; return false;
} }
@ -1558,6 +1587,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setCooldown(Flag flag) { public void setCooldown(Flag flag) {
cooldowns.put(flag.getID(), flag.getCooldown() * 1000L + System.currentTimeMillis()); cooldowns.put(flag.getID(), flag.getCooldown() * 1000L + System.currentTimeMillis());
BentoBox.getInstance().logDebug("setCooldown");
setChanged(); setChanged();
} }
@ -1573,6 +1603,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setCooldowns(Map<String, Long> cooldowns) { public void setCooldowns(Map<String, Long> cooldowns) {
this.cooldowns = cooldowns; this.cooldowns = cooldowns;
BentoBox.getInstance().logDebug("setCooldowns");
setChanged(); setChanged();
} }
@ -1588,6 +1619,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setCommandRanks(Map<String, Integer> commandRanks) { public void setCommandRanks(Map<String, Integer> commandRanks) {
this.commandRanks = commandRanks; this.commandRanks = commandRanks;
BentoBox.getInstance().logDebug("setCommandRanks");
setChanged(); setChanged();
} }
@ -1659,6 +1691,7 @@ public class Island implements DataObject, MetaDataAble {
public void setReserved(boolean reserved) { public void setReserved(boolean reserved) {
if (this.reserved != reserved) { if (this.reserved != reserved) {
this.reserved = reserved; this.reserved = reserved;
BentoBox.getInstance().logDebug("setReserved");
setChanged(); setChanged();
} }
} }
@ -1682,6 +1715,7 @@ public class Island implements DataObject, MetaDataAble {
@Override @Override
public void setMetaData(Map<String, MetaDataValue> metaData) { public void setMetaData(Map<String, MetaDataValue> metaData) {
this.metaData = metaData; this.metaData = metaData;
BentoBox.getInstance().logDebug("setMetaData");
setChanged(); setChanged();
} }
@ -1698,7 +1732,7 @@ public class Island implements DataObject, MetaDataAble {
public void setChanged() { public void setChanged() {
this.setUpdatedDate(System.currentTimeMillis()); this.setUpdatedDate(System.currentTimeMillis());
this.changed = true; this.changed = true;
//IslandsManager.updateIsland(this); IslandsManager.updateIsland(this);
} }
/** /**
@ -1737,6 +1771,7 @@ public class Island implements DataObject, MetaDataAble {
} }
this.location = location; this.location = location;
this.updateMaxEverProtectionRange(); this.updateMaxEverProtectionRange();
BentoBox.getInstance().logDebug("setProtectionCenter");
setChanged(); setChanged();
} }
@ -1795,6 +1830,7 @@ public class Island implements DataObject, MetaDataAble {
} }
} }
getHomes().put(name.toLowerCase(), location); getHomes().put(name.toLowerCase(), location);
BentoBox.getInstance().logDebug("addHome");
setChanged(); setChanged();
} }
@ -1865,6 +1901,7 @@ public class Island implements DataObject, MetaDataAble {
public void setMaxHomes(@Nullable Integer maxHomes) { public void setMaxHomes(@Nullable Integer maxHomes) {
if (this.maxHomes != maxHomes) { if (this.maxHomes != maxHomes) {
this.maxHomes = maxHomes; this.maxHomes = maxHomes;
BentoBox.getInstance().logDebug("setMaxHomes");
setChanged(); setChanged();
} }
} }
@ -1887,6 +1924,7 @@ public class Island implements DataObject, MetaDataAble {
public void setMaxMembers(Map<Integer, Integer> maxMembers) { public void setMaxMembers(Map<Integer, Integer> maxMembers) {
if (this.maxMembers != maxMembers) { if (this.maxMembers != maxMembers) {
this.maxMembers = maxMembers; this.maxMembers = maxMembers;
BentoBox.getInstance().logDebug("setMaxMembers");
setChanged(); setChanged();
} }
} }
@ -1937,6 +1975,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setBonusRanges(List<BonusRangeRecord> bonusRanges) { public void setBonusRanges(List<BonusRangeRecord> bonusRanges) {
this.bonusRanges = bonusRanges; this.bonusRanges = bonusRanges;
BentoBox.getInstance().logDebug("setBonusRanges");
setChanged(); setChanged();
} }
@ -1973,6 +2012,7 @@ public class Island implements DataObject, MetaDataAble {
public void addBonusRange(String id, int range, String message) { public void addBonusRange(String id, int range, String message) {
this.getBonusRanges().add(new BonusRangeRecord(id, range, message)); this.getBonusRanges().add(new BonusRangeRecord(id, range, message));
setMaxEverProtectionRange(this.getProtectionRange()); setMaxEverProtectionRange(this.getProtectionRange());
BentoBox.getInstance().logDebug("addBonusRange");
setChanged(); setChanged();
} }
@ -1983,6 +2023,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void clearBonusRange(String id) { public void clearBonusRange(String id) {
if (this.getBonusRanges().removeIf(r -> r.getUniqueId().equals(id))) { if (this.getBonusRanges().removeIf(r -> r.getUniqueId().equals(id))) {
BentoBox.getInstance().logDebug("clearBonusRange");
setChanged(); setChanged();
} }
} }
@ -1992,6 +2033,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void clearAllBonusRanges() { public void clearAllBonusRanges() {
this.getBonusRanges().clear(); this.getBonusRanges().clear();
BentoBox.getInstance().logDebug("clearAllBonusRanges");
setChanged(); setChanged();
} }
@ -2008,6 +2050,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setPrimary(UUID userID) { public void setPrimary(UUID userID) {
if (getPrimaries().add(userID)) { if (getPrimaries().add(userID)) {
BentoBox.getInstance().logDebug("setPrimary");
setChanged(); setChanged();
} }
} }
@ -2018,6 +2061,7 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void removePrimary(UUID userID) { public void removePrimary(UUID userID) {
if (getPrimaries().remove(userID)) { if (getPrimaries().remove(userID)) {
BentoBox.getInstance().logDebug("removePrimary");
setChanged(); setChanged();
} }
} }
@ -2074,5 +2118,28 @@ public class Island implements DataObject, MetaDataAble {
*/ */
public void setPrimaries(Set<UUID> primaries) { public void setPrimaries(Set<UUID> primaries) {
this.primaries = primaries; this.primaries = primaries;
BentoBox.getInstance().logDebug("setPrimaries");
setChanged();
} }
@Override
public int hashCode() {
return Objects.hash(uniqueId);
}
/**
* Islands are equal if they have the same uniqueId
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Island other = (Island) obj;
return Objects.equals(uniqueId, other.uniqueId);
}
} }

View File

@ -21,6 +21,7 @@ import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@ -54,15 +55,26 @@ public class IslandCache {
grids = new HashMap<>(); grids = new HashMap<>();
} }
/**
* Replace the island we have with this one
* @param island island
*/
public void updateIsland(@NonNull Island island) { public void updateIsland(@NonNull Island island) {
if (island.getCenter() != null || island.getWorld() != null) { BentoBox.getInstance().logDebug("sign editing value = " + island.getFlag(Flags.SIGN_EDITING));
islandsByLocation.put(island.getCenter(), island); if (islandsByLocation.put(island.getCenter(), island) == null) {
islandsById.put(island.getUniqueId(), island); BentoBox.getInstance().logDebug("islandsByLocation failed to update");
// Only add islands to this map if they are owned }
if (island.isOwned()) { if (islandsById.put(island.getUniqueId(), island) == null) {
islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island); BentoBox.getInstance().logDebug("islandsById failed to update");
island.getMemberSet().forEach(member -> addPlayer(member, island)); }
// Only add islands to this map if they are owned
if (island.getOwner() != null) {
Set<Island> set = islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>());
if (!set.remove(island)) {
BentoBox.getInstance().logDebug("islandsByUUID failed to remove");
} }
set.add(island);
} }
} }
@ -107,7 +119,7 @@ public class IslandCache {
* @return true if successfully added, false if not * @return true if successfully added, false if not
*/ */
private boolean addToGrid(@NonNull Island newIsland) { private boolean addToGrid(@NonNull Island newIsland) {
return grids.computeIfAbsent(newIsland.getWorld(), k -> new IslandGrid()).addToGrid(newIsland); return grids.computeIfAbsent(newIsland.getWorld(), k -> new IslandGrid(this)).addToGrid(newIsland);
} }
public void clear() { public void clear() {
@ -220,8 +232,13 @@ public class IslandCache {
* @param island island to make primary * @param island island to make primary
*/ */
public void setPrimaryIsland(@NonNull UUID uuid, @NonNull Island island) { public void setPrimaryIsland(@NonNull UUID uuid, @NonNull Island island) {
if (island.getPrimaries().contains(uuid)) {
return;
}
for (Island is : getIslands(island.getWorld(), uuid)) { for (Island is : getIslands(island.getWorld(), uuid)) {
is.removePrimary(uuid); if (is.getPrimaries().contains(uuid)) {
is.removePrimary(uuid);
}
if (is.equals(island)) { if (is.equals(island)) {
is.setPrimary(uuid); is.setPrimary(uuid);
} }
@ -396,7 +413,7 @@ public class IslandCache {
public void removeIsland(@NonNull Island island) { public void removeIsland(@NonNull Island island) {
islandsByLocation.values().removeIf(island::equals); islandsByLocation.values().removeIf(island::equals);
islandsById.values().removeIf(island::equals); islandsById.values().removeIf(island::equals);
islandsByUUID.values().removeIf(island::equals); islandsByUUID.values().forEach(s -> s.removeIf(island::equals));
World w = Util.getWorld(island.getWorld()); World w = Util.getWorld(island.getWorld());
if (w == null) { if (w == null) {
return; return;

View File

@ -12,7 +12,17 @@ import world.bentobox.bentobox.database.objects.Island;
* *
*/ */
class IslandGrid { class IslandGrid {
private final TreeMap<Integer, TreeMap<Integer, Island>> grid = new TreeMap<>(); private final TreeMap<Integer, TreeMap<Integer, String>> grid = new TreeMap<>();
private final IslandCache im;
/**
* @param im IslandsManager
*/
public IslandGrid(IslandCache im) {
super();
this.im = im;
}
/** /**
* Adds island to grid * Adds island to grid
* @param island - island to add * @param island - island to add
@ -21,9 +31,9 @@ class IslandGrid {
public boolean addToGrid(Island island) { public boolean addToGrid(Island island) {
// Check if we know about this island already // Check if we know about this island already
if (grid.containsKey(island.getMinX())) { if (grid.containsKey(island.getMinX())) {
TreeMap<Integer, Island> zEntry = grid.get(island.getMinX()); TreeMap<Integer, String> zEntry = grid.get(island.getMinX());
if (zEntry.containsKey(island.getMinZ())) { if (zEntry.containsKey(island.getMinZ())) {
if (island.getUniqueId().equals(zEntry.get(island.getMinZ()).getUniqueId())) { if (island.getUniqueId().equals(zEntry.get(island.getMinZ()))) {
BentoBox.getInstance().logDebug("I already know about this island"); BentoBox.getInstance().logDebug("I already know about this island");
return true; return true;
} }
@ -31,13 +41,13 @@ class IslandGrid {
return false; return false;
} else { } else {
// Add island // Add island
zEntry.put(island.getMinZ(), island); zEntry.put(island.getMinZ(), island.getUniqueId());
grid.put(island.getMinX(), zEntry); grid.put(island.getMinX(), zEntry);
} }
} else { } else {
// Add island // Add island
TreeMap<Integer, Island> zEntry = new TreeMap<>(); TreeMap<Integer, String> zEntry = new TreeMap<>();
zEntry.put(island.getMinZ(), island); zEntry.put(island.getMinZ(), island.getUniqueId());
grid.put(island.getMinX(), zEntry); grid.put(island.getMinX(), zEntry);
} }
return true; return true;
@ -54,7 +64,7 @@ class IslandGrid {
int x = island.getMinX(); int x = island.getMinX();
int z = island.getMinZ(); int z = island.getMinZ();
if (grid.containsKey(x)) { if (grid.containsKey(x)) {
TreeMap<Integer, Island> zEntry = grid.get(x); TreeMap<Integer, String> zEntry = grid.get(x);
if (zEntry.containsKey(z)) { if (zEntry.containsKey(z)) {
// Island exists - delete it // Island exists - delete it
zEntry.remove(z); zEntry.remove(z);
@ -67,25 +77,38 @@ class IslandGrid {
} }
/** /**
* Returns the island at the x,z location or null if there is none. * Retrieves the island located at the specified x and z coordinates, covering both the protected area
* This includes the full island space, not just the protected area. * and the full island space. Returns null if no island exists at the given location.
* *
* @param x - x coordinate * @param x the x coordinate of the location
* @param z - z coordinate * @param z the z coordinate of the location
* @return Island or null * @return the Island at the specified location, or null if no island is found
*/ */
public Island getIslandAt(int x, int z) { public Island getIslandAt(int x, int z) {
Entry<Integer, TreeMap<Integer, Island>> en = grid.floorEntry(x); // Attempt to find the closest x-coordinate entry that does not exceed 'x'
if (en != null) { Entry<Integer, TreeMap<Integer, String>> xEntry = grid.floorEntry(x);
Entry<Integer, Island> ent = en.getValue().floorEntry(z); if (xEntry == null) {
if (ent != null) { return null; // No x-coordinate entry found, return null
// Check if in the island range
Island island = ent.getValue();
if (island.inIslandSpace(x, z)) {
return island;
}
}
} }
// Attempt to find the closest z-coordinate entry that does not exceed 'z' within the found x-coordinate
Entry<Integer, String> zEntry = xEntry.getValue().floorEntry(z);
if (zEntry == null) {
return null; // No z-coordinate entry found, return null
}
// Retrieve the island using the id found in the z-coordinate entry
Island island = im.getIslandById(zEntry.getValue());
if (island == null) {
return null; // No island found by the id, return null
}
// Check if the specified coordinates are within the island space
if (island.inIslandSpace(x, z)) {
return island; // Coordinates are within island space, return the island
}
// Coordinates are outside the island space, return null
return null; return null;
} }
} }