Add fallback for block properties with > 10 entries

This commit is contained in:
themode 2021-12-28 12:40:16 +01:00 committed by TheMode
parent 6082741143
commit 9c73e72771

View File

@ -1,11 +1,11 @@
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.StringUtils; import net.minestom.server.utils.StringUtils;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class BlockUtils { public class BlockUtils {
@ -60,19 +60,18 @@ public class BlockUtils {
if (propertiesString.isEmpty()) return Map.of(); if (propertiesString.isEmpty()) return Map.of();
final int entries = StringUtils.countMatches(propertiesString, ',') + 1; final int entries = StringUtils.countMatches(propertiesString, ',') + 1;
assert entries > 0;
String[] keys = new String[entries]; String[] keys = new String[entries];
String[] values = new String[entries]; String[] values = new String[entries];
int entryCount = 0; int entryCount = 0;
final int length = propertiesString.length(); final int length = propertiesString.length();
int start = 0; int start = 0;
int end;
int index = 0; int index = 0;
while (index < length) { while (index < length) {
if (propertiesString.charAt(index) == ',' || index == length - 1) { if (propertiesString.charAt(index) == ',' || index == length - 1) {
if (index + 1 == length) index++; if (index + 1 == length) index++;
end = index; final String property = propertiesString.substring(start, index);
final String property = propertiesString.substring(start, end);
final int equalIndex = property.indexOf('='); final int equalIndex = property.indexOf('=');
if (equalIndex != -1) { if (equalIndex != -1) {
final String key = property.substring(0, equalIndex).trim(); final String key = property.substring(0, equalIndex).trim();
@ -80,50 +79,33 @@ public class BlockUtils {
keys[entryCount] = key.intern(); keys[entryCount] = key.intern();
values[entryCount++] = value.intern(); values[entryCount++] = value.intern();
} }
start = end + 1; start = index + 1;
} }
index++; index++;
} }
assert entryCount == entries; assert entryCount == entries;
return switch (entryCount) { return switch (entryCount) {
case 0 -> Map.of();
case 1 -> Map.of(keys[0], values[0]); case 1 -> Map.of(keys[0], values[0]);
case 2 -> Map.of(keys[0], values[0], keys[1], values[1]); case 2 -> Map.of(keys[0], values[0], keys[1], values[1]);
case 3 -> Map.of(keys[0], values[0], case 3 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2]);
keys[1], values[1], keys[2], values[2]); case 4 -> 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[3], values[3]);
keys[2], values[2], keys[3], values[3]); case 5 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
case 5 -> Map.of(keys[0], values[0], keys[1], values[1], keys[3], values[3], keys[4], values[4]);
keys[2], values[2], keys[3], values[3], case 6 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
keys[4], values[4]); keys[3], values[3], keys[4], values[4], keys[5], values[5]);
case 6 -> Map.of(keys[0], values[0], keys[1], values[1], case 7 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
keys[2], values[2], keys[3], values[3], keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6]);
keys[4], values[4], keys[5], values[5]); case 8 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
case 7 -> Map.of(keys[0], values[0], keys[1], values[1], keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6], values[6],
keys[2], values[2], keys[3], values[3], keys[7], values[7]);
keys[4], values[4], keys[5], values[5], case 9 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
keys[6], values[6]); 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[7], values[7], keys[8], values[8]);
keys[2], values[2], keys[3], values[3], case 10 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
keys[4], values[4], keys[5], values[5], keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6],
keys[6], values[6], keys[7], values[7]); values[6], keys[7], values[7], keys[8], values[8], keys[9], values[9]);
case 9 -> Map.of(keys[0], values[0], keys[1], values[1], default -> Map.copyOf(new Object2ObjectArrayMap<>(keys, values));
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<String, String> map = new HashMap<>(entryCount);
for (int i = 0; i < entryCount; i++) {
map.put(keys[i], values[i]);
}
yield Map.copyOf(map);
}
}; };
} }
} }