Read desc

Moved more methods into DisguiseUtilities
Renamed things to look better
This commit is contained in:
libraryaddict 2013-11-23 09:04:31 +13:00
parent 93ef0a26d4
commit a502792df2
4 changed files with 157 additions and 141 deletions

View File

@ -17,6 +17,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.MinecartWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.MinecartWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.SlimeWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.SlimeWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.utils.DisguiseUtilities;
import me.libraryaddict.disguise.utils.PacketsManager; import me.libraryaddict.disguise.utils.PacketsManager;
import me.libraryaddict.disguise.utils.ReflectionManager; import me.libraryaddict.disguise.utils.ReflectionManager;
@ -57,6 +58,7 @@ public class LibsDisguises extends JavaPlugin {
} }
} }
PacketsManager.init(this); PacketsManager.init(this);
DisguiseUtilities.init(this);
DisguiseAPI.setSoundsEnabled(getConfig().getBoolean("DisguiseSounds")); DisguiseAPI.setSoundsEnabled(getConfig().getBoolean("DisguiseSounds"));
DisguiseAPI.setVelocitySent(getConfig().getBoolean("SendVelocity")); DisguiseAPI.setVelocitySent(getConfig().getBoolean("SendVelocity"));
DisguiseAPI.setViewDisguises(getConfig().getBoolean("ViewSelfDisguises")); DisguiseAPI.setViewDisguises(getConfig().getBoolean("ViewSelfDisguises"));
@ -68,7 +70,7 @@ public class LibsDisguises extends JavaPlugin {
} }
try { try {
// Here I use reflection to set the plugin for Disguise.. // Here I use reflection to set the plugin for Disguise..
// Kinda stupid but I don't want open API calls. // Kind of stupid but I don't want open API calls for a commonly used object.
Field field = Disguise.class.getDeclaredField("plugin"); Field field = Disguise.class.getDeclaredField("plugin");
field.setAccessible(true); field.setAccessible(true);
field.set(null, this); field.set(null, this);
@ -90,6 +92,10 @@ public class LibsDisguises extends JavaPlugin {
registerValues(); registerValues();
} }
/**
* Here we create a nms entity for each disguise. Then grab their default values in their datawatcher. Then their sound volume
* for mob noises. As well as setting their watcher class and entity size.
*/
private void registerValues() { private void registerValues() {
for (DisguiseType disguiseType : DisguiseType.values()) { for (DisguiseType disguiseType : DisguiseType.values()) {
if (disguiseType.getEntityType() == null) { if (disguiseType.getEntityType() == null) {
@ -135,7 +141,7 @@ public class LibsDisguises extends JavaPlugin {
} }
} }
disguiseType.setWatcherClass(watcherClass); disguiseType.setWatcherClass(watcherClass);
String name = toReadable(disguiseType.name()); String nmsEntityName = toReadable(disguiseType.name());
switch (disguiseType) { switch (disguiseType) {
case WITHER_SKELETON: case WITHER_SKELETON:
case ZOMBIE_VILLAGER: case ZOMBIE_VILLAGER:
@ -145,61 +151,62 @@ public class LibsDisguises extends JavaPlugin {
case SKELETON_HORSE: case SKELETON_HORSE:
continue; continue;
case PRIMED_TNT: case PRIMED_TNT:
name = "TNTPrimed"; nmsEntityName = "TNTPrimed";
break; break;
case MINECART_TNT: case MINECART_TNT:
name = "MinecartTNT"; nmsEntityName = "MinecartTNT";
break; break;
case MINECART: case MINECART:
name = "MinecartRideable"; nmsEntityName = "MinecartRideable";
break; break;
case FIREWORK: case FIREWORK:
name = "Fireworks"; nmsEntityName = "Fireworks";
break; break;
case SPLASH_POTION: case SPLASH_POTION:
name = "Potion"; nmsEntityName = "Potion";
break; break;
case GIANT: case GIANT:
name = "GiantZombie"; nmsEntityName = "GiantZombie";
break; break;
case DROPPED_ITEM: case DROPPED_ITEM:
name = "Item"; nmsEntityName = "Item";
break; break;
case FIREBALL: case FIREBALL:
name = "LargeFireball"; nmsEntityName = "LargeFireball";
break; break;
case LEASH_HITCH: case LEASH_HITCH:
name = "Leash"; nmsEntityName = "Leash";
break; break;
default: default:
break; break;
} }
try { try {
Object entity = ReflectionManager.createEntityInstance(name); Object nmsEntity = ReflectionManager.createEntityInstance(nmsEntityName);
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity") Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity")
.invoke(entity); .invoke(nmsEntity);
int enumEntitySize = 0; int entitySize = 0;
for (Field field : ReflectionManager.getNmsClass("Entity").getFields()) { for (Field field : ReflectionManager.getNmsClass("Entity").getFields()) {
if (field.getType().getName().equals("EnumEntitySize")) { if (field.getType().getName().equals("EnumEntitySize")) {
Enum a = (Enum) field.get(entity); Enum enumEntitySize = (Enum) field.get(nmsEntity);
enumEntitySize = a.ordinal(); entitySize = enumEntitySize.ordinal();
break; break;
} }
} }
Values disguiseValues = new Values(disguiseType, entity.getClass(), enumEntitySize); Values disguiseValues = new Values(disguiseType, nmsEntity.getClass(), entitySize);
WrappedDataWatcher dataWatcher = WrappedDataWatcher.getEntityWatcher(bukkitEntity); WrappedDataWatcher dataWatcher = WrappedDataWatcher.getEntityWatcher(bukkitEntity);
List<WrappedWatchableObject> watchers = dataWatcher.getWatchableObjects(); List<WrappedWatchableObject> watchers = dataWatcher.getWatchableObjects();
for (WrappedWatchableObject watch : watchers) for (WrappedWatchableObject watch : watchers)
disguiseValues.setMetaValue(watch.getIndex(), watch.getValue()); disguiseValues.setMetaValue(watch.getIndex(), watch.getValue());
DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); DisguiseSound sound = DisguiseSound.getType(disguiseType.name());
if (sound != null) { if (sound != null) {
Float soundStrength = ReflectionManager.getSoundModifier(entity); Float soundStrength = ReflectionManager.getSoundModifier(nmsEntity);
if (soundStrength != null) { if (soundStrength != null) {
sound.setDamageSoundVolume((Float) soundStrength); sound.setDamageSoundVolume((Float) soundStrength);
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
System.out.print("[LibsDisguises] Trouble while making values for " + name + ": " + ex.getMessage()); System.out.print("[LibsDisguises] Trouble while making values for " + disguiseType.name() + ": "
+ ex.getMessage());
System.out.print("[LibsDisguises] Please report this to LibsDisguises author"); System.out.print("[LibsDisguises] Please report this to LibsDisguises author");
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@ -19,7 +19,7 @@ import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.utils.PacketsManager; import me.libraryaddict.disguise.utils.DisguiseUtilities;
import me.libraryaddict.disguise.utils.ReflectionManager; import me.libraryaddict.disguise.utils.ReflectionManager;
public class FlagWatcher { public class FlagWatcher {
@ -119,7 +119,7 @@ public class FlagWatcher {
float newHealth = (Float) value; float newHealth = (Float) value;
if (newHealth > 0 && hasDied) { if (newHealth > 0 && hasDied) {
hasDied = false; hasDied = false;
PacketsManager.sendSelfDisguise((Player) disguise.getEntity()); DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity());
} else if (newHealth <= 0 && !hasDied) { } else if (newHealth <= 0 && !hasDied) {
hasDied = true; hasDied = true;
} }

View File

@ -4,21 +4,30 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector;
import com.comphenix.protocol.Packets; import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class DisguiseUtilities { public class DisguiseUtilities {
// Store the entity IDs instead of entitys because then I can disguise entitys even before they exist // Store the entity IDs instead of entitys because then I can disguise entitys even before they exist
private static HashMap<Integer, Disguise> disguises = new HashMap<Integer, Disguise>(); private static HashMap<Integer, Disguise> disguises = new HashMap<Integer, Disguise>();
private static LibsDisguises libsDisguises;
// A internal storage of fake entity ID's I can use. // A internal storage of fake entity ID's I can use.
// Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID // Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID
private static HashMap<Integer, Integer> selfDisguisesIds = new HashMap<Integer, Integer>(); private static HashMap<Integer, Integer> selfDisguisesIds = new HashMap<Integer, Integer>();
@ -31,6 +40,10 @@ public class DisguiseUtilities {
return selfDisguisesIds; return selfDisguisesIds;
} }
public static void init(LibsDisguises disguises) {
libsDisguises = disguises;
}
/** /**
* @param Resends * @param Resends
* the entity to all the watching players, which is where the magic begins * the entity to all the watching players, which is where the magic begins
@ -89,14 +102,13 @@ public class DisguiseUtilities {
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
}// Resend entity metadata else he will be invisible to himself until its resent }
PacketContainer packetMetadata = new PacketContainer(Packets.Server.ENTITY_METADATA); // Resend entity metadata else he will be invisible to himself until its resent
StructureModifier<Object> mods = packetMetadata.getModifier();
mods.write(0, player.getEntityId());
packetMetadata.getWatchableCollectionModifier().write(0,
WrappedDataWatcher.getEntityWatcher(player).getWatchableObjects());
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packetMetadata); ProtocolLibrary.getProtocolManager().sendServerPacket(
player,
ProtocolLibrary.getProtocolManager().createPacketConstructor(Packets.Server.ENTITY_METADATA, player)
.createPacket(player));
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -104,6 +116,114 @@ public class DisguiseUtilities {
} }
} }
/**
* Sends the self disguise to the player
*/
public static void sendSelfDisguise(final Player player) {
try {
Object world = ReflectionManager.getWorld(player.getWorld());
Object tracker = world.getClass().getField("tracker").get(world);
Object trackedEntities = tracker.getClass().getField("trackedEntities").get(tracker);
Object entityTrackerEntry = trackedEntities.getClass().getMethod("get", int.class)
.invoke(trackedEntities, player.getEntityId());
if (entityTrackerEntry == null) {
// A check incase the tracker is null.
// If it is, then this method will be run again in one tick. Which is when it should be constructed.
// Else its going to run in a infinite loop hue hue hue..
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
sendSelfDisguise(player);
}
});
return;
}
// Add himself to his own entity tracker
((HashSet) entityTrackerEntry.getClass().getField("trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager
.getNmsEntity(player));
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
// Send the player a packet with himself being spawned
manager.sendServerPacket(player, manager.createPacketConstructor(Packets.Server.NAMED_ENTITY_SPAWN, player)
.createPacket(player));
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_METADATA, player.getEntityId(),
WrappedDataWatcher.getEntityWatcher(player), true).createPacket(player.getEntityId(),
WrappedDataWatcher.getEntityWatcher(player), true));
boolean isMoving = false;
try {
Field field = ReflectionManager.getNmsClass("EntityTrackerEntry").getDeclaredField("isMoving");
field.setAccessible(true);
isMoving = field.getBoolean(entityTrackerEntry);
} catch (Exception ex) {
ex.printStackTrace();
}
// Send the velocity packets
if (isMoving) {
Vector velocity = player.getVelocity();
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_VELOCITY, player.getEntityId(), velocity.getX(),
velocity.getY(), velocity.getZ()).createPacket(player.getEntityId(), velocity.getX(),
velocity.getY(), velocity.getZ()));
}
// Why the hell would he even need this. Meh.
if (player.getVehicle() != null && player.getEntityId() > player.getVehicle().getEntityId()) {
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.ATTACH_ENTITY, 0, player, player.getVehicle())
.createPacket(0, player, player.getVehicle()));
} else if (player.getPassenger() != null && player.getEntityId() > player.getPassenger().getEntityId()) {
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.ATTACH_ENTITY, 0, player.getPassenger(), player)
.createPacket(0, player.getPassenger(), player));
}
// Resend the armor
for (int i = 0; i < 5; i++) {
ItemStack item;
if (i == 0) {
item = player.getItemInHand();
} else {
item = player.getInventory().getArmorContents()[i - 1];
}
if (item != null && item.getType() != Material.AIR) {
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.ENTITY_EQUIPMENT, player.getEntityId(), i, item)
.createPacket(player.getEntityId(), i, item));
}
}
Location loc = player.getLocation();
// If the disguised is sleeping for w/e reason
if (player.isSleeping()) {
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_LOCATION_ACTION, player, 0, loc.getBlockX(),
loc.getBlockY(), loc.getBlockZ()).createPacket(player, 0, loc.getBlockX(), loc.getBlockY(),
loc.getBlockZ()));
}
// TODO Fix this cos it doesn't move the disguise?
byte yaw = (byte) (loc.getYaw() * 256.0F / 360.0F);
byte pitch = (byte) (loc.getPitch() * 256.0F / 360.0F);
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_LOOK, player.getEntityId(), yaw, pitch).createPacket(
player.getEntityId(), yaw, pitch));
// Resend any active potion effects
Iterator iterator = player.getActivePotionEffects().iterator();
while (iterator.hasNext()) {
PotionEffect potionEffect = (PotionEffect) iterator.next();
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.MOB_EFFECT, player.getEntityId(), potionEffect)
.createPacket(player.getEntityId(), potionEffect));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** /**
* Setup it so he can see himself when disguised * Setup it so he can see himself when disguised
*/ */
@ -128,7 +248,7 @@ public class DisguiseUtilities {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
PacketsManager.sendSelfDisguise(player); sendSelfDisguise(player);
if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) {
if (PacketsManager.isInventoryListenerEnabled()) { if (PacketsManager.isInventoryListenerEnabled()) {
player.updateInventory(); player.updateInventory();

View File

@ -1,9 +1,7 @@
package me.libraryaddict.disguise.utils; package me.libraryaddict.disguise.utils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -34,7 +32,6 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie; import org.bukkit.entity.Zombie;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.comphenix.protocol.Packets; import com.comphenix.protocol.Packets;
@ -958,114 +955,6 @@ public class PacketsManager {
return viewDisguisesListenerEnabled; return viewDisguisesListenerEnabled;
} }
/**
* Sends the self disguise to the player
*/
public static void sendSelfDisguise(final Player player) {
try {
Object world = ReflectionManager.getWorld(player.getWorld());
Object tracker = world.getClass().getField("tracker").get(world);
Object trackedEntities = tracker.getClass().getField("trackedEntities").get(tracker);
Object entityTrackerEntry = trackedEntities.getClass().getMethod("get", int.class)
.invoke(trackedEntities, player.getEntityId());
if (entityTrackerEntry == null) {
// A check incase the tracker is null.
// If it is, then this method will be run again in one tick. Which is when it should be constructed.
// Else its going to run in a infinite loop hue hue hue..
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() {
public void run() {
sendSelfDisguise(player);
}
});
return;
}
// Add himself to his own entity tracker
((HashSet) entityTrackerEntry.getClass().getField("trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager
.getNmsEntity(player));
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
// Send the player a packet with himself being spawned
manager.sendServerPacket(player, manager.createPacketConstructor(Packets.Server.NAMED_ENTITY_SPAWN, player)
.createPacket(player));
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_METADATA, player.getEntityId(),
WrappedDataWatcher.getEntityWatcher(player), true).createPacket(player.getEntityId(),
WrappedDataWatcher.getEntityWatcher(player), true));
boolean isMoving = false;
try {
Field field = ReflectionManager.getNmsClass("EntityTrackerEntry").getDeclaredField("isMoving");
field.setAccessible(true);
isMoving = field.getBoolean(entityTrackerEntry);
} catch (Exception ex) {
ex.printStackTrace();
}
// Send the velocity packets
if (isMoving) {
Vector velocity = player.getVelocity();
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_VELOCITY, player.getEntityId(), velocity.getX(),
velocity.getY(), velocity.getZ()).createPacket(player.getEntityId(), velocity.getX(),
velocity.getY(), velocity.getZ()));
}
// Why the hell would he even need this. Meh.
if (player.getVehicle() != null && player.getEntityId() > player.getVehicle().getEntityId()) {
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.ATTACH_ENTITY, 0, player, player.getVehicle())
.createPacket(0, player, player.getVehicle()));
} else if (player.getPassenger() != null && player.getEntityId() > player.getPassenger().getEntityId()) {
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.ATTACH_ENTITY, 0, player.getPassenger(), player)
.createPacket(0, player.getPassenger(), player));
}
// Resend the armor
for (int i = 0; i < 5; i++) {
ItemStack item;
if (i == 0) {
item = player.getItemInHand();
} else {
item = player.getInventory().getArmorContents()[i - 1];
}
if (item != null && item.getType() != Material.AIR) {
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.ENTITY_EQUIPMENT, player.getEntityId(), i, item)
.createPacket(player.getEntityId(), i, item));
}
}
Location loc = player.getLocation();
// If the disguised is sleeping for w/e reason
if (player.isSleeping()) {
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_LOCATION_ACTION, player, 0, loc.getBlockX(),
loc.getBlockY(), loc.getBlockZ()).createPacket(player, 0, loc.getBlockX(), loc.getBlockY(),
loc.getBlockZ()));
}
// TODO Fix this cos it doesn't move the disguise?
byte yaw = (byte) (loc.getYaw() * 256.0F / 360.0F);
byte pitch = (byte) (loc.getPitch() * 256.0F / 360.0F);
manager.sendServerPacket(
player,
manager.createPacketConstructor(Packets.Server.ENTITY_LOOK, player.getEntityId(), yaw, pitch).createPacket(
player.getEntityId(), yaw, pitch));
// Resend any active potion effects
Iterator iterator = player.getActivePotionEffects().iterator();
while (iterator.hasNext()) {
PotionEffect potionEffect = (PotionEffect) iterator.next();
manager.sendServerPacket(player,
manager.createPacketConstructor(Packets.Server.MOB_EFFECT, player.getEntityId(), potionEffect)
.createPacket(player.getEntityId(), potionEffect));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void setHearDisguisesListener(boolean enabled) { public static void setHearDisguisesListener(boolean enabled) {
if (soundsListenerEnabled != enabled) { if (soundsListenerEnabled != enabled) {
soundsListenerEnabled = enabled; soundsListenerEnabled = enabled;