mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Assume ids to start from 0 and increase 1 by 1. Improve lookup performance
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
7a4086c889
commit
c745f1e064
@ -3,7 +3,7 @@ package net.minestom.server.instance.block;
|
|||||||
import com.github.benmanes.caffeine.cache.Cache;
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||||
import net.minestom.server.registry.Registry;
|
import net.minestom.server.registry.Registry;
|
||||||
import net.minestom.server.tag.Tag;
|
import net.minestom.server.tag.Tag;
|
||||||
import net.minestom.server.utils.block.BlockUtils;
|
import net.minestom.server.utils.block.BlockUtils;
|
||||||
@ -17,7 +17,7 @@ import java.util.function.Function;
|
|||||||
|
|
||||||
final class BlockImpl implements Block {
|
final class BlockImpl implements Block {
|
||||||
// Block state -> block object
|
// Block state -> block object
|
||||||
private static final Int2ObjectOpenHashMap<Block> BLOCK_STATE_MAP = new Int2ObjectOpenHashMap<>();
|
private static final ObjectArrayList<Block> BLOCK_STATE_MAP = new ObjectArrayList<>();
|
||||||
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
||||||
(container, namespace, object) -> {
|
(container, namespace, object) -> {
|
||||||
final JsonObject stateObject = object.remove("states").getAsJsonObject();
|
final JsonObject stateObject = object.remove("states").getAsJsonObject();
|
||||||
@ -30,7 +30,7 @@ final class BlockImpl implements Block {
|
|||||||
final var propertyMap = BlockUtils.parseProperties(query);
|
final var propertyMap = BlockUtils.parseProperties(query);
|
||||||
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
||||||
unmodifiableEntries, propertyMap, null, null);
|
unmodifiableEntries, propertyMap, null, null);
|
||||||
BLOCK_STATE_MAP.put(block.stateId(), block);
|
BLOCK_STATE_MAP.add(block.stateId(), block);
|
||||||
propertyEntry.put(propertyMap, block);
|
propertyEntry.put(propertyMap, block);
|
||||||
}
|
}
|
||||||
// Register default state
|
// Register default state
|
||||||
|
@ -4,7 +4,7 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||||
import net.minestom.server.entity.EntitySpawnType;
|
import net.minestom.server.entity.EntitySpawnType;
|
||||||
import net.minestom.server.entity.EquipmentSlot;
|
import net.minestom.server.entity.EquipmentSlot;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
@ -76,7 +76,7 @@ public final class Registry {
|
|||||||
// namespace -> registry data
|
// namespace -> registry data
|
||||||
private final Map<String, T> namespaceMap = new HashMap<>();
|
private final Map<String, T> namespaceMap = new HashMap<>();
|
||||||
// id -> registry data
|
// id -> registry data
|
||||||
private final Int2ObjectOpenHashMap<T> idMap = new Int2ObjectOpenHashMap<>();
|
private final ObjectArrayList<T> idMap = new ObjectArrayList<>();
|
||||||
private final Collection<T> objects = Collections.unmodifiableCollection(namespaceMap.values());
|
private final Collection<T> objects = Collections.unmodifiableCollection(namespaceMap.values());
|
||||||
|
|
||||||
private final boolean initialized;
|
private final boolean initialized;
|
||||||
@ -111,7 +111,7 @@ public final class Registry {
|
|||||||
|
|
||||||
public void register(@NotNull T value) {
|
public void register(@NotNull T value) {
|
||||||
Check.stateCondition(initialized, "Registering is only available within the loader lambda.");
|
Check.stateCondition(initialized, "Registering is only available within the loader lambda.");
|
||||||
this.idMap.put(value.id(), value);
|
this.idMap.add(value.id(), value);
|
||||||
this.namespaceMap.put(value.name(), value);
|
this.namespaceMap.put(value.name(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user