From b221e47eb72d81b5e2829723fff8a8d32db01219 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 19 Apr 2024 20:56:37 +0200 Subject: [PATCH] Add creative mode range attributes if needed Add the range argument either with or without modifiers depending on the gamemode to otherwise reset them --- .../rewriter/EntityPacketRewriter1_20_5.java | 58 ++++++++++++++++++- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java index f80f8b2e1..f7e6592df 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java @@ -21,6 +21,7 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.entity.DimensionData; import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.RegistryEntry; @@ -39,6 +40,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5 import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Attributes1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.BannerPatterns1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundConfigurationPackets1_20_5; +import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPackets1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.storage.AcknowledgedMessagesStorage; import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.util.Key; @@ -49,6 +51,10 @@ import org.checkerframework.checker.nullness.qual.Nullable; public final class EntityPacketRewriter1_20_5 extends EntityRewriter { + private static final UUID CREATIVE_BLOCK_INTERACTION_RANGE = UUID.fromString("736565d2-e1a7-403d-a3f8-1aeb3e302542"); + private static final UUID CREATIVE_ENTITY_INTERACTION_RANGE = UUID.fromString("98491ef6-97b1-4584-ae82-71a8cc85cf73"); + private static final int CREATIVE_MODE_ID = 1; + public EntityPacketRewriter1_20_5(final Protocol1_20_5To1_20_3 protocol) { super(protocol); } @@ -204,6 +210,12 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { + final byte gamemode = wrapper.get(Type.BYTE, 0); + if (gamemode == CREATIVE_MODE_ID) { + sendRangeAttributes(wrapper.user(), true); + } + }); } }); @@ -211,8 +223,13 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { @@ -229,13 +246,12 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { wrapper.passthrough(Type.VAR_INT); // Entity ID - final int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { // From a string to a registry int ID final String attributeIdentifier = wrapper.read(Type.STRING); - final int id = Attributes1_20_5.keyToId(attributeIdentifier); - wrapper.write(Type.VAR_INT, id != -1 ? id : 0); + final int mappedId = Attributes1_20_5.keyToId(attributeIdentifier); + wrapper.write(Type.VAR_INT, mappedId != -1 ? mappedId : 0); wrapper.passthrough(Type.DOUBLE); // Base final int modifierSize = wrapper.passthrough(Type.VAR_INT); @@ -246,6 +262,18 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { + // If the gamemode changed to/from creative, update the range attribute + final short event = wrapper.passthrough(Type.UNSIGNED_BYTE); + if (event != 3) { + return; + } + + // Resend attributes either with their original list or with the creative range modifier added + final float value = wrapper.passthrough(Type.FLOAT); + sendRangeAttributes(wrapper.user(), value == CREATIVE_MODE_ID); + }); } private void updateParticleFormat(final CompoundTag options, final String particleType) { @@ -299,6 +327,30 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter {