From 3d422e18bd20e2d13a49388e07d03cac18926332 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Fri, 15 Dec 2023 15:23:55 +0800 Subject: [PATCH] Fix panda watcher don't merge custom NBTs --- .../server/ServerDisguiseWrapper.java | 6 ++++ .../watchers/types/PandaWatcher.java | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java b/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java index b404b0b9..06a4e3b5 100644 --- a/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java +++ b/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java @@ -62,6 +62,9 @@ public void mergeCompound(CompoundTag compoundTag) @Override public CompoundTag getCompound() { + if (bindingWatcher != null) + instance.compoundTag.merge(WatcherUtils.buildCompoundFromWatcher(bindingWatcher)); + return instance.compoundTag.copy(); } @@ -308,6 +311,7 @@ private void refreshRegistry() return; } + //先和watcher同步一下我们的NBT bindingWatcher.mergeFromCompound(getCompound()); //todo: 激活刷新时也刷新到玩家 @@ -321,6 +325,8 @@ private void refreshRegistry() if (bindingWatcher.getEntityType() == EntityType.GHAST) bindingWatcher.write(ValueIndex.GHAST.CHARGING, aggressive); + //然后从watcher拉取他们的NBT。 + //如果watcher有存在会随机的值,此举会将随机的值同步给我们 this.instance.compoundTag.merge(WatcherUtils.buildCompoundFromWatcher(bindingWatcher)); } } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PandaWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PandaWatcher.java index cc9e131f..e007a140 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PandaWatcher.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/PandaWatcher.java @@ -1,5 +1,6 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types; +import net.minecraft.nbt.CompoundTag; import org.bukkit.entity.EntityType; import org.bukkit.entity.Panda; import org.bukkit.entity.Player; @@ -47,4 +48,31 @@ protected void initValues() write(ValueIndex.PANDA.MAIN_GENE, (byte)mainGene.ordinal()); write(ValueIndex.PANDA.HIDDEN_GENE, (byte)hiddenGene.ordinal()); } + + @Override + public void mergeFromCompound(CompoundTag nbt) + { + super.mergeFromCompound(nbt); + + if (nbt.contains("MainGene")) + write(ValueIndex.PANDA.MAIN_GENE, (byte)getGeneFromName(nbt.getString("MainGene")).ordinal()); + + if (nbt.contains("HiddenGene")) + write(ValueIndex.PANDA.HIDDEN_GENE, (byte)getGeneFromName(nbt.getString("HiddenGene")).ordinal()); + } + + private Panda.Gene getGeneFromName(String name) + { + var gene = Panda.Gene.values(); + var match = Arrays.stream(gene).filter(g -> g.name().equalsIgnoreCase(name)) + .findFirst().orElse(null); + + if (match == null) + { + logger.warn("Null Gene for name " + name + "?!"); + match = Panda.Gene.NORMAL; + } + + return match; + } }