Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Changed entity limits to be based on integers instead of bigintegers * Added implementation to the entities tracker * Changed check for reaching of entity limits to work with the new entities tracker * Fixed incorrect naming of the entity limits listener * Added an option to recalculate the entity counts * Added getter for entities tracker * Fixed usage of Integer instead of primitive type * Added support for tracking of spawned entities * Moved tracking logic to another listener & separate class, alongside of support for untracking entities * Added missing debug message for untracking entities * Fixed entities listener not being registered * Fixed issues with detection of reaching limits * Added recalculation for entities when island chunks are loaded * Added support for stacking plugins with the entity counts * Fixed detection of reaching entity limits * Added support for stacking of entities with WildStacker and RoseStacker * Fixed a null issue with UltimateStacker when trying to get entity amounts * Fixed errors when trying to get stack sizes of entities that are not stackable * Added entity count placeholder * Removed support for stacked entities * Fixed an issue with hangings getting duplicated when their limit is reached * Fixed amount of entities cannot be the same as the limit * Merged dev branch
- Loading branch information
1 parent
38195fe
commit 94befd4
Showing
12 changed files
with
335 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 116 additions & 13 deletions
129
.../bgsoftware/superiorskyblock/island/algorithms/DefaultIslandEntitiesTrackerAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,152 @@ | ||
package com.bgsoftware.superiorskyblock.island.algorithms; | ||
|
||
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; | ||
import com.bgsoftware.superiorskyblock.api.island.Island; | ||
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandEntitiesTrackerAlgorithm; | ||
import com.bgsoftware.superiorskyblock.api.key.Key; | ||
import com.bgsoftware.superiorskyblock.key.dataset.KeyMap; | ||
import com.bgsoftware.superiorskyblock.structure.CompletableFutureList; | ||
import com.bgsoftware.superiorskyblock.threads.Executor; | ||
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger; | ||
import com.bgsoftware.superiorskyblock.utils.entities.EntityUtils; | ||
import com.bgsoftware.superiorskyblock.utils.islands.IslandUtils; | ||
import com.google.common.base.Preconditions; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public final class DefaultIslandEntitiesTrackerAlgorithm implements IslandEntitiesTrackerAlgorithm { | ||
|
||
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin(); | ||
|
||
private final KeyMap<BigInteger> entityCounts = new KeyMap<>(); | ||
private final KeyMap<Integer> entityCounts = new KeyMap<>(); | ||
|
||
private final Island island; | ||
|
||
private volatile boolean beingRecalculated = false; | ||
|
||
public DefaultIslandEntitiesTrackerAlgorithm(Island island) { | ||
this.island = island; | ||
} | ||
|
||
@Override | ||
public boolean trackEntity(Key key, BigInteger amount) { | ||
return false; | ||
public boolean trackEntity(Key key, int amount) { | ||
Preconditions.checkNotNull(key, "key parameter cannot be null."); | ||
|
||
if (amount <= 0) | ||
return false; | ||
|
||
if (!canTrackEntity(key)) | ||
return false; | ||
|
||
PluginDebugger.debug("Action: Entity Spawn, Island: " + island.getOwner().getName() + | ||
", Entity: " + key + ", Amount: " + amount); | ||
|
||
int currentAmount = entityCounts.getOrDefault(key, 0); | ||
entityCounts.put(key, currentAmount + amount); | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean untrackEntity(Key key, BigInteger amount) { | ||
return false; | ||
public boolean untrackEntity(Key key, int amount) { | ||
Preconditions.checkNotNull(key, "key parameter cannot be null."); | ||
|
||
if (amount <= 0) | ||
return false; | ||
|
||
if (!canTrackEntity(key)) | ||
return false; | ||
|
||
PluginDebugger.debug("Action: Entity Despawn, Island: " + island.getOwner().getName() + | ||
", Entity: " + key + ", Amount: " + amount); | ||
|
||
int currentAmount = entityCounts.getOrDefault(key, -1); | ||
|
||
if (currentAmount != -1) { | ||
if (currentAmount > amount) { | ||
entityCounts.put(key, currentAmount - amount); | ||
} else { | ||
entityCounts.remove(key); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public BigInteger getEntityCount(Key key) { | ||
return null; | ||
public int getEntityCount(Key key) { | ||
return entityCounts.getOrDefault(key, 0); | ||
} | ||
|
||
@Override | ||
public Map<Key, BigInteger> getEntitiesCounts() { | ||
return null; | ||
public Map<Key, Integer> getEntitiesCounts() { | ||
return Collections.unmodifiableMap(entityCounts); | ||
} | ||
|
||
@Override | ||
public void clearEntityCounts() { | ||
this.entityCounts.clear(); | ||
} | ||
|
||
@Override | ||
public void recalculateEntityCounts() { | ||
if (beingRecalculated) | ||
return; | ||
|
||
beingRecalculated = true; | ||
|
||
clearEntityCounts(); | ||
|
||
KeyMap<Integer> recalculatedEntityCounts = new KeyMap<>(); | ||
CompletableFutureList<Chunk> chunks = new CompletableFutureList<>(); | ||
|
||
for (World.Environment environment : World.Environment.values()) { | ||
try { | ||
chunks.addAll(island.getAllChunksAsync(environment, true, true, chunk -> { | ||
for (Entity entity : chunk.getEntities()) { | ||
if (EntityUtils.canBypassEntityLimit(entity)) | ||
continue; | ||
|
||
Key key = EntityUtils.getLimitEntityType(entity); | ||
|
||
if (!canTrackEntity(key)) | ||
continue; | ||
|
||
int currentEntityAmount = recalculatedEntityCounts.getOrDefault(key, 0); | ||
recalculatedEntityCounts.put(key, currentEntityAmount + 1); | ||
} | ||
})); | ||
} catch (Exception ignored) { | ||
} | ||
} | ||
|
||
Executor.async(() -> { | ||
try { | ||
//Waiting for all the chunks to load | ||
chunks.forEachCompleted(chunk -> { | ||
}, error -> { | ||
}); | ||
|
||
if (!this.entityCounts.isEmpty()) { | ||
for (Map.Entry<Key, Integer> entry : this.entityCounts.entrySet()) { | ||
Integer currentAmount = recalculatedEntityCounts.remove(entry.getKey()); | ||
if (currentAmount != null) | ||
entry.setValue(entry.getValue() + currentAmount); | ||
} | ||
} | ||
|
||
if (!recalculatedEntityCounts.isEmpty()) { | ||
this.entityCounts.putAll(recalculatedEntityCounts); | ||
} | ||
} finally { | ||
beingRecalculated = false; | ||
} | ||
}); | ||
} | ||
|
||
private boolean canTrackEntity(Key key) { | ||
return island.getEntityLimit(key) != IslandUtils.NO_LIMIT.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.