Optimise 1.8 playerlist hack

This commit is contained in:
fullwall 2014-09-17 21:45:17 +08:00
parent 576e816399
commit aa360652f1

View File

@ -1,9 +1,8 @@
package net.citizensnpcs.npc.entity;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.Socket;
import java.util.List;
@ -43,7 +42,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
@ -158,7 +156,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
// gravity. also works around an entity.onGround not updating issue
// (onGround is normally updated by the client)
}
if (Math.abs(motX) < EPSILON && Math.abs(motY) < EPSILON && Math.abs(motZ) < EPSILON)
motX = motY = motZ = 0;
if (navigating) {
@ -261,7 +259,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
private void updatePackets(boolean navigating) {
if (world.getWorld().getFullTime() % Setting.PACKET_UPDATE_DELAY.asInt() == 0) {
Location current = getBukkitEntity().getLocation(packetLocationCache);
Location current = getBukkitEntity().getLocation(packetLocationCache);
Packet[] packets = new Packet[navigating ? 6 : 7];
if (!navigating) {
packets[6] = new PacketPlayOutEntityHeadRotation(this,
@ -270,42 +268,28 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
for (int i = 0; i < 5; i++) {
packets[i] = new PacketPlayOutEntityEquipment(getId(), i, getEquipment(i));
}
boolean removeFromPlayerList = npc.data().get("removefromplayerlist", Setting.REMOVE_PLAYERS_FROM_PLAYER_LIST.asBoolean());
NMS.addOrRemoveFromPlayerList(getBukkitEntity(),removeFromPlayerList);
boolean removeFromPlayerList = npc.data().get("removefromplayerlist",
Setting.REMOVE_PLAYERS_FROM_PLAYER_LIST.asBoolean());
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);
int useListName = removeFromPlayerList ? 0 : 1;
if (useListName != this.useListName || this.useListName == -1) {
this.useListName = useListName;
}
boolean error = false;//Set to true if we cant get the protocol hack methods
//probably means that a server is using an older version of spigot / bukkit
try {
Class<?> c = PacketPlayOutPlayerInfo.class;
Object t = c.newInstance();
Method m = c.getDeclaredMethod("addPlayer", new Class[]{EntityPlayer.class});
if (removeFromPlayerList){
m = c.getDeclaredMethod("removePlayer", new Class[]{EntityPlayer.class});
}
m.setAccessible(true);
packets[5] = (Packet)m.invoke(t, getBukkitEntity().getHandle());
} catch (Exception e) {
error = true;
//Don't print a stack trace here, It will give an error if we are
//pre 1.8
}
if (error) {
//Pre protocol hack methods
try {
packets[5] = PacketPlayOutPlayerInfo.class.getConstructor(String.class,boolean.class,int.class).newInstance(getBukkitEntity().getPlayerListName(),!removeFromPlayerList,removeFromPlayerList ? 9999 : ping);
} catch (Exception e) {
e.printStackTrace();
}
}
if (PLAYER_INFO_CONSTRUCTOR != null) {
try {
packets[5] = PLAYER_INFO_CONSTRUCTOR.newInstance(getBukkitEntity().getPlayerListName(),
!removeFromPlayerList, removeFromPlayerList ? 9999 : ping);
} catch (Exception e) {
}
} else {
try {
packets[5] = (Packet) (removeFromPlayerList ? PLAYER_INFO_REMOVE_METHOD.invoke(null,
getBukkitEntity().getHandle()) : PLAYER_INFO_ADD_METHOD.invoke(null, getBukkitEntity()
.getHandle()));
} catch (Exception e) {
}
}
NMS.sendPacketsNearby(getBukkitEntity(), current, packets);
}
}
@ -362,5 +346,23 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
private static final float EPSILON = 0.005F;
private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
}
private static Method PLAYER_INFO_ADD_METHOD;
private static Constructor<PacketPlayOutPlayerInfo> PLAYER_INFO_CONSTRUCTOR;
private static Method PLAYER_INFO_REMOVE_METHOD;
static {
try {
PLAYER_INFO_CONSTRUCTOR = PacketPlayOutPlayerInfo.class.getConstructor(String.class, boolean.class,
int.class);
} catch (Exception e) {
}
try {
PLAYER_INFO_ADD_METHOD = PacketPlayOutPlayerInfo.class.getMethod("addPlayer", EntityPlayer.class);
PLAYER_INFO_REMOVE_METHOD = PacketPlayOutPlayerInfo.class.getMethod("removePlayer", EntityPlayer.class);
PLAYER_INFO_ADD_METHOD.setAccessible(true);
PLAYER_INFO_REMOVE_METHOD.setAccessible(true);
} catch (Exception e) {
}
}
}