From c5bf8dea0b0cb77349311a64162c8cdd7583d844 Mon Sep 17 00:00:00 2001 From: Dablakbandit Date: Sat, 14 Dec 2024 21:49:21 +1300 Subject: [PATCH] Add passengers to npc api Add passengers to npc api & implementation Add jetbrains annotations to api to tag with nullables --- api/build.gradle | 4 ++ .../java/lol/pyr/znpcsplus/api/npc/Npc.java | 22 +++++++++++ .../pyr/znpcsplus/entity/PacketEntity.java | 38 +++++++++++++++++-- .../switchserver/SwitchServerAction.java | 2 +- .../java/lol/pyr/znpcsplus/npc/NpcImpl.java | 15 ++++++++ .../pyr/znpcsplus/packets/PacketFactory.java | 2 +- .../znpcsplus/packets/V1_8PacketFactory.java | 5 +-- 7 files changed, 80 insertions(+), 8 deletions(-) diff --git a/api/build.gradle b/api/build.gradle index 349cf17..381069e 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -8,6 +8,10 @@ java { withJavadocJar() } +dependencies { + compileOnly 'org.jetbrains:annotations:26.0.1' +} + publishing { publications { mavenJava(MavenPublication) { diff --git a/api/src/main/java/lol/pyr/znpcsplus/api/npc/Npc.java b/api/src/main/java/lol/pyr/znpcsplus/api/npc/Npc.java index 6081060..9302b4b 100644 --- a/api/src/main/java/lol/pyr/znpcsplus/api/npc/Npc.java +++ b/api/src/main/java/lol/pyr/znpcsplus/api/npc/Npc.java @@ -6,6 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionAction; import lol.pyr.znpcsplus.util.NpcLocation; import org.bukkit.World; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Set; @@ -174,4 +175,25 @@ public interface Npc extends PropertyHolder { * @param offHand Should the hand be the offhand */ void swingHand(boolean offHand); + + /** + * Gets the passengers of this npc + * @return The list of entity ids of the passengers + */ + + @Nullable List getPassengers(); + + /** + * Adds a passenger to this npc + * @param entityId The entity id of the passenger to add + */ + void addPassenger(int entityId); + + /** + * Removes a passenger from this npc + * @param entityId The entity id of the passenger to remove + */ + void removePassenger(int entityId); + + } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java index 255a201..8b2fe03 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/PacketEntity.java @@ -13,8 +13,11 @@ import lol.pyr.znpcsplus.util.Viewable; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import java.util.UUID; +import java.util.stream.IntStream; public class PacketEntity implements PropertyHolder { private final PacketFactory packetFactory; @@ -28,6 +31,7 @@ public class PacketEntity implements PropertyHolder { private NpcLocation location; private PacketEntity vehicle; + private List passengers; public PacketEntity(PacketFactory packetFactory, PropertyHolder properties, Viewable viewable, EntityType type, NpcLocation location) { this.packetFactory = packetFactory; @@ -69,7 +73,10 @@ public class PacketEntity implements PropertyHolder { else packetFactory.spawnEntity(player, this, properties); if (vehicle != null) { vehicle.spawn(player); - packetFactory.setPassenger(player, vehicle, this); + packetFactory.setPassengers(player, vehicle.getEntityId(), this.getEntityId()); + } + if(passengers != null) { + packetFactory.setPassengers(player, this.getEntityId(), passengers.stream().mapToInt(Integer::intValue).toArray()); } } @@ -89,7 +96,7 @@ public class PacketEntity implements PropertyHolder { // remove old vehicle if (this.vehicle != null) { for (Player player : viewable.getViewers()) { - packetFactory.setPassenger(player, this.vehicle, null); + packetFactory.setPassengers(player, this.vehicle.getEntityId()); this.vehicle.despawn(player); packetFactory.teleportEntity(player, this); } @@ -101,7 +108,32 @@ public class PacketEntity implements PropertyHolder { vehicle.setLocation(location.withY(location.getY() - 0.9)); for (Player player : viewable.getViewers()) { vehicle.spawn(player); - packetFactory.setPassenger(player, vehicle, this); + packetFactory.setPassengers(player, vehicle.getEntityId(), this.getEntityId()); + } + } + + public List getPassengers() { + return passengers; + } + + public void addPassenger(int entityId) { + if (passengers == null) { + passengers = new ArrayList<>(); + } + passengers.add(entityId); + for (Player player : viewable.getViewers()) { + packetFactory.setPassengers(player, this.getEntityId(), passengers.stream().mapToInt(Integer::intValue).toArray()); + } + } + + public void removePassenger(int entityId) { + if (passengers == null) return; + passengers.remove(entityId); + for (Player player : viewable.getViewers()) { + packetFactory.setPassengers(player, this.getEntityId(), passengers.stream().mapToInt(Integer::intValue).toArray()); + } + if (passengers.isEmpty()) { + passengers = null; } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java index 94fc807..ca89839 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java @@ -32,7 +32,7 @@ public class SwitchServerAction extends InteractionActionImpl { .hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("Click to edit this action", NamedTextColor.GRAY))) .clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, - "/" + context.getLabel() + " action edit " + id + " " + index + " switcserver " + getInteractionType().name() + " " + getCooldown()/1000 + " " + getDelay() + " " + server)) + "/" + context.getLabel() + " action edit " + id + " " + index + " switchserver " + getInteractionType().name() + " " + getCooldown()/1000 + " " + getDelay() + " " + server)) .append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text("[DELETE]", NamedTextColor.RED) .hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java index e29948a..39c4c4a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -246,4 +246,19 @@ public class NpcImpl extends Viewable implements Npc { public void swingHand(boolean offHand) { for (Player viewer : getViewers()) entity.swingHand(viewer, offHand); } + + @Override + public @Nullable List getPassengers() { + return entity.getPassengers(); + } + + @Override + public void addPassenger(int entityId) { + entity.addPassenger(entityId); + } + + @Override + public void removePassenger(int entityId) { + entity.removePassenger(entityId); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java index 824ef31..aaea42a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java @@ -24,5 +24,5 @@ public interface PacketFactory { void sendMetadata(Player player, PacketEntity entity, List data); void sendHeadRotation(Player player, PacketEntity entity, float yaw, float pitch); void sendHandSwing(Player player, PacketEntity entity, boolean offHand); - void setPassenger(Player player, PacketEntity vehicle, PacketEntity passenger); + void setPassengers(Player player, int vehicle, int... passengers); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java index e1134f8..2637b83 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java @@ -154,9 +154,8 @@ public class V1_8PacketFactory implements PacketFactory { } @Override - public void setPassenger(Player player, PacketEntity vehicle, PacketEntity passenger) { - sendPacket(player, new WrapperPlayServerSetPassengers(vehicle.getEntityId(), - passenger == null ? new int[] {} : new int[] {passenger.getEntityId()})); + public void setPassengers(Player player, int vehicleEntityId, int... passengers) { + sendPacket(player, new WrapperPlayServerSetPassengers(vehicleEntityId, passengers)); } protected void sendPacket(Player player, PacketWrapper packet) {