Updated the library.

This commit is contained in:
Jitse Boonstra
2019-08-03 13:47:12 +02:00
parent b8a7dcaddb
commit e0752c5d7e
68 changed files with 1116 additions and 696 deletions
+53
View File
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<artifactId>npclib</artifactId>
<groupId>net.jitse</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>npclib-api</artifactId>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>minecraft-repo</id>
<url>https://libraries.minecraft.net</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mojang</groupId>
<artifactId>authlib</artifactId>
<version>1.5.21</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.33.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,397 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package com.comphenix.tinyprotocol;
import org.bukkit.Bukkit;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* An utility class that simplifies reflection in Bukkit plugins.
*
* @author Kristian
*/
public final class Reflection {
/**
* An interface for invoking a specific constructor.
*/
public interface ConstructorInvoker {
/**
* Invoke a constructor for a specific class.
*
* @param arguments - the arguments to pass to the constructor.
* @return The constructed object.
*/
Object invoke(Object... arguments);
}
/**
* An interface for invoking a specific method.
*/
public interface MethodInvoker {
/**
* Invoke a method on a specific target object.
*
* @param target - the target object, or NULL for a static method.
* @param arguments - the arguments to pass to the method.
* @return The return value, or NULL if is void.
*/
Object invoke(Object target, Object... arguments);
}
/**
* An interface for retrieving the field content.
*
* @param <T> - field type.
*/
public interface FieldAccessor<T> {
/**
* Retrieve the content of a field.
*
* @param target - the target object, or NULL for a static field.
* @return The value of the field.
*/
T get(Object target);
/**
* Set the content of a field.
*
* @param target - the target object, or NULL for a static field.
* @param value - the new value of the field.
*/
void set(Object target, Object value);
/**
* Determine if the given object has this field.
*
* @param target - the object to test.
* @return TRUE if it does, FALSE otherwise.
*/
boolean hasField(Object target);
}
// Deduce the net.minecraft.server.v* package
private static String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName();
private static String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server");
private static String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", "");
// Variable replacement
private static Pattern MATCH_VARIABLE = Pattern.compile("\\{([^}]+)\\}");
private Reflection() {
// Seal class
}
/**
* Retrieve a field accessor for a specific field type and name.
*
* @param target - the target type.
* @param name - the name of the field, or NULL to ignore.
* @param fieldType - a compatible field type.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType) {
return getField(target, name, fieldType, 0);
}
/**
* Retrieve a field accessor for a specific field type and name.
*
* @param className - lookup name of the class, see {@link #getClass(String)}.
* @param name - the name of the field, or NULL to ignore.
* @param fieldType - a compatible field type.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(String className, String name, Class<T> fieldType) {
return getField(getClass(className), name, fieldType, 0);
}
/**
* Retrieve a field accessor for a specific field type and name.
*
* @param target - the target type.
* @param fieldType - a compatible field type.
* @param index - the number of compatible fields to skip.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(Class<?> target, Class<T> fieldType, int index) {
return getField(target, null, fieldType, index);
}
/**
* Retrieve a field accessor for a specific field type and name.
*
* @param className - lookup name of the class, see {@link #getClass(String)}.
* @param fieldType - a compatible field type.
* @param index - the number of compatible fields to skip.
* @return The field accessor.
*/
public static <T> FieldAccessor<T> getField(String className, Class<T> fieldType, int index) {
return getField(getClass(className), fieldType, index);
}
// Common method
private static <T> FieldAccessor<T> getField(Class<?> target, String name, Class<T> fieldType, int index) {
for (final Field field : target.getDeclaredFields()) {
if ((name == null || field.getName().equals(name)) && fieldType.isAssignableFrom(field.getType()) && index-- <= 0) {
field.setAccessible(true);
// A function for retrieving a specific field value
return new FieldAccessor<T>() {
@Override
@SuppressWarnings("unchecked")
public T get(Object target) {
try {
return (T) field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access reflection.", e);
}
}
@Override
public void set(Object target, Object value) {
try {
field.set(target, value);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access reflection.", e);
}
}
@Override
public boolean hasField(Object target) {
// target instanceof DeclaringClass
return field.getDeclaringClass().isAssignableFrom(target.getClass());
}
};
}
}
// Search in parent classes
if (target.getSuperclass() != null)
return getField(target.getSuperclass(), name, fieldType, index);
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
}
/**
* Search for the first publicly and privately defined method of the given name and parameter count.
*
* @param className - lookup name of the class, see {@link #getClass(String)}.
* @param methodName - the method name, or NULL to skip.
* @param params - the expected parameters.
* @return An object that invokes this specific method.
* @throws IllegalStateException If we cannot find this method.
*/
public static MethodInvoker getMethod(String className, String methodName, Class<?>... params) {
return getTypedMethod(getClass(className), methodName, null, params);
}
/**
* Search for the first publicly and privately defined method of the given name and parameter count.
*
* @param clazz - a class to start with.
* @param methodName - the method name, or NULL to skip.
* @param params - the expected parameters.
* @return An object that invokes this specific method.
* @throws IllegalStateException If we cannot find this method.
*/
public static MethodInvoker getMethod(Class<?> clazz, String methodName, Class<?>... params) {
return getTypedMethod(clazz, methodName, null, params);
}
/**
* Search for the first publicly and privately defined method of the given name and parameter count.
*
* @param clazz - a class to start with.
* @param methodName - the method name, or NULL to skip.
* @param returnType - the expected return type, or NULL to ignore.
* @param params - the expected parameters.
* @return An object that invokes this specific method.
* @throws IllegalStateException If we cannot find this method.
*/
public static MethodInvoker getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) {
for (final Method method : clazz.getDeclaredMethods()) {
if ((methodName == null || method.getName().equals(methodName))
&& (returnType == null || method.getReturnType().equals(returnType))
&& Arrays.equals(method.getParameterTypes(), params)) {
method.setAccessible(true);
return (target, arguments) -> {
try {
return method.invoke(target, arguments);
} catch (Exception e) {
throw new RuntimeException("Cannot invoke method " + method, e);
}
};
}
}
// Search in every superclass
if (clazz.getSuperclass() != null)
return getMethod(clazz.getSuperclass(), methodName, params);
throw new IllegalStateException(String.format("Unable to find method %s (%s).", methodName, Arrays.asList(params)));
}
/**
* Search for the first publically and privately defined constructor of the given name and parameter count.
*
* @param className - lookup name of the class, see {@link #getClass(String)}.
* @param params - the expected parameters.
* @return An object that invokes this constructor.
* @throws IllegalStateException If we cannot find this method.
*/
public static ConstructorInvoker getConstructor(String className, Class<?>... params) {
return getConstructor(getClass(className), params);
}
/**
* Search for the first publicly and privately defined constructor of the given name and parameter count.
*
* @param clazz - a class to start with.
* @param params - the expected parameters.
* @return An object that invokes this constructor.
* @throws IllegalStateException If we cannot find this method.
*/
public static ConstructorInvoker getConstructor(Class<?> clazz, Class<?>... params) {
for (final Constructor<?> constructor : clazz.getDeclaredConstructors()) {
if (Arrays.equals(constructor.getParameterTypes(), params)) {
constructor.setAccessible(true);
return arguments -> {
try {
return constructor.newInstance(arguments);
} catch (Exception e) {
throw new RuntimeException("Cannot invoke constructor " + constructor, e);
}
};
}
}
throw new IllegalStateException(String.format("Unable to find constructor for %s (%s).", clazz, Arrays.asList(params)));
}
/**
* Retrieve a class from its full name, without knowing its type on compile time.
* <p>
* This is useful when looking up fields by a NMS or OBC type.
* <p>
*
* @param lookupName - the class name with variables.
* @return The class.
* @see {@link #getClass()} for more information.
*/
public static Class<Object> getUntypedClass(String lookupName) {
@SuppressWarnings({"rawtypes", "unchecked"})
Class<Object> clazz = (Class) getClass(lookupName);
return clazz;
}
/**
* Retrieve a class from its full name.
* <p>
* Strings enclosed with curly brackets - such as {TEXT} - will be replaced according to the following table:
*
* <table border="1">
* <tr>
* <th>Variable</th>
* <th>Content</th>
* </tr>
* <tr>
* <td>{nms}</td>
* <td>Actual package name of net.minecraft.server.VERSION</td>
* </tr>
* <tr>
* <td>{obc}</td>
* <td>Actual pacakge name of org.bukkit.craftbukkit.VERSION</td>
* </tr>
* <tr>
* <td>{version}</td>
* <td>The current Minecraft package VERSION, if any.</td>
* </tr>
* </table>
*
* @param lookupName - the class name with variables.
* @return The looked up class.
* @throws IllegalArgumentException If a variable or class could not be found.
*/
public static Class<?> getClass(String lookupName) {
return getCanonicalClass(expandVariables(lookupName));
}
/**
* Retrieve a class in the net.minecraft.server.VERSION.* package.
*
* @param name - the name of the class, excluding the package.
* @throws IllegalArgumentException If the class doesn't exist.
*/
public static Class<?> getMinecraftClass(String name) {
return getCanonicalClass(NMS_PREFIX + "." + name);
}
/**
* Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
*
* @param name - the name of the class, excluding the package.
* @throws IllegalArgumentException If the class doesn't exist.
*/
public static Class<?> getCraftBukkitClass(String name) {
return getCanonicalClass(OBC_PREFIX + "." + name);
}
/**
* Retrieve a class by its canonical name.
*
* @param canonicalName - the canonical name.
* @return The class.
*/
private static Class<?> getCanonicalClass(String canonicalName) {
try {
return Class.forName(canonicalName);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Cannot find " + canonicalName, e);
}
}
/**
* Expand variables such as "{nms}" and "{obc}" to their corresponding packages.
*
* @param name - the full name of the class.
* @return The expanded string.
*/
private static String expandVariables(String name) {
StringBuffer output = new StringBuffer();
Matcher matcher = MATCH_VARIABLE.matcher(name);
while (matcher.find()) {
String variable = matcher.group(1);
String replacement;
// Expand all detected variables
if ("nms".equalsIgnoreCase(variable))
replacement = NMS_PREFIX;
else if ("obc".equalsIgnoreCase(variable))
replacement = OBC_PREFIX;
else if ("version".equalsIgnoreCase(variable))
replacement = VERSION;
else
throw new IllegalArgumentException("Unknown variable: " + variable);
// Assume the expanded variables are all packages, and append a dot
if (replacement.length() > 0 && matcher.end() < name.length() && name.charAt(matcher.end()) != '.')
replacement += ".";
matcher.appendReplacement(output, Matcher.quoteReplacement(replacement));
}
matcher.appendTail(output);
return output.toString();
}
}
@@ -0,0 +1,324 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package com.comphenix.tinyprotocol;
import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import io.netty.channel.*;
import net.jitse.npclib.NPCLib;
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.PlayerLoginEvent;
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;
/**
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
*/
public abstract class TinyProtocol {
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_LOGIN_IN_START = Reflection.getMinecraftClass("PacketLoginInStart");
private static final Reflection.FieldAccessor getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START,
Reflection.getClass("com.mojang.authlib.GameProfile"), 0);
// Speedup channel lookup
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
private Listener listener;
// Channels that have already been removed
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
// List of network markers
private List<Object> networkManagers;
private final NPCLib instance;
// 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 TinyProtocol(NPCLib instance) {
this.plugin = instance.getPlugin();
this.instance = instance;
// Compute handler name
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
// Prepare existing players
registerBukkitEvents();
try {
instance.getLogger().info("Attempting to inject into netty");
registerChannelHandler();
registerPlayers(plugin);
} catch (IllegalArgumentException exception) {
// Damn you, late bind
instance.getLogger().warning("Attempting to delay injection");
new BukkitRunnable() {
@Override
public void run() {
registerChannelHandler();
registerPlayers(plugin);
instance.getLogger().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) {
instance.getLogger().severe("Cannot inject incomming channel " + channel + ". Message: "
+ exception.getMessage());
}
}
};
// 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;
// Prepare to initialize ths channel
channel.pipeline().addFirst(beginInitProtocol);
ctx.fireChannelRead(msg);
}
};
}
private void registerBukkitEvents() {
listener = new Listener() {
@SuppressWarnings("unused")
@EventHandler(priority = EventPriority.LOWEST)
public final void onPlayerLogin(PlayerLoginEvent 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 (!(item instanceof ChannelFuture))
break;
// Channel future that contains the server connection
Channel serverChannel = ((ChannelFuture) item).channel();
serverChannels.add(serverChannel);
serverChannel.pipeline().addFirst(serverChannelHandler);
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 close() {
if (!closed) {
closed = true;
// Remove our handlers
for (Player player : plugin.getServer().getOnlinePlayers()) {
// No need to guard against this if we're closing
Channel channel = getChannel(player);
if (!closed) {
uninjectedChannels.add(channel);
}
// See ChannelInjector in ProtocolLib, line 590
channel.eventLoop().execute(() -> channel.pipeline().remove(handlerName));
}
// 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();
handleLoginStart(channel, msg);
try {
msg = onPacketInAsync(player, msg);
} catch (Exception exception) {
instance.getLogger().severe("Error in onPacketInAsync(). Message: " + exception.getMessage());
}
if (msg != null) {
super.channelRead(ctx, msg);
}
}
private void handleLoginStart(Channel channel, Object packet) {
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
Object profile = getGameProfile.get(packet);
channelLookup.put((String) Reflection.getMethod(profile.getClass(), "getName").invoke(profile), channel);
}
}
}
}
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.api.utilities.Logger;
import net.jitse.npclib.listeners.ChunkListener;
import net.jitse.npclib.listeners.PacketListener;
import net.jitse.npclib.listeners.PlayerListener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
public final class NPCLib {
private final JavaPlugin plugin;
private final Logger logger;
private final Class<?> npcClass;
private double autoHideDistance = 50.0;
public NPCLib(JavaPlugin plugin) {
this.plugin = plugin;
this.logger = new Logger("NPCLib");
String versionName = plugin.getServer().getClass().getPackage().getName().split("\\.")[3];
Class<?> npcClass = null;
try {
npcClass = Class.forName("net.jitse.npclib.nms." + versionName + ".NPC_" + versionName);
} catch (ClassNotFoundException exception) {
// Version not supported, error below.
}
this.npcClass = npcClass;
if (npcClass == null) {
logger.severe("Failed to initiate. Your server's version ("
+ versionName + ") is not supported");
return;
}
PluginManager pluginManager = plugin.getServer().getPluginManager();
pluginManager.registerEvents(new PlayerListener(this), plugin);
pluginManager.registerEvents(new ChunkListener(this), plugin);
// Boot the according packet listener.
new PacketListener().start(this);
logger.info("Enabled for Minecraft " + versionName);
}
/**
* @return The JavaPlugin instance.
*/
public JavaPlugin getPlugin() {
return plugin;
}
/**
* Set a new value for the auto-hide distance.
* A recommended value (and default) is 50 blocks.
*
* @param autoHideDistance The new value.
*/
public void setAutoHideDistance(double autoHideDistance) {
this.autoHideDistance = autoHideDistance;
}
/**
* @return The auto-hide distance.
*/
public double getAutoHideDistance() {
return autoHideDistance;
}
/**
* @return The logger NPCLib uses.
*/
public Logger getLogger() {
return logger;
}
/**
* Create a new non-player character (NPC).
*
* @param lines The text you want to sendShowPackets above the NPC (null = no text).
* @return The NPC object you may use to sendShowPackets it to players.
*/
public NPC createNPC(List<String> lines) {
try {
return (NPC) npcClass.getConstructors()[0].newInstance(this, lines);
} catch (Exception exception) {
logger.warning("Failed to create NPC. Please report the following stacktrace message: " + exception.getMessage());
}
return null;
}
/**
* Create a new non-player character (NPC).
*
* @return The NPC object you may use to sendShowPackets it to players.
*/
public NPC createNPC() {
return createNPC(null);
}
}
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api;
import net.jitse.npclib.api.skin.Skin;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public interface NPC {
/**
* Set the NPC's location.
* Use this method before using {@link NPC#create}.
*
* @param location The spawn location for the NPC.
* @return object instance.
*/
NPC setLocation(Location location);
/**
* Set the NPC's skin.
* Use this method before using {@link NPC#create}.
*
* @param skin The skin(data) you'd like to apply.
* @return object instance.
*/
NPC setSkin(Skin skin);
/**
* Get the location of the NPC.
*
* @return The location of the NPC.
*/
Location getLocation();
/**
* Create all necessary packets for the NPC so it can be shown to players.
*
* @return object instance.
*/
NPC create();
/**
* Get the ID of the NPC.
*
* @return the ID of the NPC.
*/
String getId();
/**
* Test if a player can see the NPC.
* E.g. is the player is out of range, this method will return false as the NPC is automatically hidden by the library.
*
* @param player The player you'd like to check.
* @return Value on whether the player can see the NPC.
*/
boolean isShown(Player player);
/**
* Show the NPC to a player.
* Requires {@link NPC#create} to be used first.
*
* @param player the player to show the NPC to.
*/
void show(Player player);
/**
* Hide the NPC from a player.
* Will not do anything if NPC isn't shown to the player.
* Requires {@link NPC#create} to be used first.
*
* @param player The player to hide the NPC from.
*/
void hide(Player player);
/**
* Destroy the NPC, i.e. remove it from the registry.
* Requires {@link NPC#create} to be used first.
*/
void destroy();
}
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api.events;
import net.jitse.npclib.api.NPC;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* @author Jitse Boonstra
*/
public class NPCHideEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private final NPC npc;
private final Player player;
private final boolean automatic;
public NPCHideEvent(NPC npc, Player player, boolean automatic) {
this.npc = npc;
this.player = player;
this.automatic = automatic;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public NPC getNPC() {
return npc;
}
public Player getPlayer() {
return player;
}
/**
* @return Value on whether the hiding was triggered automatically.
*/
public boolean isAutomatic() {
return automatic;
}
@Override
public boolean isCancelled() {
return cancelled;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api.events;
import net.jitse.npclib.api.NPC;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* @author Jitse Boonstra
*/
public class NPCInteractEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final ClickType clickType;
private final NPC npc;
public NPCInteractEvent(Player player, ClickType clickType, NPC npc) {
this.player = player;
this.clickType = clickType;
this.npc = npc;
}
public Player getWhoClicked() {
return this.player;
}
public ClickType getClickType() {
return this.clickType;
}
public NPC getNPC() {
return this.npc;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public enum ClickType {
LEFT_CLICK, RIGHT_CLICK
}
}
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api.events;
import net.jitse.npclib.api.NPC;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* @author Jitse Boonstra
*/
public class NPCShowEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private final NPC npc;
private final Player player;
private final boolean automatic;
public NPCShowEvent(NPC npc, Player player, boolean automatic) {
this.npc = npc;
this.player = player;
this.automatic = automatic;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public NPC getNPC() {
return npc;
}
public Player getPlayer() {
return player;
}
/**
* @return Value on whether the spawn was triggered automatically.
*/
public boolean isAutomatic() {
return automatic;
}
@Override
public boolean isCancelled() {
return cancelled;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api.skin;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
/**
* @author Jitse Boonstra
*/
public class MineSkinFetcher {
private static final String MINESKIN_API = "https://api.mineskin.org/get/id/";
public static void fetchSkinFromIdAsync(int id, Callback callback) {
new Thread(() -> {
try {
StringBuilder builder = new StringBuilder();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
Scanner scanner = new Scanner(httpURLConnection.getInputStream());
while (scanner.hasNextLine()) {
builder.append(scanner.nextLine());
}
scanner.close();
httpURLConnection.disconnect();
JsonObject jsonObject = (JsonObject) new JsonParser().parse(builder.toString());
JsonObject textures = jsonObject.get("data").getAsJsonObject().get("texture").getAsJsonObject();
String value = textures.get("value").getAsString();
String signature = textures.get("signature").getAsString();
callback.call(new Skin(value, signature));
} catch (IOException exception) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Could not fetch skin! (Id: " + id + "). Message: " + exception.getMessage());
exception.printStackTrace();
callback.failed();
}
}).start();
}
public interface Callback {
void call(Skin skinData);
default void failed() {
}
}
}
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api.skin;
public class Skin {
private final String value, signature;
public Skin(String value, String signature) {
this.value = value;
this.signature = signature;
}
public String getValue() {
return this.value;
}
public String getSignature() {
return this.signature;
}
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.api.utilities;
import org.bukkit.Bukkit;
public class Logger {
private final String prefix;
private boolean enabled = true;
public Logger(String prefix) {
this.prefix = prefix + " ";
}
public void disable() {
this.enabled = false;
}
public void info(String info) {
if (!enabled) {
return;
}
Bukkit.getLogger().info(prefix + info);
}
public void warning(String warning) {
if (!enabled) {
return;
}
Bukkit.getLogger().warning(prefix + warning);
}
public void severe(String severe) {
if (!enabled) {
return;
}
Bukkit.getLogger().severe(prefix + severe);
}
}
@@ -0,0 +1,178 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.hologram;
import com.comphenix.tinyprotocol.Reflection;
import net.jitse.npclib.internal.MinecraftVersion;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Hologram {
private final double delta = 0.3;
private List<Object> armorStands = new ArrayList<>();
private Set<Object> spawnPackets = new HashSet<>();
private Set<Object> destroyPackets = new HashSet<>();
// Classes:
private static final Class<?> CHAT_COMPONENT_TEXT_CLAZZ = Reflection.getMinecraftClass("ChatComponentText");
private static final Class<?> CHAT_BASE_COMPONENT_CLAZZ = Reflection.getMinecraftClass("IChatBaseComponent");
private static final Class<?> ENTITY_ARMOR_STAND_CLAZZ = Reflection.getMinecraftClass("EntityArmorStand");
private static final Class<?> ENTITY_LIVING_CLAZZ = Reflection.getMinecraftClass("EntityLiving");
private static final Class<?> ENTITY_CLAZZ = Reflection.getMinecraftClass("Entity");
private static final Class<?> CRAFT_BUKKIT_CLASS = Reflection.getCraftBukkitClass("CraftWorld");
private static final Class<?> CRAFT_PLAYER_CLAZZ = Reflection.getCraftBukkitClass("entity.CraftPlayer");
private static final Class<?> PACKET_PLAY_OUT_SPAWN_ENTITY_LIVING_CLAZZ = Reflection.getMinecraftClass(
"PacketPlayOutSpawnEntityLiving");
private static final Class<?> PACKET_PLAY_OUT_ENTITY_DESTROY_CLAZZ = Reflection.getMinecraftClass(
"PacketPlayOutEntityDestroy");
private static final Class<?> ENTITY_PLAYER_CLAZZ = Reflection.getMinecraftClass("EntityPlayer");
private static final Class<?> PLAYER_CONNECTION_CLAZZ = Reflection.getMinecraftClass("PlayerConnection");
private static final Class<?> PACKET_CLAZZ = Reflection.getMinecraftClass("Packet");
// Constructors:
private static final Reflection.ConstructorInvoker CHAT_COMPONENT_TEXT_CONSTRUCTOR = Reflection
.getConstructor(CHAT_COMPONENT_TEXT_CLAZZ, String.class);
private static final Reflection.ConstructorInvoker PACKET_PLAY_OUT_SPAWN_ENTITY_LIVING_CONSTRUCTOR = Reflection
.getConstructor(PACKET_PLAY_OUT_SPAWN_ENTITY_LIVING_CLAZZ, ENTITY_LIVING_CLAZZ);
private static final Reflection.ConstructorInvoker PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR = Reflection
.getConstructor(PACKET_PLAY_OUT_ENTITY_DESTROY_CLAZZ, int[].class);
// Fields:
private static final Reflection.FieldAccessor playerConnectionField = Reflection.getField(ENTITY_PLAYER_CLAZZ,
"playerConnection", PLAYER_CONNECTION_CLAZZ);
// Methods:
private static final Reflection.MethodInvoker SET_LOCATION_METHOD = Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ,
"setLocation", double.class, double.class, double.class, float.class, float.class);
private static final Reflection.MethodInvoker SET_SMALL_METHOD = Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ,
"setSmall", boolean.class);
private static final Reflection.MethodInvoker SET_INVISIBLE_METHOD = Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ,
"setInvisible", boolean.class);
private static final Reflection.MethodInvoker SET_BASE_PLATE_METHOD = Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ,
"setBasePlate", boolean.class);
private static final Reflection.MethodInvoker SET_ARMS_METHOD = Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ,
"setArms", boolean.class);
private static final Reflection.MethodInvoker PLAYER_GET_HANDLE_METHOD = Reflection.getMethod(CRAFT_PLAYER_CLAZZ,
"getHandle");
private static final Reflection.MethodInvoker SEND_PACKET_METHOD = Reflection.getMethod(PLAYER_CONNECTION_CLAZZ,
"sendPacket", PACKET_CLAZZ);
private static final Reflection.MethodInvoker GET_ID_METHOD = Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ,
"getId");
private final Location start;
private final List<String> lines;
private final Object worldServer;
public Hologram(Location location, List<String> lines) {
this.start = location;
this.lines = lines;
this.worldServer = Reflection.getMethod(CRAFT_BUKKIT_CLASS, "getHandle")
.invoke(CRAFT_BUKKIT_CLASS.cast(location.getWorld()));
}
public void generatePackets(MinecraftVersion version) {
Reflection.MethodInvoker gravityMethod = (version.isAboveOrEqual(MinecraftVersion.V1_9_R2) ?
Reflection.getMethod(ENTITY_CLAZZ, "setNoGravity", boolean.class) :
Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ, "setGravity", boolean.class));
Reflection.MethodInvoker customNameMethod = (version.isAboveOrEqual(MinecraftVersion.V1_12_R1) ?
Reflection.getMethod(ENTITY_CLAZZ, "setCustomName", CHAT_BASE_COMPONENT_CLAZZ) :
Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ, "setCustomName", String.class));
Reflection.MethodInvoker customNameVisibilityMethod = (version.isAboveOrEqual(MinecraftVersion.V1_12_R1) ?
Reflection.getMethod(ENTITY_CLAZZ, "setCustomNameVisible", boolean.class) :
Reflection.getMethod(ENTITY_ARMOR_STAND_CLAZZ, "setCustomNameVisible", boolean.class));
Location location = start.clone().add(0, delta * lines.size(), 0);
Class<?> worldClass = worldServer.getClass().getSuperclass();
if (start.getWorld().getEnvironment() != World.Environment.NORMAL) {
worldClass = worldClass.getSuperclass();
}
Reflection.ConstructorInvoker entityArmorStandConstructor = (version.isAboveOrEqual(MinecraftVersion.V1_14_R1) ?
Reflection.getConstructor(ENTITY_ARMOR_STAND_CLAZZ, worldClass, double.class, double.class, double.class) :
Reflection.getConstructor(ENTITY_ARMOR_STAND_CLAZZ, worldClass));
for (String line : lines) {
Object entityArmorStand = (version.isAboveOrEqual(MinecraftVersion.V1_14_R1) ?
entityArmorStandConstructor.invoke(worldServer, location.getX(), location.getY(), location.getZ()) :
entityArmorStandConstructor.invoke(worldServer));
if (!version.isAboveOrEqual(MinecraftVersion.V1_14_R1)) {
SET_LOCATION_METHOD.invoke(entityArmorStand, location.getX(), location.getY(), location.getZ(), 0, 0);
}
customNameMethod.invoke(entityArmorStand, version.isAboveOrEqual(MinecraftVersion.V1_12_R1) ?
CHAT_COMPONENT_TEXT_CONSTRUCTOR.invoke(line) : line);
customNameVisibilityMethod.invoke(entityArmorStand, true);
gravityMethod.invoke(entityArmorStand, version.isAboveOrEqual(MinecraftVersion.V1_9_R2));
SET_SMALL_METHOD.invoke(entityArmorStand, true);
SET_INVISIBLE_METHOD.invoke(entityArmorStand, true);
SET_BASE_PLATE_METHOD.invoke(entityArmorStand, false);
SET_ARMS_METHOD.invoke(entityArmorStand, false);
location.subtract(0, delta, 0);
if (line.isEmpty()) {
continue;
}
armorStands.add(entityArmorStand);
Object spawnPacket = PACKET_PLAY_OUT_SPAWN_ENTITY_LIVING_CONSTRUCTOR.invoke(entityArmorStand);
spawnPackets.add(spawnPacket);
Object destroyPacket = PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR
.invoke(new int[]{(int) GET_ID_METHOD.invoke(entityArmorStand)});
destroyPackets.add(destroyPacket);
}
}
// public void updateText(List<String> newLines) {
// if (lines.size() != newLines.size()) {
// throw new IllegalArgumentException("New NPC text cannot differ in size from old text.");
// return;
// }
//
// int i = 0;
// for (String oldLine : lines) {
// if (oldLine.isEmpty() && !newLines.get(i).isEmpty()) {
// // Need to spawn
// }
// i++;
// }
// customNameMethod.invoke(entityArmorStand, above_1_12_r1 ? CHAT_COMPONENT_TEXT_CONSTRUCTOR.invoke(line) : line);
// }
public void spawn(Player player) {
Object playerConnection = playerConnectionField.get(PLAYER_GET_HANDLE_METHOD
.invoke(CRAFT_PLAYER_CLAZZ.cast(player)));
for (Object packet : spawnPackets) {
SEND_PACKET_METHOD.invoke(playerConnection, packet);
}
}
public void destroy(Player player) {
Object playerConnection = playerConnectionField.get(PLAYER_GET_HANDLE_METHOD
.invoke(CRAFT_PLAYER_CLAZZ.cast(player)));
for (Object packet : destroyPackets) {
SEND_PACKET_METHOD.invoke(playerConnection, packet);
}
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.internal;
public enum MinecraftVersion {
V1_8_R1, V1_8_R2, V1_8_R3, V1_9_R1, V1_9_R2, V1_10_R1, V1_11_R1, V1_12_R1, V1_13_R1, V1_13_R2, V1_14_R1;
public boolean isAboveOrEqual(MinecraftVersion compare) {
return ordinal() >= compare.ordinal();
}
}
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.internal;
import java.util.HashSet;
import java.util.Set;
/**
* @author Jitse Boonstra
*/
public final class NPCManager {
private static Set<SimpleNPC> npcs = new HashSet<>();
public static Set<SimpleNPC> getAllNPCs() {
return npcs;
}
public static void add(SimpleNPC npc) {
npcs.add(npc);
}
public static void remove(SimpleNPC npc) {
npcs.remove(npc);
}
private NPCManager() {
throw new SecurityException("You cannot initialize this class.");
}
}
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.internal;
import org.bukkit.entity.Player;
/**
* @author Jitse Boonstra
*/
interface PacketHandler {
void createPackets();
void sendShowPackets(Player player);
void sendHidePackets(Player player, boolean scheduler);
}
@@ -0,0 +1,209 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.internal;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.api.events.NPCHideEvent;
import net.jitse.npclib.api.events.NPCShowEvent;
import net.jitse.npclib.api.skin.Skin;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.*;
public abstract class SimpleNPC implements NPC, PacketHandler {
protected final UUID uuid = UUID.randomUUID();
protected final int entityId = Integer.MAX_VALUE - NPCManager.getAllNPCs().size();
protected final String name = uuid.toString().replace("-", "").substring(0, 10);
protected final GameProfile gameProfile = new GameProfile(uuid, name);
protected final List<String> lines;
private final Set<UUID> shown = new HashSet<>();
private final Set<UUID> autoHidden = new HashSet<>();
protected double cosFOV = Math.cos(Math.toRadians(60));
protected NPCLib instance;
protected Location location;
protected Skin skin;
public SimpleNPC(NPCLib instance, List<String> lines) {
this.instance = instance;
this.lines = lines == null ? Collections.emptyList() : lines;
NPCManager.add(this);
}
protected GameProfile generateGameProfile(UUID uuid, String name) {
GameProfile gameProfile = new GameProfile(uuid, name);
if (skin != null) {
gameProfile.getProperties().get("textures").clear();
gameProfile.getProperties().get("textures").add(new Property(skin.getValue(), skin.getSignature()));
}
return gameProfile;
}
public NPCLib getInstance() {
return instance;
}
@Override
public String getId() {
return name;
}
@Override
public NPC setSkin(Skin skin) {
this.skin = skin;
return this;
}
@Override
public void destroy() {
destroy(true);
}
public void destroy(boolean scheduler) {
NPCManager.remove(this);
// Destroy NPC for every player that is still seeing it.
for (UUID uuid : shown) {
if (autoHidden.contains(uuid)) {
continue;
}
hide(Bukkit.getPlayer(uuid), true, scheduler);
}
}
public void disableFOV() {
this.cosFOV = 0; // Or equals Math.cos(1/2 * Math.PI).
}
public void setFOV(double fov) {
this.cosFOV = Math.cos(Math.toRadians(fov));
}
public Set<UUID> getShown() {
return shown;
}
public Set<UUID> getAutoHidden() {
return autoHidden;
}
@Override
public Location getLocation() {
return location;
}
public int getEntityId() {
return entityId;
}
@Override
public boolean isShown(Player player) {
return shown.contains(player.getUniqueId()) && !autoHidden.contains(player.getUniqueId());
}
@Override
public NPC setLocation(Location location) {
this.location = location;
return this;
}
@Override
public NPC create() {
createPackets();
return this;
}
@Override
public void show(Player player) {
show(player, false);
}
public void show(Player player, boolean auto) {
NPCShowEvent event = new NPCShowEvent(this, player, auto);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
if (!canSeeNPC(player)) {
if (!auto) {
shown.add(player.getUniqueId());
}
autoHidden.add(player.getUniqueId());
return;
}
if (auto) {
sendShowPackets(player);
} else {
if (isShown(player)) {
throw new RuntimeException("Cannot call show method twice.");
}
if (shown.contains(player.getUniqueId())) {
return;
}
shown.add(player.getUniqueId());
if (player.getWorld().equals(location.getWorld()) && player.getLocation().distance(location)
<= instance.getAutoHideDistance()) {
sendShowPackets(player);
} else {
autoHidden.add(player.getUniqueId());
}
}
}
private boolean canSeeNPC(Player player) {
Vector dir = location.toVector().subtract(player.getEyeLocation().toVector()).normalize();
return dir.dot(player.getLocation().getDirection()) >= cosFOV;
}
@Override
public void hide(Player player) {
hide(player, false, true);
}
public void hide(Player player, boolean auto, boolean scheduler) {
NPCHideEvent event = new NPCHideEvent(this, player, auto);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
if (auto) {
sendHidePackets(player, scheduler);
} else {
if (!shown.contains(player.getUniqueId())) {
throw new RuntimeException("Cannot call hide method without calling NPC#show.");
}
shown.remove(player.getUniqueId());
if (player.getWorld().equals(location.getWorld()) && player.getLocation().distance(location)
<= instance.getAutoHideDistance()) {
sendHidePackets(player, scheduler);
} else {
autoHidden.remove(player.getUniqueId());
}
}
}
}
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.listeners;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.internal.NPCManager;
import net.jitse.npclib.internal.SimpleNPC;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import java.util.UUID;
/**
* @author Jitse Boonstras
*/
public class ChunkListener implements Listener {
private final NPCLib instance;
public ChunkListener(NPCLib instance) {
this.instance = instance;
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
Chunk chunk = event.getChunk();
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
Chunk npcChunk = npc.getLocation().getChunk();
if (chunk.equals(npcChunk)) {
// Unloaded chunk with NPC in it. Hiding it from all players currently shown to.
for (UUID uuid : npc.getShown()) {
// Safety check so it doesn't send packets if the NPC has already
// been automatically despawned by the system.
if (npc.getAutoHidden().contains(uuid)) {
continue;
}
npc.hide(Bukkit.getPlayer(uuid), true, true);
}
}
}
}
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
Chunk chunk = event.getChunk();
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
Chunk npcChunk = npc.getLocation().getChunk();
if (chunk.equals(npcChunk)) {
// Loaded chunk with NPC in it. Showing it to the players again.
for (UUID uuid : npc.getShown()) {
// Make sure not to respawn a not-hidden NPC.
if (!npc.getAutoHidden().contains(uuid)) {
continue;
}
Player player = Bukkit.getPlayer(uuid);
if (!npcChunk.getWorld().equals(player.getWorld())) {
continue; // Player and NPC are not in the same world.
}
double hideDistance = instance.getAutoHideDistance();
double distanceSquared = player.getLocation().distanceSquared(npc.getLocation());
boolean inRange = distanceSquared <= (hideDistance * hideDistance) || distanceSquared <= (Bukkit.getViewDistance() << 4);
// Show the NPC (if in range).
if (inRange) {
npc.show(player, true);
}
}
}
}
}
}
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.listeners;
import com.comphenix.tinyprotocol.Reflection;
import com.comphenix.tinyprotocol.TinyProtocol;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.events.NPCInteractEvent;
import net.jitse.npclib.internal.NPCManager;
import net.jitse.npclib.internal.SimpleNPC;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
* @author Jitse Boonstra
*/
public class PacketListener {
// Classes:
private final Class<?> packetPlayInUseEntityClazz = Reflection.getMinecraftClass("PacketPlayInUseEntity");
// Fields:
private final Reflection.FieldAccessor entityIdField = Reflection.getField(packetPlayInUseEntityClazz, "a", int.class);
private final Reflection.FieldAccessor actionField = Reflection.getField(packetPlayInUseEntityClazz, "action", Object.class);
// Prevent players from clicking at very high speeds.
private final Set<UUID> delay = new HashSet<>();
private Plugin plugin;
public void start(NPCLib instance) {
this.plugin = instance.getPlugin();
new TinyProtocol(instance) {
@Override
public Object onPacketInAsync(Player player, Object packet) {
return handleInteractPacket(player, packet) ? super.onPacketInAsync(player, packet) : null;
}
};
}
private boolean handleInteractPacket(Player player, Object packet) {
if (packetPlayInUseEntityClazz.isInstance(packet)) {
SimpleNPC npc = NPCManager.getAllNPCs().stream().filter(
check -> check.isShown(player) && check.getEntityId() == (int) entityIdField.get(packet))
.findFirst().orElse(null);
if (npc == null) {
// Default player, not doing magic with the packet.
return true;
}
if (delay.contains(player.getUniqueId())) {
return false;
}
NPCInteractEvent.ClickType clickType = actionField.get(packet).toString().equals("ATTACK")
? NPCInteractEvent.ClickType.LEFT_CLICK : NPCInteractEvent.ClickType.RIGHT_CLICK;
Bukkit.getScheduler().runTask(plugin, () ->
Bukkit.getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc)));
UUID uuid = player.getUniqueId();
delay.add(uuid);
Bukkit.getScheduler().runTask(plugin, () -> delay.remove(uuid));
return false;
}
return true;
}
}
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2018 Jitse Boonstra
*/
package net.jitse.npclib.listeners;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.internal.NPCManager;
import net.jitse.npclib.internal.SimpleNPC;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
/**
* @author Jitse Boonstra
*/
public class PlayerListener implements Listener {
private final NPCLib instance;
public PlayerListener(NPCLib instance) {
this.instance = instance;
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
npc.getAutoHidden().remove(player.getUniqueId());
// Don't need to use NPC#hide since the entity is not registered in the NMS server.
npc.getShown().remove(player.getUniqueId());
}
}
@EventHandler
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
Player player = event.getPlayer();
World from = event.getFrom();
// The PlayerTeleportEvent is call, and will handle visibility in the new world.
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
if (npc.getLocation().getWorld().equals(from)) {
if (!npc.getAutoHidden().contains(player.getUniqueId())) {
npc.getAutoHidden().add(player.getUniqueId());
npc.hide(player, true, false);
}
}
}
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
handleMove(event.getPlayer());
}
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
handleMove(event.getPlayer());
}
private void handleMove(Player player) {
World world = player.getWorld();
for (SimpleNPC npc : NPCManager.getAllNPCs()) {
if (!npc.getShown().contains(player.getUniqueId())) {
continue; // NPC was never supposed to be shown to the player.
}
if (!npc.getLocation().getWorld().equals(world)) {
continue; // NPC is not in the same world.
}
// If Bukkit doesn't track the NPC entity anymore, bypass the hiding distance variable.
// This will cause issues otherwise (e.g. custom skin disappearing).
double hideDistance = instance.getAutoHideDistance();
double distanceSquared = player.getLocation().distanceSquared(npc.getLocation());
boolean inRange = distanceSquared <= (Math.pow(hideDistance, 2))
&& distanceSquared <= (Math.pow(Bukkit.getViewDistance() << 4, 2));
if (npc.getAutoHidden().contains(player.getUniqueId())) {
// Check if the player and NPC are within the range to sendShowPackets it again.
if (inRange) {
npc.getAutoHidden().remove(player.getUniqueId());
npc.show(player, true);
}
} else {
// Check if the player and NPC are out of range to sendHidePackets it.
if (!inRange) {
npc.getAutoHidden().add(player.getUniqueId());
npc.hide(player, true, true);
}
}
}
}
}