Skip to content

Commit

Permalink
First pass at removing NPCs from the playerlist
Browse files Browse the repository at this point in the history
imperfect (causes pointless respawns) but a functional proof-of-concept.
  • Loading branch information
mcmonkey4eva committed Dec 4, 2014
1 parent afba671 commit 0d7eb8f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
54 changes: 46 additions & 8 deletions src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.util.Messages;
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.*;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
Expand All @@ -50,10 +49,7 @@
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;
Expand Down Expand Up @@ -277,13 +273,55 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
}

@EventHandler(ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
public void onPlayerJoin(final PlayerJoinEvent event) {
for (NPC npc : getAllNPCs()) {
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, ((CraftPlayer) npc
((CraftPlayer)event.getPlayer()).getHandle().playerConnection.sendPacket(
new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, ((CraftPlayer) npc
.getEntity()).getHandle()));
}
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
for (NPC npc : getAllNPCs()) {
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
((CraftPlayer)event.getPlayer()).getHandle().playerConnection.sendPacket(
new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER, ((CraftPlayer) npc
.getEntity()).getHandle()));
}
}
}
}, 60);
}

@EventHandler
public void onPlayerWalks(final PlayerMoveEvent event) {

This comment has been minimized.

Copy link
@intangir

intangir Dec 11, 2014

what is the purpose of this function exactly?
does it track player movement to decide who to show/hide npcs from? or is it for sending the packet to remove them from the tab list?

was this event handled before the 1.8 changes to npc/tab list?

its a pretty frequent event to put a handler on

This comment has been minimized.

Copy link
@mcmonkey4eva

mcmonkey4eva Dec 11, 2014

Author Member

It's my first proof-of-concept attempt at making it so when a player walks into range of an NPC, it sends the appropriate NPC spawn packet so the player can see the NPC. I tried to minimize the excessiveness of the event by making it only when the player moves across at least one entire block (rather than on every little movement)

and no, there wasn't any need for this before the tablist nonsense.

if (event.getFrom().getY() > 255 || event.getFrom().getY() < 0
|| event.getTo().getY() > 255 || event.getTo().getY() < 0) {
return; // Don't fire if players go outside the world, as that would be more difficult to handle.
}
Location from = event.getFrom().getBlock().getLocation();
Location to = event.getTo().getBlock().getLocation();
if (from.equals(to)) {
return; // Don't fire on every movement, just full block+.
}
int maxRad = 50 * 50; // TODO: Adjust me to perfection
for (final NPC npc: getAllNPCs()) {
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
if (npc.getEntity().getLocation().distanceSquared(to) < maxRad &&
npc.getEntity().getLocation().distanceSquared(from) > maxRad) {
// TODO: Replicate this with packets!
npc.despawn();
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
npc.spawn(npc.getStoredLocation());
}
}, 1);
}
}
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import net.minecraft.server.v1_8_R1.Packet;
import net.minecraft.server.v1_8_R1.PacketPlayOutEntityEquipment;
import net.minecraft.server.v1_8_R1.PacketPlayOutEntityHeadRotation;
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 Down Expand Up @@ -143,11 +142,6 @@ public PlayerControllerMove getControllerMove() {
return controllerMove;
}

private Packet getListPacket(Player player, boolean removeFromPlayerList) {
return new PacketPlayOutPlayerInfo(removeFromPlayerList ? EnumPlayerInfoAction.REMOVE_PLAYER
: EnumPlayerInfoAction.ADD_PLAYER, ((CraftPlayer) player).getHandle());
}

public NavigationAbstract getNavigation() {
return navigation;
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/net/citizensnpcs/npc/entity/HumanController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;

import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -66,7 +67,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 @@ -86,6 +87,16 @@ public void run() {
}, 1);
handle.getBukkitEntity().setSleepingIgnored(true);
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, handle));
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
// Double check that we're still spawned and haven't changed type.
if (npc.isSpawned() && npc.getEntity().getType() == EntityType.PLAYER) {
NMS.sendToOnline(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER,
((CraftPlayer) getBukkitEntity()).getHandle()));
}
}
}, 60);
return handle.getBukkitEntity();
}

Expand Down

0 comments on commit 0d7eb8f

Please sign in to comment.