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 com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
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.ArrayUtils;
|
||||||
import net.minestom.server.utils.ObjectArray;
|
import net.minestom.server.utils.ObjectArray;
|
||||||
import net.minestom.server.utils.block.BlockUtils;
|
import net.minestom.server.utils.block.BlockUtils;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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,
|
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
||||||
(container, namespace, object) -> {
|
(container, namespace, object) -> {
|
||||||
final var stateObject = (Map<String, Object>) object.get("states");
|
final var stateObject = (Map<String, Object>) object.get("states");
|
||||||
// Loop each state
|
// Retrieve the block states
|
||||||
var propertyEntry = new HashMap<Map<String, String>, Block>();
|
{
|
||||||
for (var stateEntry : stateObject.entrySet()) {
|
final var stateEntries = stateObject.entrySet();
|
||||||
final String query = stateEntry.getKey();
|
final int propertiesCount = stateEntries.size();
|
||||||
final var stateOverride = (Map<String, Object>) stateEntry.getValue();
|
Map<String, String>[] propertiesKeys = new Map[propertiesCount];
|
||||||
final var propertyMap = BlockUtils.parseProperties(query);
|
Block[] blocksValues = new Block[propertiesCount];
|
||||||
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
int propertiesOffset = 0;
|
||||||
propertyMap, null, null);
|
for (var stateEntry : stateEntries) {
|
||||||
BLOCK_STATE_MAP.set(block.stateId(), block);
|
final String query = stateEntry.getKey();
|
||||||
propertyEntry.put(propertyMap, block);
|
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);
|
||||||
|
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
|
// Register default state
|
||||||
final int defaultState = ((Number) object.get("defaultStateId")).intValue();
|
final int defaultState = ((Number) object.get("defaultStateId")).intValue();
|
||||||
container.register(getState(defaultState));
|
container.register(getState(defaultState));
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package net.minestom.server.utils;
|
package net.minestom.server.utils;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntList;
|
import it.unimi.dsi.fastutil.ints.IntList;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.function.LongConsumer;
|
import java.util.Map;
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public final class ArrayUtils {
|
public final class ArrayUtils {
|
||||||
@ -30,17 +31,32 @@ public final class ArrayUtils {
|
|||||||
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void forDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b,
|
public static <K, V> Map<K, V> toMap(@NotNull K[] keys, @NotNull V[] values, int length) {
|
||||||
@NotNull LongConsumer consumer) {
|
assert keys.length >= length && keys.length == values.length;
|
||||||
loop:
|
return switch (length) {
|
||||||
for (final long aValue : a) {
|
case 0 -> Map.of();
|
||||||
for (final long bValue : b) {
|
case 1 -> Map.of(keys[0], values[0]);
|
||||||
if (bValue == aValue) {
|
case 2 -> Map.of(keys[0], values[0], keys[1], values[1]);
|
||||||
continue loop;
|
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]);
|
||||||
consumer.accept(aValue);
|
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) {
|
public static int @NotNull [] toArray(@NotNull IntList list) {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package net.minestom.server.utils.block;
|
package net.minestom.server.utils.block;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
|
||||||
import net.minestom.server.coordinate.Point;
|
import net.minestom.server.coordinate.Point;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
|
import net.minestom.server.utils.ArrayUtils;
|
||||||
import net.minestom.server.utils.StringUtils;
|
import net.minestom.server.utils.StringUtils;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -83,29 +83,6 @@ public class BlockUtils {
|
|||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
return switch (entryCount) {
|
return ArrayUtils.toMap(keys, values, 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));
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user