Resize registry data array to actual size once collected

This commit is contained in:
Nassim Jahnke 2024-01-27 13:48:08 +01:00
parent c8d339ab30
commit a5bd8abe93
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 52 additions and 53 deletions

View File

@ -162,7 +162,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player>
@Override
public PlatformTask runRepeatingAsync(final Runnable runnable, final long ticks) {
if (FOLIA) {
return new BukkitViaTaskTask(Via.getManager().getScheduler().schedule(runnable, ticks * 50, TimeUnit.MILLISECONDS));
return new BukkitViaTaskTask(Via.getManager().getScheduler().scheduleRepeating(runnable, 0, ticks * 50, TimeUnit.MILLISECONDS));
}
return new BukkitViaTask(getServer().getScheduler().runTaskTimerAsynchronously(this, runnable, 0, ticks));
}

View File

@ -55,46 +55,47 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
registerMetadataRewriter(ClientboundPackets1_20_3.ENTITY_METADATA, Types1_20_3.METADATA_LIST, Types1_20_5.METADATA_LIST);
registerRemoveEntities(ClientboundPackets1_20_3.REMOVE_ENTITIES);
protocol.registerClientbound(ClientboundConfigurationPackets1_20_3.REGISTRY_DATA, new PacketHandlers() {
@Override
protected void register() {
handler(wrapper -> {
final CompoundTag registryData = wrapper.read(Type.COMPOUND_TAG);
cacheDimensionData(wrapper.user(), registryData);
trackBiomeSize(wrapper.user(), registryData);
protocol.registerClientbound(ClientboundConfigurationPackets1_20_3.REGISTRY_DATA, wrapper -> {
final CompoundTag registryData = wrapper.read(Type.COMPOUND_TAG);
cacheDimensionData(wrapper.user(), registryData);
trackBiomeSize(wrapper.user(), registryData);
for (final Map.Entry<String, Tag> entry : registryData.entrySet()) {
final CompoundTag entryTag = (CompoundTag) entry.getValue();
final StringTag typeTag = entryTag.get("type");
final ListTag valueTag = entryTag.get("value");
RegistryEntry[] registryEntries = new RegistryEntry[valueTag.size()];
boolean requiresDummyValues = false;
for (final Tag tag : valueTag) {
final CompoundTag compoundTag = (CompoundTag) tag;
final StringTag nameTag = compoundTag.get("name");
final int id = ((NumberTag) compoundTag.get("id")).asInt();
if (id >= registryEntries.length) {
// It was previously possible to have arbitrary ids
registryEntries = Arrays.copyOf(registryEntries, Math.max(registryEntries.length * 2, id + 1));
requiresDummyValues = true;
}
registryEntries[id] = new RegistryEntry(nameTag.getValue(), compoundTag.get("element"));
}
if (requiresDummyValues) {
fill(registryEntries);
}
final PacketWrapper registryPacket = wrapper.create(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA);
registryPacket.write(Type.STRING, typeTag.getValue());
registryPacket.write(Type.REGISTRY_ENTRY_ARRAY, registryEntries);
registryPacket.send(Protocol1_20_5To1_20_3.class);
for (final Map.Entry<String, Tag> entry : registryData.entrySet()) {
final CompoundTag entryTag = (CompoundTag) entry.getValue();
final StringTag typeTag = entryTag.get("type");
final ListTag valueTag = entryTag.get("value");
RegistryEntry[] registryEntries = new RegistryEntry[valueTag.size()];
boolean requiresDummyValues = false;
int entriesLength = registryEntries.length;
for (final Tag tag : valueTag) {
final CompoundTag compoundTag = (CompoundTag) tag;
final StringTag nameTag = compoundTag.get("name");
final int id = ((NumberTag) compoundTag.get("id")).asInt();
entriesLength = Math.max(entriesLength, id + 1);
if (id >= registryEntries.length) {
// It was previously possible to have arbitrary ids
registryEntries = Arrays.copyOf(registryEntries, Math.max(registryEntries.length * 2, id + 1));
requiresDummyValues = true;
}
wrapper.cancel();
});
registryEntries[id] = new RegistryEntry(nameTag.getValue(), compoundTag.get("element"));
}
if (requiresDummyValues) {
// Truncate and replace null values
if (registryEntries.length != entriesLength) {
registryEntries = Arrays.copyOf(registryEntries, entriesLength);
}
replaceNullValues(registryEntries);
}
final PacketWrapper registryPacket = wrapper.create(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA);
registryPacket.write(Type.STRING, typeTag.getValue());
registryPacket.write(Type.REGISTRY_ENTRY_ARRAY, registryEntries);
registryPacket.send(Protocol1_20_5To1_20_3.class);
}
wrapper.cancel();
});
protocol.registerClientbound(ClientboundPackets1_20_3.JOIN_GAME, new PacketHandlers() {
@ -126,17 +127,12 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
}
});
protocol.registerClientbound(ClientboundPackets1_20_3.RESPAWN, new PacketHandlers() {
@Override
public void register() {
handler(wrapper -> {
final String dimensionKey = wrapper.read(Type.STRING);
final DimensionData data = tracker(wrapper.user()).dimensionData(dimensionKey);
wrapper.write(Type.VAR_INT, data.id());
});
map(Type.STRING); // World
handler(worldDataTrackerHandlerByKey1_20_5(0)); // Tracks world height and name for chunk data and entity (un)tracking
}
protocol.registerClientbound(ClientboundPackets1_20_3.RESPAWN, wrapper -> {
final String dimensionKey = wrapper.read(Type.STRING);
final DimensionData data = tracker(wrapper.user()).dimensionData(dimensionKey);
wrapper.write(Type.VAR_INT, data.id());
wrapper.passthrough(Type.STRING); // World
worldDataTrackerHandlerByKey1_20_5(0).handle(wrapper); // Tracks world height and name for chunk data and entity (un)tracking
});
protocol.registerClientbound(ClientboundPackets1_20_3.ENTITY_EFFECT, wrapper -> {
@ -169,7 +165,7 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter<Clientbound
});
}
private void fill(final RegistryEntry[] entries) {
private void replaceNullValues(final RegistryEntry[] entries) {
// Find the first non-null entry and fill the array with dummy values where needed (which is easier than remapping them to different ids in a new, smaller array)
RegistryEntry first = null;
for (final RegistryEntry registryEntry : entries) {

View File

@ -103,8 +103,11 @@ public class MetaFilter {
&& (this.metaType == null || metadata.metaType() == this.metaType);
}
private boolean matchesType(@Nullable EntityType type) {
return type != null && (this.filterFamily ? type.isOrHasParent(this.type) : this.type == type);
private boolean matchesType(EntityType type) {
if (type == null) {
return false;
}
return this.filterFamily ? type.isOrHasParent(this.type) : this.type == type;
}
@Override

View File

@ -54,8 +54,8 @@ public final class TaskScheduler implements Scheduler {
scheduledExecutorService.shutdown();
try {
executorService.awaitTermination(2, TimeUnit.SECONDS);
scheduledExecutorService.awaitTermination(2, TimeUnit.SECONDS);
executorService.awaitTermination(1, TimeUnit.SECONDS);
scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
e.printStackTrace();
}