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..d5e70bb 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,35 @@ 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); + + /** + * Gets the vehicle entity id of this npc + * @return The entity id of the vehicle + */ + @Nullable Integer getVehicleId(); + + /** + * Sets the vehicle id of this npc + * @param vehicleId The entity id of the vehicle + */ + void setVehicleId(Integer vehicleId); } diff --git a/build.gradle b/build.gradle index f3ef9f5..1ec5bfc 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,7 @@ subprojects { } dependencies { + compileOnly "org.jetbrains:annotations:26.0.1" compileOnly "org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT" } 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..dc06d00 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,7 @@ import lol.pyr.znpcsplus.util.Viewable; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.util.Set; -import java.util.UUID; +import java.util.*; public class PacketEntity implements PropertyHolder { private final PacketFactory packetFactory; @@ -28,6 +27,8 @@ public class PacketEntity implements PropertyHolder { private NpcLocation location; private PacketEntity vehicle; + private Integer vehicleId; + private List passengers; public PacketEntity(PacketFactory packetFactory, PropertyHolder properties, Viewable viewable, EntityType type, NpcLocation location) { this.packetFactory = packetFactory; @@ -67,9 +68,11 @@ public class PacketEntity implements PropertyHolder { public void spawn(Player player) { if (type == EntityTypes.PLAYER) packetFactory.spawnPlayer(player, this, properties); else packetFactory.spawnEntity(player, this, properties); - if (vehicle != null) { - vehicle.spawn(player); - packetFactory.setPassenger(player, vehicle, this); + if (vehicleId != null) { + packetFactory.setPassengers(player, vehicleId, this.getEntityId()); + } + if (passengers != null) { + packetFactory.setPassengers(player, this.getEntityId(), passengers.stream().mapToInt(Integer::intValue).toArray()); } } @@ -85,14 +88,38 @@ public class PacketEntity implements PropertyHolder { return viewable; } + public void setVehicleId(Integer vehicleId) { + if (this.vehicle != null) { + for (Player player : viewable.getViewers()) { + packetFactory.setPassengers(player, this.vehicle.getEntityId()); + this.vehicle.despawn(player); + packetFactory.teleportEntity(player, this); + } + } else if (this.vehicleId != null) { + for (Player player : viewable.getViewers()) { + packetFactory.setPassengers(player, this.vehicleId); + } + } + this.vehicleId = vehicleId; + if (vehicleId == null) return; + + for (Player player : viewable.getViewers()) { + packetFactory.setPassengers(player, this.getEntityId(), vehicleId); + } + } + public void setVehicle(PacketEntity vehicle) { // 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); } + } else if (this.vehicleId != null) { + for (Player player : viewable.getViewers()) { + packetFactory.setPassengers(player, this.vehicleId); + } } this.vehicle = vehicle; @@ -101,7 +128,36 @@ 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 Integer getVehicleId() { + return vehicleId; + } + + public List getPassengers() { + return passengers == null ? Collections.emptyList() : 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..ad0061f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -20,6 +20,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -246,4 +247,29 @@ public class NpcImpl extends Viewable implements Npc { public void swingHand(boolean offHand) { for (Player viewer : getViewers()) entity.swingHand(viewer, offHand); } + + @Override + public @NotNull List getPassengers() { + return entity.getPassengers(); + } + + @Override + public void addPassenger(int entityId) { + entity.addPassenger(entityId); + } + + @Override + public void removePassenger(int entityId) { + entity.removePassenger(entityId); + } + + @Override + public @Nullable Integer getVehicleId() { + return entity.getVehicleId(); + } + + @Override + public void setVehicleId(Integer vehicleId) { + entity.setVehicleId(vehicleId); + } } 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) {