Skip to content

Commit

Permalink
Refactoring, remove some allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 8, 2014
1 parent 0de267c commit dfb2999
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 88 deletions.
103 changes: 41 additions & 62 deletions src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -29,12 +29,10 @@
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;
Expand All @@ -49,7 +47,12 @@
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;
Expand Down Expand Up @@ -291,92 +294,61 @@ public void run() {
}, 60);
}

Location roundLocation(Location input) {
return new Location(input.getWorld(), Math.floor(input.getX()),
Math.floor(input.getY()), Math.floor(input.getZ()));
@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) {
public void onPlayerTeleports(PlayerTeleportEvent 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()) {
Location npcPos = new Location(null, 0, 0, 0);
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);
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);
}
}
}
}

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) {
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+.
return;
}
if (from.getWorld() != to.getWorld()) {
return; // Ignore cross-world movement
}
int maxRad = 50 * 50; // TODO: Adjust me to perfection
for (final NPC npc: getAllNPCs()) {
Location loc = 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(loc);
if (from.getWorld() == loc.getWorld() && loc.distanceSquared(to) < maxRad
&& loc.distanceSquared(from) > maxRad) {
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(ignoreCancelled = true)
public void onVehicleEnter(VehicleEnterEvent event) {
if (!npcRegistry.isNPC(event.getEntered()))
Expand Down Expand Up @@ -435,6 +407,13 @@ private void respawnAllFromCoord(ChunkCoord coord) {
}
}

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) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/net/citizensnpcs/npc/entity/HumanController.java
Expand Up @@ -17,8 +17,6 @@
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;

Expand All @@ -27,7 +25,6 @@
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;
Expand Down Expand Up @@ -67,7 +64,7 @@ protected Entity createEntity(final Location at, final NPC npc) {
if (uuid.version() == 4) { // clear version
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
msb |= 0x0000000000002000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}

Expand All @@ -90,7 +87,8 @@ public void run() {
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);
}
Expand All @@ -106,7 +104,7 @@ public Player getBukkitEntity() {

@Override
public void remove() {
NMS.sendPlayerlistPacket(false, null, (CraftPlayer)getBukkitEntity());
NMS.sendPlayerlistPacket(false, null, getBukkitEntity());
super.remove();
}

Expand Down
100 changes: 80 additions & 20 deletions src/main/java/net/citizensnpcs/util/NMS.java
Expand Up @@ -10,13 +10,38 @@
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;
Expand Down Expand Up @@ -327,6 +352,11 @@ public static void removeFromServerPlayerList(Player player) {
((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;
Expand Down Expand Up @@ -361,6 +391,32 @@ public static void sendPacketsNearby(Player from, Location location, Packet... p
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()) {
Expand Down Expand Up @@ -436,6 +492,29 @@ public static boolean shouldJump(net.minecraft.server.v1_8_R1.Entity entity) {
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();
Expand Down Expand Up @@ -500,25 +579,6 @@ public static void updateNavigationWorld(org.bukkit.entity.Entity entity, org.bu
}
}

/**
* 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;
Expand Down

0 comments on commit dfb2999

Please sign in to comment.