From 17c0995f240fb8dfb9fb8131a4a3fb2dcab55fb4 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 11 Apr 2021 18:23:57 +0200 Subject: [PATCH 1/3] Add some Sneaking Methods --- .../spectatesystem/elements/REntity.java | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index ff5dedc..c489ab4 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -19,13 +19,21 @@ package de.steamwar.spectatesystem.elements; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import com.mojang.authlib.GameProfile; import de.steamwar.spectatesystem.SpectateSystem; import net.minecraft.server.v1_15_R1.*; import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.metadata.EntityMetadataStore; import org.bukkit.entity.Player; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -48,6 +56,7 @@ public abstract class REntity { for(Player player : Bukkit.getOnlinePlayers()){ sendToPlayer(player); } + sneak(false); } public static void playerJoins(Player player){ @@ -103,17 +112,33 @@ public abstract class REntity { } } - public void sneak(boolean sneaking) { - entity.setSneaking(sneaking); + private static Method setPoseMethod = null; - /*DataWatcher dataWatcher = new DataWatcher(entity); - System.out.println("Let " + entity.getName() + " sneak " + sneaking); - dataWatcher.register(new DataWatcherObject<>(0, DataWatcherRegistry.a), sneaking ? (byte)0x02 : (byte)0x40); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); + public void sneak(boolean sneaking) { + try { + if(setPoseMethod == null) { + setPoseMethod = Entity.class.getDeclaredMethod("setPose", EntityPose.class); + setPoseMethod.setAccessible(true); + } + setPoseMethod.invoke(entity, sneaking?EntityPose.CROUCHING:EntityPose.STANDING); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + /*System.out.println("Let " + entity.getName() + " sneak " + sneaking+ " | " + entity.getClass()); + DataWatcher dataWatcher = entity.getDataWatcher(); + + dataWatcher.set(new DataWatcherObject<>(6, DataWatcherRegistry.s), sneaking?EntityPose.CROUCHING:EntityPose.STANDING); + //dataWatcher.set(new DataWatcherObject<>(0, DataWatcherRegistry.a), Byte.valueOf((byte) (sneaking?2:0)));*/ + + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), entity.getDataWatcher(), false); for(Player player : Bukkit.getOnlinePlayers()){ - ((CraftPlayer)player).getHandle().playerConnection.sendPacket(packet); - }*/ + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } } public void animation(byte animation) { -- 2.39.5 From 7c35ac8beac338c6b6e76d91c18e3cb5fe8f2055 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 11 Apr 2021 20:14:51 +0200 Subject: [PATCH 2/3] Sneaking is Working! --- .../spectatesystem/elements/REntity.java | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index c489ab4..a9218e9 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -32,6 +32,7 @@ import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_15_R1.metadata.EntityMetadataStore; import org.bukkit.entity.Player; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -112,29 +113,14 @@ public abstract class REntity { } } - private static Method setPoseMethod = null; - public void sneak(boolean sneaking) { - try { - if(setPoseMethod == null) { - setPoseMethod = Entity.class.getDeclaredMethod("setPose", EntityPose.class); - setPoseMethod.setAccessible(true); - } - setPoseMethod.invoke(entity, sneaking?EntityPose.CROUCHING:EntityPose.STANDING); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - /*System.out.println("Let " + entity.getName() + " sneak " + sneaking+ " | " + entity.getClass()); - DataWatcher dataWatcher = entity.getDataWatcher(); + DataWatcher dataWatcher = new DataWatcher(null); - dataWatcher.set(new DataWatcherObject<>(6, DataWatcherRegistry.s), sneaking?EntityPose.CROUCHING:EntityPose.STANDING); - //dataWatcher.set(new DataWatcherObject<>(0, DataWatcherRegistry.a), Byte.valueOf((byte) (sneaking?2:0)));*/ + DataWatcherObject dataWatcherObject = new DataWatcherObject<>(6, DataWatcherRegistry.s); + dataWatcher.register(dataWatcherObject, sneaking?EntityPose.CROUCHING:EntityPose.STANDING); + dataWatcher.markDirty(dataWatcherObject); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), entity.getDataWatcher(), false); + PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); for(Player player : Bukkit.getOnlinePlayers()){ ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); -- 2.39.5 From 155875a43d14c4d70034dd3d94f4a54549d7cfe1 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 11 Apr 2021 20:17:02 +0200 Subject: [PATCH 3/3] Optimize Imports --- src/de/steamwar/spectatesystem/elements/REntity.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/de/steamwar/spectatesystem/elements/REntity.java b/src/de/steamwar/spectatesystem/elements/REntity.java index a9218e9..5356aa6 100644 --- a/src/de/steamwar/spectatesystem/elements/REntity.java +++ b/src/de/steamwar/spectatesystem/elements/REntity.java @@ -19,22 +19,13 @@ package de.steamwar.spectatesystem.elements; -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; -import com.mojang.authlib.GameProfile; import de.steamwar.spectatesystem.SpectateSystem; import net.minecraft.server.v1_15_R1.*; import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; -import org.bukkit.craftbukkit.v1_15_R1.metadata.EntityMetadataStore; import org.bukkit.entity.Player; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -- 2.39.5