From d68705d8fe94ec413c26126aa393318d6ff656c0 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Wed, 13 Dec 2023 19:16:30 +0800 Subject: [PATCH] More meta and watcher --- .../server/renderer/ServerRenderer.java | 7 +- .../renderer/network/ProtocolHandler.java | 15 +--- .../renderer/network/RenderRegistry.java | 6 +- .../renderer/network/TestPacketListener.java | 2 +- .../network/datawatcher/ValueIndex.java | 7 +- .../network/datawatcher/WatcherIndex.java | 51 +++++++++++ .../datawatcher/values/AbstractValues.java | 24 ++++- .../datawatcher/values/AllayValues.java | 14 +++ .../datawatcher/values/ArmorStandValues.java | 24 +++++ .../datawatcher/values/EntityValues.java | 18 ++-- .../values/LivingEntityValues.java | 19 ++-- .../network/datawatcher/values/MobValues.java | 13 +++ .../datawatcher/values/PlayerValues.java | 14 +-- .../datawatcher/watchers/SingleWatcher.java | 2 +- .../datawatcher/watchers/Watchers.java | 17 ---- .../watchers/types/AllayWatcher.java | 22 +++++ .../watchers/types/ArmorStandWatcher.java | 21 +++++ .../watchers/types/LivingEntityWatcher.java | 1 - .../watchers/types/PlayerWatcher.java | 4 +- .../network/listeners/IProtocolListener.java | 9 -- .../network/listeners/MetaPacketListener.java | 87 +++++++++++++++++++ .../network/listeners/ProtocolListener.java | 69 +++++++++++++++ .../network/listeners/SpawnPacketHandler.java | 60 +++---------- 23 files changed, 371 insertions(+), 135 deletions(-) create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/WatcherIndex.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AllayValues.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/ArmorStandValues.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/MobValues.java delete mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/Watchers.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/AllayWatcher.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/ArmorStandWatcher.java delete mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/IProtocolListener.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/MetaPacketListener.java create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/ProtocolListener.java diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/ServerRenderer.java b/src/main/java/xiamomc/morph/backends/server/renderer/ServerRenderer.java index 3272a681..05b4eaac 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/ServerRenderer.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/ServerRenderer.java @@ -2,15 +2,12 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; -import xiamomc.morph.MorphPlugin; import xiamomc.morph.MorphPluginObject; import xiamomc.morph.backends.server.renderer.network.RegistryParameters; import xiamomc.morph.backends.server.renderer.network.RenderRegistry; -import xiamomc.morph.backends.server.renderer.network.listeners.SpawnPacketHandler; import xiamomc.morph.backends.server.renderer.network.ProtocolHandler; -import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers; +import xiamomc.morph.backends.server.renderer.network.datawatcher.WatcherIndex; import xiamomc.morph.backends.server.renderer.skins.SkinStore; -import xiamomc.pluginbase.Managers.DependencyManager; public class ServerRenderer extends MorphPluginObject { @@ -30,7 +27,7 @@ public ServerRenderer() public void renderEntity(Player player, EntityType entityType, String name) { registry.register(player.getUniqueId(), - new RegistryParameters(entityType, name, Watchers.getWatcherForType(player, entityType))); + new RegistryParameters(entityType, name, WatcherIndex.getInstance().getWatcherForType(player, entityType))); } public void unRenderEntity(Player player) diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/ProtocolHandler.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/ProtocolHandler.java index 123f4184..4f507d0a 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/ProtocolHandler.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/ProtocolHandler.java @@ -1,21 +1,10 @@ package xiamomc.morph.backends.server.renderer.network; import com.comphenix.protocol.ProtocolLibrary; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import org.bukkit.Bukkit; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import xiamomc.morph.MorphPluginObject; -import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher; -import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers; +import xiamomc.morph.backends.server.renderer.network.listeners.MetaPacketListener; import xiamomc.morph.backends.server.renderer.network.listeners.SpawnPacketHandler; import xiamomc.pluginbase.Annotations.Initializer; -import xiamomc.pluginbase.Exceptions.NullDependencyException; - -import java.util.Map; -import java.util.UUID; public class ProtocolHandler extends MorphPluginObject { @@ -34,6 +23,8 @@ private void load() var protocolMgr = ProtocolLibrary.getProtocolManager(); protocolMgr.addPacketListener(morphPacketListener); + protocolMgr.addPacketListener(new MetaPacketListener()); + //protocolMgr.addPacketListener(new TestPacketListener()); } private boolean disposed; diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/RenderRegistry.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/RenderRegistry.java index aab0fe41..c8f19e8c 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/RenderRegistry.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/RenderRegistry.java @@ -1,7 +1,6 @@ package xiamomc.morph.backends.server.renderer.network; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import net.minecraft.world.entity.PlayerRideable; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -9,11 +8,10 @@ import org.jetbrains.annotations.Nullable; import xiamomc.morph.MorphPluginObject; import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher; -import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers; +import xiamomc.morph.backends.server.renderer.network.datawatcher.WatcherIndex; import xiamomc.pluginbase.Exceptions.NullDependencyException; import java.util.Map; -import java.util.Objects; import java.util.UUID; import java.util.function.Consumer; @@ -89,7 +87,7 @@ public void unregister(UUID uuid) */ public void register(@NotNull Player player, @NotNull org.bukkit.entity.EntityType bukkitType) { - var watcher = Watchers.getWatcherForType(player, bukkitType); + var watcher = WatcherIndex.getInstance().getWatcherForType(player, bukkitType); register(player.getUniqueId(), bukkitType, watcher); } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/TestPacketListener.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/TestPacketListener.java index f38168f4..7e8866ec 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/TestPacketListener.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/TestPacketListener.java @@ -33,7 +33,7 @@ private void load() @Override public void onPacketSending(PacketEvent event) { - logger.info("SEND! " + event.getPacket().getHandle()); + logger.info("SEND! type is '%s' handle is '%s'".formatted(event.getPacketType(), event.getPacket().getHandle())); } /** diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/ValueIndex.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/ValueIndex.java index f2e3c2a6..bd14c6d9 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/ValueIndex.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/ValueIndex.java @@ -1,12 +1,11 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher; -import xiamomc.morph.backends.server.renderer.network.datawatcher.values.AbstractValues; -import xiamomc.morph.backends.server.renderer.network.datawatcher.values.EntityValues; -import xiamomc.morph.backends.server.renderer.network.datawatcher.values.LivingEntityValues; -import xiamomc.morph.backends.server.renderer.network.datawatcher.values.PlayerValues; +import xiamomc.morph.backends.server.renderer.network.datawatcher.values.*; public class ValueIndex { + public static final ArmorStandValues ARMOR_STAND = new ArmorStandValues(); + public static final AllayValues ALLAY = new AllayValues(); public static final PlayerValues PLAYER = new PlayerValues(); public static final LivingEntityValues BASE_LIVING = new LivingEntityValues(); public static final EntityValues BASE_ENTITY = new EntityValues(); diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/WatcherIndex.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/WatcherIndex.java new file mode 100644 index 00000000..19759463 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/WatcherIndex.java @@ -0,0 +1,51 @@ +package xiamomc.morph.backends.server.renderer.network.datawatcher; + +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import xiamomc.morph.backends.server.renderer.network.datawatcher.values.ArmorStandValues; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.AllayWatcher; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.ArmorStandWatcher; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.LivingEntityWatcher; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.PlayerWatcher; + +import java.util.Map; +import java.util.function.Function; + +public class WatcherIndex +{ + public static WatcherIndex getInstance() + { + if (instance == null) instance = new WatcherIndex(); + return instance; + } + + private static WatcherIndex instance; + + public WatcherIndex() + { + instance = this; + + setTypeWatcher(EntityType.PLAYER, PlayerWatcher::new); + setTypeWatcher(EntityType.ALLAY, AllayWatcher::new); + setTypeWatcher(EntityType.ARMOR_STAND, ArmorStandWatcher::new); + } + + private void setTypeWatcher(EntityType type, Function func) + { + typeWatcherMap.put(type, func); + } + + private final Map> typeWatcherMap = new Object2ObjectOpenHashMap<>(); + + public SingleWatcher getWatcherForType(Player bindingPlayer, EntityType entityType) + { + var watcherFunc = typeWatcherMap.getOrDefault(entityType, null); + + if (watcherFunc == null) + return new LivingEntityWatcher(bindingPlayer, entityType); + + return watcherFunc.apply(bindingPlayer); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AbstractValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AbstractValues.java index 18d277bf..04d538e0 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AbstractValues.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AbstractValues.java @@ -1,8 +1,7 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher.values; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import net.minecraft.core.BlockPos; +import org.slf4j.Logger; import xiamomc.morph.MorphPlugin; import xiamomc.morph.backends.server.renderer.utilties.ProtocolRegistryUtils; @@ -10,9 +9,26 @@ public abstract class AbstractValues { + private int currentIndex = 0; + protected SingleValue getSingle(X val) + { + if (val == null) + throw new IllegalArgumentException("May not pass a null value to getIndex()"); + + var sv = SingleValue.of(currentIndex, val); + + logger.info(this.getClass().getSimpleName() + " :: Value '%s' class '%s' is on Index '%s'" + .formatted(val, val.getClass(), currentIndex)); + + currentIndex++; + return sv; + } + + protected final Logger logger = MorphPlugin.getInstance().getSLF4JLogger(); + protected final List> values = new ObjectArrayList<>(); - protected void registerValue(SingleValue... value) + protected void registerSingle(SingleValue... value) { for (SingleValue singleValue : value) registerSingle(singleValue); @@ -30,7 +46,7 @@ protected void registerSingle(SingleValue value) } catch (Throwable t) { - MorphPlugin.getInstance().getSLF4JLogger().warn("No serializer for type '%s'!".formatted(value.type())); + logger.warn("No serializer for type '%s'!".formatted(value.type())); } values.add(value); diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AllayValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AllayValues.java new file mode 100644 index 00000000..8d49dc82 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/AllayValues.java @@ -0,0 +1,14 @@ +package xiamomc.morph.backends.server.renderer.network.datawatcher.values; + +public class AllayValues extends MobValues +{ + public final SingleValue DANCING = getSingle(false); + public final SingleValue CAN_DUPLICATE = getSingle(false); + + public AllayValues() + { + super(); + + this.registerSingle(DANCING); + } +} \ No newline at end of file diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/ArmorStandValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/ArmorStandValues.java new file mode 100644 index 00000000..6957f997 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/ArmorStandValues.java @@ -0,0 +1,24 @@ +package xiamomc.morph.backends.server.renderer.network.datawatcher.values; + +import net.minecraft.core.Rotations; + +public class ArmorStandValues extends LivingEntityValues +{ + public final SingleValue DATA_FLAGS = getSingle((byte)0); + public final SingleValue HEAD_ROTATION = getSingle(new Rotations(0, 0, 0)); + public final SingleValue BODY_ROTATION = getSingle(new Rotations(0, 0, 0)); + public final SingleValue LEFT_ARM_ROTATION = getSingle(new Rotations(-10, 0, -10)); + public final SingleValue RIGHT_ARM_ROTATION = getSingle(new Rotations(-15, 0, 10)); + public final SingleValue LEFT_LEG_ROTATION = getSingle(new Rotations(-1, 0, -1)); + public final SingleValue RIGHT_LEG_ROTATION = getSingle(new Rotations(1, 0, 1)); + + public ArmorStandValues() + { + super(); + + registerSingle(DATA_FLAGS, + HEAD_ROTATION, BODY_ROTATION, + LEFT_ARM_ROTATION, RIGHT_ARM_ROTATION, + LEFT_LEG_ROTATION, RIGHT_LEG_ROTATION); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/EntityValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/EntityValues.java index 07a01fa6..5a88091a 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/EntityValues.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/EntityValues.java @@ -7,21 +7,21 @@ public class EntityValues extends AbstractValues { - public final SingleValue GENERAL = SingleValue.of(0, (byte)0); - public final SingleValue AIR_TICKS = SingleValue.of(1, 0); + public final SingleValue GENERAL = getSingle((byte)0); + public final SingleValue AIR_TICKS = getSingle(0); @Deprecated - public final SingleValue> CUSTOMNAME = SingleValue.of(2, Optional.of(Component.empty())); + public final SingleValue> CUSTOMNAME = getSingle(Optional.of(Component.empty())); - public final SingleValue CUSTOMNAME_VISIBLE = SingleValue.of(3, false); - public final SingleValue SILENT = SingleValue.of(4, false); - public final SingleValue NO_GRAVITY = SingleValue.of(5, false); - public final SingleValue POSE = SingleValue.of(6, Pose.STANDING); - public final SingleValue FROZEN_TICKS = SingleValue.of(7, 0); + public final SingleValue CUSTOMNAME_VISIBLE = getSingle(false); + public final SingleValue SILENT = getSingle(false); + public final SingleValue NO_GRAVITY = getSingle(false); + public final SingleValue POSE = getSingle(Pose.STANDING); + public final SingleValue FROZEN_TICKS = getSingle(0); public EntityValues() { - registerValue(GENERAL, AIR_TICKS, CUSTOMNAME, CUSTOMNAME_VISIBLE, SILENT, NO_GRAVITY, + registerSingle(GENERAL, AIR_TICKS, CUSTOMNAME, CUSTOMNAME_VISIBLE, SILENT, NO_GRAVITY, POSE, FROZEN_TICKS); } } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/LivingEntityValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/LivingEntityValues.java index 78b2ad54..2c75ed61 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/LivingEntityValues.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/LivingEntityValues.java @@ -1,24 +1,23 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher.values; import net.minecraft.core.BlockPos; -import org.bukkit.Location; import java.util.Optional; -public class LivingEntityValues extends AbstractValues +public class LivingEntityValues extends EntityValues { - public final SingleValue LIVING_FLAGS = SingleValue.of(8, (byte)0); - public final SingleValue HEALTH = SingleValue.of(9, 1f); - public final SingleValue POTION_COLOR = SingleValue.of(10, 0); - public final SingleValue POTION_ISAMBIENT = SingleValue.of(11, false); - public final SingleValue STUCKED_ARROWS = SingleValue.of(12, 0); - public final SingleValue BEE_STINGERS = SingleValue.of(13, 0); - public final SingleValue> BED_POS = SingleValue.of(14, Optional.of(new BlockPos(0,0,0))); + public final SingleValue LIVING_FLAGS = getSingle((byte)0); + public final SingleValue HEALTH = getSingle(1f); + public final SingleValue POTION_COLOR = getSingle(0); + public final SingleValue POTION_ISAMBIENT = getSingle(false); + public final SingleValue STUCKED_ARROWS = getSingle(0); + public final SingleValue BEE_STINGERS = getSingle(0); + public final SingleValue> BED_POS = getSingle(Optional.of(new BlockPos(0,0,0))); public LivingEntityValues() { super(); - registerValue(LIVING_FLAGS, HEALTH, POTION_COLOR, POTION_ISAMBIENT, STUCKED_ARROWS, BED_POS, BEE_STINGERS); + registerSingle(LIVING_FLAGS, HEALTH, POTION_COLOR, POTION_ISAMBIENT, STUCKED_ARROWS, BED_POS, BEE_STINGERS); } } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/MobValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/MobValues.java new file mode 100644 index 00000000..73c73ada --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/MobValues.java @@ -0,0 +1,13 @@ +package xiamomc.morph.backends.server.renderer.network.datawatcher.values; + +public class MobValues extends LivingEntityValues +{ + public final SingleValue MOB_FLAGS = getSingle((byte)0); + + public MobValues() + { + super(); + + this.registerSingle(MOB_FLAGS); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/PlayerValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/PlayerValues.java index 4729b70d..2cf25699 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/PlayerValues.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/PlayerValues.java @@ -1,16 +1,18 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher.values; -public class PlayerValues extends AbstractValues +public class PlayerValues extends LivingEntityValues { - public final SingleValue ABSORPTION_AMOUNT = SingleValue.of(15, 0f); - public final SingleValue SCORE = SingleValue.of(16, 0); - public final SingleValue SKIN = SingleValue.of(17, (byte)127); - public final SingleValue MAINHAND = SingleValue.of(18, (byte)1); + public final SingleValue ABSORPTION_AMOUNT = getSingle(0f); + public final SingleValue SCORE = getSingle(0); + public final SingleValue SKIN = getSingle((byte)0); //127 + public final SingleValue MAINHAND = getSingle((byte)1); public PlayerValues() { super(); - registerValue(ABSORPTION_AMOUNT, SCORE, SKIN, MAINHAND); + System.out.println("SKIN is on index " + SKIN.index()); + + registerSingle(ABSORPTION_AMOUNT, SCORE, SKIN, MAINHAND); } } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/SingleWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/SingleWatcher.java index 94a48a25..81cbf0bf 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/SingleWatcher.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/SingleWatcher.java @@ -79,7 +79,7 @@ protected boolean register(SingleValue singleValue) return true; } - public void write(SingleValue singleValue, Object value) + public void write(SingleValue singleValue, X value) { write(singleValue.index(), value); } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/Watchers.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/Watchers.java deleted file mode 100644 index 6dc86afe..00000000 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/Watchers.java +++ /dev/null @@ -1,17 +0,0 @@ -package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers; - -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.LivingEntityWatcher; -import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.PlayerWatcher; - -public class Watchers -{ - public static SingleWatcher getWatcherForType(Player bindingPlayer, EntityType entityType) - { - if (entityType == EntityType.PLAYER) - return new PlayerWatcher(bindingPlayer, entityType); - else - return new LivingEntityWatcher(bindingPlayer, entityType); - } -} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/AllayWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/AllayWatcher.java new file mode 100644 index 00000000..7b3760d2 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/AllayWatcher.java @@ -0,0 +1,22 @@ +package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types; + +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex; + +public class AllayWatcher extends LivingEntityWatcher +{ + public AllayWatcher(Player bindingPlayer) + { + super(bindingPlayer, EntityType.ALLAY); + + register(ValueIndex.ALLAY); + } + + @Override + public void doSync() + { + write(ValueIndex.ALLAY.DANCING, true); + super.doSync(); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/ArmorStandWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/ArmorStandWatcher.java new file mode 100644 index 00000000..cd76d01d --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/ArmorStandWatcher.java @@ -0,0 +1,21 @@ +package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types; + +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex; + +public class ArmorStandWatcher extends LivingEntityWatcher +{ + public ArmorStandWatcher(Player bindingPlayer) + { + super(bindingPlayer, EntityType.ARMOR_STAND); + + register(ValueIndex.ARMOR_STAND); + } + + @Override + public void doSync() + { + super.doSync(); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/LivingEntityWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/LivingEntityWatcher.java index 2c6b0d24..584767bb 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/LivingEntityWatcher.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/LivingEntityWatcher.java @@ -18,7 +18,6 @@ public LivingEntityWatcher(Player bindingPlayer, EntityType entityType) public void doSync() { var player = getBindingPlayer(); - //var nmsPlayer = NmsRecord.ofPlayer(player); var values = ValueIndex.BASE_LIVING; write(values.HEALTH, (float)player.getHealth()); diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PlayerWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PlayerWatcher.java index f5d38f76..188c5b9b 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PlayerWatcher.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PlayerWatcher.java @@ -11,9 +11,9 @@ public class PlayerWatcher extends LivingEntityWatcher { - public PlayerWatcher(Player bindingPlayer, EntityType entityType) + public PlayerWatcher(Player bindingPlayer) { - super(bindingPlayer, entityType); + super(bindingPlayer, EntityType.PLAYER); register(ValueIndex.PLAYER); } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/IProtocolListener.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/IProtocolListener.java deleted file mode 100644 index b4d159da..00000000 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/IProtocolListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package xiamomc.morph.backends.server.renderer.network.listeners; - -import xiamomc.morph.backends.server.renderer.network.RegistryParameters; - -import java.util.UUID; - -public interface IProtocolListener -{ -} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/MetaPacketListener.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/MetaPacketListener.java new file mode 100644 index 00000000..3768faf0 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/MetaPacketListener.java @@ -0,0 +1,87 @@ +package xiamomc.morph.backends.server.renderer.network.listeners; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.ListeningWhitelist; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.events.PacketListener; +import com.comphenix.protocol.injector.GamePhase; +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; +import org.bukkit.entity.Player; +import xiamomc.morph.MorphPlugin; +import xiamomc.morph.backends.server.renderer.network.RenderRegistry; +import xiamomc.morph.utilities.NmsUtils; +import xiamomc.pluginbase.Annotations.Resolved; + +public class MetaPacketListener extends ProtocolListener implements PacketListener +{ + @Resolved(shouldSolveImmediately = true) + private RenderRegistry registry; + + @Override + public void onPacketSending(PacketEvent event) + { + if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) + { + var packet = event.getPacket(); + if (packet.getMeta("fm").isPresent()) + { + packet.removeMeta("fm"); + return; + } + + onMetaPacket((ClientboundSetEntityDataPacket) event.getPacket().getHandle(), event); + } + } + + private void onMetaPacket(ClientboundSetEntityDataPacket packet, PacketEvent packetEvent) + { + //获取此包的来源实体 + var sourceNmsEntity = NmsUtils.getNmsLevel(packetEvent.getPlayer().getWorld()).getEntity(packet.id()); + if (!(sourceNmsEntity.getBukkitEntity() instanceof Player sourcePlayer)) return; + + var bindingParameters = registry.getParameters(sourcePlayer.getUniqueId()); + + if (bindingParameters == null) + return; + + //然后获取此包要发送的目标玩家 + var targetPlayer = packetEvent.getPlayer(); + + //只拦截其他人的Meta + if (targetPlayer == sourcePlayer) + return; + else + packetEvent.setCancelled(true); + + //取得来源玩家的伪装后的Meta,发送给目标玩家 + var meta = getMetaPackets(sourcePlayer, bindingParameters.singleWatcher()); + protocolManager().sendServerPacket(targetPlayer, meta); + } + + @Override + public void onPacketReceiving(PacketEvent event) + { + } + + @Override + public ListeningWhitelist getSendingWhitelist() + { + return ListeningWhitelist + .newBuilder() + .types(PacketType.Play.Server.ENTITY_METADATA) + .gamePhase(GamePhase.PLAYING) + .build(); + } + + @Override + public ListeningWhitelist getReceivingWhitelist() + { + return ListeningWhitelist.EMPTY_WHITELIST; + } + + @Override + public org.bukkit.plugin.Plugin getPlugin() + { + return MorphPlugin.getInstance(); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/ProtocolListener.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/ProtocolListener.java new file mode 100644 index 00000000..65616368 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/ProtocolListener.java @@ -0,0 +1,69 @@ +package xiamomc.morph.backends.server.renderer.network.listeners; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.wrappers.WrappedDataValue; +import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.bukkit.entity.Player; +import xiamomc.morph.MorphPluginObject; +import xiamomc.morph.backends.server.renderer.network.RegistryParameters; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher; +import xiamomc.morph.backends.server.renderer.utilties.ProtocolRegistryUtils; + +import java.util.List; +import java.util.UUID; + +public abstract class ProtocolListener extends MorphPluginObject +{ + protected PacketContainer getMetaPackets(Player player, SingleWatcher watcher) + { + var metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); + metaPacket.getIntegers().write(0, player.getEntityId()); + + var modifier = metaPacket.getDataValueCollectionModifier(); + + var entityWatcher = WrappedDataWatcher.getEntityWatcher(player); + entityWatcher.asMap().forEach((id, val) -> + { + //logger.info("Id '%s' is val '%s' raw '%s' class '%s'" + // .formatted(id, val.getValue(),val.getRawValue(),val.getRawValue().getClass())); + }); + + List wrappedDataValues = new ObjectArrayList<>(); + + watcher.doSync(); + watcher.getRegistry().forEach((single, v) -> + { + if (single.defaultValue().equals(v)) return; + + WrappedDataWatcher.Serializer serializer; + + try + { + serializer = ProtocolRegistryUtils.getSerializer(single.defaultValue()); + } + catch (Throwable t) + { + logger.warn("Error occurred while generating meta packet with id '%s': %s".formatted(single.index(), t.getMessage())); + return; + } + + var value = new WrappedDataValue(single.index(), serializer, v); + wrappedDataValues.add(value); + //logger.info("Writing value '%s' index '%s'".formatted(v, single.index())); + }); + + modifier.write(0, wrappedDataValues); + metaPacket.setMeta("fm", true); + + return metaPacket; + } + + protected ProtocolManager protocolManager() + { + return ProtocolLibrary.getProtocolManager(); + } +} diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/SpawnPacketHandler.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/SpawnPacketHandler.java index 6f91c4ee..bde8a6fe 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/SpawnPacketHandler.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/listeners/SpawnPacketHandler.java @@ -2,6 +2,7 @@ import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.ListeningWhitelist; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; @@ -13,10 +14,8 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.minecraft.Util; import net.minecraft.network.chat.Component; -import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; -import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket; -import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; -import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; +import net.minecraft.network.protocol.game.*; +import net.minecraft.server.packs.repository.Pack; import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.GameType; import org.bukkit.Bukkit; @@ -37,12 +36,14 @@ import xiamomc.morph.misc.MorphGameProfile; import xiamomc.morph.misc.NmsRecord; import xiamomc.morph.utilities.EntityTypeUtils; +import xiamomc.morph.utilities.NmsUtils; +import xiamomc.pluginbase.Annotations.Initializer; import xiamomc.pluginbase.Annotations.Resolved; import xiamomc.pluginbase.Exceptions.NullDependencyException; import java.util.*; -public class SpawnPacketHandler extends MorphPluginObject implements PacketListener, IProtocolListener +public class SpawnPacketHandler extends ProtocolListener implements PacketListener { @Resolved(shouldSolveImmediately = true) private RenderRegistry registry; @@ -103,7 +104,7 @@ private void unDisguiseForPlayer(@Nullable Player player) ProtocolEquipment.toPairs(player.getEquipment())); packets.add(PacketContainer.fromPacket(equipmentPacket)); - var meta = getMetaPackets(player, new PlayerWatcher(player, org.bukkit.entity.EntityType.PLAYER)); + var meta = getMetaPackets(player, new PlayerWatcher(player)); packets.add(meta); affectedPlayers.forEach(p -> @@ -146,7 +147,7 @@ private List buildSpawnPackets(Player player, DisplayParameters { List packets = new ObjectArrayList<>(); - logger.info("Build spawn packets, player is " + player.getName() + " :: parameters are " + parameters); + //logger.info("Build spawn packets, player is " + player.getName() + " :: parameters are " + parameters); var playerType = parameters.bukkitType(); var nmsType = EntityTypeUtils.getNmsType(playerType); @@ -164,7 +165,7 @@ private List buildSpawnPackets(Player player, DisplayParameters //如果是玩家 if (playerType == org.bukkit.entity.EntityType.PLAYER) { - logger.info("Building player info packet!"); + //logger.info("Building player info packet!"); Objects.requireNonNull(parameters.gameProfile(), "Null game profile!"); var gameProfile = new MorphGameProfile(parameters.gameProfile()); @@ -210,48 +211,6 @@ private List buildSpawnPackets(Player player, DisplayParameters return packets; } - private PacketContainer getMetaPackets(Player player, SingleWatcher watcher) - { - var metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); - metaPacket.getIntegers().write(0, player.getEntityId()); - - var modifier = metaPacket.getDataValueCollectionModifier(); - - var entityWatcher = WrappedDataWatcher.getEntityWatcher(player); - entityWatcher.asMap().forEach((id, val) -> - { - //logger.info("Id '%s' is val '%s' raw '%s' class '%s'" - // .formatted(id, val.getValue(),val.getRawValue(),val.getRawValue().getClass())); - }); - - List wrappedDataValues = new ObjectArrayList<>(); - - watcher.doSync(); - watcher.getRegistry().forEach((single, v) -> - { - if (single.defaultValue().equals(v)) return; - - WrappedDataWatcher.Serializer serializer; - - try - { - serializer = ProtocolRegistryUtils.getSerializer(single.defaultValue()); - } - catch (Throwable t) - { - logger.warn("Error occurred while generating meta packet with id '%s': %s".formatted(single.index(), t.getMessage())); - return; - } - - var value = new WrappedDataValue(single.index(), serializer, v); - wrappedDataValues.add(value); - }); - - modifier.write(0, wrappedDataValues); - - return metaPacket; - } - private void refreshStateForPlayer(@Nullable Player player) { if (player == null) return; @@ -394,6 +353,7 @@ public ListeningWhitelist getSendingWhitelist() { return ListeningWhitelist .newBuilder() + .types(PacketType.Play.Server.ENTITY_METADATA) .types(PacketType.Play.Server.SPAWN_ENTITY) .gamePhase(GamePhase.PLAYING) .build();