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

View File

@ -25,11 +25,11 @@ import java.util.*;
public abstract class NPC implements PacketHandler, ActionHandler {
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).
protected final int entityId = Integer.MAX_VALUE - NPCManager.getAllNPCs().size();
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> autoHidden = new HashSet<>();

View File

@ -5,14 +5,6 @@
package net.jitse.npclib.listeners;
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 java.util.HashSet;
@ -35,38 +27,38 @@ public class PacketListener {
private final Set<UUID> delay = new HashSet<>();
public void start(JavaPlugin plugin) {
new TinyProtocol(plugin) {
@Override
public Object onPacketInAsync(Player player, Channel channel, Object packet) {
if (packetPlayInUseEntityClazz.isInstance(packet)) {
NPC npc = NPCManager.getAllNPCs().stream().filter(
check -> check.isActuallyShown(player) && check.getEntityId() == (int) entityIdField.get(packet))
.findFirst().orElse(null);
if (npc == null) {
// Default player, not doing magic with the packet.
return super.onPacketInAsync(player, channel, packet);
}
if (delay.contains(player.getUniqueId())) {
return null;
}
ClickType clickType = actionField.get(packet).toString()
.equals("ATTACK") ? ClickType.LEFT_CLICK : ClickType.RIGHT_CLICK;
Bukkit.getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc));
UUID uuid = player.getUniqueId();
delay.add(uuid);
Bukkit.getScheduler().runTask(plugin, () -> delay.remove(uuid));
return null;
}
return super.onPacketInAsync(player, channel, packet);
}
};
// new TinyProtocol(plugin) {
//
// @Override
// public Object onPacketInAsync(Player player, Channel channel, Object packet) {
//
// if (packetPlayInUseEntityClazz.isInstance(packet)) {
// NPC npc = NPCManager.getAllNPCs().stream().filter(
// check -> check.isActuallyShown(player) && check.getEntityId() == (int) entityIdField.get(packet))
// .findFirst().orElse(null);
//
// if (npc == null) {
// // Default player, not doing magic with the packet.
// return super.onPacketInAsync(player, channel, packet);
// }
//
// if (delay.contains(player.getUniqueId())) {
// return null;
// }
//
// ClickType clickType = actionField.get(packet).toString()
// .equals("ATTACK") ? ClickType.LEFT_CLICK : ClickType.RIGHT_CLICK;
//
// Bukkit.getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc));
//
// UUID uuid = player.getUniqueId();
// delay.add(uuid);
// Bukkit.getScheduler().runTask(plugin, () -> delay.remove(uuid));
// return null;
// }
//
// return super.onPacketInAsync(player, channel, packet);
// }
// };
}
}

View File

@ -5,7 +5,6 @@
package net.jitse.npclib.nms.v1_7_R4;
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.PacketPlayOutNamedEntitySpawnWrapper;
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 {
private Hologram hologram;
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister, packetPlayOutScoreboardTeamUnregister;
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) {
super(plugin, skin, autoHideDistance, lines);
this.name = lines.get(0);
}
@Override
public void createPackets() {
this.hologram = new Hologram(location.clone().add(0, 0.5, 0), lines);
hologram.generatePackets(false, false);
this.legacyGameProfile = generateLegacyGameProfile(uuid, name);
this.legacyGameProfile = generateLegacyGameProfile(uuid, name.length() < 16 ? name : name.substring(0, 15));
PacketPlayOutPlayerInfoWrapper packetPlayOutPlayerInfoWrapper = new PacketPlayOutPlayerInfoWrapper();
// Packets for spawning the NPC:
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
.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()
.create(uuid, location, entityId); // Third packet to send.
.create(legacyGameProfile, location, entityId); // Third packet to send.
this.packetPlayOutEntityHeadRotation = new PacketPlayOutEntityHeadRotationWrapper()
.create(location, entityId); // Fourth packet to send.
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:
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".
this.packetPlayOutScoreboardTeamUnregister = new PacketPlayOutScoreboardTeamWrapper()
.createUnregisterTeam(name); // Third packet to send.
.createUnregisterTeam(legacyGameProfile.getId().toString().replace("-", "").substring(0, 10)); // Third packet to send.
}
@Override
@ -82,8 +78,6 @@ public class NPC_v1_7_R4 extends NPC {
playerConnection.sendPacket(packetPlayOutNamedEntitySpawn);
playerConnection.sendPacket(packetPlayOutEntityHeadRotation);
hologram.spawn(player);
Bukkit.getScheduler().runTaskLater(plugin, () ->
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove), 50);
}
@ -95,8 +89,6 @@ public class NPC_v1_7_R4 extends NPC {
playerConnection.sendPacket(packetPlayOutEntityDestroy);
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove);
hologram.destroy(player);
if (scheduler) {
// Sending this a bit later so the player doesn't see the name (for that split second).
Bukkit.getScheduler().runTaskLater(plugin, () ->

View File

@ -7,22 +7,21 @@ package net.jitse.npclib.nms.v1_7_R4.packets;
import com.comphenix.tinyprotocol.Reflection;
import net.minecraft.server.v1_7_R4.DataWatcher;
import net.minecraft.server.v1_7_R4.PacketPlayOutNamedEntitySpawn;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import org.bukkit.Location;
import java.util.UUID;
/**
* @author Jitse Boonstra
*/
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();
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "a", int.class)
.set(packetPlayOutNamedEntitySpawn, entityId);
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "b", UUID.class)
.set(packetPlayOutNamedEntitySpawn, uuid);
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "b", GameProfile.class)
.set(packetPlayOutNamedEntitySpawn, gameProfile);
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "c", int.class)
.set(packetPlayOutNamedEntitySpawn, (int) Math.floor(location.getX() * 32.0D));
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "d", int.class)

View File

@ -6,7 +6,6 @@ package net.jitse.npclib.nms.v1_7_R4.packets;
import com.comphenix.tinyprotocol.Reflection;
import net.minecraft.server.v1_7_R4.PacketPlayOutScoreboardTeam;
import org.bukkit.ChatColor;
import java.util.Collection;
@ -15,38 +14,41 @@ import java.util.Collection;
*/
public class PacketPlayOutScoreboardTeamWrapper {
public PacketPlayOutScoreboardTeam createRegisterTeam(String name) {
public PacketPlayOutScoreboardTeam createRegisterTeam(String uuidName, String name) {
PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam();
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "h", int.class)
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "f", int.class)
.set(packetPlayOutScoreboardTeam, 0);
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)
.set(packetPlayOutScoreboardTeam, name);
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "e", String.class)
.set(packetPlayOutScoreboardTeam, "never");
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "i", int.class)
.set(packetPlayOutScoreboardTeam, uuidName);
if (name.length() > 16) {
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "d", String.class)
.set(packetPlayOutScoreboardTeam, name.substring(15));
}
if (name.length() > 32) {
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "c", String.class)
.set(packetPlayOutScoreboardTeam, name.substring(31));
}
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "g", int.class)
.set(packetPlayOutScoreboardTeam, 1);
// Could not get this working in the PacketPlayOutPlayerInfoWrapper class.
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "c", String.class)
.set(packetPlayOutScoreboardTeam, ChatColor.BLUE + "[NPC] ");
Reflection.FieldAccessor<Collection> collectionFieldAccessor = Reflection.getField(
packetPlayOutScoreboardTeam.getClass(), "g", Collection.class);
packetPlayOutScoreboardTeam.getClass(), "e", Collection.class);
Collection collection = collectionFieldAccessor.get(packetPlayOutScoreboardTeam);
collection.add(name);
collection.add(name.length() < 16 ? name : name.substring(0, 15));
collectionFieldAccessor.set(packetPlayOutScoreboardTeam, collection);
return packetPlayOutScoreboardTeam;
}
public PacketPlayOutScoreboardTeam createUnregisterTeam(String name) {
public PacketPlayOutScoreboardTeam createUnregisterTeam(String uuidName) {
PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam();
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "h", int.class)
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "f", int.class)
.set(packetPlayOutScoreboardTeam, 1);
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.class)
.set(packetPlayOutScoreboardTeam, name);
.set(packetPlayOutScoreboardTeam, uuidName);
return packetPlayOutScoreboardTeam;
}