+17
@@ -0,0 +1,17 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Intellij
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
*.iws
|
||||||
|
out
|
||||||
|
|
||||||
|
# Mac
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Maven
|
||||||
|
log/
|
||||||
|
target/
|
||||||
@@ -11,6 +11,7 @@ import net.jitse.npclib.events.NPCDestroyEvent;
|
|||||||
import net.jitse.npclib.events.NPCSpawnEvent;
|
import net.jitse.npclib.events.NPCSpawnEvent;
|
||||||
import net.jitse.npclib.events.trigger.TriggerType;
|
import net.jitse.npclib.events.trigger.TriggerType;
|
||||||
import net.jitse.npclib.skin.Skin;
|
import net.jitse.npclib.skin.Skin;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
@@ -38,27 +39,35 @@ public abstract class NPC {
|
|||||||
protected Location location;
|
protected Location location;
|
||||||
|
|
||||||
public NPC(JavaPlugin plugin, Skin skin, double autoHideDistance, List<String> lines) {
|
public NPC(JavaPlugin plugin, Skin skin, double autoHideDistance, List<String> lines) {
|
||||||
if (skin == null) {
|
|
||||||
throw new IllegalArgumentException("Skin cannot be null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.skin = skin;
|
this.skin = skin;
|
||||||
this.autoHideDistance = autoHideDistance;
|
this.autoHideDistance = autoHideDistance;
|
||||||
this.lines = (lines == null ? new ArrayList<>() : lines);
|
this.lines = (lines == null ? Collections.emptyList() : lines);
|
||||||
|
|
||||||
NPCManager.add(this);
|
NPCManager.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected GameProfile generateGameProfile(UUID uuid, String name) {
|
protected GameProfile generateGameProfile(UUID uuid, String name) {
|
||||||
GameProfile gameProfile = new GameProfile(uuid, name);
|
GameProfile gameProfile = new GameProfile(uuid, name);
|
||||||
gameProfile.getProperties().removeAll("textures");
|
|
||||||
|
if (skin != null) {
|
||||||
gameProfile.getProperties().put("textures", new Property("textures", skin.getValue(), skin.getSignature()));
|
gameProfile.getProperties().put("textures", new Property("textures", skin.getValue(), skin.getSignature()));
|
||||||
|
}
|
||||||
|
|
||||||
return gameProfile;
|
return gameProfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
NPCManager.remove(this);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<UUID> getShown() {
|
public Set<UUID> getShown() {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerMoveEvent;
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jitse Boonstra
|
* @author Jitse Boonstra
|
||||||
@@ -26,23 +27,31 @@ public class PlayerMoveListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player player = event.getPlayer();
|
handleMove(event.getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
||||||
|
handleMove(event.getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMove(Player player) {
|
||||||
for (NPC npc : NPCManager.getAllNPCs()) {
|
for (NPC npc : NPCManager.getAllNPCs()) {
|
||||||
if (!npc.getShown().contains(player.getUniqueId())) {
|
if (!npc.getShown().contains(player.getUniqueId())) {
|
||||||
continue; // NPC was never supposed to be shown to the player.
|
continue; // NPC was never supposed to be shown to the player.
|
||||||
}
|
}
|
||||||
|
|
||||||
double distance = player.getLocation().distance(npc.getLocation());
|
double hideDistance = npc.getAutoHideDistance();
|
||||||
|
boolean inRange = player.getLocation().distanceSquared(npc.getLocation()) <= (hideDistance * hideDistance);
|
||||||
if (npc.getAutoHidden().contains(player.getUniqueId())) {
|
if (npc.getAutoHidden().contains(player.getUniqueId())) {
|
||||||
// Check if the player and NPC are within the range to sendShowPackets it again.
|
// Check if the player and NPC are within the range to sendShowPackets it again.
|
||||||
if (distance <= npc.getAutoHideDistance()) {
|
if (inRange) {
|
||||||
npc.show(player, true);
|
npc.show(player, true);
|
||||||
npc.getAutoHidden().remove(player.getUniqueId());
|
npc.getAutoHidden().remove(player.getUniqueId());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check if the player and NPC are out of range to sendHidePackets it.
|
// Check if the player and NPC are out of range to sendHidePackets it.
|
||||||
if (distance > npc.getAutoHideDistance()) {
|
if (!inRange) {
|
||||||
npc.hide(player, true);
|
npc.hide(player, true);
|
||||||
npc.getAutoHidden().add(player.getUniqueId());
|
npc.getAutoHidden().add(player.getUniqueId());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import net.jitse.npclib.NPCLib;
|
|||||||
import net.jitse.npclib.api.NPC;
|
import net.jitse.npclib.api.NPC;
|
||||||
import net.jitse.npclib.plugin.listeners.NPCListener;
|
import net.jitse.npclib.plugin.listeners.NPCListener;
|
||||||
import net.jitse.npclib.skin.MineSkinFetcher;
|
import net.jitse.npclib.skin.MineSkinFetcher;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@@ -28,8 +27,8 @@ public class NPCLibPlugin extends JavaPlugin implements Listener {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
this.npcLib = new NPCLib(this);
|
this.npcLib = new NPCLib(this);
|
||||||
Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " + ChatColor.WHITE + "plugin enabled.");
|
getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " + ChatColor.WHITE + "plugin enabled.");
|
||||||
Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " +
|
getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " +
|
||||||
ChatColor.GRAY + "This is a test plugin usually used for development reasons. " +
|
ChatColor.GRAY + "This is a test plugin usually used for development reasons. " +
|
||||||
"You can spawn NPCs by pressing [shift] in game.");
|
"You can spawn NPCs by pressing [shift] in game.");
|
||||||
|
|
||||||
@@ -40,7 +39,7 @@ public class NPCLibPlugin extends JavaPlugin implements Listener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " + ChatColor.WHITE + "plugin disabled.");
|
getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[NPCLib] " + ChatColor.WHITE + "plugin disabled.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@@ -57,7 +56,7 @@ public class NPCLibPlugin extends JavaPlugin implements Listener {
|
|||||||
));
|
));
|
||||||
npc.create(event.getPlayer().getLocation());
|
npc.create(event.getPlayer().getLocation());
|
||||||
|
|
||||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
for (Player player : getServer().getOnlinePlayers()) {
|
||||||
npc.show(player);
|
npc.show(player);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user