Skip to content

Commit

Permalink
Remove an unused Map in cache. (#2361)
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento authored May 11, 2024
1 parent 4a0d44c commit 52a280d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 210 deletions.
171 changes: 11 additions & 160 deletions src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
Expand All @@ -31,8 +30,6 @@
* @author tastybento
*/
public class IslandCache {
@NonNull
private final Map<@NonNull Location, @NonNull Island> islandsByLocation;
/**
* Map of all islands with island uniqueId as key
*/
Expand All @@ -49,7 +46,6 @@ public class IslandCache {
private final Map<@NonNull World, @NonNull IslandGrid> grids;

public IslandCache() {
islandsByLocation = new HashMap<>();
islandsById = new HashMap<>();
islandsByUUID = new HashMap<>();
grids = new HashMap<>();
Expand All @@ -66,7 +62,6 @@ public void updateIsland(@NonNull Island newIsland) {
}
// Get the old island
Island oldIsland = islandsById.get(newIsland.getUniqueId());
compareIslands(oldIsland, newIsland);
Set<UUID> newMembers = newIsland.getMembers().keySet();
if (oldIsland != null) {
Set<UUID> oldMembers = oldIsland.getMembers().keySet();
Expand All @@ -86,128 +81,12 @@ public void updateIsland(@NonNull Island newIsland) {
islandsByUUID.put(newMember, set);
}

if (islandsByLocation.put(newIsland.getCenter(), newIsland) == null) {
BentoBox.getInstance().logError("islandsByLocation failed to update");

}
if (islandsById.put(newIsland.getUniqueId(), newIsland) == null) {
BentoBox.getInstance().logError("islandsById failed to update");
}

}

/**
* TODO REMOVE THIS DEBUG METHOD
* @param island1 island1
* @param island2 island 2
*/
public void compareIslands(Island island1, Island island2) {
if (island1 == null || island2 == null) {
BentoBox.getInstance().logDebug("One or both islands are null. Cannot compare.");
return;
}

if (!island1.getUniqueId().equals(island2.getUniqueId())) {
BentoBox.getInstance().logDebug("Island unique IDs are different.");
}

if (island1.isDeleted() != island2.isDeleted()) {
BentoBox.getInstance().logDebug("Island deleted states are different.");
}

if (!Objects.equals(island1.getCenter(), island2.getCenter())) {
BentoBox.getInstance().logDebug("Island centers are different.");
}

if (island1.getRange() != island2.getRange()) {
BentoBox.getInstance().logDebug("Island ranges are different.");
}

if (island1.getProtectionRange() != island2.getProtectionRange()) {
BentoBox.getInstance().logDebug("Island protection ranges are different.");
}

if (!island1.getBonusRanges().equals(island2.getBonusRanges())) {
BentoBox.getInstance().logDebug("Island bonus ranges are different.");
}

if (island1.getMaxEverProtectionRange() != island2.getMaxEverProtectionRange()) {
BentoBox.getInstance().logDebug("Island max ever protection ranges are different.");
}

if (!island1.getWorld().equals(island2.getWorld())) {
BentoBox.getInstance().logDebug("Island worlds are different.");
}

if (!Objects.equals(island1.getGameMode(), island2.getGameMode())) {
BentoBox.getInstance().logDebug("Island game modes are different.");
}

if (!Objects.equals(island1.getName(), island2.getName())) {
BentoBox.getInstance().logDebug("Island names are different.");
}

if (island1.getCreatedDate() != island2.getCreatedDate()) {
BentoBox.getInstance().logDebug("Island created dates are different.");
}

if (island1.getUpdatedDate() != island2.getUpdatedDate()) {
BentoBox.getInstance().logDebug("Island updated dates are different.");
}

if (!Objects.equals(island1.getOwner(), island2.getOwner())) {
BentoBox.getInstance().logDebug("Island owners are different.");
}

if (!island1.getMembers().equals(island2.getMembers())) {
BentoBox.getInstance().logDebug("Island members are different.");
}

if (!Objects.equals(island1.getMaxMembers(), island2.getMaxMembers())) {
BentoBox.getInstance().logDebug("Island max members are different.");
}

if (island1.isSpawn() != island2.isSpawn()) {
BentoBox.getInstance().logDebug("Island spawn states are different.");
}

if (!island1.getFlags().equals(island2.getFlags())) {
BentoBox.getInstance().logDebug("Island flags are different.");
}

if (!island1.getHistory().equals(island2.getHistory())) {
BentoBox.getInstance().logDebug("Island histories are different.");
}

if (!island1.getSpawnPoint().equals(island2.getSpawnPoint())) {
BentoBox.getInstance().logDebug("Island spawn points are different.");
}

if (island1.isDoNotLoad() != island2.isDoNotLoad()) {
BentoBox.getInstance().logDebug("Island do not load states are different.");
}

if (!island1.getCooldowns().equals(island2.getCooldowns())) {
BentoBox.getInstance().logDebug("Island cooldowns are different.");
}

if (!Objects.equals(island1.getCommandRanks(), island2.getCommandRanks())) {
BentoBox.getInstance().logDebug("Island command ranks are different.");
}

if (!Objects.equals(island1.getMetaData(), island2.getMetaData())) {
BentoBox.getInstance().logDebug("Island metadata are different.");
}

if (!Objects.equals(island1.getHomes(), island2.getHomes())) {
BentoBox.getInstance().logDebug("Island homes are different.");
}

if (!Objects.equals(island1.getMaxHomes(), island2.getMaxHomes())) {
BentoBox.getInstance().logDebug("Island max homes are different.");
}
}

/**
* Adds an island to the grid
*
Expand All @@ -219,7 +98,6 @@ public boolean addIsland(@NonNull Island island) {
return false;
}
if (addToGrid(island)) {
islandsByLocation.put(island.getCenter(), island);
islandsById.put(island.getUniqueId(), island);
// Only add islands to this map if they are owned
if (island.isOwned()) {
Expand Down Expand Up @@ -253,7 +131,6 @@ private boolean addToGrid(@NonNull Island newIsland) {
}

public void clear() {
islandsByLocation.clear();
islandsById.clear();
islandsByUUID.clear();
}
Expand All @@ -262,32 +139,19 @@ public void clear() {
* Deletes an island from the cache. Does not remove blocks.
*
* @param island island to delete
* @return true if successful, false if not
*/
public boolean deleteIslandFromCache(@NonNull Island island) {
if (!islandsByLocation.remove(island.getCenter(), island)) {
// Already deleted
return false;
}
islandsById.remove(island.getUniqueId());
public void deleteIslandFromCache(@NonNull Island island) {
islandsById.remove(island.getUniqueId(), island);
removeFromIslandsByUUID(island);
// Remove from grid
if (grids.containsKey(island.getWorld())) {
return grids.get(island.getWorld()).removeFromGrid(island);
grids.get(island.getWorld()).removeFromGrid(island);
}
return false;
}

private void removeFromIslandsByUUID(Island island) {
for (Set<Island> set : islandsByUUID.values()) {
Iterator<Island> is = set.iterator();
while (is.hasNext()) {
Island i = is.next();
if (i.equals(island)) {
is.remove();
}
}
// set.removeIf(island::equals);
set.removeIf(island::equals);
}
}

Expand All @@ -296,22 +160,10 @@ private void removeFromIslandsByUUID(Island island) {
*
* @param uniqueId - island unique ID
*/
public boolean deleteIslandFromCache(@NonNull String uniqueId) {
public void deleteIslandFromCache(@NonNull String uniqueId) {
if (islandsById.containsKey(uniqueId)) {
return deleteIslandFromCache(islandsById.get(uniqueId));
deleteIslandFromCache(islandsById.get(uniqueId));
}
return false;
}

/**
* Get island based on the exact center location of the island
*
* @param location location to search for
* @return island or null if it does not exist
*/
@Nullable
public Island get(@NonNull Location location) {
return islandsByLocation.get(location);
}

/**
Expand Down Expand Up @@ -400,7 +252,7 @@ public Island getIslandAt(@NonNull Location location) {
*/
@NonNull
public Collection<Island> getIslands() {
return Collections.unmodifiableCollection(islandsByLocation.values());
return Collections.unmodifiableCollection(islandsById.values());
}

/**
Expand All @@ -418,8 +270,8 @@ public Collection<Island> getIslands(@NonNull World world) {
if (overworld == null) {
return Collections.emptyList();
}
return islandsByLocation.entrySet().stream()
.filter(entry -> overworld.equals(Util.getWorld(entry.getKey().getWorld()))) // shouldn't make NPEs
return islandsById.entrySet().stream()
.filter(entry -> overworld.equals(Util.getWorld(entry.getValue().getWorld()))) // shouldn't make NPEs
.map(Map.Entry::getValue).toList();
}

Expand Down Expand Up @@ -494,7 +346,7 @@ public void removePlayer(@NonNull Island island, @NonNull UUID uuid) {
* @return the number of islands
*/
public int size() {
return islandsByLocation.size();
return islandsById.size();
}

/**
Expand All @@ -504,7 +356,7 @@ public int size() {
* @return the number of islands
*/
public long size(World world) {
return this.islandsByLocation.keySet().stream().map(Location::getWorld).filter(world::equals).count();
return this.islandsById.values().stream().map(Island::getWorld).filter(world::equals).count();
}

/**
Expand All @@ -519,7 +371,6 @@ public void setOwner(@NonNull Island island, @Nullable UUID newOwnerUUID) {
islandsByUUID.computeIfAbsent(newOwnerUUID, k -> new HashSet<>()).add(island);
}
island.setRank(newOwnerUUID, RanksManager.OWNER_RANK);
islandsByLocation.put(island.getCenter(), island);
islandsById.put(island.getUniqueId(), island);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public void testAddIsland() {
assertTrue(ic.addIsland(island));
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
}

/**
Expand All @@ -142,56 +141,8 @@ public void testClear() {
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
ic.clear();
assertNull(ic.get(world, owner));
assertNull(ic.get(location));
}

/**
* Test for {@link IslandCache#deleteIslandFromCache(Island)}
*/
@Test
public void testDeleteIslandFromCache() {
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
boolean result = ic.deleteIslandFromCache(island);
assertTrue(result);
assertNull(ic.get(world, owner));
assertNull(ic.get(location));

// Test removing an island that is not in the cache
World world = mock(World.class);
Island island2 = mock(Island.class);
Location location2 = mock(Location.class);
when(location2.getWorld()).thenReturn(world);
when(location2.getBlockX()).thenReturn(0);
when(location2.getBlockY()).thenReturn(0);
when(location2.getBlockZ()).thenReturn(0);
when(island2.getCenter()).thenReturn(location2);
when(island2.getOwner()).thenReturn(UUID.randomUUID());
Builder<UUID> members = new ImmutableSet.Builder<>();
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
when(island2.getMemberSet()).thenReturn(members.build());
when(island2.getMinX()).thenReturn(-400);
when(island2.getMinZ()).thenReturn(-400);

assertFalse(ic.deleteIslandFromCache(island2));

}

/**
* Test for {@link IslandCache#get(Location)}
*/
@Test
public void testGetLocation() {
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(location));
}

/**
Expand Down Expand Up @@ -304,7 +255,6 @@ public void testSetOwner() {

Mockito.verify(island).setOwner(newOwnerUUID);
assertEquals(island, ic.get(world, newOwnerUUID));
assertEquals(island, ic.get(island.getCenter()));
}

/**
Expand Down

0 comments on commit 52a280d

Please sign in to comment.