Skip to content

Commit

Permalink
Cat, Parrot, Goat, RandomValues
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 15, 2023
1 parent fabdd50 commit ca41a17
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ValueIndex
public static final EntityValues BASE_ENTITY = new EntityValues();
public static final LivingEntityValues BASE_LIVING = new LivingEntityValues();
public static final AgeableMobValues AGEABLE_MOB = new AgeableMobValues();
public static final TameableAnimalValues TAMEABLE = new TameableAnimalValues();

public static final ArmorStandValues ARMOR_STAND = new ArmorStandValues();
public static final AllayValues ALLAY = new AllayValues();
Expand All @@ -19,4 +20,7 @@ public class ValueIndex
public static final ChestedHorseValues CHESTED_HORSE = new ChestedHorseValues();
public static final LlamaValues LLAMA = new LlamaValues();
public static final FoxValues FOX = new FoxValues();
public static final ParrotValues PARROT = new ParrotValues();
public static final CatValues CAT = new CatValues();
public static final GoatValues GOAT = new GoatValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.watchers.GoatWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.*;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.horses.AbstractHorseWatcher;
Expand Down Expand Up @@ -48,6 +49,9 @@ public WatcherIndex()
setTypeWatcher(EntityType.TRADER_LLAMA, TraderLlamaWatcher::new);

setTypeWatcher(EntityType.FOX, FoxWatcher::new);
setTypeWatcher(EntityType.PARROT, ParrotWatcher::new);
setTypeWatcher(EntityType.CAT, CatWatcher::new);
setTypeWatcher(EntityType.GOAT, GoatWatcher::new);
}

private void setTypeWatcher(EntityType type, Function<Player, SingleWatcher> func)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.animal.CatVariant;
import org.bukkit.entity.Cat;

public class CatValues extends TameableAnimalValues
{
public final SingleValue<CatVariant> CAT_VARIANT = getSingle(BuiltInRegistries.CAT_VARIANT.getOrThrow(CatVariant.TABBY));
public final SingleValue<Boolean> IS_LYING = getSingle(false);
public final SingleValue<Boolean> RELAXED = getSingle(false);
public final SingleValue<Integer> COLLAR_COLOR = getSingle(14);

public CatValues()
{
super();

registerSingle(CAT_VARIANT, IS_LYING, RELAXED, COLLAR_COLOR);
}
}
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).withRandom(0, 1);
public final SingleValue<Integer> FOX_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 All @@ -13,6 +13,6 @@ public FoxValues()
{
super();

registerSingle(VARIANT, FLAGS);
registerSingle(FOX_VARIANT, FLAGS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

public class GoatValues extends AnimalValues
{
public final SingleValue<Boolean> IS_SCREAMING = getSingle(false);
public final SingleValue<Boolean> HAS_LEFT_HORN = getSingle(true);
public final SingleValue<Boolean> HAS_RIGHT_HORN = getSingle(true);

public GoatValues()
{
super();

registerSingle(IS_SCREAMING, HAS_LEFT_HORN, HAS_RIGHT_HORN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public class HorseValues extends AbstractHorseValues
{
public final SingleValue<Integer> VARIANT = getSingle(0);
public final SingleValue<Integer> HORSE_VARIANT = getSingle(0);

public HorseValues()
{
super();

registerSingle(VARIANT);
registerSingle(HORSE_VARIANT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

public class ParrotValues extends TameableAnimalValues
{
public final SingleValue<Integer> PARROT_VARIANT = getSingle(0).withRandom(0, 1, 2, 3, 4);

public ParrotValues()
{
super();

registerSingle(PARROT_VARIANT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

import java.util.UUID;

public class TameableAnimalValues extends AnimalValues
{
public final SingleValue<Byte> TAMEABLE_FLAGS = getSingle((byte)0);
public final SingleValue<UUID> OWNER = getSingle(UUID.randomUUID());

public TameableAnimalValues()
{
super();

registerSingle(TAMEABLE_FLAGS, OWNER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers;

import net.minecraft.nbt.CompoundTag;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.LivingEntityWatcher;

import java.util.Random;

public class GoatWatcher extends LivingEntityWatcher
{
public GoatWatcher(Player bindingPlayer)
{
super(bindingPlayer, EntityType.GOAT);
}

@Override
protected void initRegistry()
{
super.initRegistry();

register(ValueIndex.GOAT);
}

@Override
protected void initValues()
{
super.initValues();

var random = new Random();

if (random.nextInt(0, 100) <= 15)
write(ValueIndex.GOAT.HAS_LEFT_HORN, false);

if (random.nextInt(0, 100) <= 15)
write(ValueIndex.GOAT.HAS_RIGHT_HORN, false);
}

@Override
public void mergeFromCompound(CompoundTag nbt)
{
super.mergeFromCompound(nbt);

if (nbt.contains("HasLeftHorn"))
write(ValueIndex.GOAT.HAS_LEFT_HORN, nbt.getBoolean("HasLeftHorn"));

if (nbt.contains("HasRightHorn"))
write(ValueIndex.GOAT.HAS_RIGHT_HORN, nbt.getBoolean("HasRightHorn"));

if (nbt.contains("IsScreamingGoat"))
write(ValueIndex.GOAT.IS_SCREAMING, nbt.getBoolean("IsScreamingGoat"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ protected void initRegistry()
{
}

protected void initValues()
{
for (SingleValue<?> singleValue : registry.keySet())
{
var newValue = singleValue.defaultValue();
var randoms = singleValue.getRandomValues();
if (!randoms.isEmpty())
{
var index = new Random().nextInt(randoms.size());
newValue = randoms.get(index);

this.write(singleValue.index(), newValue);
}
}
}

public final UUID bindingUUID;

private Player bindingPlayer;
Expand Down Expand Up @@ -65,6 +81,7 @@ public SingleWatcher(Player bindingPlayer, EntityType entityType)

this.entityType = entityType;
initRegistry();
initValues();

sync();
}
Expand Down Expand Up @@ -137,17 +154,7 @@ protected boolean register(SingleValue<?> singleValue)
{
if (registry.keySet().stream().anyMatch(sv -> sv.index() == singleValue.index())) return false;

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);
registry.put(singleValue, singleValue.defaultValue());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.animal.CatVariant;
import org.bukkit.entity.Cat;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;

import java.util.Arrays;
import java.util.Random;

public class CatWatcher extends LivingEntityWatcher
{
public CatWatcher(Player bindingPlayer)
{
super(bindingPlayer, EntityType.CAT);
}

@Override
protected void initRegistry()
{
super.initRegistry();

register(ValueIndex.CAT);
}

public Cat.Type getCatType()
{
var value = get(ValueIndex.CAT.CAT_VARIANT);
var index = BuiltInRegistries.CAT_VARIANT.getId(value);

return Cat.Type.values()[index];
}

@Override
protected void initValues()
{
super.initValues();

var random = new Random();
var availableVariants = Arrays.stream(Cat.Type.values()).toList();
var targetIndex = random.nextInt(availableVariants.size());
var targetValue = bukkitTypeToNms(availableVariants.get(targetIndex));

logger.info("Write value " + targetValue);
this.write(ValueIndex.CAT.CAT_VARIANT, targetValue);
logger.info("Get value :" + getCatType());
}

private CatVariant bukkitTypeToNms(Cat.Type bukkitType)
{
var bukkitKey = bukkitType.getKey();
ResourceLocation key = new ResourceLocation(bukkitKey.namespace(), bukkitKey.getKey());
return BuiltInRegistries.CAT_VARIANT.get(key);
}

@Override
public void mergeFromCompound(CompoundTag nbt)
{
super.mergeFromCompound(nbt);

if (nbt.contains("variant"))
{
var name = nbt.getString("variant");
var match = Arrays.stream(Cat.Type.values())
.filter(t -> t.name().equalsIgnoreCase(name))
.findFirst();

match.ifPresent(type ->
{
var finalValue = bukkitTypeToNms(type);
this.write(ValueIndex.CAT.CAT_VARIANT, finalValue);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ public void mergeFromCompound(CompoundTag nbt)
{
super.mergeFromCompound(nbt);

var haveType = nbt.contains("Type");

if (haveType)
if (nbt.contains("Type"))
{
var isSnow = nbt.getString("Type").equalsIgnoreCase("SNOW");
write(ValueIndex.FOX.VARIANT, isSnow ? 1 : 0);
write(ValueIndex.FOX.FOX_VARIANT, isSnow ? 1 : 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types;

import net.minecraft.nbt.CompoundTag;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;

public class ParrotWatcher extends LivingEntityWatcher
{
public ParrotWatcher(Player bindingPlayer)
{
super(bindingPlayer, EntityType.PARROT);
}

@Override
protected void initRegistry()
{
super.initRegistry();

register(ValueIndex.PARROT);
}

@Override
public void mergeFromCompound(CompoundTag nbt)
{
super.mergeFromCompound(nbt);

if (nbt.contains("Variant"))
{
var variant = nbt.getInt("Variant");
this.write(ValueIndex.PARROT.PARROT_VARIANT, variant);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void mergeFromCompound(CompoundTag nbt)
{
super.mergeFromCompound(nbt);

var horseChested = nbt.getBoolean("ChestedHorse");
write(ValueIndex.CHESTED_HORSE.HAS_CHEST, horseChested);
if (nbt.contains("ChestedHorse"))
write(ValueIndex.CHESTED_HORSE.HAS_CHEST, nbt.getBoolean("ChestedHorse"));
}
}
Loading

0 comments on commit ca41a17

Please sign in to comment.