implement fox properties

This commit is contained in:
Pyrbu 2023-08-07 20:54:38 +02:00
parent 84fa7eb2c9
commit ddabf9dc2c
12 changed files with 68 additions and 122 deletions

@ -5,16 +5,13 @@ import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import lol.pyr.znpcsplus.api.entity.EntityProperty; import lol.pyr.znpcsplus.api.entity.EntityProperty;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class EntityPropertyImpl<T> implements EntityProperty<T> { public abstract class EntityPropertyImpl<T> implements EntityProperty<T> {
private final String name; private final String name;
private final T defaultValue; private final T defaultValue;
private final Class<T> clazz; private final Class<T> clazz;
private final List<EntityPropertyImpl<?>> dependencies = new ArrayList<>(); private final Set<EntityPropertyImpl<?>> dependencies = new HashSet<>();
private boolean playerModifiable = true; private boolean playerModifiable = true;
protected EntityPropertyImpl(String name, T defaultValue, Class<T> clazz) { protected EntityPropertyImpl(String name, T defaultValue, Class<T> clazz) {

@ -11,6 +11,7 @@ import lol.pyr.znpcsplus.entity.serializers.*;
import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.skin.cache.MojangSkinCache; import lol.pyr.znpcsplus.skin.cache.MojangSkinCache;
import lol.pyr.znpcsplus.util.*; import lol.pyr.znpcsplus.util.*;
import org.bukkit.Color;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import java.util.*; import java.util.*;
@ -102,13 +103,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
// Evoker // Evoker
registerType("evoker_spell", SpellType.NONE); registerType("evoker_spell", SpellType.NONE);
// Fox
registerType("fox_variant", FoxVariant.RED);
registerType("fox_sitting", false);
registerType("fox_crouching", false);
registerType("fox_sleeping", false);
registerType("fox_faceplanted", false);
// Frog // Frog
registerType("frog_variant", FrogVariant.TEMPERATE); registerType("frog_variant", FrogVariant.TEMPERATE);
@ -217,7 +211,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
register(new GlowProperty(packetFactory)); register(new GlowProperty(packetFactory));
register(new BitsetProperty("fire", 0, 0x01)); register(new BitsetProperty("fire", 0, 0x01));
register(new BitsetProperty("invisible", 0, 0x20)); register(new BitsetProperty("invisible", 0, 0x20));
register(new HoloItemProperty()); register(new HologramItemProperty());
linkProperties("glow", "fire", "invisible"); linkProperties("glow", "fire", "invisible");
register(new BooleanProperty("silent", 4, false, legacyBooleans)); register(new BooleanProperty("silent", 4, false, legacyBooleans));
@ -226,7 +220,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) potionIndex = 9; else if (ver.isNewerThanOrEquals(ServerVersion.V_1_14)) potionIndex = 9;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) potionIndex = 8; else if (ver.isNewerThanOrEquals(ServerVersion.V_1_10)) potionIndex = 8;
else potionIndex = 7; else potionIndex = 7;
register(new ColorProperty("potion_color", potionIndex++, null)); register(new EncodedIntegerProperty<>("potion_color", Color.class, potionIndex++, Color::asRGB));
register(new BooleanProperty("potion_ambient", potionIndex, false, legacyBooleans)); register(new BooleanProperty("potion_ambient", potionIndex, false, legacyBooleans));
// Player // Player
@ -257,13 +251,13 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
register(new BitsetProperty("small", armorStandIndex, 0x01)); register(new BitsetProperty("small", armorStandIndex, 0x01));
register(new BitsetProperty("arms", armorStandIndex, 0x04)); register(new BitsetProperty("arms", armorStandIndex, 0x04));
register(new BitsetProperty("base_plate", armorStandIndex++, 0x08, true)); register(new BitsetProperty("base_plate", armorStandIndex++, 0x08, true));
linkProperties("small", "arms", "base_plate");
register(new RotationProperty("head_rotation", armorStandIndex++, Vector3f.zero())); register(new RotationProperty("head_rotation", armorStandIndex++, Vector3f.zero()));
register(new RotationProperty("body_rotation", armorStandIndex++, Vector3f.zero())); register(new RotationProperty("body_rotation", armorStandIndex++, Vector3f.zero()));
register(new RotationProperty("left_arm_rotation", armorStandIndex++, new Vector3f(-10, 0, -10))); register(new RotationProperty("left_arm_rotation", armorStandIndex++, new Vector3f(-10, 0, -10)));
register(new RotationProperty("right_arm_rotation", armorStandIndex++, new Vector3f(-15, 0, 10))); register(new RotationProperty("right_arm_rotation", armorStandIndex++, new Vector3f(-15, 0, 10)));
register(new RotationProperty("left_leg_rotation", armorStandIndex++, new Vector3f(-1, 0, -1))); register(new RotationProperty("left_leg_rotation", armorStandIndex++, new Vector3f(-1, 0, -1)));
register(new RotationProperty("right_leg_rotation", armorStandIndex, new Vector3f(1, 0, 1))); register(new RotationProperty("right_leg_rotation", armorStandIndex, new Vector3f(1, 0, 1)));
linkProperties("small", "arms", "base_plate");
// Ghast // Ghast
final int ghastAttackingIndex; final int ghastAttackingIndex;
@ -275,13 +269,32 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
else ghastAttackingIndex = 16; else ghastAttackingIndex = 16;
register(new BooleanProperty("attacking", ghastAttackingIndex, false, legacyBooleans)); register(new BooleanProperty("attacking", ghastAttackingIndex, false, legacyBooleans));
if (!ver.isNewerThanOrEquals(ServerVersion.V_1_14)) return;
// Fox
int foxIndex;
if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) foxIndex = 17;
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_15)) foxIndex = 16;
else foxIndex = 15;
register(new EncodedIntegerProperty<>("fox_variant", FoxVariant.RED, foxIndex++, Enum::ordinal));
register(new BitsetProperty("fox_sitting", foxIndex, 0x01));
register(new BitsetProperty("fox_crouching", foxIndex, 0x04));
register(new BitsetProperty("fox_sleeping", foxIndex, 0x20));
linkProperties("fox_sitting", "fox_crouching", "fox_sleeping");
if (!ver.isNewerThanOrEquals(ServerVersion.V_1_15)) return;
register(new BitsetProperty("fox_faceplanted", foxIndex, 0x40));
linkProperties("fox_sitting", "fox_crouching", "fox_sleeping", "fox_faceplanted");
if (!ver.isNewerThanOrEquals(ServerVersion.V_1_17)) return; if (!ver.isNewerThanOrEquals(ServerVersion.V_1_17)) return;
// Goat // Goat
register(new BooleanProperty("has_left_horn", 18, true, legacyBooleans)); register(new BooleanProperty("has_left_horn", 18, true, legacyBooleans));
register(new BooleanProperty("has_right_horn", 19, true, legacyBooleans)); register(new BooleanProperty("has_right_horn", 19, true, legacyBooleans));
register(new ShakingProperty(7)); register(new EncodedIntegerProperty<>("shaking", false,7, enabled -> enabled ? 140 : 0));
} }
private void registerSerializer(PropertySerializer<?> serializer) { private void registerSerializer(PropertySerializer<?> serializer) {

@ -1,26 +0,0 @@
package lol.pyr.znpcsplus.entity.properties;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import org.bukkit.Color;
import org.bukkit.entity.Player;
import java.util.Map;
public class ColorProperty extends EntityPropertyImpl<Color> {
private final int index;
public ColorProperty(String name, int index, Color def) {
super(name, def, Color.class);
this.index = index;
}
@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
Color color = entity.getProperty(this);
if (color == null) return;
properties.put(index, newEntityData(index, EntityDataTypes.INT, color.asRGB()));
}
}

@ -0,0 +1,40 @@
package lol.pyr.znpcsplus.entity.properties;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import org.bukkit.entity.Player;
import java.util.Map;
public class EncodedIntegerProperty<T> extends EntityPropertyImpl<T> {
private final IntegerDecoder<T> decoder;
private final int index;
protected EncodedIntegerProperty(String name, T defaultValue, Class<T> clazz, int index, IntegerDecoder<T> decoder) {
super(name, defaultValue, clazz);
this.decoder = decoder;
this.index = index;
}
@SuppressWarnings("unchecked")
public EncodedIntegerProperty(String name, T defaultValue, int index, IntegerDecoder<T> decoder) {
this(name, defaultValue, (Class<T>) defaultValue.getClass(), index, decoder);
}
public EncodedIntegerProperty(String name, Class<T> clazz, int index, IntegerDecoder<T> decoder) {
this(name, null, clazz, index, decoder);
}
@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
T value = entity.getProperty(this);
if (value == null) return;
properties.put(index, newEntityData(index, EntityDataTypes.INT, decoder.decode(value)));
}
public interface IntegerDecoder<T> {
int decode(T obj);
}
}

@ -9,9 +9,9 @@ import org.bukkit.entity.Player;
import java.util.Map; import java.util.Map;
public class HoloItemProperty extends EntityPropertyImpl<ItemStack> { public class HologramItemProperty extends EntityPropertyImpl<ItemStack> {
public HoloItemProperty() { public HologramItemProperty() {
super("holo_item", null, ItemStack.class); super("holo_item", null, ItemStack.class);
setPlayerModifiable(false); setPlayerModifiable(false);
} }

@ -1,23 +0,0 @@
package lol.pyr.znpcsplus.entity.properties;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import org.bukkit.entity.Player;
import java.util.Map;
public class ShakingProperty extends EntityPropertyImpl<Boolean> {
private final int index;
public ShakingProperty(int index) {
super("shaking", false, Boolean.class);
this.index = index;
}
@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
properties.put(index, newEntityData(index, EntityDataTypes.INT, entity.getProperty(this) ? 140 : 0));
}
}

@ -24,7 +24,6 @@ import org.bukkit.DyeColor;
@Deprecated @Deprecated
public interface MetadataFactory { public interface MetadataFactory {
EntityData noGravity();
EntityData pose(EntityPose pose); EntityData pose(EntityPose pose);
EntityData usingItem(boolean enabled, boolean offhand, boolean riptide); EntityData usingItem(boolean enabled, boolean offhand, boolean riptide);
@ -64,10 +63,6 @@ public interface MetadataFactory {
// Evoker // Evoker
EntityData evokerSpell(int spell); EntityData evokerSpell(int spell);
// Fox
EntityData foxVariant(int variant);
EntityData foxProperties(boolean sitting, boolean crouching, boolean sleeping, boolean facePlanted);
// Frog // Frog
EntityData frogVariant(int variant); EntityData frogVariant(int variant);

@ -7,11 +7,6 @@ import lol.pyr.znpcsplus.util.CreeperState;
@Deprecated @Deprecated
public class V1_10MetadataFactory extends V1_9MetadataFactory { public class V1_10MetadataFactory extends V1_9MetadataFactory {
@Override
public EntityData noGravity() {
return newEntityData(5, EntityDataTypes.BOOLEAN, true);
}
@Override @Override
public EntityData batHanging(boolean hanging) { public EntityData batHanging(boolean hanging) {
return newEntityData(12, EntityDataTypes.BYTE, (byte) (hanging ? 0x01 : 0)); return newEntityData(12, EntityDataTypes.BYTE, (byte) (hanging ? 0x01 : 0));

@ -72,16 +72,6 @@ public class V1_14MetadataFactory extends V1_13MetadataFactory {
return newEntityData(15, EntityDataTypes.BYTE, (byte) spell); return newEntityData(15, EntityDataTypes.BYTE, (byte) spell);
} }
@Override
public EntityData foxVariant(int variant) {
return newEntityData(15, EntityDataTypes.INT, variant);
}
@Override
public EntityData foxProperties(boolean sitting, boolean crouching, boolean sleeping, boolean facePlanted) {
return newEntityData(16, EntityDataTypes.BYTE, (byte) ((sitting ? 0x01 : 0) | (crouching ? 0x04 : 0) | (sleeping ? 0x20 : 0)));
}
@Override @Override
public EntityData villagerData(int type, int profession, int level) { public EntityData villagerData(int type, int profession, int level) {
return newEntityData(16, EntityDataTypes.VILLAGER_DATA, new VillagerData(type, profession, level)); return newEntityData(16, EntityDataTypes.VILLAGER_DATA, new VillagerData(type, profession, level));

@ -70,16 +70,6 @@ public class V1_15MetadataFactory extends V1_14MetadataFactory {
return newEntityData(16, EntityDataTypes.BYTE, (byte) spell); return newEntityData(16, EntityDataTypes.BYTE, (byte) spell);
} }
@Override
public EntityData foxVariant(int variant) {
return newEntityData(16, EntityDataTypes.INT, variant);
}
@Override
public EntityData foxProperties(boolean sitting, boolean crouching, boolean sleeping, boolean facePlanted) {
return newEntityData(17, EntityDataTypes.BYTE, (byte) ((sitting ? 0x01 : 0) | (crouching ? 0x04 : 0) | (sleeping ? 0x20 : 0) | (facePlanted ? 0x40 : 0)));
}
@Override @Override
public EntityData villagerData(int type, int profession, int level) { public EntityData villagerData(int type, int profession, int level) {
return newEntityData(17, EntityDataTypes.VILLAGER_DATA, new VillagerData(type, profession, level)); return newEntityData(17, EntityDataTypes.VILLAGER_DATA, new VillagerData(type, profession, level));

@ -106,16 +106,6 @@ public class V1_17MetadataFactory extends V1_16MetadataFactory {
return newEntityData(17, EntityDataTypes.BYTE, (byte) spell); return newEntityData(17, EntityDataTypes.BYTE, (byte) spell);
} }
@Override
public EntityData foxVariant(int variant) {
return newEntityData(17, EntityDataTypes.INT, variant);
}
@Override
public EntityData foxProperties(boolean sitting, boolean crouching, boolean sleeping, boolean facePlanted) {
return newEntityData(18, EntityDataTypes.BYTE, (byte) ((sitting ? 0x01 : 0) | (crouching ? 0x04 : 0) | (sleeping ? 0x20 : 0) | (facePlanted ? 0x40 : 0)));
}
@Override @Override
public EntityData hoglinImmuneToZombification(boolean immuneToZombification) { public EntityData hoglinImmuneToZombification(boolean immuneToZombification) {
return newEntityData(17, EntityDataTypes.BOOLEAN, immuneToZombification); return newEntityData(17, EntityDataTypes.BOOLEAN, immuneToZombification);

@ -10,11 +10,6 @@ import org.bukkit.DyeColor;
@Deprecated @Deprecated
public class V1_8MetadataFactory implements MetadataFactory { public class V1_8MetadataFactory implements MetadataFactory {
@Override
public EntityData noGravity() {
throw new UnsupportedOperationException("The gravity entity data isn't supported on this version");
}
@Override @Override
public EntityData pose(EntityPose pose) { public EntityData pose(EntityPose pose) {
throw new UnsupportedOperationException("The pose entity data isn't supported on this version"); throw new UnsupportedOperationException("The pose entity data isn't supported on this version");
@ -115,16 +110,6 @@ public class V1_8MetadataFactory implements MetadataFactory {
throw new UnsupportedOperationException("The evoker spell entity data isn't supported on this version"); throw new UnsupportedOperationException("The evoker spell entity data isn't supported on this version");
} }
@Override
public EntityData foxVariant(int variant) {
throw new UnsupportedOperationException("The fox variant entity data isn't supported on this version");
}
@Override
public EntityData foxProperties(boolean sitting, boolean crouching, boolean sleeping, boolean facePlanted) {
throw new UnsupportedOperationException("The fox properties entity data isn't supported on this version");
}
@Override @Override
public EntityData frogVariant(int variant) { public EntityData frogVariant(int variant) {
throw new UnsupportedOperationException("The frog variant entity data isn't supported on this version"); throw new UnsupportedOperationException("The frog variant entity data isn't supported on this version");