Skip to content

Commit

Permalink
Optimized usage of Key
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Aug 12, 2023
1 parent bf25002 commit 0bf6e51
Show file tree
Hide file tree
Showing 108 changed files with 2,731 additions and 1,302 deletions.
Expand Up @@ -3,8 +3,7 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.google.common.base.Preconditions;
import gcspawners.ASAPI;
Expand Down Expand Up @@ -58,7 +57,7 @@ public void onSpawnerStack(AdvancedSpawnerPlaceEvent e) {

if (island != null)
island.handleBlockPlace(
KeyImpl.of(Materials.SPAWNER.toBukkitType() + "", e.getEntityType()),
Keys.ofSpawner(e.getEntityType()),
e.getCountPlaced());
}

Expand All @@ -67,7 +66,7 @@ public void onSpawnerUnstack(AdvancedSpawnersBreakEvent e) {
Island island = plugin.getGrid().getIslandAt(e.getSpawner().getLocation());
if (island != null)
island.handleBlockBreak(
KeyImpl.of(Materials.SPAWNER.toBukkitType() + "", e.getEntityType()),
Keys.ofSpawner(e.getEntityType()),
e.getCountBroken());
}

Expand Down
Expand Up @@ -5,7 +5,7 @@
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.schematic.Schematic;
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.world.schematic.BaseSchematic;
Expand Down Expand Up @@ -132,12 +132,12 @@ private void readBlock(Object baseBlock) {
if (ADAPT.isValid() && GET_BLOCK_TYPE.isValid() && GET_INTERNAL_ID.isValid()) {
Material material = ADAPT.invoke(null, GET_BLOCK_TYPE.invoke(baseBlock));
int data = GET_INTERNAL_ID.invokeWithDef(baseBlock, 0);
key = KeyImpl.of(material, (byte) data);
key = Keys.of(material, (byte) data);
} else {
int id = GET_ID.invoke(baseBlock);
int data = GET_DATA.invoke(baseBlock);
//noinspection deprecation
key = KeyImpl.of(Material.getMaterial(id), (byte) data);
key = Keys.of(Material.getMaterial(id), (byte) data);
}

cachedCounts.put(key, cachedCounts.getRaw(key, 0) + 1);
Expand Down
Expand Up @@ -2,7 +2,7 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.listener.BlockChangesListener;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void onMinionBreak(MinerBlockBreakEvent e) {
if (unstackResult.shouldCancelOriginalEvent()) {
e.setCancelled(true);
} else {
blockChangesListener.get().onBlockBreak(KeyImpl.of(e.getBlock()), e.getBlock().getLocation(),
blockChangesListener.get().onBlockBreak(Keys.of(e.getBlock()), e.getBlock().getLocation(),
plugin.getNMSWorld().getDefaultAmount(e.getBlock()),
BlockChangesListener.Flag.DIRTY_CHUNK, BlockChangesListener.Flag.SAVE_BLOCK_COUNT);
}
Expand Down
Expand Up @@ -3,8 +3,7 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.google.common.base.Preconditions;
import com.vk2gpz.mergedspawner.api.MergedSpawnerAPI;
Expand Down Expand Up @@ -67,7 +66,7 @@ public void onSpawnerUnstack(MergedSpawnerBreakEvent e) {
Island island = plugin.getGrid().getIslandAt(e.getBlock().getLocation());
int decreaseAmount = e.getOldCount() - e.getNewCount();
if (island != null)
island.handleBlockBreak(KeyImpl.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawnerType() + ""), decreaseAmount);
island.handleBlockBreak(Keys.ofSpawner(e.getSpawnerType()), decreaseAmount);
}

}
Expand Down
Expand Up @@ -3,8 +3,7 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.google.common.base.Preconditions;
import de.candc.events.SpawnerBreakEvent;
Expand Down Expand Up @@ -50,14 +49,14 @@ private class StackerListener implements Listener {
public void onSpawnerPlace(SpawnerPlaceEvent e) {
Island island = plugin.getGrid().getIslandAt(e.getSpawner().getLocation());
if (island != null)
island.handleBlockPlace(KeyImpl.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawnedEntity() + ""), 1);
island.handleBlockPlace(Keys.ofSpawner(e.getSpawnedEntity()), 1);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSpawnerUnstack(SpawnerBreakEvent e) {
Island island = plugin.getGrid().getIslandAt(e.getSpawner().getLocation());
if (island != null)
island.handleBlockBreak(KeyImpl.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawnedEntity() + ""), 1);
island.handleBlockBreak(Keys.ofSpawner(e.getSpawnedEntity()), 1);
}

}
Expand Down
Expand Up @@ -7,7 +7,7 @@
import com.bgsoftware.superiorskyblock.api.island.IslandPrivilege;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.external.slimefun.ProtectionModule_Dev999;
Expand Down Expand Up @@ -116,7 +116,7 @@ public void onAndroidMiner(AndroidMineEvent e) {
if (unstackResult.shouldCancelOriginalEvent()) {
e.setCancelled(true);
} else {
blockChangesListener.get().onBlockBreak(KeyImpl.of(e.getBlock()), e.getBlock().getLocation(),
blockChangesListener.get().onBlockBreak(Keys.of(e.getBlock()), e.getBlock().getLocation(),
plugin.getNMSWorld().getDefaultAmount(e.getBlock()),
BlockChangesListener.Flag.SAVE_BLOCK_COUNT, BlockChangesListener.Flag.DIRTY_CHUNK);
}
Expand All @@ -128,7 +128,7 @@ private static class AutoPlacerPlaceListener implements Listener {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onAutoPlacerPlaceBlock(BlockPlacerPlaceEvent e) {
blockChangesListener.get().onBlockPlace(KeyImpl.of(e.getBlock()), e.getBlock().getLocation(),
blockChangesListener.get().onBlockPlace(Keys.of(e.getBlock()), e.getBlock().getLocation(),
plugin.getNMSWorld().getDefaultAmount(e.getBlock()), null,
BlockChangesListener.Flag.DIRTY_CHUNK, BlockChangesListener.Flag.SAVE_BLOCK_COUNT);
}
Expand Down
Expand Up @@ -6,9 +6,8 @@
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.external.WildStackerSnapshotsContainer;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void onSpawnerPlace(SpawnerPlaceEvent e) {
if (island == null)
return;

Key blockKey = Key.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawner().getSpawnedType() + "");
Key blockKey = Keys.ofSpawner(e.getSpawner().getSpawnedType());
int increaseAmount = e.getSpawner().getStackAmount();

if (island.hasReachedBlockLimit(blockKey, increaseAmount)) {
Expand All @@ -90,7 +89,7 @@ public void onSpawnerStack(SpawnerStackEvent e) {
if (island == null)
return;

Key blockKey = Key.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawner().getSpawnedType() + "");
Key blockKey = Keys.ofSpawner(e.getSpawner().getSpawnedType());
int increaseAmount = e.getTarget().getStackAmount();

if (increaseAmount < 0) {
Expand All @@ -106,7 +105,7 @@ public void onSpawnerStack(SpawnerStackEvent e) {
public void onSpawnerUnstack(SpawnerUnstackEvent e) {
Island island = plugin.getGrid().getIslandAt(e.getSpawner().getLocation());
if (island != null)
island.handleBlockBreak(KeyImpl.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawner().getSpawnedType() + ""), e.getAmount());
island.handleBlockBreak(Keys.ofSpawner(e.getSpawner().getSpawnedType()), e.getAmount());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand All @@ -116,7 +115,7 @@ public void onSpawnerPlaceInventory(SpawnerPlaceInventoryEvent e) {
if (island == null)
return;

Key blockKey = Key.of(Materials.SPAWNER.toBukkitType() + "", e.getSpawner().getSpawnedType() + "");
Key blockKey = Keys.ofSpawner(e.getSpawner().getSpawnedType());
int increaseAmount = e.getIncreaseAmount();

if (island.hasReachedBlockLimit(blockKey, increaseAmount)) {
Expand Down
Expand Up @@ -8,9 +8,11 @@
import com.bgsoftware.superiorskyblock.api.key.KeyMap;
import com.bgsoftware.superiorskyblock.core.CalculatedChunk;
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.Counter;
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.KeyMapImpl;
import com.bgsoftware.superiorskyblock.core.key.KeyIndicator;
import com.bgsoftware.superiorskyblock.core.key.KeyMaps;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.nms.NMSChunks;
import com.bgsoftware.superiorskyblock.nms.v117.chunks.CropsBlockEntity;
Expand Down Expand Up @@ -63,7 +65,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class NMSChunksImpl implements NMSChunks {
Expand Down Expand Up @@ -315,8 +316,8 @@ public List<Location> getBlockEntities(Chunk chunk) {
}

private static CalculatedChunk calculateChunk(ChunkPosition chunkPosition, LevelChunkSection[] chunkSections) {
KeyMap<Integer> blockCounts = KeyMapImpl.createHashMap();
Set<Location> spawnersLocations = new HashSet<>();
KeyMap<Counter> blockCounts = KeyMaps.createHashMap(KeyIndicator.MATERIAL);
List<Location> spawnersLocations = new LinkedList<>();

for (LevelChunkSection levelChunkSection : chunkSections) {
if (levelChunkSection != null && !levelChunkSection.isEmpty()) {
Expand All @@ -339,8 +340,8 @@ private static CalculatedChunk calculateChunk(ChunkPosition chunkPosition, Level
}

private static void calculateChunkInternal(BlockState blockState, int x, int y, int z, ChunkPosition chunkPosition,
LevelChunkSection levelChunkSection, KeyMap<Integer> blockCounts,
Set<Location> spawnersLocations) {
LevelChunkSection levelChunkSection, KeyMap<Counter> blockCounts,
List<Location> spawnersLocations) {
Block block = blockState.getBlock();

if (block == Blocks.AIR)
Expand All @@ -358,9 +359,8 @@ private static void calculateChunkInternal(BlockState blockState, int x, int y,
blockState = blockState.setValue(SlabBlock.TYPE, SlabType.BOTTOM);
}

Key rawBlockKey = KeyBlocksCache.getBlockKey(blockState.getBlock());
Key blockKey = KeyImpl.of(rawBlockKey, location);
blockCounts.put(blockKey, blockCounts.getOrDefault(blockKey, 0) + blockAmount);
Key blockKey = Keys.of(KeyBlocksCache.getBlockKey(blockState.getBlock()), location);
blockCounts.computeIfAbsent(blockKey, b -> new Counter(0)).inc(blockAmount);
if (block == Blocks.SPAWNER) {
spawnersLocations.add(location);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.listener.SignsListener;
import com.bgsoftware.superiorskyblock.nms.ICachedBlock;
import com.bgsoftware.superiorskyblock.nms.NMSWorld;
Expand Down Expand Up @@ -89,9 +89,7 @@ public Key getBlockKey(ChunkSnapshot chunkSnapshot, int x, int y, int z) {
(chunkSnapshot.getZ() << 4) + z
);

Key rawBlockKey = KeyBlocksCache.getBlockKey(block);

return KeyImpl.of(rawBlockKey, location);
return Keys.of(KeyBlocksCache.getBlockKey(block), location);
}

@Override
Expand Down
@@ -1,7 +1,7 @@
package com.bgsoftware.superiorskyblock.nms.v117.world;

import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import org.bukkit.Material;
Expand All @@ -21,7 +21,7 @@ private KeyBlocksCache() {
public static Key getBlockKey(Block block) {
return BLOCK_TO_KEY.computeIfAbsent(block, unused -> {
Material blockType = CraftMagicNumbers.getMaterial(block);
return KeyImpl.of(blockType.name() + "", "0");
return Keys.of(blockType, (short) 0);
});
}

Expand Down
Expand Up @@ -7,9 +7,11 @@
import com.bgsoftware.superiorskyblock.api.key.KeyMap;
import com.bgsoftware.superiorskyblock.core.CalculatedChunk;
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.Counter;
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.KeyMapImpl;
import com.bgsoftware.superiorskyblock.core.key.KeyIndicator;
import com.bgsoftware.superiorskyblock.core.key.KeyMaps;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.nms.NMSChunks;
import com.bgsoftware.superiorskyblock.nms.v1182.chunks.CropsBlockEntity;
Expand Down Expand Up @@ -60,12 +62,10 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class NMSChunksImpl implements NMSChunks {
Expand Down Expand Up @@ -418,8 +418,8 @@ public List<Location> getBlockEntities(Chunk chunk) {
}

private static CalculatedChunk calculateChunk(ChunkPosition chunkPosition, LevelChunkSection[] chunkSections) {
KeyMap<Integer> blockCounts = KeyMapImpl.createHashMap();
Set<Location> spawnersLocations = new HashSet<>();
KeyMap<Counter> blockCounts = KeyMaps.createHashMap(KeyIndicator.MATERIAL);
List<Location> spawnersLocations = new LinkedList<>();

for (LevelChunkSection levelChunkSection : chunkSections) {
if (levelChunkSection != null && !levelChunkSection.hasOnlyAir()) {
Expand All @@ -442,8 +442,8 @@ private static CalculatedChunk calculateChunk(ChunkPosition chunkPosition, Level
}

private static void calculateChunkInternal(BlockState blockState, int x, int y, int z, ChunkPosition chunkPosition,
LevelChunkSection levelChunkSection, KeyMap<Integer> blockCounts,
Set<Location> spawnersLocations) {
LevelChunkSection levelChunkSection, KeyMap<Counter> blockCounts,
List<Location> spawnersLocations) {
Block block = blockState.getBlock();

if (block == Blocks.AIR)
Expand All @@ -461,9 +461,8 @@ private static void calculateChunkInternal(BlockState blockState, int x, int y,
blockState = blockState.setValue(SlabBlock.TYPE, SlabType.BOTTOM);
}

Key rawBlockKey = KeyBlocksCache.getBlockKey(blockState.getBlock());
Key blockKey = KeyImpl.of(rawBlockKey, location);
blockCounts.put(blockKey, blockCounts.getOrDefault(blockKey, 0) + blockAmount);
Key blockKey = Keys.of(KeyBlocksCache.getBlockKey(blockState.getBlock()), location);
blockCounts.computeIfAbsent(blockKey, b -> new Counter(0)).inc(blockAmount);
if (block == Blocks.SPAWNER) {
spawnersLocations.add(location);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.listener.SignsListener;
import com.bgsoftware.superiorskyblock.nms.ICachedBlock;
import com.bgsoftware.superiorskyblock.nms.NMSWorld;
Expand Down Expand Up @@ -90,9 +90,7 @@ public Key getBlockKey(ChunkSnapshot chunkSnapshot, int x, int y, int z) {
(chunkSnapshot.getZ() << 4) + z
);

Key rawBlockKey = KeyBlocksCache.getBlockKey(block);

return KeyImpl.of(rawBlockKey, location);
return Keys.of(KeyBlocksCache.getBlockKey(block), location);
}

@Override
Expand Down
@@ -1,7 +1,7 @@
package com.bgsoftware.superiorskyblock.nms.v1182.world;

import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import org.bukkit.Material;
Expand All @@ -21,7 +21,7 @@ private KeyBlocksCache() {
public static Key getBlockKey(Block block) {
return BLOCK_TO_KEY.computeIfAbsent(block, unused -> {
Material blockType = CraftMagicNumbers.getMaterial(block);
return KeyImpl.of(blockType.name() + "", "0");
return Keys.of(blockType, (short) 0);
});
}

Expand Down

0 comments on commit 0bf6e51

Please sign in to comment.