From fabdd503716bf3bb71a0a3040d15999ebb6875c7 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Fri, 15 Dec 2023 13:21:58 +0800 Subject: [PATCH] Random values --- .../server/ServerDisguiseWrapper.java | 7 +- .../network/datawatcher/values/FoxValues.java | 2 +- .../datawatcher/values/SingleValue.java | 24 ++++ .../datawatcher/watchers/SingleWatcher.java | 14 ++- .../watchers/types/FoxWatcher.java | 9 +- .../renderer/utilties/WatcherUtils.java | 112 ++++++++++++++++++ 6 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 src/main/java/xiamomc/morph/backends/server/renderer/utilties/WatcherUtils.java diff --git a/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java b/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java index 12e7db97..614f7261 100644 --- a/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java +++ b/src/main/java/xiamomc/morph/backends/server/ServerDisguiseWrapper.java @@ -21,6 +21,7 @@ import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.GhastWatcher; import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.InventoryLivingWatcher; import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex; +import xiamomc.morph.backends.server.renderer.utilties.WatcherUtils; import xiamomc.morph.misc.DisguiseEquipment; import xiamomc.morph.misc.DisguiseState; import xiamomc.morph.utilities.NbtUtils; @@ -317,7 +318,9 @@ private void refreshRegistry() bindingWatcher.write(EntryIndex.EQUIPMENT, this.equipment); } - if (bindingWatcher instanceof GhastWatcher ghastWatcher) - ghastWatcher.write(ValueIndex.GHAST.CHARGING, aggressive); + if (bindingWatcher.getEntityType() == EntityType.GHAST) + bindingWatcher.write(ValueIndex.GHAST.CHARGING, aggressive); + + this.instance.compoundTag.merge(WatcherUtils.buildCompoundFromWatcher(bindingWatcher)); } } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/FoxValues.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/FoxValues.java index 4aa8ea08..92e08ada 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/FoxValues.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/FoxValues.java @@ -4,7 +4,7 @@ public class FoxValues extends AnimalValues { - public final SingleValue VARIANT = getSingle(0); + public final SingleValue VARIANT = getSingle(0).withRandom(0, 1); public final SingleValue FLAGS = getSingle((byte)0); public final SingleValue TRUSTED_ID_0 = getSingle(UUID.randomUUID()); public final SingleValue TRUSTED_ID_1 = getSingle(UUID.randomUUID()); diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/SingleValue.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/SingleValue.java index 79340bd1..28d0aba0 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/SingleValue.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/values/SingleValue.java @@ -1,9 +1,13 @@ package xiamomc.morph.backends.server.renderer.network.datawatcher.values; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.List; + public class SingleValue { private final Class type; @@ -35,6 +39,26 @@ public SingleValue(Class type, int index, @NotNull T defaultValue) this.defaultValue = defaultValue; } + private final List randomValues = new ObjectArrayList<>(); + + public List getRandomValues() + { + return new ObjectArrayList<>(randomValues); + } + + public SingleValue withRandom(List values) + { + this.randomValues.clear(); + this.randomValues.addAll(values); + + return this; + } + + public SingleValue withRandom(T... randomValues) + { + return withRandom(Arrays.stream(randomValues).toList()); + } + @Override public boolean equals(Object obj) { 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 97ff214c..b3131ce2 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 @@ -4,6 +4,7 @@ import com.comphenix.protocol.events.PacketContainer; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.RandomSequence; import net.minecraft.world.level.GameType; import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; @@ -20,6 +21,7 @@ import java.util.List; import java.util.Map; +import java.util.Random; import java.util.UUID; public abstract class SingleWatcher extends MorphPluginObject @@ -135,7 +137,17 @@ protected boolean register(SingleValue singleValue) { if (registry.keySet().stream().anyMatch(sv -> sv.index() == singleValue.index())) return false; - registry.put(singleValue, singleValue.defaultValue()); + var initialValue = singleValue.defaultValue(); + var randoms = singleValue.getRandomValues(); + if (!randoms.isEmpty()) + { + var index = new Random().nextInt(randoms.size()); + initialValue = randoms.get(index); + + logger.info("Get random value '%s'".formatted(initialValue)); + } + + registry.put(singleValue, initialValue); return true; } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/FoxWatcher.java b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/FoxWatcher.java index dcd1216b..1725008f 100644 --- a/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/FoxWatcher.java +++ b/src/main/java/xiamomc/morph/backends/server/renderer/network/datawatcher/watchers/types/FoxWatcher.java @@ -26,7 +26,12 @@ public void mergeFromCompound(CompoundTag nbt) { super.mergeFromCompound(nbt); - var isSnow = nbt.getString("Type").equalsIgnoreCase("SNOW"); - write(ValueIndex.FOX.VARIANT, isSnow ? 1 : 0); + var haveType = nbt.contains("Type"); + + if (haveType) + { + var isSnow = nbt.getString("Type").equalsIgnoreCase("SNOW"); + write(ValueIndex.FOX.VARIANT, isSnow ? 1 : 0); + } } } diff --git a/src/main/java/xiamomc/morph/backends/server/renderer/utilties/WatcherUtils.java b/src/main/java/xiamomc/morph/backends/server/renderer/utilties/WatcherUtils.java new file mode 100644 index 00000000..2838ec35 --- /dev/null +++ b/src/main/java/xiamomc/morph/backends/server/renderer/utilties/WatcherUtils.java @@ -0,0 +1,112 @@ +package xiamomc.morph.backends.server.renderer.utilties; + +import net.minecraft.nbt.CompoundTag; +import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher; +import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.slimemagma.AbstractSlimeWatcher; + +public class WatcherUtils +{ + public static CompoundTag buildCompoundFromWatcher(SingleWatcher watcher) + { + var tag = new CompoundTag(); + + switch (watcher.getEntityType()) + { + case SLIME, MAGMA_CUBE -> + { + var size = watcher.get(ValueIndex.SLIME_MAGMA.SIZE); + tag.putInt("Size", size); + } +/* + case HORSE -> + { + var color = ((HorseWatcher) watcher).getColor().ordinal(); + var style = ((HorseWatcher) watcher).getStyle().ordinal(); + compoundTag.putInt("Variant", color | style << 8); + } + + case PARROT -> + { + var variant = ((ParrotWatcher) watcher).getVariant().ordinal(); + compoundTag.putInt("Variant", variant); + } + + case CAT -> + { + var variant = ((CatWatcher) watcher).getType().getKey().asString(); + compoundTag.putString("variant", variant); + } + + case TROPICAL_FISH -> + { + var variant = ((TropicalFishWatcher) watcher).getVariant(); + + compoundTag.putInt("Variant", variant); + } + + case RABBIT -> + { + var type = ((RabbitWatcher) watcher).getType().getTypeId(); + compoundTag.putInt("RabbitType", type); + } + */ + + case FOX -> + { + var foxType = watcher.get(ValueIndex.FOX.VARIANT) == 0 ? "red" : "snow"; + tag.putString("Type", foxType); + } +/* + case FROG -> + { + var variant = ((FrogWatcher) watcher).getVariant().getKey().asString(); + compoundTag.putString("variant", variant); + } + + case GOAT -> + { + var goatWatcher = ((GoatWatcher) watcher); + + var hasLeftHorn = goatWatcher.hasLeftHorn(); + var hasRightHorn = goatWatcher.hasRightHorn(); + var isScreaming = goatWatcher.isScreaming(); + + compoundTag.putBoolean("HasLeftHorn", hasLeftHorn); + compoundTag.putBoolean("HasRightHorn", hasRightHorn); + compoundTag.putBoolean("IsScreamingGoat", isScreaming); + } + + case PANDA -> + { + var pandaWatcher = ((PandaWatcher) watcher); + var mainGene = pandaWatcher.getMainGene(); + var hiddenGene = pandaWatcher.getHiddenGene(); + + compoundTag.putString("MainGene", mainGene.toString().toLowerCase()); + compoundTag.putString("HiddenGene", hiddenGene.toString().toLowerCase()); + } + + case VILLAGER -> + { + if (!compoundTag.contains("VillagerData")) + { + var villagerData = ((VillagerWatcher) watcher).getVillagerData(); + var profession = villagerData.getProfession(); + var type = villagerData.getType(); + var level = villagerData.getLevel(); + + var compound = new CompoundTag(); + compound.putInt("level", level); + compound.putString("profession", profession.getKey().asString()); + compound.putString("type", type.getKey().asString()); + + compoundTag.put("VillagerData", compound); + } + } + */ + } + + return tag; + } +}