switch to using bukkit Color class

This commit is contained in:
Pyrbu 2023-06-26 12:51:48 +02:00
parent f76d9a1830
commit 2a19c974b8
9 changed files with 62 additions and 88 deletions

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

@ -50,10 +50,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.*;
import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.World;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -264,7 +261,7 @@ public class ZNpcsPlus extends JavaPlugin {
manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage)); manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage));
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(PotionColor.class, new PotionColorParser(incorrectUsageMessage)); manager.registerParser(Color.class, new ColorParser(incorrectUsageMessage));
registerEnumParser(manager, NpcPose.class, incorrectUsageMessage); registerEnumParser(manager, NpcPose.class, incorrectUsageMessage);
registerEnumParser(manager, DyeColor.class, incorrectUsageMessage); registerEnumParser(manager, DyeColor.class, incorrectUsageMessage);

@ -11,9 +11,9 @@ 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;
import org.bukkit.Color;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -32,6 +32,8 @@ public class PropertyCommand implements CommandHandler {
NpcImpl npc = entry.getNpc(); NpcImpl npc = entry.getNpc();
EntityPropertyImpl<?> property = context.parse(EntityPropertyImpl.class); EntityPropertyImpl<?> property = context.parse(EntityPropertyImpl.class);
// TODO: find a way to do this better & rewrite this mess
if (!npc.getType().getAllowedProperties().contains(property)) context.halt(Component.text("Property " + property.getName() + " not allowed for npc type " + npc.getType().getName(), NamedTextColor.RED)); if (!npc.getType().getAllowedProperties().contains(property)) context.halt(Component.text("Property " + property.getName() + " not allowed for npc type " + npc.getType().getName(), NamedTextColor.RED));
Class<?> type = property.getType(); Class<?> type = property.getType();
Object value; Object value;
@ -50,8 +52,8 @@ 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) { else if (type == Color.class && context.argSize() < 1 && npc.getProperty(property) != null) {
value = PotionColor.DEFAULT; value = Color.WHITE;
valueName = "NONE"; valueName = "NONE";
} }
else { else {
@ -74,7 +76,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"); if (type == Color.class) return context.suggestLiteral("0x0F00FF", "#FFFFFF", "16711935");
} }
return Collections.emptyList(); return Collections.emptyList();
} }

@ -7,9 +7,9 @@ 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;
import org.bukkit.Color;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import java.util.Collection; import java.util.Collection;
@ -29,7 +29,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerSerializer(new NamedTextColorPropertySerializer()); registerSerializer(new NamedTextColorPropertySerializer());
registerSerializer(new SkinDescriptorSerializer(skinCache)); registerSerializer(new SkinDescriptorSerializer(skinCache));
registerSerializer(new ItemStackPropertySerializer()); registerSerializer(new ItemStackPropertySerializer());
registerSerializer(new PotionColorPropertySerializer()); registerSerializer(new ColorPropertySerializer());
registerEnumSerializer(NpcPose.class); registerEnumSerializer(NpcPose.class);
registerEnumSerializer(DyeColor.class); registerEnumSerializer(DyeColor.class);
@ -51,7 +51,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerType("offhand", ItemStack.class); registerType("offhand", ItemStack.class);
registerType("using_item", false); // TODO: fix it for 1.8 and add new property to use offhand item and riptide animation registerType("using_item", false); // TODO: fix it for 1.8 and add new property to use offhand item and riptide animation
registerType("potion_color", PotionColor.DEFAULT, PotionColor.class); registerType("potion_color", Color.WHITE);
registerType("potion_ambient", false); registerType("potion_ambient", false);
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.entity.PropertySerializer;
import org.bukkit.Color;
public class ColorPropertySerializer implements PropertySerializer<Color> {
@Override
public String serialize(Color property) {
return String.valueOf(property.asRGB());
}
@Override
public Color deserialize(String property) {
return Color.fromRGB(Integer.parseInt(property));
}
@Override
public Class<Color> getTypeClass() {
return Color.class;
}
}

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

@ -17,9 +17,9 @@ 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;
import lol.pyr.znpcsplus.util.NpcLocation; import lol.pyr.znpcsplus.util.NpcLocation;
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;
import org.bukkit.Color;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -142,7 +142,7 @@ public class V1_8PacketFactory implements PacketFactory {
); );
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.usingItem(properties.getProperty(propertyRegistry.getByName("using_item", Boolean.class)), false, false)); add(data, metadataFactory.usingItem(properties.getProperty(propertyRegistry.getByName("using_item", Boolean.class)), false, false));
add(data, metadataFactory.potionColor(properties.getProperty(propertyRegistry.getByName("potion_color", PotionColor.class)).getColor())); add(data, metadataFactory.potionColor(properties.getProperty(propertyRegistry.getByName("potion_color", Color.class)).asRGB()));
add(data, metadataFactory.potionAmbient(properties.getProperty(propertyRegistry.getByName("potion_ambient", Boolean.class)))); 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,28 @@
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 org.bukkit.Color;
import java.util.Deque;
public class ColorParser extends ParserType<Color> {
public ColorParser(Message<CommandContext> message) {
super(message);
}
@Override
public Color parse(Deque<String> deque) throws CommandExecutionException {
String color = deque.pop();
if (color.startsWith("0x")) color = color.substring(2);
if (color.startsWith("&")) color = color.substring(1);
if (color.startsWith("#")) color = color.substring(1);
try {
return Color.fromRGB(Integer.parseInt(color));
} catch (IllegalArgumentException exception) {
throw new CommandExecutionException();
}
}
}

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