From da058f230c5704824f69642a02bd9cffa8d62835 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 26 Apr 2023 18:50:40 +0100 Subject: [PATCH] fix some bugs, make holograms work --- .../java/lol/pyr/znpcsplus/ZNPCsPlus.java | 18 ++++--- .../pyr/znpcsplus/entity/PacketLocation.java | 4 ++ .../lol/pyr/znpcsplus/hologram/Hologram.java | 47 +++++++++++++++++-- .../pyr/znpcsplus/hologram/HologramLine.java | 2 +- .../znpcsplus/metadata/MetadataFactory.java | 6 ++- .../pyr/znpcsplus/metadata/V1_13Factory.java | 5 +- .../pyr/znpcsplus/metadata/V1_8Factory.java | 2 +- src/main/java/lol/pyr/znpcsplus/npc/NPC.java | 11 ++++- .../java/lol/pyr/znpcsplus/npc/NPCType.java | 28 ++++++++--- .../pyr/znpcsplus/packets/PacketFactory.java | 5 +- 10 files changed, 101 insertions(+), 27 deletions(-) diff --git a/src/main/java/lol/pyr/znpcsplus/ZNPCsPlus.java b/src/main/java/lol/pyr/znpcsplus/ZNPCsPlus.java index 58d850c..e77680a 100644 --- a/src/main/java/lol/pyr/znpcsplus/ZNPCsPlus.java +++ b/src/main/java/lol/pyr/znpcsplus/ZNPCsPlus.java @@ -3,13 +3,10 @@ package lol.pyr.znpcsplus; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.event.PacketListenerPriority; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder; import io.github.znetworkw.znpcservers.listeners.InventoryListener; import io.github.znetworkw.znpcservers.utility.BungeeUtils; import io.github.znetworkw.znpcservers.utility.SchedulerUtils; -import io.github.znetworkw.znpcservers.utility.itemstack.ItemStackSerializer; import lol.pyr.znpcsplus.config.Configs; import lol.pyr.znpcsplus.entity.EntityProperty; import lol.pyr.znpcsplus.entity.PacketLocation; @@ -28,13 +25,13 @@ import lol.pyr.znpcsplus.updater.UpdateNotificationListener; import lol.pyr.znpcsplus.user.User; import lol.pyr.znpcsplus.user.UserListener; import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.io.FileUtils; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; -import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -45,11 +42,6 @@ public class ZNPCsPlus extends JavaPlugin { public static Logger LOGGER; public static File PLUGIN_FOLDER; public static File PATH_FOLDER; - public static final Gson GSON = new GsonBuilder() - .registerTypeHierarchyAdapter(ItemStack.class, new ItemStackSerializer()) - .setPrettyPrinting() - .disableHtmlEscaping() - .create(); private static final int PLUGIN_ID = 18244; public static SchedulerUtils SCHEDULER; public static BungeeUtils BUNGEE_UTILS; @@ -58,6 +50,11 @@ public class ZNPCsPlus extends JavaPlugin { private boolean enabled = false; + public static void debug(String str) { + if (!Configs.config().debugEnabled()) return; + LOGGER.info("[DEBUG] " + str); + } + @Override public void onLoad() { PacketEvents.setAPI(SpigotPacketEventsBuilder.build(this)); @@ -144,7 +141,8 @@ public class ZNPCsPlus extends JavaPlugin { npc.setProperty(EntityProperty.INVISIBLE, true); } npc.setProperty(EntityProperty.GLOW, NamedTextColor.RED); - npc.setProperty(EntityProperty.FIRE, true); + // npc.setProperty(EntityProperty.FIRE, true); + npc.getHologram().addLine(Component.text("Hello, World!")); NPCRegistry.register("debug_npc" + (z * wrap + x), npc); if (x++ > wrap) { x = 0; diff --git a/src/main/java/lol/pyr/znpcsplus/entity/PacketLocation.java b/src/main/java/lol/pyr/znpcsplus/entity/PacketLocation.java index 631442b..bbc6b17 100644 --- a/src/main/java/lol/pyr/znpcsplus/entity/PacketLocation.java +++ b/src/main/java/lol/pyr/znpcsplus/entity/PacketLocation.java @@ -49,6 +49,10 @@ public class PacketLocation { return new Location(world, this.x, this.y, this.z, this.yaw, this.pitch); } + public PacketLocation withY(double y) { + return new PacketLocation(x, y, z, yaw, pitch); + } + public Vector toVector() { return new Vector(x, y, z); } diff --git a/src/main/java/lol/pyr/znpcsplus/hologram/Hologram.java b/src/main/java/lol/pyr/znpcsplus/hologram/Hologram.java index 79a6e51..e1e250b 100644 --- a/src/main/java/lol/pyr/znpcsplus/hologram/Hologram.java +++ b/src/main/java/lol/pyr/znpcsplus/hologram/Hologram.java @@ -1,29 +1,50 @@ package lol.pyr.znpcsplus.hologram; +import lol.pyr.znpcsplus.config.Configs; +import lol.pyr.znpcsplus.entity.PacketLocation; import lol.pyr.znpcsplus.util.Viewable; import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; +import java.util.Set; public class Hologram extends Viewable { + private PacketLocation location; private final List lines = new ArrayList<>(); - public Hologram() { - + public Hologram(PacketLocation location) { + this.location = location; } public void addLine(Component line) { - lines.add(new HologramLine(null, line)); // TODO: Location + HologramLine newLine = new HologramLine(null, line); + lines.add(newLine); + relocateLines(); + for (Player viewer : getViewers()) newLine.show(viewer.getPlayer()); } public HologramLine getLine(int index) { return lines.get(index); } + public void removeLine(int index) { + HologramLine line = lines.remove(index); + for (Player viewer : getViewers()) line.hide(viewer); + relocateLines(); + } + + public void clearLines() { + UNSAFE_hideAll(); + lines.clear(); + } + public void insertLine(int index, Component line) { - lines.add(index, new HologramLine(null, line)); // TODO: Location + HologramLine newLine = new HologramLine(null, line); + lines.add(index, newLine); + relocateLines(); + for (Player viewer : getViewers()) newLine.show(viewer.getPlayer()); } @Override @@ -35,4 +56,22 @@ public class Hologram extends Viewable { protected void _hide(Player player) { for (HologramLine line : lines) line.hide(player); } + + public void setLocation(PacketLocation location) { + this.location = location; + relocateLines(); + } + + private void relocateLines() { + relocateLines(null); + } + + private void relocateLines(HologramLine newLine) { + final double lineSpacing = Configs.config().lineSpacing(); + double height = location.getY() + lines.size() * lineSpacing; + for (HologramLine line : lines) { + line.setLocation(location.withY(height), line == newLine ? Set.of() : getViewers()); + height -= lineSpacing; + } + } } diff --git a/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java b/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java index eeb48e3..b98d94e 100644 --- a/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java +++ b/src/main/java/lol/pyr/znpcsplus/hologram/HologramLine.java @@ -44,7 +44,7 @@ public class HologramLine implements PropertyHolder { public T getProperty(EntityProperty key) { if (key == EntityProperty.INVISIBLE) return (T) Boolean.TRUE; if (key == EntityProperty.NAME) return (T) text; - return null; + return key.getDefaultValue(); } @Override diff --git a/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java b/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java index c49b978..95abc45 100644 --- a/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java +++ b/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java @@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.metadata; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import lol.pyr.znpcsplus.ZNPCsPlus; import lol.pyr.znpcsplus.util.LazyLoader; import net.kyori.adventure.text.Component; @@ -40,7 +41,9 @@ public interface MetadataFactory { for (ServerVersion v : ServerVersion.reversedValues()) { if (v.isNewerThan(version)) continue; if (!factories.containsKey(v)) continue; - return factories.get(v).get(); + MetadataFactory f = factories.get(v).get(); + ZNPCsPlus.debug("Using MetadataFactory Version " + v.name() + " (" + f.getClass().getName() + ")"); + return f; } throw new RuntimeException("Unsupported version!"); } @@ -49,6 +52,7 @@ public interface MetadataFactory { HashMap> map = new HashMap<>(); map.put(ServerVersion.V_1_8, LazyLoader.of(V1_8Factory::new)); map.put(ServerVersion.V_1_9, LazyLoader.of(V1_9Factory::new)); + map.put(ServerVersion.V_1_13, LazyLoader.of(V1_13Factory::new)); map.put(ServerVersion.V_1_14, LazyLoader.of(V1_14Factory::new)); map.put(ServerVersion.V_1_16, LazyLoader.of(V1_16Factory::new)); map.put(ServerVersion.V_1_17, LazyLoader.of(V1_17Factory::new)); diff --git a/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java b/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java index 3e45cf5..0f1d89c 100644 --- a/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java +++ b/src/main/java/lol/pyr/znpcsplus/metadata/V1_13Factory.java @@ -7,12 +7,13 @@ import net.kyori.adventure.text.Component; import java.util.Collection; import java.util.List; +import java.util.Optional; -public class V1_13Factory extends V1_8Factory { +public class V1_13Factory extends V1_9Factory { @Override public Collection name(Component name) { return List.of( - new EntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, AdventureSerializer.getGsonSerializer().serialize(name)), + new EntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, Optional.of(AdventureSerializer.getGsonSerializer().serialize(name))), new EntityData(3, EntityDataTypes.BOOLEAN, true) ); } diff --git a/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java b/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java index a669d85..f170001 100644 --- a/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java +++ b/src/main/java/lol/pyr/znpcsplus/metadata/V1_8Factory.java @@ -16,7 +16,7 @@ public class V1_8Factory implements MetadataFactory { @Override public EntityData effects(boolean onFire, boolean glowing, boolean invisible) { - return new EntityData(0, EntityDataTypes.BYTE, (onFire ? 0x01 : 0) | (invisible ? 0x20 : 0)); + return new EntityData(0, EntityDataTypes.BYTE, (byte) ((onFire ? 0x01 : 0) | (invisible ? 0x20 : 0))); } @Override diff --git a/src/main/java/lol/pyr/znpcsplus/npc/NPC.java b/src/main/java/lol/pyr/znpcsplus/npc/NPC.java index befc4c9..4f4d038 100644 --- a/src/main/java/lol/pyr/znpcsplus/npc/NPC.java +++ b/src/main/java/lol/pyr/znpcsplus/npc/NPC.java @@ -4,6 +4,7 @@ import lol.pyr.znpcsplus.entity.EntityProperty; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.entity.PacketLocation; import lol.pyr.znpcsplus.entity.PropertyHolder; +import lol.pyr.znpcsplus.hologram.Hologram; import lol.pyr.znpcsplus.interaction.NPCAction; import lol.pyr.znpcsplus.util.Viewable; import org.bukkit.Bukkit; @@ -20,6 +21,7 @@ public class NPC extends Viewable implements PropertyHolder { private PacketEntity entity; private PacketLocation location; private NPCType type; + private final Hologram hologram; private final Map, Object> propertyMap = new HashMap<>(); private final Set actions = new HashSet<>(); @@ -29,7 +31,7 @@ public class NPC extends Viewable implements PropertyHolder { this.type = type; this.location = location; entity = new PacketEntity(this, type.getType(), location); - + hologram = new Hologram(location.withY(location.getY() + type.getHologramOffset())); _ALL_NPCS.add(this); } @@ -55,6 +57,11 @@ public class NPC extends Viewable implements PropertyHolder { public void setLocation(PacketLocation location) { this.location = location; entity.setLocation(location, viewers); + hologram.setLocation(location.withY(location.getY() + type.getHologramOffset())); + } + + public Hologram getHologram() { + return hologram; } public World getWorld() { @@ -74,11 +81,13 @@ public class NPC extends Viewable implements PropertyHolder { @Override protected void _show(Player player) { entity.spawn(player); + hologram.show(player); } @Override protected void _hide(Player player) { entity.despawn(player); + hologram.hide(player); } private void _refreshMeta() { diff --git a/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java b/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java index 8fcaa86..0ca1b2c 100644 --- a/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java +++ b/src/main/java/lol/pyr/znpcsplus/npc/NPCType.java @@ -22,10 +22,12 @@ public class NPCType { private final EntityType type; private final Set> allowedProperties; private final String name; + private final double hologramOffset; - private NPCType(String name, EntityType type, Set> allowedProperties) { + private NPCType(String name, EntityType type, double hologramOffset, Set> allowedProperties) { this.name = name.toUpperCase(); this.type = type; + this.hologramOffset = hologramOffset; this.allowedProperties = allowedProperties; } @@ -37,6 +39,10 @@ public class NPCType { return type; } + public double getHologramOffset() { + return hologramOffset; + } + public Set> getAllowedProperties() { return allowedProperties; } @@ -52,10 +58,14 @@ public class NPCType { static { register(new Builder("player", EntityTypes.PLAYER) - .addProperties(EntityProperty.SKIN, EntityProperty.SKIN_LAYERS)); - register(new Builder("creeper", EntityTypes.CREEPER)); - register(new Builder("zombie", EntityTypes.ZOMBIE)); - register(new Builder("skeleton", EntityTypes.SKELETON)); + .addProperties(EntityProperty.SKIN, EntityProperty.SKIN_LAYERS) + .setHologramOffset(-0.45D)); + register(new Builder("creeper", EntityTypes.CREEPER) + .setHologramOffset(-0.6D)); + register(new Builder("zombie", EntityTypes.ZOMBIE) + .setHologramOffset(-0.3D)); + register(new Builder("skeleton", EntityTypes.SKELETON) + .setHologramOffset(-0.3D)); } private static final class Builder { @@ -63,6 +73,7 @@ public class NPCType { private final EntityType type; private final List> allowedProperties = new ArrayList<>(); private boolean globalProperties = true; + private double hologramOffset = 0; private Builder(String name, EntityType type) { this.name = name; @@ -79,6 +90,11 @@ public class NPCType { return this; } + public Builder setHologramOffset(double hologramOffset) { + this.hologramOffset = hologramOffset; + return this; + } + public NPCType build() { if (globalProperties) { allowedProperties.add(EntityProperty.FIRE); @@ -87,7 +103,7 @@ public class NPCType { if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9)) allowedProperties.add(EntityProperty.GLOW); } - return new NPCType(name, type, Set.copyOf(allowedProperties)); + return new NPCType(name, type, hologramOffset, Set.copyOf(allowedProperties)); } } } diff --git a/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java b/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java index fc151fa..58eb9e5 100644 --- a/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java +++ b/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java @@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.packets; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import lol.pyr.znpcsplus.ZNPCsPlus; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.entity.PropertyHolder; import lol.pyr.znpcsplus.util.LazyLoader; @@ -35,7 +36,9 @@ public interface PacketFactory { for (ServerVersion v : ServerVersion.reversedValues()) { if (v.isNewerThan(version)) continue; if (!factories.containsKey(v)) continue; - return factories.get(v).get(); + PacketFactory f = factories.get(v).get(); + ZNPCsPlus.debug("Using PacketFactory Version " + v.name() + " (" + f.getClass().getName() + ")"); + return f; } throw new RuntimeException("Unsupported version!"); }