From 413abd5f66499802a8083a03db7f6a9f10386fa3 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 3 Mar 2023 17:31:35 +0100 Subject: [PATCH] Fix REntity Metadata --- .../src/de/steamwar/core/ChatWrapper19.java | 14 +++++++++++++ .../src/de/steamwar/core/ChatWrapper8.java | 19 ++++++++++++++++++ .../comphenix/tinyprotocol/Reflection.java | 10 ---------- .../src/de/steamwar/core/ChatWrapper.java | 6 ++++++ .../src/de/steamwar/entity/REntity.java | 20 ++----------------- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/SpigotCore_19/src/de/steamwar/core/ChatWrapper19.java b/SpigotCore_19/src/de/steamwar/core/ChatWrapper19.java index bef03b5..f4a03b0 100644 --- a/SpigotCore_19/src/de/steamwar/core/ChatWrapper19.java +++ b/SpigotCore_19/src/de/steamwar/core/ChatWrapper19.java @@ -21,6 +21,10 @@ package de.steamwar.core; import net.minecraft.network.chat.IChatMutableComponent; import net.minecraft.network.chat.contents.LiteralContents; +import net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata; +import net.minecraft.network.syncher.DataWatcher; + +import java.util.ArrayList; public class ChatWrapper19 implements ChatWrapper { @@ -28,4 +32,14 @@ public class ChatWrapper19 implements ChatWrapper { public Object stringToChatComponent(String text) { return IChatMutableComponent.a(new LiteralContents(text)); } + + @Override + public Object getDataWatcherPacket(int entityId, Object... dataWatcherKeyValues) { + ArrayList> nativeWatchers = new ArrayList<>(1); + for(int i = 0; i < dataWatcherKeyValues.length; i+=2) { + nativeWatchers.add((DataWatcher.b) BountifulWrapper.impl.getDataWatcherItem(dataWatcherKeyValues[i], dataWatcherKeyValues[i+1])); + } + + return new PacketPlayOutEntityMetadata(entityId, nativeWatchers); + } } diff --git a/SpigotCore_8/src/de/steamwar/core/ChatWrapper8.java b/SpigotCore_8/src/de/steamwar/core/ChatWrapper8.java index 68b8e07..ce3ba90 100644 --- a/SpigotCore_8/src/de/steamwar/core/ChatWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/ChatWrapper8.java @@ -21,6 +21,9 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; +import java.util.ArrayList; +import java.util.List; + public class ChatWrapper8 implements ChatWrapper { private static final Reflection.ConstructorInvoker chatComponentConstructor = Reflection.getConstructor(Reflection.getClass("{nms.network.chat}.ChatComponentText"), String.class); @@ -28,4 +31,20 @@ public class ChatWrapper8 implements ChatWrapper { public Object stringToChatComponent(String text) { return chatComponentConstructor.invoke(text); } + + private static final Reflection.FieldAccessor metadataEntity = Reflection.getField(metadataPacket, int.class, 0); + private static final Reflection.FieldAccessor metadataMetadata = Reflection.getField(metadataPacket, List.class, 0); + @Override + public Object getDataWatcherPacket(int entityId, Object... dataWatcherKeyValues) { + Object packet = Reflection.newInstance(metadataPacket); + metadataEntity.set(packet, entityId); + + ArrayList nativeWatchers = new ArrayList<>(1); + for(int i = 0; i < dataWatcherKeyValues.length; i+=2) { + nativeWatchers.add(BountifulWrapper.impl.getDataWatcherItem(dataWatcherKeyValues[i], dataWatcherKeyValues[i+1])); + } + metadataMetadata.set(packet, nativeWatchers); + + return packet; + } } diff --git a/SpigotCore_Main/src/com/comphenix/tinyprotocol/Reflection.java b/SpigotCore_Main/src/com/comphenix/tinyprotocol/Reflection.java index 882c62f..fc1e8c1 100644 --- a/SpigotCore_Main/src/com/comphenix/tinyprotocol/Reflection.java +++ b/SpigotCore_Main/src/com/comphenix/tinyprotocol/Reflection.java @@ -63,7 +63,6 @@ public final class Reflection { * @param value - the new value of the field. */ void set(Object target, Object value); - void setInt(Object target, int value); /** * Determine if the given object has this field. @@ -163,15 +162,6 @@ public final class Reflection { } } - @Override - public void setInt(Object target, int value) { - try { - field.setInt(target, value); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException("Cannot access reflection.", e); - } - } - @Override public boolean hasField(Object target) { // target instanceof DeclaringClass diff --git a/SpigotCore_Main/src/de/steamwar/core/ChatWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ChatWrapper.java index 2f4df8f..b28f6e9 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ChatWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ChatWrapper.java @@ -19,8 +19,14 @@ package de.steamwar.core; +import com.comphenix.tinyprotocol.Reflection; + public interface ChatWrapper { ChatWrapper impl = VersionDependent.getVersionImpl(Core.getInstance()); + Class metadataPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityMetadata"); + Object stringToChatComponent(String text); + + Object getDataWatcherPacket(int entityId, Object... dataWatcherKeyValues); } diff --git a/SpigotCore_Main/src/de/steamwar/entity/REntity.java b/SpigotCore_Main/src/de/steamwar/entity/REntity.java index f087e27..9ffbdc2 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/REntity.java +++ b/SpigotCore_Main/src/de/steamwar/entity/REntity.java @@ -20,10 +20,7 @@ package de.steamwar.entity; import com.comphenix.tinyprotocol.Reflection; -import de.steamwar.core.BountifulWrapper; -import de.steamwar.core.Core; -import de.steamwar.core.FlatteningWrapper; -import de.steamwar.core.ProtocolWrapper; +import de.steamwar.core.*; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; import lombok.Getter; @@ -349,21 +346,8 @@ public class REntity { return status; } - private static final Class metadataPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityMetadata"); - private static final Reflection.FieldAccessor metadataEntity = Reflection.getField(metadataPacket, int.class, 0); - private static final Reflection.FieldAccessor metadataMetadata = Reflection.getField(metadataPacket, List.class, 0); - protected Object getDataWatcherPacket(Object... dataWatcherKeyValues) { - Object packet = Reflection.newInstance(metadataPacket); - metadataEntity.setInt(packet, entityId); - - ArrayList nativeWatchers = new ArrayList<>(1); - for(int i = 0; i < dataWatcherKeyValues.length; i+=2) { - nativeWatchers.add(BountifulWrapper.impl.getDataWatcherItem(dataWatcherKeyValues[i], dataWatcherKeyValues[i+1])); - } - metadataMetadata.set(packet, nativeWatchers); - - return packet; + return ChatWrapper.impl.getDataWatcherPacket(entityId, dataWatcherKeyValues); } private static final Class teleportPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport");