Refactoring, remove some allocations

This commit is contained in:
fullwall 2014-12-08 15:45:09 +08:00
parent b5cda10271
commit a6df72fa1b
3 changed files with 130 additions and 93 deletions

View File

@ -29,12 +29,10 @@ import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_8_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
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.EntityDeathEvent;
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.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
@ -291,58 +294,17 @@ public class EventListen implements Listener {
}, 60);
}
Location roundLocation(Location input) {
return new Location(input.getWorld(), Math.floor(input.getX()),
Math.floor(input.getY()), Math.floor(input.getZ()));
}
@EventHandler
public void onPlayerWalks(final PlayerMoveEvent event) {
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);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer());
if (event.getPlayer().isInsideVehicle()) {
NPC npc = npcRegistry.getNPC(event.getPlayer().getVehicle());
if (npc != null) {
event.getPlayer().leaveVehicle();
}
}
}
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
public void onPlayerTeleports(PlayerTeleportEvent event) {
Location from = roundLocation(event.getFrom());
@ -351,28 +313,38 @@ public class EventListen implements Listener {
return; // Don't fire on every movement, just full block+.
}
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 to.world=npc.world and in-range, and if (from.world = npc.world and out-of-range, or from a different world)
Location npcPos = npc.getEntity().getLocation();
if ((to.getWorld() == npcPos.getWorld()
&& npcPos.distanceSquared(to) < maxRad)
&& ((from.getWorld() == npcPos.getWorld()
&& npcPos.distanceSquared(from) > maxRad)
|| from.getWorld() != to.getWorld())) {
showNPCReset(event.getPlayer(), npc);
npc.getEntity().getLocation(npcPos);
if ((to.getWorld() == npcPos.getWorld() && npcPos.distanceSquared(to) < maxRad)
&& ((from.getWorld() == npcPos.getWorld() && npcPos.distanceSquared(from) > maxRad) || from
.getWorld() != to.getWorld())) {
NMS.showNPCReset(event.getPlayer(), npc);
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
Editor.leave(event.getPlayer());
if (event.getPlayer().isInsideVehicle()) {
NPC npc = npcRegistry.getNPC(event.getPlayer().getVehicle());
if (npc != null) {
event.getPlayer().leaveVehicle();
@EventHandler
public void onPlayerWalks(final PlayerMoveEvent event) {
Location from = roundLocation(event.getFrom());
Location to = roundLocation(event.getTo());
if (from.equals(to)) {
return;
}
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) {
Location spawn = npc.getTrait(CurrentLocation.class).getLocation();
if (spawn == null) {

View File

@ -17,8 +17,6 @@ import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.AbstractEntityController;
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.WorldServer;
@ -27,7 +25,6 @@ import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
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.EntityType;
import org.bukkit.entity.Player;
@ -67,7 +64,7 @@ public class HumanController extends AbstractEntityController {
if (uuid.version() == 4) { // clear version
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
msb |= 0x0000000000002000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
@ -90,7 +87,8 @@ public class HumanController extends AbstractEntityController {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
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) {
NMS.sendPlayerlistPacket(false, null, npc);
}
@ -106,7 +104,7 @@ public class HumanController extends AbstractEntityController {
@Override
public void remove() {
NMS.sendPlayerlistPacket(false, null, (CraftPlayer)getBukkitEntity());
NMS.sendPlayerlistPacket(false, null, getBukkitEntity());
super.remove();
}

View File

@ -10,13 +10,38 @@ import java.util.Map;
import java.util.Random;
import java.util.WeakHashMap;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.entity.EntityHumanNPC;
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.bukkit.Bukkit;
@ -327,6 +352,11 @@ public class NMS {
((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) {
if (packet == null)
return;
@ -361,6 +391,32 @@ public class NMS {
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) {
Validate.notNull(packets, "packets cannot be null");
for (Player player : Bukkit.getOnlinePlayers()) {
@ -436,6 +492,29 @@ public class NMS {
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,
Class<? extends Entity> clazz, EntityType type) {
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) {
if (!npc.isSpawned() || !npc.getEntity().getType().isAlive())
return;