Skip to content

Commit

Permalink
Fix destroy entities handling in 1.7->1.8
Browse files Browse the repository at this point in the history
Split into multiple packets since 1.8 can handle a larger list of entities than 1.7 can, causing NegativeArraySizeException's to appear on some servers.
  • Loading branch information
FlorianMichael committed May 6, 2024
1 parent ed8cc47 commit 89e76d3
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.metadata;

import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.viaversion.viarewind.ViaRewind;
import com.viaversion.viarewind.api.rewriter.VREntityRewriter;
import com.viaversion.viarewind.api.type.Types1_7_6_10;
Expand Down Expand Up @@ -87,14 +89,19 @@ public void register() {
});
}
});
protocol.registerClientbound(ClientboundPackets1_8.DESTROY_ENTITIES, new PacketHandlers() {
@Override
public void register() {
map(Type.VAR_INT_ARRAY_PRIMITIVE, Types1_7_6_10.BYTE_INT_ARRAY); // Entity ids
handler(wrapper -> {
final int[] entities = wrapper.get(Types1_7_6_10.BYTE_INT_ARRAY, 0);
untrackEntities(wrapper.user(), entities);
});
protocol.registerClientbound(ClientboundPackets1_8.DESTROY_ENTITIES, wrapper -> {
final int[] entities = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
untrackEntities(wrapper.user(), entities);

wrapper.cancel();

// Split entity destroy packets into smaller packets because 1.8 can handle more entities at once then 1.7 can.
final List<List<Integer>> parts = Lists.partition(Ints.asList(entities), Byte.MAX_VALUE);

for (List<Integer> part : parts) {
final PacketWrapper destroy = PacketWrapper.create(ClientboundPackets1_7_2_5.DESTROY_ENTITIES, wrapper.user());
destroy.write(Types1_7_6_10.BYTE_INT_ARRAY, part.stream().mapToInt(Integer::intValue).toArray());
destroy.scheduleSend(Protocol1_7_6_10To1_8.class);
}
});
protocol.registerClientbound(ClientboundPackets1_8.ENTITY_METADATA, new PacketHandlers() {
Expand Down

0 comments on commit 89e76d3

Please sign in to comment.