Skip to content

Commit

Permalink
misc: Revert ArmorStandWatcher, add resetRegistries method to SingleW…
Browse files Browse the repository at this point in the history
…atcher
  • Loading branch information
MATRIX-feather committed Oct 13, 2024
1 parent 92f280d commit 8ed5f58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ public <X> X readEntry(CustomEntry<X> entry)

//endregion Custom Registry

public void resetRegistries()
{
Map<Integer, Object> registryCopy = new Object2ObjectOpenHashMap<>(registry);

registryCopy.forEach((id, val) ->
{
var sv = this.knownValues.getOrDefault(id, null);
if (sv != null)
this.writePersistent((SingleValue<Object>) sv, sv.defaultValue());

this.registry.remove(id);
});

Map<String, Object> crCopy = new Object2ObjectOpenHashMap<>(customRegistry);
crCopy.clear();
}

//region Value Registry

protected final Map<Integer, Object> registry = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import net.minecraft.nbt.ListTag;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xyz.nifeather.morph.backends.server.renderer.network.datawatcher.values.SingleValue;
import xyz.nifeather.morph.backends.server.renderer.network.registries.CustomEntries;
import xyz.nifeather.morph.backends.server.renderer.network.registries.ValueIndex;
import xyz.nifeather.morph.misc.disguiseProperty.DisguiseProperties;
import xyz.nifeather.morph.misc.disguiseProperty.SingleProperty;
import xyz.nifeather.morph.misc.disguiseProperty.values.ArmorStandProperties;
import xyz.nifeather.morph.utilities.NbtUtils;

public class ArmorStandWatcher extends InventoryLivingWatcher
{
Expand Down Expand Up @@ -46,23 +43,26 @@ public byte getArmorStandFlags(boolean small, boolean showArms, boolean noBasePl

private boolean isSmall()
{
return this.readEntryOrDefault(CustomEntries.ARMOR_STAND_SMALL, false);
return (read(ValueIndex.ARMOR_STAND.DATA_FLAGS) & 0x01) == 0x01;
}

private boolean noBasePlate()
{
return this.readEntryOrDefault(CustomEntries.ARMOR_STAND_NO_BASE_PLATE, false);
return (read(ValueIndex.ARMOR_STAND.DATA_FLAGS) & 0x08) == 0x08;
}

private boolean showArms()
{
return this.readEntryOrDefault(CustomEntries.ARMOR_STAND_SHOW_ARMS, false);
return (read(ValueIndex.ARMOR_STAND.DATA_FLAGS) & 0x04) == 0x04;
}

private Rotations getVec3(ListTag listTag, Rotations defaultValue)
{
if (listTag.isEmpty())
{
logger.warn("Empty listTag! Using defaultValue...");
listTag = defaultValue.save();
}

return new Rotations(listTag);
}
Expand All @@ -75,7 +75,6 @@ protected <X> void onPropertyWrite(SingleProperty<X> property, X value)
if (property.equals(properties.SHOW_ARMS))
{
var val = (Boolean) value;
this.writeEntry(CustomEntries.ARMOR_STAND_SHOW_ARMS, val);
this.writePersistent(ValueIndex.ARMOR_STAND.DATA_FLAGS, getArmorStandFlags(this.isSmall(), val, this.noBasePlate()));
}

Expand All @@ -91,22 +90,13 @@ public void mergeFromCompound(CompoundTag nbt)
boolean showArms = showArms();

if (nbt.contains("Small"))
{
small = nbt.getBoolean("Small");
this.writeEntry(CustomEntries.ARMOR_STAND_SMALL, small);
}

if (nbt.contains("NoBasePlate"))
{
noBasePlate = nbt.getBoolean("NoBasePlate");
this.writeEntry(CustomEntries.ARMOR_STAND_NO_BASE_PLATE, noBasePlate);
}

if (nbt.contains("ShowArms"))
{
showArms = nbt.getBoolean("ShowArms");
this.writeEntry(CustomEntries.ARMOR_STAND_SHOW_ARMS, showArms);
}

//Tag "Invisible" is not supported as it's synced with the player

Expand Down Expand Up @@ -165,34 +155,18 @@ public void writeToCompound(CompoundTag nbt)
{
super.writeToCompound(nbt);

var small = this.readEntry(CustomEntries.ARMOR_STAND_SMALL);
if (small != null)
nbt.putBoolean("Small", small);

var noBasePlate = this.readEntry(CustomEntries.ARMOR_STAND_NO_BASE_PLATE);
if (noBasePlate != null)
nbt.putBoolean("NoBasePlate", noBasePlate);

var showArms = this.readEntry(CustomEntries.ARMOR_STAND_SHOW_ARMS);
if (showArms != null)
nbt.putBoolean("ShowArms", showArms);
nbt.putBoolean("Small", this.isSmall());
nbt.putBoolean("NoBasePlate", this.noBasePlate());
nbt.putBoolean("ShowArms", this.showArms());

var poseCompound = new CompoundTag();
saveCompoundIfSet(poseCompound, "Head", ValueIndex.ARMOR_STAND.HEAD_ROTATION);
saveCompoundIfSet(poseCompound, "Body", ValueIndex.ARMOR_STAND.BODY_ROTATION);
saveCompoundIfSet(poseCompound, "LeftArm", ValueIndex.ARMOR_STAND.LEFT_ARM_ROTATION);
saveCompoundIfSet(poseCompound, "RightArm", ValueIndex.ARMOR_STAND.RIGHT_ARM_ROTATION);
saveCompoundIfSet(poseCompound, "LeftLeg", ValueIndex.ARMOR_STAND.LEFT_LEG_ROTATION);
saveCompoundIfSet(poseCompound, "RightLeg", ValueIndex.ARMOR_STAND.RIGHT_LEG_ROTATION);
poseCompound.put("Head", read(ValueIndex.ARMOR_STAND.HEAD_ROTATION).save());
poseCompound.put("Body", read(ValueIndex.ARMOR_STAND.BODY_ROTATION).save());
poseCompound.put("LeftArm", read(ValueIndex.ARMOR_STAND.LEFT_ARM_ROTATION).save());
poseCompound.put("RightArm", read(ValueIndex.ARMOR_STAND.RIGHT_ARM_ROTATION).save());
poseCompound.put("LeftLeg", read(ValueIndex.ARMOR_STAND.LEFT_LEG_ROTATION).save());
poseCompound.put("RightLeg", read(ValueIndex.ARMOR_STAND.RIGHT_LEG_ROTATION).save());

nbt.put("Pose", poseCompound);
}

private void saveCompoundIfSet(CompoundTag compoundTag, String name, SingleValue<Rotations> sv)
{
var value = readOr(sv, null);
if (value == null) return;

compoundTag.put(name, value.save());
}
}
}

0 comments on commit 8ed5f58

Please sign in to comment.