fix some bugs, make holograms work

This commit is contained in:
Pyrbu 2023-04-26 18:50:40 +01:00
parent 63c72fe04b
commit 2372748ddb
10 changed files with 101 additions and 27 deletions

@ -3,13 +3,10 @@ package lol.pyr.znpcsplus;
import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerPriority; import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; 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.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
import io.github.znetworkw.znpcservers.listeners.InventoryListener; import io.github.znetworkw.znpcservers.listeners.InventoryListener;
import io.github.znetworkw.znpcservers.utility.BungeeUtils; import io.github.znetworkw.znpcservers.utility.BungeeUtils;
import io.github.znetworkw.znpcservers.utility.SchedulerUtils; 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.config.Configs;
import lol.pyr.znpcsplus.entity.EntityProperty; import lol.pyr.znpcsplus.entity.EntityProperty;
import lol.pyr.znpcsplus.entity.PacketLocation; 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.User;
import lol.pyr.znpcsplus.user.UserListener; import lol.pyr.znpcsplus.user.UserListener;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; 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.format.NamedTextColor;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
@ -45,11 +42,6 @@ public class ZNPCsPlus extends JavaPlugin {
public static Logger LOGGER; public static Logger LOGGER;
public static File PLUGIN_FOLDER; public static File PLUGIN_FOLDER;
public static File PATH_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; private static final int PLUGIN_ID = 18244;
public static SchedulerUtils SCHEDULER; public static SchedulerUtils SCHEDULER;
public static BungeeUtils BUNGEE_UTILS; public static BungeeUtils BUNGEE_UTILS;
@ -58,6 +50,11 @@ public class ZNPCsPlus extends JavaPlugin {
private boolean enabled = false; private boolean enabled = false;
public static void debug(String str) {
if (!Configs.config().debugEnabled()) return;
LOGGER.info("[DEBUG] " + str);
}
@Override @Override
public void onLoad() { public void onLoad() {
PacketEvents.setAPI(SpigotPacketEventsBuilder.build(this)); PacketEvents.setAPI(SpigotPacketEventsBuilder.build(this));
@ -144,7 +141,8 @@ public class ZNPCsPlus extends JavaPlugin {
npc.setProperty(EntityProperty.INVISIBLE, true); npc.setProperty(EntityProperty.INVISIBLE, true);
} }
npc.setProperty(EntityProperty.GLOW, NamedTextColor.RED); 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); NPCRegistry.register("debug_npc" + (z * wrap + x), npc);
if (x++ > wrap) { if (x++ > wrap) {
x = 0; x = 0;

@ -49,6 +49,10 @@ public class PacketLocation {
return new Location(world, this.x, this.y, this.z, this.yaw, this.pitch); 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() { public Vector toVector() {
return new Vector(x, y, z); return new Vector(x, y, z);
} }

@ -1,29 +1,50 @@
package lol.pyr.znpcsplus.hologram; package lol.pyr.znpcsplus.hologram;
import lol.pyr.znpcsplus.config.Configs;
import lol.pyr.znpcsplus.entity.PacketLocation;
import lol.pyr.znpcsplus.util.Viewable; import lol.pyr.znpcsplus.util.Viewable;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
public class Hologram extends Viewable { public class Hologram extends Viewable {
private PacketLocation location;
private final List<HologramLine> lines = new ArrayList<>(); private final List<HologramLine> lines = new ArrayList<>();
public Hologram() { public Hologram(PacketLocation location) {
this.location = location;
} }
public void addLine(Component line) { 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) { public HologramLine getLine(int index) {
return lines.get(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) { 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 @Override
@ -35,4 +56,22 @@ public class Hologram extends Viewable {
protected void _hide(Player player) { protected void _hide(Player player) {
for (HologramLine line : lines) line.hide(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;
}
}
} }

@ -44,7 +44,7 @@ public class HologramLine implements PropertyHolder {
public <T> T getProperty(EntityProperty<T> key) { public <T> T getProperty(EntityProperty<T> key) {
if (key == EntityProperty.INVISIBLE) return (T) Boolean.TRUE; if (key == EntityProperty.INVISIBLE) return (T) Boolean.TRUE;
if (key == EntityProperty.NAME) return (T) text; if (key == EntityProperty.NAME) return (T) text;
return null; return key.getDefaultValue();
} }
@Override @Override

@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.metadata;
import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import lol.pyr.znpcsplus.ZNPCsPlus;
import lol.pyr.znpcsplus.util.LazyLoader; import lol.pyr.znpcsplus.util.LazyLoader;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -40,7 +41,9 @@ public interface MetadataFactory {
for (ServerVersion v : ServerVersion.reversedValues()) { for (ServerVersion v : ServerVersion.reversedValues()) {
if (v.isNewerThan(version)) continue; if (v.isNewerThan(version)) continue;
if (!factories.containsKey(v)) 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!"); throw new RuntimeException("Unsupported version!");
} }
@ -49,6 +52,7 @@ public interface MetadataFactory {
HashMap<ServerVersion, LazyLoader<? extends MetadataFactory>> map = new HashMap<>(); HashMap<ServerVersion, LazyLoader<? extends MetadataFactory>> map = new HashMap<>();
map.put(ServerVersion.V_1_8, LazyLoader.of(V1_8Factory::new)); 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_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_14, LazyLoader.of(V1_14Factory::new));
map.put(ServerVersion.V_1_16, LazyLoader.of(V1_16Factory::new)); map.put(ServerVersion.V_1_16, LazyLoader.of(V1_16Factory::new));
map.put(ServerVersion.V_1_17, LazyLoader.of(V1_17Factory::new)); map.put(ServerVersion.V_1_17, LazyLoader.of(V1_17Factory::new));

@ -7,12 +7,13 @@ import net.kyori.adventure.text.Component;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional;
public class V1_13Factory extends V1_8Factory { public class V1_13Factory extends V1_9Factory {
@Override @Override
public Collection<EntityData> name(Component name) { public Collection<EntityData> name(Component name) {
return List.of( 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) new EntityData(3, EntityDataTypes.BOOLEAN, true)
); );
} }

@ -16,7 +16,7 @@ public class V1_8Factory implements MetadataFactory {
@Override @Override
public EntityData effects(boolean onFire, boolean glowing, boolean invisible) { 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 @Override

@ -4,6 +4,7 @@ import lol.pyr.znpcsplus.entity.EntityProperty;
import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.entity.PacketLocation; import lol.pyr.znpcsplus.entity.PacketLocation;
import lol.pyr.znpcsplus.entity.PropertyHolder; import lol.pyr.znpcsplus.entity.PropertyHolder;
import lol.pyr.znpcsplus.hologram.Hologram;
import lol.pyr.znpcsplus.interaction.NPCAction; import lol.pyr.znpcsplus.interaction.NPCAction;
import lol.pyr.znpcsplus.util.Viewable; import lol.pyr.znpcsplus.util.Viewable;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -20,6 +21,7 @@ public class NPC extends Viewable implements PropertyHolder {
private PacketEntity entity; private PacketEntity entity;
private PacketLocation location; private PacketLocation location;
private NPCType type; private NPCType type;
private final Hologram hologram;
private final Map<EntityProperty<?>, Object> propertyMap = new HashMap<>(); private final Map<EntityProperty<?>, Object> propertyMap = new HashMap<>();
private final Set<NPCAction> actions = new HashSet<>(); private final Set<NPCAction> actions = new HashSet<>();
@ -29,7 +31,7 @@ public class NPC extends Viewable implements PropertyHolder {
this.type = type; this.type = type;
this.location = location; this.location = location;
entity = new PacketEntity(this, type.getType(), location); entity = new PacketEntity(this, type.getType(), location);
hologram = new Hologram(location.withY(location.getY() + type.getHologramOffset()));
_ALL_NPCS.add(this); _ALL_NPCS.add(this);
} }
@ -55,6 +57,11 @@ public class NPC extends Viewable implements PropertyHolder {
public void setLocation(PacketLocation location) { public void setLocation(PacketLocation location) {
this.location = location; this.location = location;
entity.setLocation(location, viewers); entity.setLocation(location, viewers);
hologram.setLocation(location.withY(location.getY() + type.getHologramOffset()));
}
public Hologram getHologram() {
return hologram;
} }
public World getWorld() { public World getWorld() {
@ -74,11 +81,13 @@ public class NPC extends Viewable implements PropertyHolder {
@Override @Override
protected void _show(Player player) { protected void _show(Player player) {
entity.spawn(player); entity.spawn(player);
hologram.show(player);
} }
@Override @Override
protected void _hide(Player player) { protected void _hide(Player player) {
entity.despawn(player); entity.despawn(player);
hologram.hide(player);
} }
private void _refreshMeta() { private void _refreshMeta() {

@ -22,10 +22,12 @@ public class NPCType {
private final EntityType type; private final EntityType type;
private final Set<EntityProperty<?>> allowedProperties; private final Set<EntityProperty<?>> allowedProperties;
private final String name; private final String name;
private final double hologramOffset;
private NPCType(String name, EntityType type, Set<EntityProperty<?>> allowedProperties) { private NPCType(String name, EntityType type, double hologramOffset, Set<EntityProperty<?>> allowedProperties) {
this.name = name.toUpperCase(); this.name = name.toUpperCase();
this.type = type; this.type = type;
this.hologramOffset = hologramOffset;
this.allowedProperties = allowedProperties; this.allowedProperties = allowedProperties;
} }
@ -37,6 +39,10 @@ public class NPCType {
return type; return type;
} }
public double getHologramOffset() {
return hologramOffset;
}
public Set<EntityProperty<?>> getAllowedProperties() { public Set<EntityProperty<?>> getAllowedProperties() {
return allowedProperties; return allowedProperties;
} }
@ -52,10 +58,14 @@ public class NPCType {
static { static {
register(new Builder("player", EntityTypes.PLAYER) register(new Builder("player", EntityTypes.PLAYER)
.addProperties(EntityProperty.SKIN, EntityProperty.SKIN_LAYERS)); .addProperties(EntityProperty.SKIN, EntityProperty.SKIN_LAYERS)
register(new Builder("creeper", EntityTypes.CREEPER)); .setHologramOffset(-0.45D));
register(new Builder("zombie", EntityTypes.ZOMBIE)); register(new Builder("creeper", EntityTypes.CREEPER)
register(new Builder("skeleton", EntityTypes.SKELETON)); .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 { private static final class Builder {
@ -63,6 +73,7 @@ public class NPCType {
private final EntityType type; private final EntityType type;
private final List<EntityProperty<?>> allowedProperties = new ArrayList<>(); private final List<EntityProperty<?>> allowedProperties = new ArrayList<>();
private boolean globalProperties = true; private boolean globalProperties = true;
private double hologramOffset = 0;
private Builder(String name, EntityType type) { private Builder(String name, EntityType type) {
this.name = name; this.name = name;
@ -79,6 +90,11 @@ public class NPCType {
return this; return this;
} }
public Builder setHologramOffset(double hologramOffset) {
this.hologramOffset = hologramOffset;
return this;
}
public NPCType build() { public NPCType build() {
if (globalProperties) { if (globalProperties) {
allowedProperties.add(EntityProperty.FIRE); allowedProperties.add(EntityProperty.FIRE);
@ -87,7 +103,7 @@ public class NPCType {
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9)) if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9))
allowedProperties.add(EntityProperty.GLOW); allowedProperties.add(EntityProperty.GLOW);
} }
return new NPCType(name, type, Set.copyOf(allowedProperties)); return new NPCType(name, type, hologramOffset, Set.copyOf(allowedProperties));
} }
} }
} }

@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.packets;
import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData; 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.PacketEntity;
import lol.pyr.znpcsplus.entity.PropertyHolder; import lol.pyr.znpcsplus.entity.PropertyHolder;
import lol.pyr.znpcsplus.util.LazyLoader; import lol.pyr.znpcsplus.util.LazyLoader;
@ -35,7 +36,9 @@ public interface PacketFactory {
for (ServerVersion v : ServerVersion.reversedValues()) { for (ServerVersion v : ServerVersion.reversedValues()) {
if (v.isNewerThan(version)) continue; if (v.isNewerThan(version)) continue;
if (!factories.containsKey(v)) 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!"); throw new RuntimeException("Unsupported version!");
} }