Cure NPCs of skin loss once and for all

...hopefully.
This commit is contained in:
Morphan1 2015-03-11 22:35:24 -04:00
parent bdade4a077
commit 54abeece09
2 changed files with 48 additions and 22 deletions

View File

@ -1,5 +1,6 @@
package net.citizensnpcs;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -279,33 +280,38 @@ public class EventListen implements Listener {
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerJoin(final PlayerJoinEvent event) {
new BukkitRunnable() {
@Override
public void run() {
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
if (player == null || !player.isValid())
return;
Location location = player.getLocation().getBlock().getLocation();
final List<EntityPlayer> nearbyNPCs = new ArrayList<EntityPlayer>();
for (NPC npc : getAllNPCs()) {
Entity entity = npc.getEntity();
if (entity instanceof Player && entity.getLocation().distanceSquared(location) < 200*200) {
final EntityPlayer entitynpc = ((CraftPlayer) entity).getHandle();
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, entitynpc));
Entity npcEntity = npc.getEntity();
if (npcEntity instanceof Player && player.canSee((Player) npcEntity)) {
nearbyNPCs.add(((CraftPlayer) npcEntity).getHandle());
}
}
new BukkitRunnable() {
@Override
public void run() {
if (!player.isValid())
return;
for (EntityPlayer nearbyNPC : nearbyNPCs) {
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entitynpc));
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, nearbyNPC));
}
new BukkitRunnable() {
@Override
public void run() {
if (!player.isValid())
return;
for (EntityPlayer nearbyNPC : nearbyNPCs) {
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, nearbyNPC));
}
}
}.runTaskLater(CitizensAPI.getPlugin(), 2);
}
}
}
}.runTaskLater(CitizensAPI.getPlugin(), 30);
}.runTaskLater(CitizensAPI.getPlugin(), 40);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
@ -23,12 +24,15 @@ import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_8_R2.Packet;
import net.minecraft.server.v1_8_R2.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_8_R2.PacketPlayOutPlayerInfo;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
@ -38,6 +42,7 @@ import org.bukkit.metadata.FixedMetadataValue;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import org.bukkit.scheduler.BukkitRunnable;
public class CitizensNPC extends AbstractNPC {
private EntityController entityController;
@ -216,7 +221,22 @@ public class CitizensNPC extends AbstractNPC {
NMS.setStepHeight(NMS.getHandle(entity), 1);
}
if (getEntity() instanceof Player) {
NMS.replaceTrackerEntry((Player) getEntity());
final CraftPlayer player = (CraftPlayer) getEntity();
NMS.replaceTrackerEntry(player);
new BukkitRunnable() {
@Override
public void run() {
NMS.sendPacketsNearby(player, player.getLocation(), Arrays.asList((Packet) new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, player.getHandle())), 200.0);
new BukkitRunnable() {
@Override
public void run() {
NMS.sendPacketsNearby(player, player.getLocation(), Arrays.asList((Packet) new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, player.getHandle())), 200.0);
}
}.runTaskLater(CitizensAPI.getPlugin(), 2);
}
}.runTaskLater(CitizensAPI.getPlugin(), 2);
}
}
return true;