Skip to content

Commit

Permalink
Cure NPCs of skin loss once and for all
Browse files Browse the repository at this point in the history
...hopefully.
  • Loading branch information
Morphan1 committed Mar 12, 2015
1 parent d451158 commit d080dc7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
48 changes: 27 additions & 21 deletions src/main/java/net/citizensnpcs/EventListen.java
@@ -1,5 +1,6 @@
package net.citizensnpcs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -279,33 +280,38 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerJoin(final PlayerJoinEvent event) {
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
Location location = player.getLocation().getBlock().getLocation();
final List<EntityPlayer> nearbyNPCs = new ArrayList<EntityPlayer>();
for (NPC npc : getAllNPCs()) {
Entity npcEntity = npc.getEntity();
if (npcEntity instanceof Player && player.canSee((Player) npcEntity)) {
nearbyNPCs.add(((CraftPlayer) npcEntity).getHandle());
}
}
new BukkitRunnable() {
@Override
public void run() {
final Player player = event.getPlayer();
if (player == null || !player.isValid())
if (!player.isValid())
return;
Location location = player.getLocation().getBlock().getLocation();
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));
new BukkitRunnable() {
@Override
public void run() {
if (!player.isValid())
return;
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entitynpc));
}
}.runTaskLater(CitizensAPI.getPlugin(), 2);
}
for (EntityPlayer nearbyNPC : nearbyNPCs) {
NMS.sendPacket(player, new PacketPlayOutPlayerInfo(
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)
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/net/citizensnpcs/npc/CitizensNPC.java
@@ -1,5 +1,6 @@
package net.citizensnpcs.npc;

import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;

Expand All @@ -23,12 +24,15 @@
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;
Expand All @@ -38,6 +42,7 @@

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;
Expand Down Expand Up @@ -216,7 +221,22 @@ public boolean spawn(Location at) {
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;
Expand Down

1 comment on commit d080dc7

@whoshenry
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still unable to get the Skins to maintain for the client. They disappear if you move too far away or change worlds. I am using the latest version of this and the latest ProtocolLIB.

Please sign in to comment.