Maybe make the entire runnable this much less laggier.

This commit is contained in:
libraryaddict 2014-01-21 05:29:14 +13:00
parent 7da523c783
commit 62c2050587
2 changed files with 51 additions and 43 deletions

@ -5,6 +5,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
@ -104,9 +105,7 @@ public abstract class Disguise {
// Ok.. So it aint a horse // Ok.. So it aint a horse
} }
} }
double fallSpeed = 0.0005; final boolean alwaysSendVelocity;
boolean movement = false;
boolean alwaysSend = false;
switch (getType()) { switch (getType()) {
case EGG: case EGG:
case ENDER_PEARL: case ENDER_PEARL:
@ -118,14 +117,16 @@ public abstract class Disguise {
case SPLASH_POTION: case SPLASH_POTION:
case THROWN_EXP_BOTTLE: case THROWN_EXP_BOTTLE:
case WITHER_SKULL: case WITHER_SKULL:
alwaysSend = true; alwaysSendVelocity = true;
break; break;
default: default:
alwaysSendVelocity = false;
break; break;
} }
double velocitySpeed = 0.0005;
switch (getType()) { switch (getType()) {
case FIREWORK: case FIREWORK:
fallSpeed = -0.040; velocitySpeed = -0.040;
break; break;
case EGG: case EGG:
case ENDER_PEARL: case ENDER_PEARL:
@ -135,17 +136,17 @@ public abstract class Disguise {
case SNOWBALL: case SNOWBALL:
case SPLASH_POTION: case SPLASH_POTION:
case THROWN_EXP_BOTTLE: case THROWN_EXP_BOTTLE:
fallSpeed = 0.0005; velocitySpeed = 0.0005;
break; break;
case WITHER_SKULL: case WITHER_SKULL:
fallSpeed = 0.000001D; velocitySpeed = 0.000001D;
break; break;
case ARROW: case ARROW:
case BOAT: case BOAT:
case ENDER_CRYSTAL: case ENDER_CRYSTAL:
case ENDER_DRAGON: case ENDER_DRAGON:
case GHAST: case GHAST:
// case ITEM_FRAME: case ITEM_FRAME:
case MINECART: case MINECART:
case MINECART_CHEST: case MINECART_CHEST:
case MINECART_FURNACE: case MINECART_FURNACE:
@ -155,44 +156,39 @@ public abstract class Disguise {
case PAINTING: case PAINTING:
case PLAYER: case PLAYER:
case SQUID: case SQUID:
fallSpeed = 0; velocitySpeed = 0;
break; break;
case DROPPED_ITEM: case DROPPED_ITEM:
case PRIMED_TNT: case PRIMED_TNT:
case WITHER: case WITHER:
case FALLING_BLOCK: case FALLING_BLOCK:
fallSpeed = 0.04; velocitySpeed = 0.04;
break; break;
case EXPERIENCE_ORB: case EXPERIENCE_ORB:
fallSpeed = 0.0221; velocitySpeed = 0.0221;
movement = true;
break; break;
case SPIDER: case SPIDER:
case CAVE_SPIDER: case CAVE_SPIDER:
fallSpeed = 0.0040; velocitySpeed = 0.0040;
break; break;
default: default:
break; break;
} }
final boolean sendMovementPacket = movement; final double vectorY = velocitySpeed;
final double vectorY = fallSpeed;
final boolean alwaysSendVelocity = alwaysSend;
final TargetedDisguise disguise = (TargetedDisguise) this; final TargetedDisguise disguise = (TargetedDisguise) this;
// A scheduler to clean up any unused disguises. // A scheduler to clean up any unused disguises.
velocityRunnable = new BukkitRunnable() { velocityRunnable = new BukkitRunnable() {
private int i = 0; private int refreshDisguise = 0;
public void run() { public void run() {
// If entity is no longer valid. Remove it. // If entity is no longer valid. Remove it.
if (!getEntity().isValid()) { if (!getEntity().isValid()) {
DisguiseAPI.undisguiseToAll(getEntity()); removeDisguise();
} else { } else {
// If the disguise type is tnt, we need to resend the entity packet else it will turn invisible // If the disguise type is tnt, we need to resend the entity packet else it will turn invisible
if (getType() == DisguiseType.PRIMED_TNT || getType() == DisguiseType.FIREWORK) { if (getType() == DisguiseType.PRIMED_TNT || getType() == DisguiseType.FIREWORK) {
i++; if (refreshDisguise++ % 40 == 0) {
if (i % 40 == 0) { refreshDisguise = 0;
i = 0;
DisguiseUtilities.refreshTrackers(disguise); DisguiseUtilities.refreshTrackers(disguise);
if (getEntity() instanceof Player && isSelfDisguiseVisible()) { if (getEntity() instanceof Player && isSelfDisguiseVisible()) {
DisguiseUtilities.sendSelfDisguise((Player) getEntity()); DisguiseUtilities.sendSelfDisguise((Player) getEntity());
@ -206,10 +202,12 @@ public abstract class Disguise {
// If this disguise has velocity sending enabled and the entity is flying. // If this disguise has velocity sending enabled and the entity is flying.
if (vectorY != 0 && isVelocitySent() && (alwaysSendVelocity || !getEntity().isOnGround())) { if (vectorY != 0 && isVelocitySent() && (alwaysSendVelocity || !getEntity().isOnGround())) {
Vector vector = getEntity().getVelocity(); Vector vector = getEntity().getVelocity();
// If the entity doesn't have velocity changes already // If the entity doesn't have velocity changes already - You know. I really can't wrap my head about the if statement.
// But it doesn't seem to do anything wrong..
if (vector.getY() != 0 && !(vector.getY() < 0 && alwaysSendVelocity && getEntity().isOnGround())) { if (vector.getY() != 0 && !(vector.getY() < 0 && alwaysSendVelocity && getEntity().isOnGround())) {
return; return;
} }
// If disguise isn't a experience orb, or the entity isn't standing on the ground
if (getType() != DisguiseType.EXPERIENCE_ORB || !getEntity().isOnGround()) { if (getType() != DisguiseType.EXPERIENCE_ORB || !getEntity().isOnGround()) {
PacketContainer lookPacket = null; PacketContainer lookPacket = null;
if (getType() == DisguiseType.WITHER_SKULL) { if (getType() == DisguiseType.WITHER_SKULL) {
@ -221,7 +219,8 @@ public abstract class Disguise {
4, 4,
PacketsManager.getYaw(getType(), getEntity().getType(), PacketsManager.getYaw(getType(), getEntity().getType(),
(byte) Math.floor(loc.getYaw() * 256.0F / 360.0F))); (byte) Math.floor(loc.getYaw() * 256.0F / 360.0F)));
mods.write(5, (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)); mods.write(5, PacketsManager.getPitch(getType(), DisguiseType.getType(getEntity().getType()),
(byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)));
if (isSelfDisguiseVisible() && getEntity() instanceof Player) { if (isSelfDisguiseVisible() && getEntity() instanceof Player) {
PacketContainer selfLookPacket = lookPacket.shallowClone(); PacketContainer selfLookPacket = lookPacket.shallowClone();
selfLookPacket.getModifier().write(0, DisguiseAPI.getFakeDisguise(getEntity().getEntityId())); selfLookPacket.getModifier().write(0, DisguiseAPI.getFakeDisguise(getEntity().getEntityId()));
@ -234,10 +233,11 @@ public abstract class Disguise {
} }
} }
try { try {
Field ping = ReflectionManager.getNmsClass("EntityPlayer").getField("ping"); PacketContainer velocityPacket = new PacketContainer(PacketType.Play.Server.ENTITY_VELOCITY);
StructureModifier<Integer> mods = velocityPacket.getIntegers();
mods.write(1, (int) (vector.getX() * 8000));
mods.write(3, (int) (vector.getZ() * 8000));
for (Player player : DisguiseUtilities.getPerverts(disguise)) { for (Player player : DisguiseUtilities.getPerverts(disguise)) {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_VELOCITY);
StructureModifier<Object> mods = packet.getModifier();
if (getEntity() == player) { if (getEntity() == player) {
if (!isSelfDisguiseVisible()) { if (!isSelfDisguiseVisible()) {
continue; continue;
@ -246,34 +246,31 @@ public abstract class Disguise {
} else { } else {
mods.write(0, getEntity().getEntityId()); mods.write(0, getEntity().getEntityId());
} }
mods.write(1, (int) (vector.getX() * 8000)); mods.write(2, (int) (8000D * (vectorY * ReflectionManager.getPing(player)) * 0.069D));
mods.write(
2,
(int) (8000 * (vectorY * (double) ping.getInt(ReflectionManager.getNmsEntity(player)) * 0.069)));
mods.write(3, (int) (vector.getZ() * 8000));
if (lookPacket != null) { if (lookPacket != null) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, lookPacket); ProtocolLibrary.getProtocolManager().sendServerPacket(player, lookPacket);
} }
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); ProtocolLibrary.getProtocolManager().sendServerPacket(player, velocityPacket);
velocityPacket = velocityPacket.shallowClone();
mods = velocityPacket.getIntegers();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
// If we need to send more packets because else it still 'sinks' // If we need to send a packet to update the exp orbs position as it likes to gravitate client sided to
if (sendMovementPacket) { // players.
if (getType() == DisguiseType.EXPERIENCE_ORB) {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE); PacketContainer packet = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE);
StructureModifier<Object> mods = packet.getModifier(); packet.getIntegers().write(0, getEntity().getEntityId());
mods.write(0, getEntity().getEntityId()); try {
for (Player player : DisguiseUtilities.getPerverts(disguise)) { for (Player player : DisguiseUtilities.getPerverts(disguise)) {
if (DisguiseAPI.isViewDisguises() || getEntity() != player) { if (DisguiseAPI.isViewDisguises() || getEntity() != player) {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
} }
} }
} catch (InvocationTargetException e) {
e.printStackTrace();
} }
} }
} }

@ -53,6 +53,16 @@ public class ReflectionManager {
private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
private static Method damageAndIdleSoundMethod; private static Method damageAndIdleSoundMethod;
private static Class itemClass; private static Class itemClass;
private static Field pingField;
public static double getPing(Player player) {
try {
return (double) pingField.getInt(ReflectionManager.getNmsEntity(player));
} catch (Exception ex) {
ex.printStackTrace();
}
return 0D;
}
static { static {
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
@ -73,6 +83,7 @@ public class ReflectionManager {
} }
try { try {
itemClass = getCraftClass("inventory.CraftItemStack"); itemClass = getCraftClass("inventory.CraftItemStack");
pingField = getNmsClass("EntityPlayer").getField("ping");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }