2018-04-26 13:52:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Jitse Boonstra
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.jitse.npclib.nms.v1_8_R3;
|
|
|
|
|
2019-08-03 13:47:12 +02:00
|
|
|
import net.jitse.npclib.NPCLib;
|
2019-10-20 11:36:37 +02:00
|
|
|
import net.jitse.npclib.api.state.NPCSlot;
|
2019-10-21 09:49:23 +02:00
|
|
|
import net.jitse.npclib.hologram._Hologram;
|
2019-08-03 13:47:12 +02:00
|
|
|
import net.jitse.npclib.internal.MinecraftVersion;
|
2019-10-21 09:49:23 +02:00
|
|
|
import net.jitse.npclib.internal.NPCBase;
|
2019-10-20 11:36:37 +02:00
|
|
|
import net.jitse.npclib.nms.v1_8_R3.packets.*;
|
2018-04-26 13:52:49 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.*;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
|
2019-10-20 11:36:37 +02:00
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
|
2018-04-26 13:52:49 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2019-10-20 11:36:37 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2018-04-26 13:52:49 +02:00
|
|
|
|
2019-09-26 00:02:37 +02:00
|
|
|
import java.util.HashSet;
|
2018-04-26 13:52:49 +02:00
|
|
|
import java.util.List;
|
2019-09-26 00:02:37 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
2018-04-26 13:52:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Jitse Boonstra
|
|
|
|
*/
|
2019-10-21 09:49:23 +02:00
|
|
|
public class NPC_v1_8_R3 extends NPCBase {
|
2018-04-26 13:52:49 +02:00
|
|
|
|
2019-10-21 09:49:23 +02:00
|
|
|
private _Hologram hologram;
|
2018-04-26 13:52:49 +02:00
|
|
|
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
|
2019-09-26 00:02:37 +02:00
|
|
|
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister;
|
2018-04-26 13:52:49 +02:00
|
|
|
private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove;
|
|
|
|
private PacketPlayOutEntityHeadRotation packetPlayOutEntityHeadRotation;
|
|
|
|
private PacketPlayOutEntityDestroy packetPlayOutEntityDestroy;
|
2019-09-26 00:02:37 +02:00
|
|
|
private Set<UUID> hasTeamRegistered = new HashSet<>();
|
2018-04-26 13:52:49 +02:00
|
|
|
|
2019-08-03 13:47:12 +02:00
|
|
|
public NPC_v1_8_R3(NPCLib instance, List<String> lines) {
|
|
|
|
super(instance, lines);
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-05-14 22:09:16 +02:00
|
|
|
public void createPackets() {
|
2019-10-21 09:49:23 +02:00
|
|
|
this.hologram = new _Hologram(location.clone().add(0, 0.5, 0), text);
|
2019-08-03 13:47:12 +02:00
|
|
|
hologram.generatePackets(MinecraftVersion.V1_8_R3);
|
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.
|
2019-09-26 00:02:37 +02:00
|
|
|
}
|
2018-04-26 13:52:49 +02:00
|
|
|
|
2019-09-26 00:02:37 +02:00
|
|
|
@Override
|
|
|
|
public void onLogout(Player player) {
|
|
|
|
super.onLogout(player);
|
|
|
|
hasTeamRegistered.remove(player.getUniqueId());
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendShowPackets(Player player) {
|
|
|
|
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
|
|
|
|
|
2019-09-26 00:02:37 +02:00
|
|
|
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.spawn(player);
|
|
|
|
|
|
|
|
|
2019-08-03 13:47:12 +02:00
|
|
|
Bukkit.getScheduler().runTaskLater(instance.getPlugin(), () ->
|
2018-05-14 22:09:16 +02:00
|
|
|
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove), 50);
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-26 00:02:37 +02:00
|
|
|
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.destroy(player);
|
|
|
|
}
|
2019-10-20 11:36:37 +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;
|
|
|
|
|
|
|
|
ItemStack item;
|
|
|
|
switch (slot) {
|
|
|
|
case HELMET:
|
|
|
|
item = helmet;
|
|
|
|
break;
|
|
|
|
case CHESTPLATE:
|
|
|
|
item = chestplate;
|
|
|
|
break;
|
|
|
|
case LEGGINGS:
|
|
|
|
item = leggings;
|
|
|
|
break;
|
|
|
|
case BOOTS:
|
|
|
|
item = boots;
|
|
|
|
break;
|
|
|
|
case MAINHAND:
|
|
|
|
item = inHand;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!auto) {
|
|
|
|
throw new IllegalArgumentException(slot.toString() + " is not a supported slot for the version of your server");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PacketPlayOutEntityEquipment packet = new PacketPlayOutEntityEquipment(entityId, slot.getSlot(), CraftItemStack.asNMSCopy(item));
|
|
|
|
playerConnection.sendPacket(packet);
|
|
|
|
}
|
2018-04-26 13:52:49 +02:00
|
|
|
}
|