Merge pull request #320 from mcmonkey4eva/EntityPatch

Fix EntityTypes usage, fixes #239
This commit is contained in:
fullwall 2015-04-07 22:15:23 +08:00
commit 36cf6e8ea8
2 changed files with 56 additions and 26 deletions

View File

@ -52,10 +52,7 @@ 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.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
@ -279,22 +276,56 @@ public class EventListen implements Listener {
Bukkit.getPluginManager().callEvent(rightClickEvent);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
recalculatePlayer(event.getPlayer());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerTeleport(PlayerTeleportEvent event) {
recalculatePlayer(event.getPlayer());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerChangeWorld(PlayerChangedWorldEvent event) {
recalculatePlayer(event.getPlayer());
}
public void recalculatePlayer(final Player player) {
new BukkitRunnable() {
@Override
public void run() {
final List<EntityPlayer> nearbyNPCs = new ArrayList<EntityPlayer>();
for (NPC npc : getAllNPCs()) {
Entity npcEntity = npc.getEntity();
if (npcEntity instanceof Player && player.canSee((Player) npcEntity)) {
if (npcEntity instanceof Player && player.canSee((Player) npcEntity)
&& player.getWorld().equals(npcEntity.getWorld())
&& player.getLocation().distanceSquared(npcEntity.getLocation()) < 100 * 100) {
nearbyNPCs.add(((CraftPlayer) npcEntity).getHandle());
}
}
new BukkitRunnable() {
@Override
public void run() {
sendToPlayer(player, nearbyNPCs);
}
}.runTaskLater(CitizensAPI.getPlugin(), 30);
new BukkitRunnable() {
@Override
public void run() {
sendToPlayer(player, nearbyNPCs);
}
}.runTaskLater(CitizensAPI.getPlugin(), 70);
}
}.runTaskLater(CitizensAPI.getPlugin(), 10);
}
void sendToPlayer(final Player player, final List<EntityPlayer> nearbyNPCs) {
if (!player.isValid())
return;
for (EntityPlayer nearbyNPC : nearbyNPCs) {
if (nearbyNPC.isAlive())
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, nearbyNPC));
}
@ -304,14 +335,13 @@ public class EventListen implements Listener {
if (!player.isValid())
return;
for (EntityPlayer nearbyNPC : nearbyNPCs) {
if (nearbyNPC.isAlive())
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, nearbyNPC));
}
}
}.runTaskLater(CitizensAPI.getPlugin(), 2);
}
}.runTaskLater(CitizensAPI.getPlugin(), 40);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {

View File

@ -666,7 +666,7 @@ public class NMS {
try {
Field field = getField(EntityTypes.class, "f");
ENTITY_CLASS_TO_INT = (Map<Class<?>, Integer>) field.get(null);
field = getField(EntityTypes.class, "c");
field = getField(EntityTypes.class, "d");
ENTITY_CLASS_TO_NAME = (Map<Class<?>, String>) field.get(null);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());