NPCLib/nms/v1_9_R2/src/main/java/net/jitse/npclib/nms/v1_9_R2/NPC_v1_9_R2.java

109 lines
4.4 KiB
Java
Raw Normal View History

2018-04-26 13:52:49 +02:00
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.nms.v1_9_R2;
2019-08-03 13:47:12 +02:00
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.state.NPCSlot;
import net.jitse.npclib.hologram.Hologram;
2019-08-03 13:47:12 +02:00
import net.jitse.npclib.internal.MinecraftVersion;
import net.jitse.npclib.internal.NPCBase;
import net.jitse.npclib.nms.v1_9_R2.packets.*;
2018-04-26 13:52:49 +02:00
import net.minecraft.server.v1_9_R2.*;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack;
2018-04-26 13:52:49 +02:00
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
2018-04-26 13:52:49 +02:00
import java.util.List;
/**
* @author Jitse Boonstra
*/
public class NPC_v1_9_R2 extends NPCBase {
2018-04-26 13:52:49 +02:00
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister;
2018-04-26 13:52:49 +02:00
private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove;
private PacketPlayOutEntityHeadRotation packetPlayOutEntityHeadRotation;
private PacketPlayOutEntityDestroy packetPlayOutEntityDestroy;
2019-08-03 13:47:12 +02:00
public NPC_v1_9_R2(NPCLib instance, List<String> lines) {
super(instance, lines);
2018-04-26 13:52:49 +02:00
}
@Override
public void createPackets() {
this.hologram = new Hologram(MinecraftVersion.V1_9_R2, location.clone().subtract(0, 0.5, 0), text);
2018-04-26 13:52:49 +02:00
PacketPlayOutPlayerInfoWrapper packetPlayOutPlayerInfoWrapper = new PacketPlayOutPlayerInfoWrapper();
// Packets for spawning the NPC:
this.packetPlayOutScoreboardTeamRegister = new PacketPlayOutScoreboardTeamWrapper()
.createRegisterTeam(name); // First packet to send.
this.packetPlayOutPlayerInfoAdd = packetPlayOutPlayerInfoWrapper
.create(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, gameProfile, name); // Second packet to send.
this.packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawnWrapper()
.create(uuid, location, entityId); // Third packet to send.
this.packetPlayOutEntityHeadRotation = new PacketPlayOutEntityHeadRotationWrapper()
.create(location, entityId); // Fourth packet to send.
this.packetPlayOutPlayerInfoRemove = packetPlayOutPlayerInfoWrapper
.create(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, gameProfile, name); // Fifth packet to send (delayed).
// Packet for destroying the NPC:
this.packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(entityId); // First packet to send.
}
2018-04-26 13:52:49 +02:00
@Override
public void sendShowPackets(Player player) {
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
if (hasTeamRegistered.add(player.getUniqueId()))
playerConnection.sendPacket(packetPlayOutScoreboardTeamRegister);
2018-04-26 13:52:49 +02:00
playerConnection.sendPacket(packetPlayOutPlayerInfoAdd);
playerConnection.sendPacket(packetPlayOutNamedEntitySpawn);
playerConnection.sendPacket(packetPlayOutEntityHeadRotation);
hologram.show(player);
2018-04-26 13:52:49 +02:00
2020-04-11 20:51:05 +02:00
// Removing the player info after 10 seconds.
2019-08-03 13:47:12 +02:00
Bukkit.getScheduler().runTaskLater(instance.getPlugin(), () ->
2020-04-11 20:51:05 +02:00
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove), 200);
2018-04-26 13:52:49 +02:00
}
@Override
public void sendHidePackets(Player player) {
2018-04-26 13:52:49 +02:00
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
playerConnection.sendPacket(packetPlayOutEntityDestroy);
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove);
hologram.hide(player);
2018-04-26 13:52:49 +02:00
}
@Override
public void sendMetadataPacket(Player player) {
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadataWrapper().create(activeStates, entityId);
playerConnection.sendPacket(packet);
}
@Override
public void sendEquipmentPacket(Player player, NPCSlot slot, boolean auto) {
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
EnumItemSlot nmsSlot = slot.getNmsEnum(EnumItemSlot.class);
ItemStack item = getItem(slot);
PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment(entityId, nmsSlot, CraftItemStack.asNMSCopy(item));
playerConnection.sendPacket(packet);
}
2018-04-26 13:52:49 +02:00
}