parent
b8a7dcaddb
commit
e0752c5d7e
@ -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> |
@ -1,3 +1,7 @@ |
||||
/* |
||||
* Copyright (c) 2018 Jitse Boonstra |
||||
*/ |
||||
|
||||
package com.comphenix.tinyprotocol; |
||||
|
||||
import org.bukkit.Bukkit; |
@ -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,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,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(); |
||||
} |
||||
} |
@ -1,51 +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</artifactId> |
||||
<version>1.5-SNAPSHOT</version> |
||||
</parent> |
||||
|
||||
<artifactId>npclib-commons</artifactId> |
||||
|
||||
<!-- |
||||
<build> |
||||
<resources> |
||||
<resource> |
||||
<targetPath>.</targetPath> |
||||
<directory>${basedir}/src/main/java/net/jitse/npclib</directory> |
||||
<filtering>true</filtering> |
||||
<includes> |
||||
<include>NPCLib.java</include> |
||||
</includes> |
||||
</resource> |
||||
</resources> |
||||
</build> |
||||
--> |
||||
|
||||
<repositories> |
||||
<repository> |
||||
<id>spigot-repo</id> |
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> |
||||
</repository> |
||||
</repositories> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.spigotmc</groupId> |
||||
<artifactId>spigot-api</artifactId> |
||||
<version>1.13.2-R0.1-SNAPSHOT</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> |
@ -1,120 +0,0 @@ |
||||
/* |
||||
* Copyright (c) 2018 Jitse Boonstra |
||||
*/ |
||||
|
||||
package net.jitse.npclib; |
||||
|
||||
import net.jitse.npclib.api.NPC; |
||||
import net.jitse.npclib.listeners.ChunkListener; |
||||
import net.jitse.npclib.listeners.PacketListener; |
||||
import net.jitse.npclib.listeners.PlayerListener; |
||||
import net.jitse.npclib.logging.NPCLibLogger; |
||||
import net.jitse.npclib.skin.Skin; |
||||
import org.bukkit.plugin.PluginManager; |
||||
import org.bukkit.plugin.java.JavaPlugin; |
||||
|
||||
import java.util.List; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* @author Jitse Boonstra |
||||
*/ |
||||
public class NPCLib { |
||||
|
||||
private final JavaPlugin plugin; |
||||
private final Class<?> npcClass; |
||||
|
||||
private Logger logger; |
||||
|
||||
public NPCLib(JavaPlugin plugin) { |
||||
this.plugin = plugin; |
||||
this.logger = new NPCLibLogger(plugin); |
||||
|
||||
// TODO: Change this variable to a dynamic variable (maven file filtering?).
|
||||
// logger.info("Initiating NPCLib v1.4");
|
||||
|
||||
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.log(Level.SEVERE, "Failed to initiate. Your server's version (" |
||||
+ versionName + ") is not supported"); |
||||
return; |
||||
} |
||||
|
||||
logger.info("Enabled for MC " + versionName); |
||||
|
||||
registerInternal(); |
||||
} |
||||
|
||||
private void registerInternal() { |
||||
PluginManager pluginManager = plugin.getServer().getPluginManager(); |
||||
|
||||
pluginManager.registerEvents(new PlayerListener(), plugin); |
||||
pluginManager.registerEvents(new ChunkListener(), plugin); |
||||
|
||||
// Boot the according packet listener.
|
||||
new PacketListener().start(plugin); |
||||
} |
||||
|
||||
/** |
||||
* Create a new non-player character (NPC). |
||||
* |
||||
* @param skin The skin you want the NPC to have. |
||||
* @param autoHideDistance Distance from where you want to NPC to hide from the player (50 recommended). |
||||
* @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(Skin skin, double autoHideDistance, List<String> lines) { |
||||
try { |
||||
return (NPC) npcClass.getConstructors()[0].newInstance(plugin, skin, autoHideDistance, lines); |
||||