Fix team management and ranks

This commit is contained in:
tastybento 2024-04-10 21:32:11 -07:00
parent 56481ac6d0
commit 37b43b6cc1
3 changed files with 74 additions and 30 deletions

View File

@ -13,6 +13,7 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -1016,8 +1017,9 @@ public class Island implements DataObject, MetaDataAble {
* @param playerUUID - uuid of player * @param playerUUID - uuid of player
*/ */
public void removeMember(UUID playerUUID) { public void removeMember(UUID playerUUID) {
BentoBox.getInstance().logDebug("removeMember - island ");
if (members.remove(playerUUID) != null) { if (members.remove(playerUUID) != null) {
BentoBox.getInstance().logDebug("removeMember"); BentoBox.getInstance().logDebug("removeMember done");
setChanged(); setChanged();
} }
} }
@ -1237,20 +1239,34 @@ public class Island implements DataObject, MetaDataAble {
* @param rank rank value * @param rank rank value
* @since 1.1 * @since 1.1
*/ */
public void setRank(@Nullable UUID uuid, int rank) { public void setRank(@Nullable UUID uuid, int newRank) {
// Early return if the UUID is null, to avoid unnecessary processing.
if (uuid == null) { if (uuid == null) {
return; // Defensive code return;
} }
members.compute(uuid, (key, value) -> {
if (value == null || !value.equals(rank)) { // Use an AtomicBoolean to track if the member's rank has been changed.
setChanged(); // Call setChanged only if the value is updated. AtomicBoolean isRankChanged = new AtomicBoolean(false);
BentoBox.getInstance().logDebug("Set rank for " + uuid + " to " + rank);
return rank; // Attempt to update the member's rank, if necessary.
members.compute(uuid, (key, existingRank) -> {
// If the member does not exist or their rank is different, update the rank.
if (existingRank == null || existingRank != newRank) {
isRankChanged.set(true);
return newRank; // Update the rank.
} }
return value; // No change needed; return the existing rank.
return existingRank;
}); });
// If the rank was changed, notify the change and log the update.
if (isRankChanged.get()) {
setChanged(); // Notify that a change has occurred.
BentoBox.getInstance().logDebug("Set rank for UUID " + uuid + " to " + newRank);
}
} }
/** /**
* @param ranks the ranks to set * @param ranks the ranks to set
*/ */
@ -2060,8 +2076,9 @@ public class Island implements DataObject, MetaDataAble {
* @param userID user UUID * @param userID user UUID
*/ */
public void removePrimary(UUID userID) { public void removePrimary(UUID userID) {
BentoBox.getInstance().logDebug("Removing primary ");
if (getPrimaries().remove(userID)) { if (getPrimaries().remove(userID)) {
BentoBox.getInstance().logDebug("removePrimary"); BentoBox.getInstance().logDebug("removePrimary done");
setChanged(); setChanged();
} }
} }
@ -2127,9 +2144,6 @@ public class Island implements DataObject, MetaDataAble {
return Objects.hash(uniqueId); return Objects.hash(uniqueId);
} }
/**
* Islands are equal if they have the same uniqueId
*/
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
@ -2142,4 +2156,5 @@ public class Island implements DataObject, MetaDataAble {
return Objects.equals(uniqueId, other.uniqueId); return Objects.equals(uniqueId, other.uniqueId);
} }
} }

View File

@ -508,7 +508,6 @@ public class IslandsManager {
if (island.getOwner() == null) { if (island.getOwner() == null) {
// No owner, no rank settings // No owner, no rank settings
island.setMaxMembers(null); island.setMaxMembers(null);
BentoBox.getInstance().logDebug("getMaxMembers no owner, no rank settings");
updateIsland(island); updateIsland(island);
return 0; return 0;
} }
@ -533,7 +532,6 @@ public class IslandsManager {
Integer change = islandMax == worldDefault ? null : islandMax; Integer change = islandMax == worldDefault ? null : islandMax;
if (island.getMaxMembers().get(rank) != change) { if (island.getMaxMembers().get(rank) != change) {
island.setMaxMembers(rank, change); island.setMaxMembers(rank, change);
BentoBox.getInstance().logDebug("getMaxMembers");
updateIsland(island); updateIsland(island);
} }
return islandMax; return islandMax;
@ -576,7 +574,6 @@ public class IslandsManager {
Integer change = islandMax == plugin.getIWM().getMaxHomes(island.getWorld()) ? null : islandMax; Integer change = islandMax == plugin.getIWM().getMaxHomes(island.getWorld()) ? null : islandMax;
if (island.getMaxHomes() != change) { if (island.getMaxHomes() != change) {
island.setMaxHomes(change); island.setMaxHomes(change);
BentoBox.getInstance().logDebug("getMaxHomes");
updateIsland(island); updateIsland(island);
} }
return islandMax; return islandMax;
@ -768,7 +765,6 @@ public class IslandsManager {
public boolean setHomeLocation(@Nullable Island island, Location location, String name) { public boolean setHomeLocation(@Nullable Island island, Location location, String name) {
if (island != null && (island.getHome(name) == null || !island.getHome(name).equals(location))) { if (island != null && (island.getHome(name) == null || !island.getHome(name).equals(location))) {
island.addHome(name, location); island.addHome(name, location);
BentoBox.getInstance().logDebug("setHomeLocation");
updateIsland(island); updateIsland(island);
return true; return true;
} }

View File

@ -56,24 +56,51 @@ public class IslandCache {
/** /**
* Replace the island we have with this one * Replace the island we have with this one
* @param island island * @param newIsland island
*/ */
public void updateIsland(@NonNull Island island) { public void updateIsland(@NonNull Island newIsland) {
if (islandsByLocation.put(island.getCenter(), island) == null) { // Get the old island
BentoBox.getInstance().logDebug("islandsByLocation failed to update"); Island oldIsland = islandsById.get(newIsland.getUniqueId());
Set<UUID> newMembers = newIsland.getMembers().keySet();
if (oldIsland != null) {
Set<UUID> oldMembers = oldIsland.getMembers().keySet();
// Remove any members who are not in the new island
for (UUID oldMember : oldMembers) {
if (!newMembers.contains(oldMember)) {
BentoBox.getInstance().logDebug("Removing a member from the team " + oldMember);
// Member has been removed - remove island
if (islandsByUUID.computeIfAbsent(oldMember, k -> new HashSet<>()).remove(oldIsland)) {
BentoBox.getInstance().logDebug("removed");
} else {
BentoBox.getInstance().logDebug("not removed!");
}
;
}
}
} }
if (islandsById.put(island.getUniqueId(), island) == null) { // Update the members with the new island object
BentoBox.getInstance().logDebug("Updating island. New members are:");
newMembers.forEach(BentoBox.getInstance()::logDebug);
for (UUID newMember : newMembers) {
Set<Island> set = islandsByUUID.computeIfAbsent(newMember, k -> new HashSet<>());
if (set.remove(oldIsland)) {
BentoBox.getInstance().logDebug("removed old island for " + newMember);
} else {
BentoBox.getInstance().logDebug("Did not remove old island for " + newMember);
}
set.add(newIsland);
BentoBox.getInstance().logDebug("Added island to set");
islandsByUUID.put(newMember, set);
}
if (islandsByLocation.put(newIsland.getCenter(), newIsland) == null) {
BentoBox.getInstance().logDebug("islandsByLocation failed to update");
}
if (islandsById.put(newIsland.getUniqueId(), newIsland) == null) {
BentoBox.getInstance().logDebug("islandsById failed to update"); BentoBox.getInstance().logDebug("islandsById failed to update");
} }
// 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);
}
} }
/** /**
@ -192,17 +219,22 @@ public class IslandCache {
@Nullable @Nullable
public Island get(@NonNull World world, @NonNull UUID uuid) { public Island get(@NonNull World world, @NonNull UUID uuid) {
List<Island> islands = getIslands(world, uuid); List<Island> islands = getIslands(world, uuid);
BentoBox.getInstance().logDebug("Getting islands for " + uuid + " and there are " + islands.size());
if (islands.isEmpty()) { if (islands.isEmpty()) {
return null; return null;
} }
for (Island island : islands) { for (Island island : islands) {
if (island.isPrimary(uuid)) { if (island.isPrimary(uuid)) {
BentoBox.getInstance().logDebug("Primary island found");
island.getMembers().keySet().forEach(BentoBox.getInstance()::logDebug);
return island; return island;
} }
} }
BentoBox.getInstance().logDebug("No Primary island set");
// If there is no primary set, then set one - it doesn't matter which. // If there is no primary set, then set one - it doesn't matter which.
Island result = islands.iterator().next(); Island result = islands.iterator().next();
result.setPrimary(uuid); result.setPrimary(uuid);
result.getMembers().keySet().forEach(BentoBox.getInstance()::logDebug);
return result; return result;
} }
@ -347,6 +379,7 @@ public class IslandCache {
* @param uuid uuid of member to remove * @param uuid uuid of member to remove
*/ */
public void removePlayer(@NonNull Island island, @NonNull UUID uuid) { public void removePlayer(@NonNull Island island, @NonNull UUID uuid) {
BentoBox.getInstance().logDebug("Removing the player " + uuid);
Set<Island> islandSet = islandsByUUID.get(uuid); Set<Island> islandSet = islandsByUUID.get(uuid);
if (islandSet != null) { if (islandSet != null) {
islandSet.remove(island); islandSet.remove(island);