Made NPCs spawn for 1.7.10 (1.7 R4) (#10).

Disabled TinyProtocol to work on some other things first.
This commit is contained in:
Jitse Boonstra
2018-12-14 15:28:28 +01:00
parent d741db78de
commit bff059abae
5 changed files with 63 additions and 78 deletions
@@ -25,11 +25,11 @@ import java.util.*;
public abstract class NPC implements PacketHandler, ActionHandler { public abstract class NPC implements PacketHandler, ActionHandler {
protected final UUID uuid = UUID.randomUUID(); protected final UUID uuid = UUID.randomUUID();
protected final String name = uuid.toString().replace("-", "").substring(0, 10);
// Below was previously = (int) Math.ceil(Math.random() * 100000) + 100000 (new is experimental). // Below was previously = (int) Math.ceil(Math.random() * 100000) + 100000 (new is experimental).
protected final int entityId = Integer.MAX_VALUE - NPCManager.getAllNPCs().size(); protected final int entityId = Integer.MAX_VALUE - NPCManager.getAllNPCs().size();
protected double cosFOV = Math.cos(Math.toRadians(60)); protected double cosFOV = Math.cos(Math.toRadians(60));
protected String name = uuid.toString().replace("-", "").substring(0, 10);
private final Set<UUID> shown = new HashSet<>(); private final Set<UUID> shown = new HashSet<>();
private final Set<UUID> autoHidden = new HashSet<>(); private final Set<UUID> autoHidden = new HashSet<>();
@@ -5,14 +5,6 @@
package net.jitse.npclib.listeners; package net.jitse.npclib.listeners;
import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.Reflection;
import com.comphenix.tinyprotocol.TinyProtocol;
import io.netty.channel.Channel;
import net.jitse.npclib.NPCManager;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.events.NPCInteractEvent;
import net.jitse.npclib.events.click.ClickType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashSet; import java.util.HashSet;
@@ -35,38 +27,38 @@ public class PacketListener {
private final Set<UUID> delay = new HashSet<>(); private final Set<UUID> delay = new HashSet<>();
public void start(JavaPlugin plugin) { public void start(JavaPlugin plugin) {
new TinyProtocol(plugin) { // new TinyProtocol(plugin) {
//
@Override // @Override
public Object onPacketInAsync(Player player, Channel channel, Object packet) { // public Object onPacketInAsync(Player player, Channel channel, Object packet) {
//
if (packetPlayInUseEntityClazz.isInstance(packet)) { // if (packetPlayInUseEntityClazz.isInstance(packet)) {
NPC npc = NPCManager.getAllNPCs().stream().filter( // NPC npc = NPCManager.getAllNPCs().stream().filter(
check -> check.isActuallyShown(player) && check.getEntityId() == (int) entityIdField.get(packet)) // check -> check.isActuallyShown(player) && check.getEntityId() == (int) entityIdField.get(packet))
.findFirst().orElse(null); // .findFirst().orElse(null);
//
if (npc == null) { // if (npc == null) {
// Default player, not doing magic with the packet. // // Default player, not doing magic with the packet.
return super.onPacketInAsync(player, channel, packet); // return super.onPacketInAsync(player, channel, packet);
} // }
//
if (delay.contains(player.getUniqueId())) { // if (delay.contains(player.getUniqueId())) {
return null; // return null;
} // }
//
ClickType clickType = actionField.get(packet).toString() // ClickType clickType = actionField.get(packet).toString()
.equals("ATTACK") ? ClickType.LEFT_CLICK : ClickType.RIGHT_CLICK; // .equals("ATTACK") ? ClickType.LEFT_CLICK : ClickType.RIGHT_CLICK;
//
Bukkit.getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc)); // Bukkit.getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc));
//
UUID uuid = player.getUniqueId(); // UUID uuid = player.getUniqueId();
delay.add(uuid); // delay.add(uuid);
Bukkit.getScheduler().runTask(plugin, () -> delay.remove(uuid)); // Bukkit.getScheduler().runTask(plugin, () -> delay.remove(uuid));
return null; // return null;
} // }
//
return super.onPacketInAsync(player, channel, packet); // return super.onPacketInAsync(player, channel, packet);
} // }
}; // };
} }
} }
@@ -5,7 +5,6 @@
package net.jitse.npclib.nms.v1_7_R4; package net.jitse.npclib.nms.v1_7_R4;
import net.jitse.npclib.api.NPC; import net.jitse.npclib.api.NPC;
import net.jitse.npclib.nms.holograms.Hologram;
import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutEntityHeadRotationWrapper; import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutEntityHeadRotationWrapper;
import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutNamedEntitySpawnWrapper; import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutNamedEntitySpawnWrapper;
import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutPlayerInfoWrapper; import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutPlayerInfoWrapper;
@@ -27,7 +26,6 @@ import java.util.UUID;
*/ */
public class NPC_v1_7_R4 extends NPC { public class NPC_v1_7_R4 extends NPC {
private Hologram hologram;
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn; private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister, packetPlayOutScoreboardTeamUnregister; private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister, packetPlayOutScoreboardTeamUnregister;
private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove; private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove;
@@ -38,31 +36,29 @@ public class NPC_v1_7_R4 extends NPC {
public NPC_v1_7_R4(JavaPlugin plugin, Skin skin, double autoHideDistance, List<String> lines) { public NPC_v1_7_R4(JavaPlugin plugin, Skin skin, double autoHideDistance, List<String> lines) {
super(plugin, skin, autoHideDistance, lines); super(plugin, skin, autoHideDistance, lines);
this.name = lines.get(0);
} }
@Override @Override
public void createPackets() { public void createPackets() {
this.hologram = new Hologram(location.clone().add(0, 0.5, 0), lines); this.legacyGameProfile = generateLegacyGameProfile(uuid, name.length() < 16 ? name : name.substring(0, 15));
hologram.generatePackets(false, false);
this.legacyGameProfile = generateLegacyGameProfile(uuid, name);
PacketPlayOutPlayerInfoWrapper packetPlayOutPlayerInfoWrapper = new PacketPlayOutPlayerInfoWrapper(); PacketPlayOutPlayerInfoWrapper packetPlayOutPlayerInfoWrapper = new PacketPlayOutPlayerInfoWrapper();
// Packets for spawning the NPC: // Packets for spawning the NPC:
this.packetPlayOutScoreboardTeamRegister = new PacketPlayOutScoreboardTeamWrapper() this.packetPlayOutScoreboardTeamRegister = new PacketPlayOutScoreboardTeamWrapper()
.createRegisterTeam(name); // First packet to send. .createRegisterTeam(legacyGameProfile.getId().toString().replace("-", "").substring(0, 10), name); // First packet to send.
this.packetPlayOutPlayerInfoAdd = packetPlayOutPlayerInfoWrapper this.packetPlayOutPlayerInfoAdd = packetPlayOutPlayerInfoWrapper
.create(0, legacyGameProfile, name); // Second packet to send. .create(0, legacyGameProfile, name.length() < 16 ? name : name.length() < 16 ? name : name.substring(0, 15)); // Second packet to send.
this.packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawnWrapper() this.packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawnWrapper()
.create(uuid, location, entityId); // Third packet to send. .create(legacyGameProfile, location, entityId); // Third packet to send.
this.packetPlayOutEntityHeadRotation = new PacketPlayOutEntityHeadRotationWrapper() this.packetPlayOutEntityHeadRotation = new PacketPlayOutEntityHeadRotationWrapper()
.create(location, entityId); // Fourth packet to send. .create(location, entityId); // Fourth packet to send.
this.packetPlayOutPlayerInfoRemove = packetPlayOutPlayerInfoWrapper this.packetPlayOutPlayerInfoRemove = packetPlayOutPlayerInfoWrapper
.create(4, legacyGameProfile, name); // Fifth packet to send (delayed). .create(4, legacyGameProfile, name.length() < 16 ? name : name.substring(0, 15)); // Fifth packet to send (delayed).
// Packet for destroying the NPC: // Packet for destroying the NPC:
this.packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(entityId); // First packet to send. this.packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(entityId); // First packet to send.
@@ -70,7 +66,7 @@ public class NPC_v1_7_R4 extends NPC {
// Second packet to send is "packetPlayOutPlayerInfoRemove". // Second packet to send is "packetPlayOutPlayerInfoRemove".
this.packetPlayOutScoreboardTeamUnregister = new PacketPlayOutScoreboardTeamWrapper() this.packetPlayOutScoreboardTeamUnregister = new PacketPlayOutScoreboardTeamWrapper()
.createUnregisterTeam(name); // Third packet to send. .createUnregisterTeam(legacyGameProfile.getId().toString().replace("-", "").substring(0, 10)); // Third packet to send.
} }
@Override @Override
@@ -82,8 +78,6 @@ public class NPC_v1_7_R4 extends NPC {
playerConnection.sendPacket(packetPlayOutNamedEntitySpawn); playerConnection.sendPacket(packetPlayOutNamedEntitySpawn);
playerConnection.sendPacket(packetPlayOutEntityHeadRotation); playerConnection.sendPacket(packetPlayOutEntityHeadRotation);
hologram.spawn(player);
Bukkit.getScheduler().runTaskLater(plugin, () -> Bukkit.getScheduler().runTaskLater(plugin, () ->
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove), 50); playerConnection.sendPacket(packetPlayOutPlayerInfoRemove), 50);
} }
@@ -95,8 +89,6 @@ public class NPC_v1_7_R4 extends NPC {
playerConnection.sendPacket(packetPlayOutEntityDestroy); playerConnection.sendPacket(packetPlayOutEntityDestroy);
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove); playerConnection.sendPacket(packetPlayOutPlayerInfoRemove);
hologram.destroy(player);
if (scheduler) { if (scheduler) {
// Sending this a bit later so the player doesn't see the name (for that split second). // Sending this a bit later so the player doesn't see the name (for that split second).
Bukkit.getScheduler().runTaskLater(plugin, () -> Bukkit.getScheduler().runTaskLater(plugin, () ->
@@ -7,22 +7,21 @@ package net.jitse.npclib.nms.v1_7_R4.packets;
import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.Reflection;
import net.minecraft.server.v1_7_R4.DataWatcher; import net.minecraft.server.v1_7_R4.DataWatcher;
import net.minecraft.server.v1_7_R4.PacketPlayOutNamedEntitySpawn; import net.minecraft.server.v1_7_R4.PacketPlayOutNamedEntitySpawn;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import org.bukkit.Location; import org.bukkit.Location;
import java.util.UUID;
/** /**
* @author Jitse Boonstra * @author Jitse Boonstra
*/ */
public class PacketPlayOutNamedEntitySpawnWrapper { public class PacketPlayOutNamedEntitySpawnWrapper {
public PacketPlayOutNamedEntitySpawn create(UUID uuid, Location location, int entityId) { public PacketPlayOutNamedEntitySpawn create(GameProfile gameProfile, Location location, int entityId) {
PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawn(); PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawn();
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "a", int.class) Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "a", int.class)
.set(packetPlayOutNamedEntitySpawn, entityId); .set(packetPlayOutNamedEntitySpawn, entityId);
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "b", UUID.class) Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "b", GameProfile.class)
.set(packetPlayOutNamedEntitySpawn, uuid); .set(packetPlayOutNamedEntitySpawn, gameProfile);
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "c", int.class) Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "c", int.class)
.set(packetPlayOutNamedEntitySpawn, (int) Math.floor(location.getX() * 32.0D)); .set(packetPlayOutNamedEntitySpawn, (int) Math.floor(location.getX() * 32.0D));
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "d", int.class) Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "d", int.class)
@@ -6,7 +6,6 @@ package net.jitse.npclib.nms.v1_7_R4.packets;
import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.Reflection;
import net.minecraft.server.v1_7_R4.PacketPlayOutScoreboardTeam; import net.minecraft.server.v1_7_R4.PacketPlayOutScoreboardTeam;
import org.bukkit.ChatColor;
import java.util.Collection; import java.util.Collection;
@@ -15,38 +14,41 @@ import java.util.Collection;
*/ */
public class PacketPlayOutScoreboardTeamWrapper { public class PacketPlayOutScoreboardTeamWrapper {
public PacketPlayOutScoreboardTeam createRegisterTeam(String name) { public PacketPlayOutScoreboardTeam createRegisterTeam(String uuidName, String name) {
PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam(); PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam();
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "h", int.class) Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "f", int.class)
.set(packetPlayOutScoreboardTeam, 0); .set(packetPlayOutScoreboardTeam, 0);
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "b", String.class) Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "b", String.class)
.set(packetPlayOutScoreboardTeam, name); .set(packetPlayOutScoreboardTeam, name.length() < 16 ? name : name.substring(0, 15));
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.class) Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.class)
.set(packetPlayOutScoreboardTeam, name); .set(packetPlayOutScoreboardTeam, uuidName);
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "e", String.class) if (name.length() > 16) {
.set(packetPlayOutScoreboardTeam, "never"); Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "d", String.class)
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "i", int.class) .set(packetPlayOutScoreboardTeam, name.substring(15));
.set(packetPlayOutScoreboardTeam, 1); }
// Could not get this working in the PacketPlayOutPlayerInfoWrapper class. if (name.length() > 32) {
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "c", String.class) Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "c", String.class)
.set(packetPlayOutScoreboardTeam, ChatColor.BLUE + "[NPC] "); .set(packetPlayOutScoreboardTeam, name.substring(31));
}
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "g", int.class)
.set(packetPlayOutScoreboardTeam, 1);
Reflection.FieldAccessor<Collection> collectionFieldAccessor = Reflection.getField( Reflection.FieldAccessor<Collection> collectionFieldAccessor = Reflection.getField(
packetPlayOutScoreboardTeam.getClass(), "g", Collection.class); packetPlayOutScoreboardTeam.getClass(), "e", Collection.class);
Collection collection = collectionFieldAccessor.get(packetPlayOutScoreboardTeam); Collection collection = collectionFieldAccessor.get(packetPlayOutScoreboardTeam);
collection.add(name); collection.add(name.length() < 16 ? name : name.substring(0, 15));
collectionFieldAccessor.set(packetPlayOutScoreboardTeam, collection); collectionFieldAccessor.set(packetPlayOutScoreboardTeam, collection);
return packetPlayOutScoreboardTeam; return packetPlayOutScoreboardTeam;
} }
public PacketPlayOutScoreboardTeam createUnregisterTeam(String name) { public PacketPlayOutScoreboardTeam createUnregisterTeam(String uuidName) {
PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam(); PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam();
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "h", int.class) Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "f", int.class)
.set(packetPlayOutScoreboardTeam, 1); .set(packetPlayOutScoreboardTeam, 1);
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.class) Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.class)
.set(packetPlayOutScoreboardTeam, name); .set(packetPlayOutScoreboardTeam, uuidName);
return packetPlayOutScoreboardTeam; return packetPlayOutScoreboardTeam;
} }