Add passengers to npc api
Add passengers to npc api & implementation Add jetbrains annotations to api to tag with nullables
This commit is contained in:
parent
84ee701029
commit
c5bf8dea0b
7 changed files with 80 additions and 8 deletions
|
@ -8,6 +8,10 @@ java {
|
|||
withJavadocJar()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly 'org.jetbrains:annotations:26.0.1'
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
|
|
|
@ -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<Integer> 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);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Integer> 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<Integer> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<Integer> getPassengers() {
|
||||
return entity.getPassengers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPassenger(int entityId) {
|
||||
entity.addPassenger(entityId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePassenger(int entityId) {
|
||||
entity.removePassenger(entityId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,5 +24,5 @@ public interface PacketFactory {
|
|||
void sendMetadata(Player player, PacketEntity entity, List<EntityData> 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);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue