mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-08 03:40:27 +01:00
Add ArrayUtils#toMap
This commit is contained in:
parent
c2245f3915
commit
4b89ce33ee
@ -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<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
||||
(container, namespace, object) -> {
|
||||
final var stateObject = (Map<String, Object>) object.get("states");
|
||||
// Loop each state
|
||||
var propertyEntry = new HashMap<Map<String, String>, Block>();
|
||||
for (var stateEntry : stateObject.entrySet()) {
|
||||
// Retrieve the block states
|
||||
{
|
||||
final var stateEntries = stateObject.entrySet();
|
||||
final int propertiesCount = stateEntries.size();
|
||||
Map<String, String>[] 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<String, Object>) 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);
|
||||
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));
|
||||
|
@ -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 <K, V> Map<K, V> 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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user