Use an array to store custom block id's in BlockManager for faster lookups

This commit is contained in:
Nesaak 2020-09-08 09:27:11 -04:00
parent d3d3e73eef
commit e39e692cde

View File

@ -1,6 +1,5 @@
package net.minestom.server.instance.block; package net.minestom.server.instance.block;
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap; import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
import net.minestom.server.instance.block.rule.BlockPlacementRule; import net.minestom.server.instance.block.rule.BlockPlacementRule;
@ -10,7 +9,7 @@ import java.util.Map;
public class BlockManager { public class BlockManager {
// custom block id -> custom block // custom block id -> custom block
private Short2ObjectMap<CustomBlock> customBlocksInternalId = new Short2ObjectOpenHashMap<>(); private CustomBlock[] customBlocksInternalId = new CustomBlock[Short.MAX_VALUE];
// custom block identifier -> custom block // custom block identifier -> custom block
private Map<String, CustomBlock> customBlocksId = new HashMap<>(); private Map<String, CustomBlock> customBlocksId = new HashMap<>();
@ -23,9 +22,10 @@ public class BlockManager {
* @param customBlock the custom block to register * @param customBlock the custom block to register
*/ */
public void registerCustomBlock(CustomBlock customBlock) { public void registerCustomBlock(CustomBlock customBlock) {
if (customBlock.getCustomBlockId() < 0) throw new IllegalArgumentException("Custom block ID must be > 0, got: " + customBlock.getCustomBlockId());
final String identifier = customBlock.getIdentifier(); final String identifier = customBlock.getIdentifier();
final short id = customBlock.getCustomBlockId(); final short id = customBlock.getCustomBlockId();
this.customBlocksInternalId.put(id, customBlock); this.customBlocksInternalId[id] = customBlock;
this.customBlocksId.put(identifier, customBlock); this.customBlocksId.put(identifier, customBlock);
} }
@ -77,7 +77,7 @@ public class BlockManager {
* @return the {@link CustomBlock} associated with the id, null if not any * @return the {@link CustomBlock} associated with the id, null if not any
*/ */
public CustomBlock getCustomBlock(short id) { public CustomBlock getCustomBlock(short id) {
return customBlocksInternalId.get(id); return customBlocksInternalId[id];
} }
} }