Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change GlowColor property #106

Merged
merged 8 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions api/src/main/java/lol/pyr/znpcsplus/util/NamedColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lol.pyr.znpcsplus.util;

public enum NamedColor {
BLACK,
DARK_BLUE,
DARK_GREEN,
DARK_AQUA,
DARK_RED,
DARK_PURPLE,
GOLD,
GRAY,
DARK_GRAY,
BLUE,
GREEN,
AQUA,
RED,
LIGHT_PURPLE,
YELLOW,
WHITE
}
2 changes: 1 addition & 1 deletion plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private void registerCommands(NpcRegistryImpl npcRegistry, MojangSkinCache skinC
manager.registerParser(Double.class, new DoubleParser(incorrectUsageMessage));
manager.registerParser(Float.class, new FloatParser(incorrectUsageMessage));
manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage));
manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage));
manager.registerParser(NamedColor.class, new NamedColorParser(incorrectUsageMessage));
manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage));
manager.registerParser(Color.class, new ColorParser(incorrectUsageMessage));
manager.registerParser(Vector3f.class, new Vector3fParser(incorrectUsageMessage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void run(CommandContext context) throws CommandExecutionException {
valueName = bukkitStack.toString();
}
}
else if (type == NamedTextColor.class && context.argSize() < 1 && npc.getProperty(property) != null) {
else if (type == NamedColor.class && context.argSize() < 1 && npc.getProperty(property) != null) {
value = null;
valueName = "NONE";
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public List<String> suggest(CommandContext context) throws CommandExecutionExcep
if (type == Vector3f.class && context.argSize() <= 5) return context.suggestLiteral("0", "0.0");
if (context.argSize() == 3) {
if (type == Boolean.class) return context.suggestLiteral("true", "false");
if (type == NamedTextColor.class) return context.suggestCollection(NamedTextColor.NAMES.keys());
if (type == NamedColor.class) return context.suggestEnum(NamedColor.values());
if (type == Color.class) return context.suggestLiteral("0x0F00FF", "#FFFFFF");
if (type == BlockState.class) return context.suggestLiteral("hand", "looking_at", "block");
if (type == SpellType.class) return PacketEvents.getAPI().getServerManager().getVersion().isOlderThan(ServerVersion.V_1_13) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.DyeColor;
import org.bukkit.inventory.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {

public EntityPropertyRegistryImpl(MojangSkinCache skinCache, ConfigManager configManager) {
registerSerializer(new ComponentPropertySerializer());
registerSerializer(new NamedTextColorPropertySerializer());
registerSerializer(new NamedColorPropertySerializer());
registerSerializer(new SkinDescriptorSerializer(skinCache));
registerSerializer(new ItemStackPropertySerializer());
registerSerializer(new ColorPropertySerializer());
Expand Down Expand Up @@ -273,7 +273,7 @@ public void registerTypes(PacketFactory packetFactory) {
else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) guardianIndex = 11;
else guardianIndex = 16;
register(new BitsetProperty("is_elder", guardianIndex, 0x04, false, legacyBooleans));
register(new BitsetProperty("is_retracting_spikes", guardianIndex++, 0x02, false, legacyBooleans));
register(new BitsetProperty("is_retracting_spikes", guardianIndex, 0x02, false, legacyBooleans));
linkProperties("is_elder", "is_retracting_spikes");
// TODO: add guardian beam target
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.packets.PacketFactory;
import net.kyori.adventure.text.format.NamedTextColor;
import lol.pyr.znpcsplus.util.NamedColor;
import org.bukkit.entity.Player;

import java.util.Map;

public class GlowProperty extends EntityPropertyImpl<NamedTextColor> {
public class GlowProperty extends EntityPropertyImpl<NamedColor> {
private final PacketFactory packetFactory;

public GlowProperty(PacketFactory packetFactory) {
super("glow", null, NamedTextColor.class);
super("glow", null, NamedColor.class);
this.packetFactory = packetFactory;
}

@Override
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) {
NamedTextColor value = entity.getProperty(this);
NamedColor value = entity.getProperty(this);
EntityData oldData = properties.get(0);
byte oldValue = oldData == null ? 0 : (byte) oldData.getValue();
properties.put(0, newEntityData(0, EntityDataTypes.BYTE, (byte) (oldValue | (value == null ? 0 : 0x40))));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lol.pyr.znpcsplus.entity.serializers;

import lol.pyr.znpcsplus.entity.PropertySerializer;
import lol.pyr.znpcsplus.util.NamedColor;

public class NamedColorPropertySerializer implements PropertySerializer<NamedColor> {
@Override
public String serialize(NamedColor property) {
return property.name();
}

@Override
public NamedColor deserialize(String property) {
try {
return NamedColor.valueOf(property.toUpperCase());
} catch (IllegalArgumentException exception) {
return NamedColor.WHITE;
}
}

@Override
public Class<NamedColor> getTypeClass() {
return NamedColor.class;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.github.retrooper.packetevents.protocol.player.Equipment;
import lol.pyr.znpcsplus.api.entity.PropertyHolder;
import lol.pyr.znpcsplus.entity.PacketEntity;
import net.kyori.adventure.text.format.NamedTextColor;
import lol.pyr.znpcsplus.util.NamedColor;
import org.bukkit.entity.Player;

import java.util.List;
Expand All @@ -17,7 +17,7 @@ public interface PacketFactory {
void teleportEntity(Player player, PacketEntity entity);
CompletableFuture<Void> addTabPlayer(Player player, PacketEntity entity, PropertyHolder properties);
void removeTabPlayer(Player player, PacketEntity entity);
void createTeam(Player player, PacketEntity entity, NamedTextColor glowColor);
void createTeam(Player player, PacketEntity entity, NamedColor namedColor);
void removeTeam(Player player, PacketEntity entity);
void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties);
void sendEquipment(Player player, PacketEntity entity, Equipment equipment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.util.NamedColor;
import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
Expand All @@ -27,6 +27,6 @@ public void spawnEntity(Player player, PacketEntity entity, PropertyHolder prope
sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(),
npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d())));
sendAllMetadata(player, entity, properties);
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class)));
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.util.NamedColor;
import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
Expand All @@ -28,7 +28,7 @@ public V1_20_2PacketFactory(TaskScheduler scheduler, PacketEventsAPI<Plugin> pac
@Override
public void spawnPlayer(Player player, PacketEntity entity, PropertyHolder properties) {
addTabPlayer(player, entity, properties).thenAccept(ignored -> {
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class)));
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));
NpcLocation location = entity.getLocation();
sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(),
npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.skin.BaseSkinDescriptor;
import lol.pyr.znpcsplus.util.NamedColor;
import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand Down Expand Up @@ -48,7 +49,7 @@ public V1_8PacketFactory(TaskScheduler scheduler, PacketEventsAPI<Plugin> packet
@Override
public void spawnPlayer(Player player, PacketEntity entity, PropertyHolder properties) {
addTabPlayer(player, entity, properties).thenAccept(ignored -> {
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class)));
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));
NpcLocation location = entity.getLocation();
sendPacket(player, new WrapperPlayServerSpawnPlayer(entity.getEntityId(),
entity.getUuid(), npcLocationToVector(location), location.getYaw(), location.getPitch(), Collections.emptyList()));
Expand All @@ -69,7 +70,7 @@ public void spawnEntity(Player player, PacketEntity entity, PropertyHolder prope
new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(), npcLocationToVector(location),
location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.empty()));
sendAllMetadata(player, entity, properties);
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class)));
createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedColor.class)));
}

protected Vector3d npcLocationToVector(NpcLocation location) {
Expand Down Expand Up @@ -111,12 +112,12 @@ public void removeTabPlayer(Player player, PacketEntity entity) {
}

@Override
public void createTeam(Player player, PacketEntity entity, NamedTextColor glowColor) {
public void createTeam(Player player, PacketEntity entity, NamedColor namedColor) {
sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.CREATE, new WrapperPlayServerTeams.ScoreBoardTeamInfo(
Component.empty(), Component.empty(), Component.empty(),
WrapperPlayServerTeams.NameTagVisibility.NEVER,
WrapperPlayServerTeams.CollisionRule.NEVER,
glowColor == null ? NamedTextColor.WHITE : glowColor,
namedColor == null ? NamedTextColor.WHITE : NamedTextColor.NAMES.value(namedColor.name().toLowerCase()),
WrapperPlayServerTeams.OptionData.NONE
)));
sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.ADD_ENTITIES, (WrapperPlayServerTeams.ScoreBoardTeamInfo) null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.NamedColor;
import java.util.Deque;

public class NamedColorParser extends ParserType<NamedColor> {
public NamedColorParser(Message<CommandContext> message) {
super(message);
}

@Override
public NamedColor parse(Deque<String> deque) throws CommandExecutionException {
try {
return NamedColor.valueOf(deque.pop());
} catch (IllegalArgumentException exception) {
throw new CommandExecutionException();
}
}
}

This file was deleted.