From c018cb0eed2c75eb32f1e3664f6dd0299b552a71 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Sun, 17 Dec 2023 13:55:05 +0800 Subject: [PATCH] Living flags; Don't put values that doesn't change to dirtySingles --- .../datawatcher/watchers/SingleWatcher.java | 4 +- .../watchers/types/LivingEntityWatcher.java | 80 ++++++++++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) 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 25ab2c73..e3b76c26 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 @@ -176,7 +176,9 @@ public void write(int index, Object value) var prev = registry.getOrDefault(single, null); registry.put(single, value); - dirtySingles.put(single, value); + + if (!value.equals(prev)) + dirtySingles.put(single, value); onTrackerWrite(index, prev, value); 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 ea390c1e..57440754 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 @@ -1,12 +1,35 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types; +import io.papermc.paper.event.player.PlayerStopUsingItemEvent; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.bukkit.Bukkit; +import org.bukkit.Color; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerItemConsumeEvent; +import org.bukkit.event.player.PlayerItemHeldEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.MainHand; +import org.bukkit.potion.PotionEffect; import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex; import xiamomc.morph.misc.NmsRecord; -public class LivingEntityWatcher extends EntityWatcher +import java.util.List; +import java.util.Map; + +public class LivingEntityWatcher extends EntityWatcher implements Listener { + public LivingEntityWatcher(Player bindingPlayer, EntityType entityType) + { + super(bindingPlayer, entityType); + + Bukkit.getPluginManager().registerEvents(this, plugin); + } + @Override protected void initRegistry() { @@ -15,19 +38,70 @@ protected void initRegistry() register(ValueIndex.BASE_LIVING); } - public LivingEntityWatcher(Player bindingPlayer, EntityType entityType) + private final Map handMap = new Object2ObjectOpenHashMap<>(); + + @EventHandler + public void onPlayerStartUsingItem(PlayerInteractEvent e) { - super(bindingPlayer, entityType); + handMap.put(e.getPlayer(), e.getHand()); } @Override protected void doSync() { var player = getBindingPlayer(); + var nmsPlayer = NmsRecord.ofPlayer(player); var values = ValueIndex.BASE_LIVING; write(values.HEALTH, (float)player.getHealth()); + var flagBit = 0x00; + + if (nmsPlayer.isUsingItem()) + { + flagBit |= 0x01; + + var handInUse = handMap.remove(player); + + if (handInUse == null) + { + logger.warn("No hand in use but using item? Defaulting to HAND"); + handInUse = EquipmentSlot.HAND; + } + + boolean isOffhand = handInUse == EquipmentSlot.OFF_HAND; + if (isOffhand) flagBit |= 0x02; + } + + if (player.isRiptiding()) + flagBit |= 0x04; + + write(values.LIVING_FLAGS, (byte)flagBit); + + int potionColor = 0; + List colors = new ObjectArrayList<>(); + boolean hasAmbient = false; + for (PotionEffect effect : player.getActivePotionEffects()) + { + colors.add(effect.getType().getColor()); + + if (effect.isAmbient()) + hasAmbient = effect.isAmbient(); + } + + if (!colors.isEmpty()) + { + var firstColor = colors.remove(0); + var finalColor = firstColor.mixColors(colors.toArray(new Color[]{})); + potionColor = finalColor.asRGB(); + } + + write(values.POTION_COLOR, potionColor); + write(values.POTION_ISAMBIENT, hasAmbient); + + write(values.STUCKED_ARROWS, player.getArrowsInBody()); + write(values.BEE_STINGERS, player.getBeeStingersInBody()); + super.doSync(); } }