implement shaking property

This commit is contained in:
Pyrbu 2023-08-07 20:27:01 +02:00
parent 0f6a953a5e
commit 84fa7eb2c9
10 changed files with 66 additions and 70 deletions

@ -200,6 +200,8 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
public void registerTypes(PacketFactory packetFactory) {
ServerVersion ver = PacketEvents.getAPI().getServerManager().getVersion();
boolean legacyBooleans = ver.isOlderThan(ServerVersion.V_1_9);
boolean legacyNames = ver.isOlderThan(ServerVersion.V_1_9);
boolean optionalComponents = ver.isNewerThanOrEquals(ServerVersion.V_1_13);
register(new EquipmentProperty(packetFactory, "helmet", EquipmentSlot.HELMET));
register(new EquipmentProperty(packetFactory, "chestplate", EquipmentSlot.CHEST_PLATE));
@ -208,13 +210,10 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
register(new EquipmentProperty(packetFactory, "hand", EquipmentSlot.MAIN_HAND));
register(new EquipmentProperty(packetFactory, "offhand", EquipmentSlot.OFF_HAND));
boolean legacyName = ver.isOlderThan(ServerVersion.V_1_9);
boolean optionalComponent = ver.isNewerThanOrEquals(ServerVersion.V_1_13);
register(new NameProperty(legacyName, optionalComponent));
register(new DinnerboneProperty(legacyName, optionalComponent));
register(new NameProperty(legacyNames, optionalComponents));
register(new DinnerboneProperty(legacyNames, optionalComponents));
register(new DummyProperty<>("look", false));
register(new GlowProperty(packetFactory));
register(new BitsetProperty("fire", 0, 0x01));
register(new BitsetProperty("invisible", 0, 0x20));
@ -276,13 +275,13 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
else ghastAttackingIndex = 16;
register(new BooleanProperty("attacking", ghastAttackingIndex, false, legacyBooleans));
if (!ver.isNewerThanOrEquals(ServerVersion.V_1_17)) return;
// Goat
if (ver.isNewerThanOrEquals(ServerVersion.V_1_17)) {
register(new BooleanProperty("has_left_horn", 18, true, legacyBooleans));
register(new BooleanProperty("has_right_horn", 19, true, legacyBooleans));
}
register(new ShakingProperty(7));
}
private void registerSerializer(PropertySerializer<?> serializer) {

@ -11,16 +11,24 @@ import java.util.Map;
public class BooleanProperty extends EntityPropertyImpl<Boolean> {
private final int index;
private final boolean legacy;
private final boolean inverted;
public BooleanProperty(String name, int index, boolean defaultValue, boolean legacy) {
this(name, index, defaultValue, legacy, false);
}
public BooleanProperty(String name, int index, boolean defaultValue, boolean legacy, boolean inverted) {
super(name, defaultValue, Boolean.class);
this.index = index;
this.legacy = legacy;
this.inverted = inverted;
}
@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
if (legacy) properties.put(index, new EntityData(index, EntityDataTypes.BYTE, (entity.getProperty(this) ? 1 : 0)));
else properties.put(index, new EntityData(index, EntityDataTypes.BOOLEAN, entity.getProperty(this)));
boolean enabled = entity.getProperty(this);
if (inverted) enabled = !enabled;
if (legacy) properties.put(index, new EntityData(index, EntityDataTypes.BYTE, (enabled ? 1 : 0)));
else properties.put(index, new EntityData(index, EntityDataTypes.BOOLEAN, enabled));
}
}

@ -0,0 +1,23 @@
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 IntegerProperty extends EntityPropertyImpl<Integer> {
private final int index;
protected IntegerProperty(String name, int index, Integer defaultValue) {
super(name, defaultValue, Integer.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)));
}
}

@ -0,0 +1,23 @@
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));
}
}

@ -26,10 +26,8 @@ public interface MetadataFactory {
EntityData noGravity();
EntityData pose(EntityPose pose);
EntityData shaking(boolean enabled);
EntityData usingItem(boolean enabled, boolean offhand, boolean riptide);
EntityData potionColor(int color);
EntityData potionAmbient(boolean ambient);
EntityData shoulderEntityLeft(ParrotVariant variant);
EntityData shoulderEntityRight(ParrotVariant variant);

@ -12,16 +12,6 @@ public class V1_10MetadataFactory extends V1_9MetadataFactory {
return newEntityData(5, EntityDataTypes.BOOLEAN, true);
}
@Override
public EntityData potionColor(int color) {
return newEntityData(8, EntityDataTypes.INT, color);
}
@Override
public EntityData potionAmbient(boolean ambient) {
return newEntityData(9, EntityDataTypes.BOOLEAN, ambient);
}
@Override
public EntityData batHanging(boolean hanging) {
return newEntityData(12, EntityDataTypes.BYTE, (byte) (hanging ? 0x01 : 0));

@ -22,16 +22,6 @@ public class V1_14MetadataFactory extends V1_13MetadataFactory {
return newEntityData(7, EntityDataTypes.BYTE, (byte) ((usingItem ? 0x01 : 0) | (offHand ? 0x02 : 0) | (riptide ? 0x04 : 0)));
}
@Override
public EntityData potionColor(int color) {
return newEntityData(9, EntityDataTypes.INT, color);
}
@Override
public EntityData potionAmbient(boolean ambient) {
return newEntityData(10, EntityDataTypes.BOOLEAN, ambient);
}
@Override
public EntityData shoulderEntityLeft(ParrotVariant variant) {
return createShoulderEntityLeft(17, variant);

@ -11,26 +11,11 @@ import org.bukkit.DyeColor;
@Deprecated
public class V1_17MetadataFactory extends V1_16MetadataFactory {
@Override
public EntityData shaking(boolean enabled) {
return newEntityData(7, EntityDataTypes.INT, enabled ? 140 : 0);
}
@Override
public EntityData usingItem(boolean usingItem, boolean offHand, boolean riptide) {
return newEntityData(8, EntityDataTypes.BYTE, (byte) ((usingItem ? 0x01 : 0) | (offHand ? 0x02 : 0) | (riptide ? 0x04 : 0)));
}
@Override
public EntityData potionColor(int color) {
return newEntityData(10, EntityDataTypes.INT, color);
}
@Override
public EntityData potionAmbient(boolean ambient) {
return newEntityData(11, EntityDataTypes.BOOLEAN, ambient);
}
@Override
public EntityData shoulderEntityLeft(ParrotVariant variant) {
return createShoulderEntityLeft(19, variant);

@ -20,26 +20,11 @@ public class V1_8MetadataFactory implements MetadataFactory {
throw new UnsupportedOperationException("The pose entity data isn't supported on this version");
}
@Override
public EntityData shaking(boolean enabled) {
throw new UnsupportedOperationException("The shaking entity data isn't supported on this version");
}
@Override
public EntityData usingItem(boolean enabled, boolean offHand, boolean riptide) {
throw new UnsupportedOperationException("The standalone using item data isn't supported on this version");
}
@Override
public EntityData potionColor(int color) {
return newEntityData(7, EntityDataTypes.INT, color);
}
@Override
public EntityData potionAmbient(boolean ambient) {
return newEntityData(8, EntityDataTypes.BYTE, (byte) (ambient ? 1 : 0));
}
@Override
public EntityData shoulderEntityLeft(ParrotVariant variant) {
throw new UnsupportedOperationException("The shoulder entity data isn't supported on this version");

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