Skip to content

Commit

Permalink
Random values
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 15, 2023
1 parent 9f6d60e commit fabdd50
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class FoxValues extends AnimalValues
{
public final SingleValue<Integer> VARIANT = getSingle(0);
public final SingleValue<Integer> VARIANT = getSingle(0).withRandom(0, 1);
public final SingleValue<Byte> FLAGS = getSingle((byte)0);
public final SingleValue<UUID> TRUSTED_ID_0 = getSingle(UUID.randomUUID());
public final SingleValue<UUID> TRUSTED_ID_1 = getSingle(UUID.randomUUID());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T>
{
private final Class<T> type;
Expand Down Expand Up @@ -35,6 +39,26 @@ public SingleValue(Class<T> type, int index, @NotNull T defaultValue)
this.defaultValue = defaultValue;
}

private final List<T> randomValues = new ObjectArrayList<>();

public List<T> getRandomValues()
{
return new ObjectArrayList<>(randomValues);
}

public SingleValue<T> withRandom(List<T> values)
{
this.randomValues.clear();
this.randomValues.addAll(values);

return this;
}

public SingleValue<T> withRandom(T... randomValues)
{
return withRandom(Arrays.stream(randomValues).toList());
}

@Override
public boolean equals(Object obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit fabdd50

Please sign in to comment.