3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 09:00:09 +01:00

Clean up entriesLength usage

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-05-05 13:33:21 +02:00
Ursprung 9549a18cec
Commit fc9d5b6d9d
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
2 geänderte Dateien mit 15 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -24,7 +24,7 @@ import it.unimi.dsi.fastutil.objects.ObjectSet;
public final class DamageTypes1_20_3 { public final class DamageTypes1_20_3 {
private final static Object2ObjectMap<String, CompoundTag> DAMAGE_TYPES = new Object2ObjectOpenHashMap<>(); private static final Object2ObjectMap<String, CompoundTag> DAMAGE_TYPES = new Object2ObjectOpenHashMap<>();
static { static {
DAMAGE_TYPES.put("in_fire", create("inFire", 0.1F)); DAMAGE_TYPES.put("in_fire", create("inFire", 0.1F));

Datei anzeigen

@ -121,15 +121,15 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
// Calculate number of entries (exclude duplicated ids) // Calculate number of entries (exclude duplicated ids)
RegistryEntry[] registryEntries = new RegistryEntry[valueTag.stream().map(e -> e.getInt("id")).distinct().toArray().length]; RegistryEntry[] registryEntries = new RegistryEntry[valueTag.stream().map(e -> e.getInt("id")).distinct().toArray().length];
boolean requiresDummyValues = false; boolean requiresDummyValues = false;
int entriesLength = registryEntries.length; int highestId = -1;
IntSet ids = new IntArraySet(); final IntSet ids = new IntArraySet();
for (final CompoundTag tag : valueTag) { for (final CompoundTag tag : valueTag) {
final String name = tag.getString("name"); final String name = tag.getString("name");
final int id = tag.getInt("id"); final int id = tag.getInt("id");
if (ids.add(id)) { // Override duplicated id without incrementing entries length if (ids.add(id)) { // Override duplicated id without incrementing entries length
entriesLength = Math.max(entriesLength, id + 1); highestId = Math.max(highestId, id);
if (id >= registryEntries.length) { if (id >= registryEntries.length) {
// It was previously possible to have arbitrary ids // It was previously possible to have arbitrary ids, increase array length if needed
registryEntries = Arrays.copyOf(registryEntries, Math.max(registryEntries.length * 2, id + 1)); registryEntries = Arrays.copyOf(registryEntries, Math.max(registryEntries.length * 2, id + 1));
requiresDummyValues = true; requiresDummyValues = true;
} }
@ -140,13 +140,13 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
if (Key.stripMinecraftNamespace(type).equals("damage_type")) { if (Key.stripMinecraftNamespace(type).equals("damage_type")) {
// Add spit damage type // Add spit damage type
final int length = registryEntries.length; highestId++;
registryEntries = Arrays.copyOf(registryEntries, length + 1); registryEntries = Arrays.copyOf(registryEntries, highestId + 1);
final CompoundTag spitData = new CompoundTag(); final CompoundTag spitData = new CompoundTag();
spitData.putString("scaling", "when_caused_by_living_non_player"); spitData.putString("scaling", "when_caused_by_living_non_player");
spitData.putString("message_id", "mob"); spitData.putString("message_id", "mob");
spitData.putFloat("exhaustion", 0.1F); spitData.putFloat("exhaustion", 0.1F);
registryEntries[length] = new RegistryEntry("minecraft:spit", spitData); registryEntries[highestId] = new RegistryEntry("minecraft:spit", spitData);
// Fill in missing damage types with 1.20.3/4 defaults // Fill in missing damage types with 1.20.3/4 defaults
final Set<String> registryEntryKeys = Arrays.stream(registryEntries).map(e -> Key.stripMinecraftNamespace(e.key())).collect(Collectors.toSet()); final Set<String> registryEntryKeys = Arrays.stream(registryEntries).map(e -> Key.stripMinecraftNamespace(e.key())).collect(Collectors.toSet());
@ -154,15 +154,18 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
if (registryEntryKeys.contains(key)) { if (registryEntryKeys.contains(key)) {
continue; continue;
} }
registryEntries = Arrays.copyOf(registryEntries, registryEntries.length + 1);
registryEntries[registryEntries.length - 1] = new RegistryEntry(Key.namespaced(key), DamageTypes1_20_3.get(key)); highestId++;
registryEntries = Arrays.copyOf(registryEntries, highestId + 1);
registryEntries[highestId] = new RegistryEntry(Key.namespaced(key), DamageTypes1_20_3.get(key));
} }
} }
if (requiresDummyValues) { if (requiresDummyValues) {
// Truncate and replace null values // Truncate and replace null values
if (registryEntries.length != entriesLength) { final int finalLength = highestId + 1;
registryEntries = Arrays.copyOf(registryEntries, entriesLength); if (registryEntries.length != finalLength) {
registryEntries = Arrays.copyOf(registryEntries, finalLength);
} }
replaceNullValues(registryEntries); replaceNullValues(registryEntries);
} }