mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-28 03:57:35 +01:00
Refactoring, remove some allocations
This commit is contained in:
parent
b5cda10271
commit
a6df72fa1b
@ -29,12 +29,10 @@ import net.citizensnpcs.trait.Controllable;
|
|||||||
import net.citizensnpcs.trait.CurrentLocation;
|
import net.citizensnpcs.trait.CurrentLocation;
|
||||||
import net.citizensnpcs.util.Messages;
|
import net.citizensnpcs.util.Messages;
|
||||||
import net.citizensnpcs.util.NMS;
|
import net.citizensnpcs.util.NMS;
|
||||||
import net.minecraft.server.v1_8_R1.*;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
|
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -49,7 +47,12 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
import org.bukkit.event.entity.EntityDamageEvent;
|
||||||
import org.bukkit.event.entity.EntityDeathEvent;
|
import org.bukkit.event.entity.EntityDeathEvent;
|
||||||
import org.bukkit.event.entity.EntityTargetEvent;
|
import org.bukkit.event.entity.EntityTargetEvent;
|
||||||
import org.bukkit.event.player.*;
|
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||||
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||||
import org.bukkit.event.vehicle.VehicleEnterEvent;
|
import org.bukkit.event.vehicle.VehicleEnterEvent;
|
||||||
import org.bukkit.event.world.ChunkLoadEvent;
|
import org.bukkit.event.world.ChunkLoadEvent;
|
||||||
import org.bukkit.event.world.ChunkUnloadEvent;
|
import org.bukkit.event.world.ChunkUnloadEvent;
|
||||||
@ -291,57 +294,16 @@ public class EventListen implements Listener {
|
|||||||
}, 60);
|
}, 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
Location roundLocation(Location input) {
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||||
return new Location(input.getWorld(), Math.floor(input.getX()),
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||||
Math.floor(input.getY()), Math.floor(input.getZ()));
|
Editor.leave(event.getPlayer());
|
||||||
}
|
if (event.getPlayer().isInsideVehicle()) {
|
||||||
|
NPC npc = npcRegistry.getNPC(event.getPlayer().getVehicle());
|
||||||
@EventHandler
|
if (npc != null) {
|
||||||
public void onPlayerWalks(final PlayerMoveEvent event) {
|
event.getPlayer().leaveVehicle();
|
||||||
Location from = roundLocation(event.getFrom());
|
|
||||||
Location to = roundLocation(event.getTo());
|
|
||||||
if (from.equals(to)) {
|
|
||||||
return; // Don't fire on every movement, just full block+.
|
|
||||||
}
|
|
||||||
if (from.getWorld() != to.getWorld()) {
|
|
||||||
return; // Ignore cross-world movement for now.
|
|
||||||
}
|
|
||||||
int maxRad = 50 * 50; // TODO: Adjust me to perfection
|
|
||||||
for (final NPC npc: getAllNPCs()) {
|
|
||||||
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
|
||||||
if (from.getWorld().getName().equalsIgnoreCase(npc.getEntity().getLocation().getWorld().getName())
|
|
||||||
&& npc.getEntity().getLocation().distanceSquared(to) < maxRad
|
|
||||||
&& npc.getEntity().getLocation().distanceSquared(from) > maxRad) {
|
|
||||||
showNPCReset(event.getPlayer(), npc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void showNPCReset(final Player player, final NPC npc) {
|
|
||||||
((CraftPlayer)player).getHandle().playerConnection.sendPacket
|
|
||||||
(new PacketPlayOutEntityDestroy(npc.getEntity().getEntityId()));
|
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (player.isOnline() && player.isValid()
|
|
||||||
&& npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
|
||||||
NMS.sendPlayerlistPacket(true, player, npc);
|
|
||||||
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutNamedEntitySpawn
|
|
||||||
(((CraftPlayer) npc.getEntity()).getHandle()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 1);
|
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (player.isOnline() && player.isValid()
|
|
||||||
&& npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
|
||||||
NMS.sendPlayerlistPacket(false, player, npc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 61);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerTeleports(PlayerTeleportEvent event) {
|
public void onPlayerTeleports(PlayerTeleportEvent event) {
|
||||||
@ -351,28 +313,38 @@ public class EventListen implements Listener {
|
|||||||
return; // Don't fire on every movement, just full block+.
|
return; // Don't fire on every movement, just full block+.
|
||||||
}
|
}
|
||||||
int maxRad = 50 * 50; // TODO: Adjust me to perfection
|
int maxRad = 50 * 50; // TODO: Adjust me to perfection
|
||||||
for (final NPC npc: getAllNPCs()) {
|
Location npcPos = new Location(null, 0, 0, 0);
|
||||||
|
for (final NPC npc : getAllNPCs()) {
|
||||||
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
||||||
// if to.world=npc.world and in-range, and if (from.world = npc.world and out-of-range, or from a different world)
|
npc.getEntity().getLocation(npcPos);
|
||||||
Location npcPos = npc.getEntity().getLocation();
|
if ((to.getWorld() == npcPos.getWorld() && npcPos.distanceSquared(to) < maxRad)
|
||||||
if ((to.getWorld() == npcPos.getWorld()
|
&& ((from.getWorld() == npcPos.getWorld() && npcPos.distanceSquared(from) > maxRad) || from
|
||||||
&& npcPos.distanceSquared(to) < maxRad)
|
.getWorld() != to.getWorld())) {
|
||||||
&& ((from.getWorld() == npcPos.getWorld()
|
NMS.showNPCReset(event.getPlayer(), npc);
|
||||||
&& npcPos.distanceSquared(from) > maxRad)
|
|
||||||
|| from.getWorld() != to.getWorld())) {
|
|
||||||
showNPCReset(event.getPlayer(), npc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
@EventHandler
|
||||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
public void onPlayerWalks(final PlayerMoveEvent event) {
|
||||||
Editor.leave(event.getPlayer());
|
Location from = roundLocation(event.getFrom());
|
||||||
if (event.getPlayer().isInsideVehicle()) {
|
Location to = roundLocation(event.getTo());
|
||||||
NPC npc = npcRegistry.getNPC(event.getPlayer().getVehicle());
|
if (from.equals(to)) {
|
||||||
if (npc != null) {
|
return;
|
||||||
event.getPlayer().leaveVehicle();
|
}
|
||||||
|
if (from.getWorld() != to.getWorld()) {
|
||||||
|
return; // Ignore cross-world movement
|
||||||
|
}
|
||||||
|
int maxRad = 50 * 50; // TODO: Adjust me to perfection
|
||||||
|
Location loc = new Location(null, 0, 0, 0);
|
||||||
|
for (final NPC npc : getAllNPCs()) {
|
||||||
|
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
||||||
|
npc.getEntity().getLocation(loc);
|
||||||
|
if (from.getWorld() == loc.getWorld() && loc.distanceSquared(to) < maxRad
|
||||||
|
&& loc.distanceSquared(from) > maxRad) {
|
||||||
|
NMS.showNPCReset(event.getPlayer(), npc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -435,6 +407,13 @@ public class EventListen implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Location roundLocation(Location input) {
|
||||||
|
input.setX(input.getBlockX());
|
||||||
|
input.setY(input.getBlockY());
|
||||||
|
input.setZ(input.getBlockZ());
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean spawn(NPC npc) {
|
private boolean spawn(NPC npc) {
|
||||||
Location spawn = npc.getTrait(CurrentLocation.class).getLocation();
|
Location spawn = npc.getTrait(CurrentLocation.class).getLocation();
|
||||||
if (spawn == null) {
|
if (spawn == null) {
|
||||||
|
@ -17,8 +17,6 @@ import net.citizensnpcs.api.util.Colorizer;
|
|||||||
import net.citizensnpcs.api.util.Messaging;
|
import net.citizensnpcs.api.util.Messaging;
|
||||||
import net.citizensnpcs.npc.AbstractEntityController;
|
import net.citizensnpcs.npc.AbstractEntityController;
|
||||||
import net.citizensnpcs.util.NMS;
|
import net.citizensnpcs.util.NMS;
|
||||||
import net.minecraft.server.v1_8_R1.EnumPlayerInfoAction;
|
|
||||||
import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerInfo;
|
|
||||||
import net.minecraft.server.v1_8_R1.PlayerInteractManager;
|
import net.minecraft.server.v1_8_R1.PlayerInteractManager;
|
||||||
import net.minecraft.server.v1_8_R1.WorldServer;
|
import net.minecraft.server.v1_8_R1.WorldServer;
|
||||||
|
|
||||||
@ -27,7 +25,6 @@ import org.bukkit.ChatColor;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
|
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
|
||||||
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
|
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
|
||||||
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
|
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -90,7 +87,8 @@ public class HumanController extends AbstractEntityController {
|
|||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// Double check that we're still spawned and haven't changed type.
|
// Double check that we're still spawned and haven't changed
|
||||||
|
// type.
|
||||||
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
|
||||||
NMS.sendPlayerlistPacket(false, null, npc);
|
NMS.sendPlayerlistPacket(false, null, npc);
|
||||||
}
|
}
|
||||||
@ -106,7 +104,7 @@ public class HumanController extends AbstractEntityController {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove() {
|
public void remove() {
|
||||||
NMS.sendPlayerlistPacket(false, null, (CraftPlayer)getBukkitEntity());
|
NMS.sendPlayerlistPacket(false, null, getBukkitEntity());
|
||||||
super.remove();
|
super.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,13 +10,38 @@ import java.util.Map;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
|
import net.citizensnpcs.api.CitizensAPI;
|
||||||
import net.citizensnpcs.api.command.exception.CommandException;
|
import net.citizensnpcs.api.command.exception.CommandException;
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.api.util.Messaging;
|
import net.citizensnpcs.api.util.Messaging;
|
||||||
import net.citizensnpcs.npc.ai.NPCHolder;
|
import net.citizensnpcs.npc.ai.NPCHolder;
|
||||||
import net.citizensnpcs.npc.entity.EntityHumanNPC;
|
import net.citizensnpcs.npc.entity.EntityHumanNPC;
|
||||||
import net.citizensnpcs.npc.network.EmptyChannel;
|
import net.citizensnpcs.npc.network.EmptyChannel;
|
||||||
import net.minecraft.server.v1_8_R1.*;
|
import net.minecraft.server.v1_8_R1.AttributeInstance;
|
||||||
|
import net.minecraft.server.v1_8_R1.Block;
|
||||||
|
import net.minecraft.server.v1_8_R1.BlockPosition;
|
||||||
|
import net.minecraft.server.v1_8_R1.ControllerJump;
|
||||||
|
import net.minecraft.server.v1_8_R1.DamageSource;
|
||||||
|
import net.minecraft.server.v1_8_R1.EnchantmentManager;
|
||||||
|
import net.minecraft.server.v1_8_R1.Entity;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityHorse;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityHuman;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityInsentient;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityLiving;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityMinecartAbstract;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityPlayer;
|
||||||
|
import net.minecraft.server.v1_8_R1.EntityTypes;
|
||||||
|
import net.minecraft.server.v1_8_R1.EnumPlayerInfoAction;
|
||||||
|
import net.minecraft.server.v1_8_R1.GenericAttributes;
|
||||||
|
import net.minecraft.server.v1_8_R1.MathHelper;
|
||||||
|
import net.minecraft.server.v1_8_R1.NavigationAbstract;
|
||||||
|
import net.minecraft.server.v1_8_R1.NetworkManager;
|
||||||
|
import net.minecraft.server.v1_8_R1.Packet;
|
||||||
|
import net.minecraft.server.v1_8_R1.PacketPlayOutEntityDestroy;
|
||||||
|
import net.minecraft.server.v1_8_R1.PacketPlayOutNamedEntitySpawn;
|
||||||
|
import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerInfo;
|
||||||
|
import net.minecraft.server.v1_8_R1.PathfinderGoalSelector;
|
||||||
|
import net.minecraft.server.v1_8_R1.World;
|
||||||
|
|
||||||
import org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -327,6 +352,11 @@ public class NMS {
|
|||||||
((CraftServer) Bukkit.getServer()).getHandle().players.remove(handle);
|
((CraftServer) Bukkit.getServer()).getHandle().players.remove(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void sendDestroyPacket(final Player player, final NPC npc) {
|
||||||
|
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutEntityDestroy(npc.getEntity()
|
||||||
|
.getEntityId()));
|
||||||
|
}
|
||||||
|
|
||||||
public static void sendPacket(Player player, Packet packet) {
|
public static void sendPacket(Player player, Packet packet) {
|
||||||
if (packet == null)
|
if (packet == null)
|
||||||
return;
|
return;
|
||||||
@ -361,6 +391,32 @@ public class NMS {
|
|||||||
NMS.sendPacketsNearby(from, location, Arrays.asList(packets), 64);
|
NMS.sendPacketsNearby(from, location, Arrays.asList(packets), 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a PlayerInfo packet (adds or removes the NPC to or from the tab
|
||||||
|
* list) to the player.
|
||||||
|
*
|
||||||
|
* @param player
|
||||||
|
* The player to send the packet to, or null for all players.
|
||||||
|
*/
|
||||||
|
public static void sendPlayerlistPacket(boolean showInPlayerlist, Player player, NPC npc) {
|
||||||
|
sendPlayerlistPacket(showInPlayerlist, player, (CraftPlayer) npc.getEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendPlayerlistPacket(boolean showInPlayerlist, Player player, Player npc) {
|
||||||
|
PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(showInPlayerlist ? EnumPlayerInfoAction.ADD_PLAYER
|
||||||
|
: EnumPlayerInfoAction.REMOVE_PLAYER, ((CraftPlayer) npc).getHandle());
|
||||||
|
if (player == null) {
|
||||||
|
sendToOnline(packet);
|
||||||
|
} else {
|
||||||
|
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sendSpawnPacket(final Player player, final NPC npc) {
|
||||||
|
((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutNamedEntitySpawn(
|
||||||
|
((CraftPlayer) npc.getEntity()).getHandle()));
|
||||||
|
}
|
||||||
|
|
||||||
public static void sendToOnline(Packet... packets) {
|
public static void sendToOnline(Packet... packets) {
|
||||||
Validate.notNull(packets, "packets cannot be null");
|
Validate.notNull(packets, "packets cannot be null");
|
||||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
@ -436,6 +492,29 @@ public class NMS {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void showNPCReset(final Player player, final NPC npc) {
|
||||||
|
sendDestroyPacket(player, npc);
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (player.isOnline() && player.isValid() && npc.isSpawned()
|
||||||
|
&& npc.getEntity().getType() == EntityType.PLAYER) {
|
||||||
|
sendPlayerlistPacket(true, player, npc);
|
||||||
|
sendSpawnPacket(player, npc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 1);
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (player.isOnline() && player.isValid() && npc.isSpawned()
|
||||||
|
&& npc.getEntity().getType() == EntityType.PLAYER) {
|
||||||
|
sendPlayerlistPacket(false, player, npc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 61);
|
||||||
|
}
|
||||||
|
|
||||||
public static org.bukkit.entity.Entity spawnCustomEntity(org.bukkit.World world, Location at,
|
public static org.bukkit.entity.Entity spawnCustomEntity(org.bukkit.World world, Location at,
|
||||||
Class<? extends Entity> clazz, EntityType type) {
|
Class<? extends Entity> clazz, EntityType type) {
|
||||||
World handle = ((CraftWorld) world).getHandle();
|
World handle = ((CraftWorld) world).getHandle();
|
||||||
@ -500,25 +579,6 @@ public class NMS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Send a PlayerInfo packet (adds or removes the NPC to or from the tab list) to the player.
|
|
||||||
* @param player The player to send the packet to, or null for all players.
|
|
||||||
*/
|
|
||||||
public static void sendPlayerlistPacket(boolean showInPlayerlist, Player player, NPC npc) {
|
|
||||||
sendPlayerlistPacket(showInPlayerlist, player, (CraftPlayer)npc.getEntity());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendPlayerlistPacket(boolean showInPlayerlist, Player player, CraftPlayer npc) {
|
|
||||||
PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(showInPlayerlist ? EnumPlayerInfoAction.ADD_PLAYER:
|
|
||||||
EnumPlayerInfoAction.REMOVE_PLAYER, npc.getHandle());
|
|
||||||
if (player == null) {
|
|
||||||
sendToOnline(packet);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
|
public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
|
||||||
if (!npc.isSpawned() || !npc.getEntity().getType().isAlive())
|
if (!npc.isSpawned() || !npc.getEntity().getType().isAlive())
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user