Skip to content

Commit

Permalink
Fixed recalculation of block counts not taking into-count minecarts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Sep 1, 2023
1 parent 4c8fa17 commit 8c444b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Expand Up @@ -3,6 +3,7 @@
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.core.Materials;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;

import java.util.Optional;

Expand All @@ -28,6 +29,13 @@ public class ConstantKeys {
public static final Key CHORUS_FLOWER = Optional.ofNullable(Materials.getMaterialSafe("CHORUS_FLOWER"))
.map(Keys::of).orElse(Keys.EMPTY);

public static final Key ENTITY_MINECART_COMMAND = Keys.of(EntityType.MINECART_COMMAND);
public static final Key ENTITY_MINECART_CHEST = Keys.of(EntityType.MINECART_CHEST);
public static final Key ENTITY_MINECART_FURNACE = Keys.of(EntityType.MINECART_FURNACE);
public static final Key ENTITY_MINECART_TNT = Keys.of(EntityType.MINECART_TNT);
public static final Key ENTITY_MINECART_HOPPER = Keys.of(EntityType.MINECART_HOPPER);
public static final Key ENTITY_MINECART_MOB_SPAWNER = Keys.of(EntityType.MINECART_MOB_SPAWNER);

private ConstantKeys() {

}
Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.Counter;
import com.bgsoftware.superiorskyblock.core.collections.CompletableFutureList;
import com.bgsoftware.superiorskyblock.core.key.ConstantKeys;
import com.bgsoftware.superiorskyblock.core.key.KeyIndicator;
import com.bgsoftware.superiorskyblock.core.key.KeyMaps;
import com.bgsoftware.superiorskyblock.core.key.Keys;
Expand All @@ -28,6 +29,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -38,6 +40,7 @@ public class DefaultIslandCalculationAlgorithm implements IslandCalculationAlgor

public static final Map<ChunkPosition, CalculatedChunk> CACHED_CALCULATED_CHUNKS = new ConcurrentHashMap<>();

private static final List<Pair<Key, Key>> MINECART_BLOCK_TYPES = createMinecartBlockTypes();
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

private static final DefaultIslandCalculationAlgorithm INSTANCE = new DefaultIslandCalculationAlgorithm();
Expand Down Expand Up @@ -120,6 +123,7 @@ public CompletableFuture<IslandCalculationResult> calculateIsland(Island island)
Key blockKey;
int blockCount;

// Calculate spawner counts
for (SpawnerInfo spawnerInfo : spawnersToCheck) {
try {
CreatureSpawner creatureSpawner = (CreatureSpawner) spawnerInfo.location.getBlock().getState();
Expand All @@ -144,13 +148,21 @@ public CompletableFuture<IslandCalculationResult> calculateIsland(Island island)
}
spawnersToCheck.clear();

// Calculate stacked block counts
for (ChunkPosition chunkPosition : chunksToCheck) {
for (Pair<Key, Integer> pair : plugin.getProviders().getStackedBlocksProvider()
.getBlocks(chunkPosition.getWorld(), chunkPosition.getX(), chunkPosition.getZ())) {
blockCounts.addCounts(pair.getKey(), pair.getValue() - 1);
}
}

// Calculate minecart block counts
MINECART_BLOCK_TYPES.forEach(minecartTypes -> {
int count = island.getEntitiesTracker().getEntityCount(minecartTypes.getKey());
if (count > 0)
blockCounts.addCounts(minecartTypes.getValue(), count);
});

chunksToCheck.clear();

Profiler.end(profiler);
Expand All @@ -161,6 +173,19 @@ public CompletableFuture<IslandCalculationResult> calculateIsland(Island island)
return result;
}

private static List<Pair<Key, Key>> createMinecartBlockTypes() {
List<Pair<Key, Key>> minecartBlockTypes = new LinkedList<>();

minecartBlockTypes.add(new Pair<>(ConstantKeys.ENTITY_MINECART_COMMAND, ConstantKeys.COMMAND_BLOCK));
minecartBlockTypes.add(new Pair<>(ConstantKeys.ENTITY_MINECART_CHEST, ConstantKeys.CHEST));
minecartBlockTypes.add(new Pair<>(ConstantKeys.ENTITY_MINECART_FURNACE, ConstantKeys.FURNACE));
minecartBlockTypes.add(new Pair<>(ConstantKeys.ENTITY_MINECART_TNT, ConstantKeys.TNT));
minecartBlockTypes.add(new Pair<>(ConstantKeys.ENTITY_MINECART_HOPPER, ConstantKeys.HOPPER));
minecartBlockTypes.add(new Pair<>(ConstantKeys.ENTITY_MINECART_MOB_SPAWNER, ConstantKeys.MOB_SPAWNER));

return Collections.unmodifiableList(minecartBlockTypes);
}

private static class BlockCountsTracker implements IslandCalculationResult {

private final KeyMap<BigInteger> blockCounts = KeyMaps.createConcurrentHashMap(KeyIndicator.MATERIAL);
Expand Down
Expand Up @@ -153,7 +153,7 @@ public boolean canRecalculateEntityCounts() {
}

private boolean canTrackEntity(Key key) {
return island.getEntityLimit(key) != -1;
return island.getEntityLimit(key) != -1 || key.toString().contains("MINECART");
}

}

0 comments on commit 8c444b8

Please sign in to comment.