For 1.4.2.
This commit is contained in:
parent
2a883ebfdf
commit
1dbe797e01
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-api</artifactId>
|
||||
|
@ -19,12 +19,6 @@
|
|||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms-v1_7_R4</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms-v1_8_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-commons</artifactId>
|
||||
|
@ -31,14 +31,7 @@
|
|||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot</artifactId>
|
||||
<version>1.7.10-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Netty (implemented in versions above 1.7.10) -->
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.0.23.Final</version>
|
||||
<version>1.13.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -1,331 +0,0 @@
|
|||
package com.comphenix.tinyprotocol;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import net.jitse.npclib.logging.NPCLibLogger;
|
||||
import net.minecraft.util.com.mojang.authlib.GameProfile;
|
||||
import net.minecraft.util.io.netty.channel.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
|
||||
*/
|
||||
public abstract class LegacyTinyProtocol {
|
||||
|
||||
private static final AtomicInteger ID = new AtomicInteger(0);
|
||||
|
||||
// Used in order to lookup a channel
|
||||
private static final Reflection.MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
||||
private static final Reflection.FieldAccessor<Object> getConnection = Reflection.getField("{nms}.EntityPlayer", "playerConnection", Object.class);
|
||||
private static final Reflection.FieldAccessor<Object> getManager = Reflection.getField("{nms}.PlayerConnection", "networkManager", Object.class);
|
||||
private static final Reflection.FieldAccessor<Channel> getChannel = Reflection.getField("{nms}.NetworkManager", Channel.class, 0);
|
||||
|
||||
// Looking up ServerConnection
|
||||
private static final Class<Object> minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer");
|
||||
private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection");
|
||||
private static final Reflection.FieldAccessor<Object> getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0);
|
||||
private static final Reflection.FieldAccessor<Object> getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0);
|
||||
private static final Reflection.MethodInvoker getNetworkMarkers = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass);
|
||||
|
||||
// Packets we have to intercept
|
||||
private static final Class<?> PACKET_SET_PROTOCOL = Reflection.getMinecraftClass("PacketHandshakingInSetProtocol");
|
||||
private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getMinecraftClass("PacketLoginInStart");
|
||||
private static final Reflection.FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
||||
private static final Reflection.FieldAccessor<Integer> protocolId = Reflection.getField(PACKET_SET_PROTOCOL, int.class, 0);
|
||||
private static final Reflection.FieldAccessor<Enum> protocolType = Reflection.getField(PACKET_SET_PROTOCOL, Enum.class, 0);
|
||||
|
||||
|
||||
// Speedup channel lookup
|
||||
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||
private Map<Channel, Integer> protocolLookup = new MapMaker().weakKeys().makeMap();
|
||||
private Listener listener;
|
||||
|
||||
private Logger logger;
|
||||
|
||||
// Channels that have already been removed
|
||||
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
||||
|
||||
// List of network markers
|
||||
private List<Object> networkManagers;
|
||||
|
||||
// Injected channel handlers
|
||||
private List<Channel> serverChannels = Lists.newArrayList();
|
||||
private ChannelInboundHandlerAdapter serverChannelHandler;
|
||||
private ChannelInitializer<Channel> beginInitProtocol;
|
||||
private ChannelInitializer<Channel> endInitProtocol;
|
||||
|
||||
// Current handler name
|
||||
private String handlerName;
|
||||
|
||||
private volatile boolean closed;
|
||||
protected Plugin plugin;
|
||||
|
||||
protected LegacyTinyProtocol(final Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.logger = new NPCLibLogger(plugin);
|
||||
|
||||
// Compute handler name
|
||||
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
||||
|
||||
// Prepare existing players
|
||||
registerBukkitEvents();
|
||||
|
||||
try {
|
||||
logger.info("Attempting to inject into netty");
|
||||
registerChannelHandler();
|
||||
registerPlayers(plugin);
|
||||
} catch (IllegalArgumentException exceptionx) {
|
||||
// Damn you, late bind
|
||||
logger.log(Level.WARNING, "Attempting to delay injection");
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
registerChannelHandler();
|
||||
registerPlayers(plugin);
|
||||
logger.info("Injection complete");
|
||||
}
|
||||
}.runTask(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
private void createServerChannelHandler() {
|
||||
// Handle connected channels
|
||||
endInitProtocol = new ChannelInitializer<Channel>() {
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@Override
|
||||
protected void initChannel(Channel channel) throws Exception {
|
||||
try {
|
||||
// This can take a while, so we need to stop the main thread from interfering
|
||||
synchronized (networkManagers) {
|
||||
// Stop injecting channels
|
||||
if (!closed) {
|
||||
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
||||
}
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
logger.log(Level.SEVERE, "Cannot inject incomming channel " + channel, exception);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// This is executed before Minecraft's channel handler
|
||||
beginInitProtocol = new ChannelInitializer<Channel>() {
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@Override
|
||||
protected void initChannel(Channel channel) throws Exception {
|
||||
channel.pipeline().addLast(endInitProtocol);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
serverChannelHandler = new ChannelInboundHandlerAdapter() {
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
Channel channel = (Channel) msg;
|
||||
|
||||
channel.pipeline().addFirst(beginInitProtocol);
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private void registerBukkitEvents() {
|
||||
listener = new Listener() {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public final void onPlayerLogin(PlayerJoinEvent e) {
|
||||
if (closed)
|
||||
return;
|
||||
|
||||
Channel channel = getChannel(e.getPlayer());
|
||||
|
||||
// Don't inject players that have been explicitly uninjected
|
||||
if (!uninjectedChannels.contains(channel)) {
|
||||
injectPlayer(e.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler
|
||||
public final void onPluginDisable(PluginDisableEvent e) {
|
||||
if (e.getPlugin().equals(plugin)) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void registerChannelHandler() {
|
||||
Object mcServer = getMinecraftServer.get(Bukkit.getServer());
|
||||
Object serverConnection = getServerConnection.get(mcServer);
|
||||
boolean looking = true;
|
||||
|
||||
// We need to synchronize against this list
|
||||
networkManagers = (List<Object>) getNetworkMarkers.invoke(null, serverConnection);
|
||||
createServerChannelHandler();
|
||||
|
||||
// Find the correct list, or implicitly throw an exception
|
||||
for (int i = 0; looking; i++) {
|
||||
List<Object> list = Reflection.getField(serverConnection.getClass(), List.class, i).get(serverConnection);
|
||||
|
||||
for (Object item : list) {
|
||||
//if (!ChannelFuture.class.isInstance(item))
|
||||
// break;
|
||||
|
||||
// Channel future that contains the server connection
|
||||
Channel serverChannel = ((ChannelFuture) item).channel();
|
||||
|
||||
serverChannels.add(serverChannel);
|
||||
|
||||
serverChannel.pipeline().addFirst(serverChannelHandler);
|
||||
logger.info("Server channel handler injected (" + serverChannel + ")");
|
||||
looking = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void unregisterChannelHandler() {
|
||||
if (serverChannelHandler == null)
|
||||
return;
|
||||
|
||||
for (Channel serverChannel : serverChannels) {
|
||||
final ChannelPipeline pipeline = serverChannel.pipeline();
|
||||
|
||||
// Remove channel handler
|
||||
serverChannel.eventLoop().execute(() -> {
|
||||
try {
|
||||
pipeline.remove(serverChannelHandler);
|
||||
} catch (NoSuchElementException exception) {
|
||||
// That's fine
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void registerPlayers(Plugin plugin) {
|
||||
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||
injectPlayer(player);
|
||||
}
|
||||
}
|
||||
|
||||
public Object onPacketInAsync(Player sender, Object packet) {
|
||||
return packet;
|
||||
}
|
||||
|
||||
private void injectPlayer(Player player) {
|
||||
injectChannelInternal(getChannel(player)).player = player;
|
||||
}
|
||||
|
||||
private PacketInterceptor injectChannelInternal(Channel channel) {
|
||||
try {
|
||||
PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(handlerName);
|
||||
|
||||
// Inject our packet interceptor
|
||||
if (interceptor == null) {
|
||||
interceptor = new PacketInterceptor();
|
||||
channel.pipeline().addBefore("packet_handler", handlerName, interceptor);
|
||||
uninjectedChannels.remove(channel);
|
||||
}
|
||||
|
||||
return interceptor;
|
||||
} catch (IllegalArgumentException exception) {
|
||||
// Try again
|
||||
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
||||
}
|
||||
}
|
||||
|
||||
private Channel getChannel(Player player) {
|
||||
Channel channel = channelLookup.get(player.getName());
|
||||
|
||||
// Lookup channel again
|
||||
if (channel == null) {
|
||||
Object connection = getConnection.get(getPlayerHandle.invoke(player));
|
||||
Object manager = getManager.get(connection);
|
||||
|
||||
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
private void uninjectChannel(final Channel channel) {
|
||||
// No need to guard against this if we're closing
|
||||
if (!closed) {
|
||||
uninjectedChannels.add(channel);
|
||||
}
|
||||
|
||||
// See ChannelInjector in ProtocolLib, line 590
|
||||
channel.eventLoop().execute(() -> channel.pipeline().remove(handlerName));
|
||||
}
|
||||
|
||||
private void close() {
|
||||
if (!closed) {
|
||||
closed = true;
|
||||
|
||||
// Remove our handlers
|
||||
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||
uninjectChannel(getChannel(player));
|
||||
}
|
||||
|
||||
// Clean up Bukkit
|
||||
HandlerList.unregisterAll(listener);
|
||||
unregisterChannelHandler();
|
||||
}
|
||||
}
|
||||
|
||||
private final class PacketInterceptor extends ChannelDuplexHandler {
|
||||
// Updated by the login event
|
||||
public volatile Player player;
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
// Intercept channel
|
||||
final Channel channel = ctx.channel();
|
||||
if (PACKET_LOGIN_IN_START.isInstance(msg)) {
|
||||
GameProfile profile = getGameProfile.get(msg);
|
||||
channelLookup.put(profile.getName(), channel);
|
||||
} else if (PACKET_SET_PROTOCOL.isInstance(msg)) {
|
||||
String protocol = protocolType.get(msg).name();
|
||||
if (protocol.equalsIgnoreCase("LOGIN")) {
|
||||
protocolLookup.put(channel, protocolId.get(msg));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
msg = onPacketInAsync(player, msg);
|
||||
} catch (Exception exception) {
|
||||
logger.log(Level.SEVERE, "Error in onPacketInAsync()", exception);
|
||||
}
|
||||
|
||||
if (msg != null) {
|
||||
super.channelRead(ctx, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
package net.jitse.npclib.listeners;
|
||||
|
||||
import com.comphenix.tinyprotocol.LegacyTinyProtocol;
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import net.jitse.npclib.NPCManager;
|
||||
|
@ -39,33 +38,13 @@ public class PacketListener {
|
|||
public void start(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
boolean legacyProtocol = false;
|
||||
new TinyProtocol(plugin) {
|
||||
|
||||
try {
|
||||
Class.forName("io.netty.channel.Channel");
|
||||
} catch (ClassNotFoundException exception) {
|
||||
legacyProtocol = true;
|
||||
}
|
||||
|
||||
if (legacyProtocol) {
|
||||
// 1.7 R4 packet interaction.
|
||||
new LegacyTinyProtocol(plugin) {
|
||||
|
||||
@Override
|
||||
public Object onPacketInAsync(Player player, Object packet) {
|
||||
return handleInteractPacket(player, packet) ? super.onPacketInAsync(player, packet) : null;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
// 1.8 (and above) packet interaction.
|
||||
new TinyProtocol(plugin) {
|
||||
|
||||
@Override
|
||||
public Object onPacketInAsync(Player player, Object packet) {
|
||||
return handleInteractPacket(player, packet) ? super.onPacketInAsync(player, packet) : null;
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
public Object onPacketInAsync(Player player, Object packet) {
|
||||
return handleInteractPacket(player, packet) ? super.onPacketInAsync(player, packet) : null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private boolean handleInteractPacket(Player player, Object packet) {
|
||||
|
|
|
@ -7,13 +7,12 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
|
||||
<modules>
|
||||
<module>v1_7_R4</module>
|
||||
<module>v1_8_R1</module>
|
||||
<module>v1_8_R2</module>
|
||||
<module>v1_8_R3</module>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_10_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_11_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_12_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_13_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_13_R2</artifactId>
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_7_R4</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot</artifactId>
|
||||
<version>1.7.10-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -1,111 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Jitse Boonstra
|
||||
*/
|
||||
|
||||
package net.jitse.npclib.nms.v1_7_R4;
|
||||
|
||||
import net.jitse.npclib.api.NPC;
|
||||
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;
|
||||
import net.jitse.npclib.nms.v1_7_R4.packets.PacketPlayOutScoreboardTeamWrapper;
|
||||
import net.jitse.npclib.skin.Skin;
|
||||
import net.minecraft.server.v1_7_R4.*;
|
||||
import net.minecraft.util.com.mojang.authlib.GameProfile;
|
||||
import net.minecraft.util.com.mojang.authlib.properties.Property;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Jitse Boonstra
|
||||
*/
|
||||
public class NPC_v1_7_R4 extends NPC {
|
||||
|
||||
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
|
||||
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister, packetPlayOutScoreboardTeamUnregister;
|
||||
private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove;
|
||||
private PacketPlayOutEntityHeadRotation packetPlayOutEntityHeadRotation;
|
||||
private PacketPlayOutEntityDestroy packetPlayOutEntityDestroy;
|
||||
|
||||
private GameProfile legacyGameProfile;
|
||||
|
||||
public NPC_v1_7_R4(JavaPlugin plugin, Skin skin, double autoHideDistance, List<String> lines) {
|
||||
super(plugin, skin, autoHideDistance, lines);
|
||||
// TODO: Add multi-line text support.
|
||||
this.name = lines.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createPackets() {
|
||||
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(legacyGameProfile.getId().toString().replace("-", "").substring(0, 10), name); // First packet to send.
|
||||
|
||||
this.packetPlayOutPlayerInfoAdd = packetPlayOutPlayerInfoWrapper
|
||||
.create(0, legacyGameProfile, name.length() < 16 ? name : name.length() < 16 ? name : name.substring(0, 15)); // Second packet to send.
|
||||
|
||||
this.packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawnWrapper()
|
||||
.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.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.
|
||||
|
||||
// Second packet to send is "packetPlayOutPlayerInfoRemove".
|
||||
|
||||
this.packetPlayOutScoreboardTeamUnregister = new PacketPlayOutScoreboardTeamWrapper()
|
||||
.createUnregisterTeam(legacyGameProfile.getId().toString().replace("-", "").substring(0, 10)); // Third packet to send.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendShowPackets(Player player) {
|
||||
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
|
||||
|
||||
playerConnection.sendPacket(packetPlayOutScoreboardTeamRegister);
|
||||
playerConnection.sendPacket(packetPlayOutPlayerInfoAdd);
|
||||
playerConnection.sendPacket(packetPlayOutNamedEntitySpawn);
|
||||
playerConnection.sendPacket(packetPlayOutEntityHeadRotation);
|
||||
|
||||
Bukkit.getScheduler().runTaskLater(plugin, () ->
|
||||
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove), 50);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendHidePackets(Player player, boolean scheduler) {
|
||||
PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection;
|
||||
|
||||
playerConnection.sendPacket(packetPlayOutEntityDestroy);
|
||||
playerConnection.sendPacket(packetPlayOutPlayerInfoRemove);
|
||||
|
||||
if (scheduler) {
|
||||
// Sending this a bit later so the player doesn't see the name (for that split second).
|
||||
Bukkit.getScheduler().runTaskLater(plugin, () ->
|
||||
playerConnection.sendPacket(packetPlayOutScoreboardTeamUnregister), 5);
|
||||
} else {
|
||||
playerConnection.sendPacket(packetPlayOutScoreboardTeamUnregister);
|
||||
}
|
||||
}
|
||||
|
||||
private GameProfile generateLegacyGameProfile(UUID uuid, String name) {
|
||||
GameProfile gameProfile = new GameProfile(uuid, name);
|
||||
|
||||
if (skin != null) {
|
||||
gameProfile.getProperties().put("textures", new Property("textures", skin.getValue(), skin.getSignature()));
|
||||
}
|
||||
|
||||
return gameProfile;
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Jitse Boonstra
|
||||
*/
|
||||
|
||||
package net.jitse.npclib.nms.v1_7_R4.packets;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import net.minecraft.server.v1_7_R4.PacketPlayOutEntityHeadRotation;
|
||||
import org.bukkit.Location;
|
||||
|
||||
/**
|
||||
* @author Jitse Boonstra
|
||||
*/
|
||||
public class PacketPlayOutEntityHeadRotationWrapper {
|
||||
|
||||
public PacketPlayOutEntityHeadRotation create(Location location, int entityId) {
|
||||
PacketPlayOutEntityHeadRotation packetPlayOutEntityHeadRotation = new PacketPlayOutEntityHeadRotation();
|
||||
|
||||
Reflection.getField(packetPlayOutEntityHeadRotation.getClass(), "a", int.class).
|
||||
set(packetPlayOutEntityHeadRotation, entityId);
|
||||
Reflection.getField(packetPlayOutEntityHeadRotation.getClass(), "b", byte.class)
|
||||
.set(packetPlayOutEntityHeadRotation, (byte) ((int) location.getYaw() * 256.0F / 360.0F));
|
||||
|
||||
return packetPlayOutEntityHeadRotation;
|
||||
}
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Jitse Boonstra
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Jitse Boonstra
|
||||
*/
|
||||
public class PacketPlayOutNamedEntitySpawnWrapper {
|
||||
|
||||
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", 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)
|
||||
.set(packetPlayOutNamedEntitySpawn, (int) Math.floor(location.getY() * 32.0D));
|
||||
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "e", int.class)
|
||||
.set(packetPlayOutNamedEntitySpawn, (int) Math.floor(location.getZ() * 32.0D));
|
||||
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "f", byte.class)
|
||||
.set(packetPlayOutNamedEntitySpawn, (byte) ((int) (location.getYaw() * 256.0F / 360.0F)));
|
||||
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "g", byte.class)
|
||||
.set(packetPlayOutNamedEntitySpawn, (byte) ((int) (location.getPitch() * 256.0F / 360.0F)));
|
||||
|
||||
DataWatcher dataWatcher = new DataWatcher(null);
|
||||
dataWatcher.a(10, (byte) 127);
|
||||
|
||||
Reflection.getField(packetPlayOutNamedEntitySpawn.getClass(), "i", DataWatcher.class)
|
||||
.set(packetPlayOutNamedEntitySpawn, dataWatcher);
|
||||
|
||||
return packetPlayOutNamedEntitySpawn;
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Jitse Boonstra
|
||||
*/
|
||||
|
||||
package net.jitse.npclib.nms.v1_7_R4.packets;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import net.minecraft.server.v1_7_R4.PacketPlayOutPlayerInfo;
|
||||
import net.minecraft.util.com.mojang.authlib.GameProfile;
|
||||
|
||||
/**
|
||||
* @author Jitse Boonstra
|
||||
*/
|
||||
public class PacketPlayOutPlayerInfoWrapper {
|
||||
|
||||
public PacketPlayOutPlayerInfo create(int action, GameProfile gameProfile, String name) {
|
||||
PacketPlayOutPlayerInfo packetPlayOutPlayerInfo = new PacketPlayOutPlayerInfo();
|
||||
|
||||
// Action values:
|
||||
// 0 = Add player
|
||||
// 1 = Update gamemode
|
||||
// 2 = Update latency
|
||||
// 3 = Update display name
|
||||
// 4 = Remove player
|
||||
Reflection.getField(packetPlayOutPlayerInfo.getClass(), "action", int.class)
|
||||
.set(packetPlayOutPlayerInfo, action);
|
||||
Reflection.getField(packetPlayOutPlayerInfo.getClass(), "player", GameProfile.class)
|
||||
.set(packetPlayOutPlayerInfo, gameProfile);
|
||||
Reflection.getField(packetPlayOutPlayerInfo.getClass(), "username", String.class)
|
||||
.set(packetPlayOutPlayerInfo, name);
|
||||
Reflection.getField(packetPlayOutPlayerInfo.getClass(), "ping", int.class)
|
||||
.set(packetPlayOutPlayerInfo, 0);
|
||||
Reflection.getField(packetPlayOutPlayerInfo.getClass(), "gamemode", int.class)
|
||||
.set(packetPlayOutPlayerInfo, 1);
|
||||
|
||||
return packetPlayOutPlayerInfo;
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Jitse Boonstra
|
||||
*/
|
||||
|
||||
package net.jitse.npclib.nms.v1_7_R4.packets;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import net.minecraft.server.v1_7_R4.PacketPlayOutScoreboardTeam;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author Jitse Boonstra
|
||||
*/
|
||||
public class PacketPlayOutScoreboardTeamWrapper {
|
||||
|
||||
public PacketPlayOutScoreboardTeam createRegisterTeam(String uuidName, String name) {
|
||||
PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam();
|
||||
|
||||
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "f", int.class)
|
||||
.set(packetPlayOutScoreboardTeam, 0);
|
||||
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "b", String.class)
|
||||
.set(packetPlayOutScoreboardTeam, name.length() < 16 ? name : name.substring(0, 15));
|
||||
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.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);
|
||||
Reflection.FieldAccessor<Collection> collectionFieldAccessor = Reflection.getField(
|
||||
packetPlayOutScoreboardTeam.getClass(), "e", Collection.class);
|
||||
Collection collection = collectionFieldAccessor.get(packetPlayOutScoreboardTeam);
|
||||
collection.add(name.length() < 16 ? name : name.substring(0, 15));
|
||||
collectionFieldAccessor.set(packetPlayOutScoreboardTeam, collection);
|
||||
|
||||
return packetPlayOutScoreboardTeam;
|
||||
}
|
||||
|
||||
public PacketPlayOutScoreboardTeam createUnregisterTeam(String uuidName) {
|
||||
PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeam = new PacketPlayOutScoreboardTeam();
|
||||
|
||||
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "f", int.class)
|
||||
.set(packetPlayOutScoreboardTeam, 1);
|
||||
Reflection.getField(packetPlayOutScoreboardTeam.getClass(), "a", String.class)
|
||||
.set(packetPlayOutScoreboardTeam, uuidName);
|
||||
|
||||
return packetPlayOutScoreboardTeam;
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_8_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_8_R2</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_8_R3</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_9_R1</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib-nms</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-nms-v1_9_R2</artifactId>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>net.jitse</groupId>
|
||||
<artifactId>npclib</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>npclib-plugin</artifactId>
|
||||
|
|
Loading…
Reference in New Issue