Implemented potion color and potion ambient properties

This commit is contained in:
D3v1s0m 2023-06-25 13:05:28 +05:30
parent 16c48043b4
commit 6d4cd5199a
No known key found for this signature in database
GPG Key ID: 3B6EC35367B8D82E
14 changed files with 134 additions and 1 deletions

@ -0,0 +1,31 @@
package lol.pyr.znpcsplus.util;
public class PotionColor {
private final String color;
public static PotionColor DEFAULT = new PotionColor("0");
public PotionColor(String color) {
this.color = color;
}
public int getColor() {
if (color.startsWith("#")) {
if (color.length() != 7)
throw new IllegalArgumentException("Hex color must be 6 characters long");
return Integer.parseInt(color.substring(1), 16);
} else if (color.startsWith("0x")) {
if (color.length() != 8)
throw new IllegalArgumentException("Hex color must be 6 characters long");
return Integer.parseInt(color.substring(2), 16);
} else {
return Integer.parseInt(color);
}
}
public String toString() {
return String.valueOf(color);
}
}

@ -259,6 +259,7 @@ public class ZNpcsPlus extends JavaPlugin {
manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage)); manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage));
manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage)); manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage));
manager.registerParser(NpcPose.class, new NpcPoseParser(incorrectUsageMessage)); manager.registerParser(NpcPose.class, new NpcPoseParser(incorrectUsageMessage));
manager.registerParser(PotionColor.class, new PotionColorParser(incorrectUsageMessage));
manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root"))
.addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry)) .addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry))

@ -11,6 +11,7 @@ import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.util.NpcPose; import lol.pyr.znpcsplus.util.NpcPose;
import lol.pyr.znpcsplus.util.PotionColor;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -49,6 +50,10 @@ public class PropertyCommand implements CommandHandler {
value = null; value = null;
valueName = "NONE"; valueName = "NONE";
} }
else if (type == PotionColor.class && context.argSize() < 1 && npc.getProperty(property) != null) {
value = PotionColor.DEFAULT;
valueName = "NONE";
}
else { else {
value = context.parse(type); value = context.parse(type);
valueName = String.valueOf(value); valueName = String.valueOf(value);
@ -69,6 +74,7 @@ public class PropertyCommand implements CommandHandler {
if (type == Boolean.class) return context.suggestLiteral("true", "false"); if (type == Boolean.class) return context.suggestLiteral("true", "false");
if (type == NamedTextColor.class) return context.suggestCollection(NamedTextColor.NAMES.keys()); if (type == NamedTextColor.class) return context.suggestCollection(NamedTextColor.NAMES.keys());
if (type == NpcPose.class) return context.suggestEnum(NpcPose.values()); if (type == NpcPose.class) return context.suggestEnum(NpcPose.values());
if (property.getName().equals("potion_color")) return context.suggestLiteral("0x0F00FF", "#FFFFFF", "16711935");
} }
return Collections.emptyList(); return Collections.emptyList();
} }

@ -7,6 +7,7 @@ import lol.pyr.znpcsplus.api.skin.SkinDescriptor;
import lol.pyr.znpcsplus.entity.serializers.*; import lol.pyr.znpcsplus.entity.serializers.*;
import lol.pyr.znpcsplus.skin.cache.SkinCache; import lol.pyr.znpcsplus.skin.cache.SkinCache;
import lol.pyr.znpcsplus.util.NpcPose; import lol.pyr.znpcsplus.util.NpcPose;
import lol.pyr.znpcsplus.util.PotionColor;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -28,6 +29,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerSerializer(new SkinDescriptorSerializer(skinCache)); registerSerializer(new SkinDescriptorSerializer(skinCache));
registerSerializer(new ItemStackPropertySerializer()); registerSerializer(new ItemStackPropertySerializer());
registerSerializer(new NpcPosePropertySerializer()); registerSerializer(new NpcPosePropertySerializer());
registerSerializer(new PotionColorPropertySerializer());
registerType("glow", NamedTextColor.class); registerType("glow", NamedTextColor.class);
registerType("fire", false); registerType("fire", false);
@ -46,7 +48,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerType("offhand", ItemStack.class); registerType("offhand", ItemStack.class);
registerType("using_item", false); // TODO: Eating/Drinking/Blocking with sword/etc registerType("using_item", false); // TODO: Eating/Drinking/Blocking with sword/etc
registerType("potion_color", 0xFFFFFF); // TODO registerType("potion_color", PotionColor.class); // TODO
registerType("potion_ambient", false); // TODO registerType("potion_ambient", false); // TODO
registerType("shaking", false); registerType("shaking", false);
registerType("baby", false); // TODO registerType("baby", false); // TODO

@ -0,0 +1,21 @@
package lol.pyr.znpcsplus.entity.serializers;
import lol.pyr.znpcsplus.util.PotionColor;
import lol.pyr.znpcsplus.entity.PropertySerializer;
public class PotionColorPropertySerializer implements PropertySerializer<PotionColor> {
@Override
public String serialize(PotionColor property) {
return property.toString();
}
@Override
public PotionColor deserialize(String property) {
return new PotionColor(property);
}
@Override
public Class<PotionColor> getTypeClass() {
return PotionColor.class;
}
}

@ -27,4 +27,6 @@ public interface MetadataFactory {
EntityData noGravity(); EntityData noGravity();
EntityData pose(EntityPose pose); EntityData pose(EntityPose pose);
EntityData shaking(boolean enabled); EntityData shaking(boolean enabled);
EntityData potionColor(int color);
EntityData potionAmbient(boolean ambient);
} }

@ -8,4 +8,14 @@ public class V1_10MetadataFactory extends V1_9MetadataFactory {
public EntityData noGravity() { public EntityData noGravity() {
return newEntityData(5, EntityDataTypes.BOOLEAN, true); 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);
}
} }

@ -14,4 +14,14 @@ public class V1_14MetadataFactory extends V1_13MetadataFactory {
public EntityData pose(EntityPose pose) { public EntityData pose(EntityPose pose) {
return newEntityData(6, EntityDataTypes.ENTITY_POSE, pose); return newEntityData(6, EntityDataTypes.ENTITY_POSE, pose);
} }
@Override
public EntityData potionColor(int color) {
return newEntityData(9, EntityDataTypes.INT, color);
}
@Override
public EntityData potionAmbient(boolean ambient) {
return newEntityData(10, EntityDataTypes.BOOLEAN, ambient);
}
} }

@ -18,4 +18,14 @@ public class V1_17MetadataFactory extends V1_16MetadataFactory {
public EntityData shaking(boolean enabled) { public EntityData shaking(boolean enabled) {
return newEntityData(7, EntityDataTypes.INT, enabled ? 140 : 0); return newEntityData(7, EntityDataTypes.INT, enabled ? 140 : 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);
}
} }

@ -45,6 +45,16 @@ public class V1_8MetadataFactory implements MetadataFactory {
throw new UnsupportedOperationException("The shaking entity data isn't supported on this version"); throw new UnsupportedOperationException("The shaking entity 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 @Override
public EntityData silent(boolean enabled) { public EntityData silent(boolean enabled) {
return newEntityData(4, EntityDataTypes.BYTE, (byte) (enabled ? 1 : 0)); return newEntityData(4, EntityDataTypes.BYTE, (byte) (enabled ? 1 : 0));

@ -18,6 +18,11 @@ public class V1_9MetadataFactory extends V1_8MetadataFactory {
return newEntityData(0, EntityDataTypes.BYTE, (byte) ((onFire ? 0x01 : 0) | (invisible ? 0x20 : 0) | (glowing ? 0x40 : 0) | (usingElytra ? 0x80 : 0))); return newEntityData(0, EntityDataTypes.BYTE, (byte) ((onFire ? 0x01 : 0) | (invisible ? 0x20 : 0) | (glowing ? 0x40 : 0) | (usingElytra ? 0x80 : 0)));
} }
@Override
public EntityData potionAmbient(boolean ambient) {
return newEntityData(8, EntityDataTypes.BOOLEAN, ambient);
}
@Override @Override
public Collection<EntityData> name(Component name) { public Collection<EntityData> name(Component name) {
return list( return list(

@ -77,6 +77,8 @@ public class NpcTypeImpl implements NpcType {
allowedProperties.add(propertyRegistry.getByName("silent")); allowedProperties.add(propertyRegistry.getByName("silent"));
allowedProperties.add(propertyRegistry.getByName("look")); allowedProperties.add(propertyRegistry.getByName("look"));
allowedProperties.add(propertyRegistry.getByName("skin_cape")); allowedProperties.add(propertyRegistry.getByName("skin_cape"));
allowedProperties.add(propertyRegistry.getByName("potion_color"));
allowedProperties.add(propertyRegistry.getByName("potion_ambient"));
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9)) if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9))
allowedProperties.add(propertyRegistry.getByName("glow")); allowedProperties.add(propertyRegistry.getByName("glow"));
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_14)) if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_14))

@ -15,6 +15,7 @@ import lol.pyr.znpcsplus.api.skin.SkinDescriptor;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.util.PotionColor;
import lol.pyr.znpcsplus.metadata.MetadataFactory; import lol.pyr.znpcsplus.metadata.MetadataFactory;
import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.skin.BaseSkinDescriptor; import lol.pyr.znpcsplus.skin.BaseSkinDescriptor;
@ -142,6 +143,8 @@ public class V1_8PacketFactory implements PacketFactory {
false) false)
); );
add(data, metadataFactory.silent(properties.getProperty(propertyRegistry.getByName("silent", Boolean.class)))); add(data, metadataFactory.silent(properties.getProperty(propertyRegistry.getByName("silent", Boolean.class))));
add(data, metadataFactory.potionColor(properties.getProperty(propertyRegistry.getByName("potion_color", PotionColor.class)).getColor()));
add(data, metadataFactory.potionAmbient(properties.getProperty(propertyRegistry.getByName("potion_ambient", Boolean.class))));
if (properties.hasProperty(propertyRegistry.getByName("name"))) addAll(data, metadataFactory.name(properties.getProperty(propertyRegistry.getByName("name", Component.class)))); if (properties.hasProperty(propertyRegistry.getByName("name"))) addAll(data, metadataFactory.name(properties.getProperty(propertyRegistry.getByName("name", Component.class))));
return data; return data;
} }

@ -0,0 +1,20 @@
package lol.pyr.znpcsplus.parsers;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.adventure.parse.ParserType;
import lol.pyr.director.common.command.CommandExecutionException;
import lol.pyr.director.common.message.Message;
import lol.pyr.znpcsplus.util.PotionColor;
import java.util.Deque;
public class PotionColorParser extends ParserType<PotionColor> {
public PotionColorParser(Message<CommandContext> message) {
super(message);
}
@Override
public PotionColor parse(Deque<String> deque) throws CommandExecutionException {
return new PotionColor(deque.pop());
}
}