From 4b89ce33ee6ccc91ef66d50b2c0626b468c69059 Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 30 Dec 2021 12:07:16 +0100 Subject: [PATCH] Add ArrayUtils#toMap --- .../server/instance/block/BlockImpl.java | 31 +++++++++----- .../net/minestom/server/utils/ArrayUtils.java | 40 +++++++++++++------ .../server/utils/block/BlockUtils.java | 27 +------------ 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/block/BlockImpl.java b/src/main/java/net/minestom/server/instance/block/BlockImpl.java index c9f0fe2bd..4516ecc66 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockImpl.java +++ b/src/main/java/net/minestom/server/instance/block/BlockImpl.java @@ -4,6 +4,7 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import net.minestom.server.registry.Registry; import net.minestom.server.tag.Tag; +import net.minestom.server.utils.ArrayUtils; import net.minestom.server.utils.ObjectArray; import net.minestom.server.utils.block.BlockUtils; import org.jetbrains.annotations.NotNull; @@ -29,18 +30,26 @@ record BlockImpl(@NotNull Registry.BlockEntry registry, private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS, (container, namespace, object) -> { final var stateObject = (Map) object.get("states"); - // Loop each state - var propertyEntry = new HashMap, Block>(); - for (var stateEntry : stateObject.entrySet()) { - final String query = stateEntry.getKey(); - final var stateOverride = (Map) stateEntry.getValue(); - final var propertyMap = BlockUtils.parseProperties(query); - final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride), - propertyMap, null, null); - BLOCK_STATE_MAP.set(block.stateId(), block); - propertyEntry.put(propertyMap, block); + // Retrieve the block states + { + final var stateEntries = stateObject.entrySet(); + final int propertiesCount = stateEntries.size(); + Map[] propertiesKeys = new Map[propertiesCount]; + Block[] blocksValues = new Block[propertiesCount]; + int propertiesOffset = 0; + for (var stateEntry : stateEntries) { + final String query = stateEntry.getKey(); + final var stateOverride = (Map) stateEntry.getValue(); + final var propertyMap = BlockUtils.parseProperties(query); + final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride), + propertyMap, null, null); + BLOCK_STATE_MAP.set(block.stateId(), block); + propertiesKeys[propertiesOffset] = propertyMap; + blocksValues[propertiesOffset++] = block; + } + POSSIBLE_STATES.set(((Number) object.get("id")).intValue(), + ArrayUtils.toMap(propertiesKeys, blocksValues, propertiesOffset)); } - POSSIBLE_STATES.set(((Number) object.get("id")).intValue(), Map.copyOf(propertyEntry)); // Register default state final int defaultState = ((Number) object.get("defaultStateId")).intValue(); container.register(getState(defaultState)); diff --git a/src/main/java/net/minestom/server/utils/ArrayUtils.java b/src/main/java/net/minestom/server/utils/ArrayUtils.java index 941767c35..fb5cfe494 100644 --- a/src/main/java/net/minestom/server/utils/ArrayUtils.java +++ b/src/main/java/net/minestom/server/utils/ArrayUtils.java @@ -1,10 +1,11 @@ package net.minestom.server.utils; import it.unimi.dsi.fastutil.ints.IntList; +import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; -import java.util.function.LongConsumer; +import java.util.Map; @ApiStatus.Internal public final class ArrayUtils { @@ -30,17 +31,32 @@ public final class ArrayUtils { System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index); } - public static void forDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b, - @NotNull LongConsumer consumer) { - loop: - for (final long aValue : a) { - for (final long bValue : b) { - if (bValue == aValue) { - continue loop; - } - } - consumer.accept(aValue); - } + public static Map toMap(@NotNull K[] keys, @NotNull V[] values, int length) { + assert keys.length >= length && keys.length == values.length; + return switch (length) { + case 0 -> Map.of(); + case 1 -> Map.of(keys[0], values[0]); + case 2 -> Map.of(keys[0], values[0], keys[1], values[1]); + case 3 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2]); + case 4 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3]); + case 5 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3], keys[4], values[4]); + case 6 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3], keys[4], values[4], keys[5], values[5]); + case 7 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6]); + case 8 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6], + keys[7], values[7]); + case 9 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6], + keys[7], values[7], keys[8], values[8]); + case 10 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], + keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6], + keys[7], values[7], keys[8], values[8], keys[9], values[9]); + default -> Map.copyOf(new Object2ObjectArrayMap<>(keys, values, length)); + }; } public static int @NotNull [] toArray(@NotNull IntList list) { diff --git a/src/main/java/net/minestom/server/utils/block/BlockUtils.java b/src/main/java/net/minestom/server/utils/block/BlockUtils.java index d02604176..76084df26 100644 --- a/src/main/java/net/minestom/server/utils/block/BlockUtils.java +++ b/src/main/java/net/minestom/server/utils/block/BlockUtils.java @@ -1,9 +1,9 @@ package net.minestom.server.utils.block; -import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import net.minestom.server.coordinate.Point; import net.minestom.server.instance.Instance; import net.minestom.server.instance.block.Block; +import net.minestom.server.utils.ArrayUtils; import net.minestom.server.utils.StringUtils; import java.util.Map; @@ -83,29 +83,6 @@ public class BlockUtils { } index++; } - return switch (entryCount) { - case 0 -> Map.of(); - case 1 -> Map.of(keys[0], values[0]); - case 2 -> Map.of(keys[0], values[0], keys[1], values[1]); - case 3 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2]); - case 4 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3]); - case 5 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3], keys[4], values[4]); - case 6 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3], keys[4], values[4], keys[5], values[5]); - case 7 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6]); - case 8 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6], - keys[7], values[7]); - case 9 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6], - keys[7], values[7], keys[8], values[8]); - case 10 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2], - keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], - values[6], keys[7], values[7], keys[8], values[8], keys[9], values[9]); - default -> Map.copyOf(new Object2ObjectArrayMap<>(keys, values, entryCount)); - }; + return ArrayUtils.toMap(keys, values, entryCount); } }