Rework packet sending to avoid replacing the entity tracker entry

This commit is contained in:
fullwall 2020-05-16 19:13:19 +08:00
parent df5ed08e32
commit e7368747c7
7 changed files with 72 additions and 117 deletions

View File

@ -33,6 +33,7 @@ import net.citizensnpcs.nms.v1_14_R1.util.PlayerControllerJump;
import net.citizensnpcs.nms.v1_14_R1.util.PlayerControllerLook;
import net.citizensnpcs.nms.v1_14_R1.util.PlayerControllerMove;
import net.citizensnpcs.nms.v1_14_R1.util.PlayerNavigation;
import net.citizensnpcs.nms.v1_14_R1.util.PlayerlistTracker;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.skin.SkinPacketTracker;
@ -69,11 +70,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
private PlayerControllerJump controllerJump;
private PlayerControllerLook controllerLook;
private PlayerControllerMove controllerMove;
private boolean isTracked = false;
private int jumpTicks = 0;
private PlayerNavigation navigation;
private final CitizensNPC npc;
private final Location packetLocationCache = new Location(null, 0, 0, 0);
private PlayerlistTracker playerlistTracker;
private final SkinPacketTracker skinTracker;
private int updateCounter = 0;
@ -100,7 +101,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
@Override
public boolean a(EntityPlayer entityplayer) {
if (npc != null && !isTracked) {
if (npc != null && playerlistTracker == null) {
return false;
}
return super.a(entityplayer);
@ -330,6 +331,14 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
}
}
@Override
public Packet<?> N() {
if (playerlistTracker != null) {
playerlistTracker.updateLastPlayer();
}
return super.N();
}
@Override
public void playerTick() {
if (npc == null) {
@ -386,8 +395,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
controllerLook.a(target.getX(), target.getY(), target.getZ(), 10, 40);
}
public void setTracked() {
isTracked = true;
public void setTracked(PlayerlistTracker tracker) {
this.playerlistTracker = tracker;
}
@Override

View File

@ -2,6 +2,11 @@ package net.citizensnpcs.nms.v1_14_R1.util;
import java.lang.invoke.MethodHandle;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.nms.v1_14_R1.entity.EntityHumanNPC;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_14_R1.Entity;
@ -11,21 +16,31 @@ import net.minecraft.server.v1_14_R1.PlayerChunkMap;
import net.minecraft.server.v1_14_R1.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private EntityPlayer lastUpdatedPlayer;
private final Entity tracker;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
setTrackerEntry(new TablistRemovingTrackerEntry(map.world, entity, j, flag, this::broadcast, trackedPlayers));
this.tracker = entity;
}
public PlayerlistTracker(PlayerChunkMap map, EntityTracker entry) {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(entry));
}
private void setTrackerEntry(EntityTrackerEntry entry) {
try {
TRACKER_ENTRY_SETTER.invoke(this, entry);
} catch (Throwable e) {
e.printStackTrace();
}
public void updateLastPlayer() {
if (tracker.dead)
return;
final EntityPlayer entityplayer = lastUpdatedPlayer;
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
if (!Setting.DISABLE_TABLIST.asBoolean())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
});
}
@Override
@ -76,6 +91,5 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e");
private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker");
private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry");
private static final MethodHandle TRACKER_ENTRY_SETTER = NMS.getSetter(EntityTracker.class, "trackerEntry");
private static final MethodHandle TRACKING_DISTANCE = NMS.getGetter(EntityTracker.class, "trackingDistance");
}

View File

@ -1,46 +0,0 @@
package net.citizensnpcs.nms.v1_14_R1.util;
import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_14_R1.Entity;
import net.minecraft.server.v1_14_R1.EntityPlayer;
import net.minecraft.server.v1_14_R1.EntityTrackerEntry;
import net.minecraft.server.v1_14_R1.Packet;
import net.minecraft.server.v1_14_R1.PlayerConnection;
import net.minecraft.server.v1_14_R1.WorldServer;
public class TablistRemovingTrackerEntry extends EntityTrackerEntry {
private final Entity tracker;
public TablistRemovingTrackerEntry(WorldServer worldserver, Entity entity, int i, boolean flag,
Consumer<Packet<?>> consumer, Set<EntityPlayer> trackedPlayers) {
super(worldserver, entity, i, flag, consumer, trackedPlayers);
this.tracker = entity;
}
@Override
public void b(EntityPlayer entityplayer) {
PlayerConnection playerconnection = entityplayer.playerConnection;
if (!tracker.dead) {
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
this.a(playerconnection::sendPacket, entityplayer);
this.tracker.b(entityplayer);
entityplayer.d(this.tracker);
if (!tracker.dead && Setting.DISABLE_TABLIST.asBoolean()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
});
}
}
}

View File

@ -33,6 +33,7 @@ import net.citizensnpcs.nms.v1_15_R1.util.PlayerControllerJump;
import net.citizensnpcs.nms.v1_15_R1.util.PlayerControllerLook;
import net.citizensnpcs.nms.v1_15_R1.util.PlayerControllerMove;
import net.citizensnpcs.nms.v1_15_R1.util.PlayerNavigation;
import net.citizensnpcs.nms.v1_15_R1.util.PlayerlistTracker;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.skin.SkinPacketTracker;
@ -69,11 +70,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
private PlayerControllerJump controllerJump;
private PlayerControllerLook controllerLook;
private PlayerControllerMove controllerMove;
private boolean isTracked = false;
private int jumpTicks = 0;
private PlayerNavigation navigation;
private final CitizensNPC npc;
private final Location packetLocationCache = new Location(null, 0, 0, 0);
private PlayerlistTracker playerlistTracker;
private final SkinPacketTracker skinTracker;
private int updateCounter = 0;
@ -100,7 +101,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
@Override
public boolean a(EntityPlayer entityplayer) {
if (npc != null && !isTracked) {
if (npc != null && playerlistTracker == null) {
return false;
}
return super.a(entityplayer);
@ -312,6 +313,14 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
return npc.getNavigator().isNavigating();
}
@Override
public Packet<?> L() {
if (playerlistTracker != null) {
playerlistTracker.updateLastPlayer();
}
return super.L();
}
private void moveOnCurrentHeading() {
if (jumping) {
if (onGround && jumpTicks == 0) {
@ -386,8 +395,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
controllerLook.a(target.getX(), target.getY(), target.getZ());
}
public void setTracked() {
isTracked = true;
public void setTracked(PlayerlistTracker tracker) {
this.playerlistTracker = tracker;
}
@Override

View File

@ -980,7 +980,7 @@ public class NMSImpl implements NMSBridge {
PlayerlistTracker replace = new PlayerlistTracker(server.getChunkProvider().playerChunkMap, entry);
server.getChunkProvider().playerChunkMap.trackedEntities.put(player.getEntityId(), replace);
if (getHandle(player) instanceof EntityHumanNPC) {
((EntityHumanNPC) getHandle(player)).setTracked();
((EntityHumanNPC) getHandle(player)).setTracked(replace);
}
}

View File

@ -2,6 +2,11 @@ package net.citizensnpcs.nms.v1_15_R1.util;
import java.lang.invoke.MethodHandle;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.nms.v1_15_R1.entity.EntityHumanNPC;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_15_R1.Entity;
@ -11,27 +16,38 @@ import net.minecraft.server.v1_15_R1.PlayerChunkMap;
import net.minecraft.server.v1_15_R1.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private EntityPlayer lastUpdatedPlayer;
private final Entity tracker;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
setTrackerEntry(new TablistRemovingTrackerEntry(map.world, entity, j, flag, this::broadcast, trackedPlayers));
this.tracker = entity;
}
public PlayerlistTracker(PlayerChunkMap map, EntityTracker entry) {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(entry));
}
private void setTrackerEntry(EntityTrackerEntry entry) {
try {
TRACKER_ENTRY_SETTER.invoke(this, entry);
} catch (Throwable e) {
e.printStackTrace();
}
public void updateLastPlayer() {
if (tracker.dead)
return;
final EntityPlayer entityplayer = lastUpdatedPlayer;
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
if (!Setting.DISABLE_TABLIST.asBoolean())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
});
}
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (!(entityplayer instanceof EntityHumanNPC)) {
// prevent updates to NPC "viewers"
this.lastUpdatedPlayer = entityplayer;
super.updatePlayer(entityplayer);
}
}
@ -76,6 +92,5 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e");
private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker");
private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry");
private static final MethodHandle TRACKER_ENTRY_SETTER = NMS.getSetter(EntityTracker.class, "trackerEntry");
private static final MethodHandle TRACKING_DISTANCE = NMS.getGetter(EntityTracker.class, "trackingDistance");
}

View File

@ -1,46 +0,0 @@
package net.citizensnpcs.nms.v1_15_R1.util;
import java.util.Set;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_15_R1.Entity;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.EntityTrackerEntry;
import net.minecraft.server.v1_15_R1.Packet;
import net.minecraft.server.v1_15_R1.PlayerConnection;
import net.minecraft.server.v1_15_R1.WorldServer;
public class TablistRemovingTrackerEntry extends EntityTrackerEntry {
private final Entity tracker;
public TablistRemovingTrackerEntry(WorldServer worldserver, Entity entity, int i, boolean flag,
Consumer<Packet<?>> consumer, Set<EntityPlayer> trackedPlayers) {
super(worldserver, entity, i, flag, consumer, trackedPlayers);
this.tracker = entity;
}
@Override
public void b(EntityPlayer entityplayer) {
PlayerConnection playerconnection = entityplayer.playerConnection;
if (!tracker.dead) {
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
this.a(playerconnection::sendPacket, entityplayer);
this.tracker.b(entityplayer);
entityplayer.d(this.tracker);
if (!tracker.dead && Setting.DISABLE_TABLIST.asBoolean()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
});
}
}
}