Read desc

Removed a few classes to a new package
Renamed methods and deprecated them as retarded names.
Fixed DisguiseAPI instance opening new calls and just moved that into a
new class
This commit is contained in:
libraryaddict 2013-11-23 08:52:15 +13:00
parent 81933b90bd
commit 93ef0a26d4
23 changed files with 340 additions and 313 deletions

View File

@ -1,38 +1,24 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.events.DisguiseEvent; import me.libraryaddict.disguise.events.DisguiseEvent;
import me.libraryaddict.disguise.events.UndisguiseEvent; import me.libraryaddict.disguise.events.UndisguiseEvent;
import me.libraryaddict.disguise.utils.PacketsManager;
import me.libraryaddict.disguise.utils.ReflectionManager;
import me.libraryaddict.disguise.utils.DisguiseUtilities;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class DisguiseAPI { public class DisguiseAPI {
// 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 boolean hearSelfDisguise; private static boolean hearSelfDisguise;
private static boolean hidingArmor; private static boolean hidingArmor;
private static boolean hidingHeldItem; private static boolean hidingHeldItem;
// 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
private static HashMap<Integer, Integer> selfDisguisesIds = new HashMap<Integer, Integer>();
private static boolean sendVelocity; private static boolean sendVelocity;
@Deprecated
public static boolean canHearSelfDisguise() { public static boolean canHearSelfDisguise() {
return hearSelfDisguise; return hearSelfDisguise;
} }
@ -43,14 +29,14 @@ public class DisguiseAPI {
public static void disguiseNextEntity(Disguise disguise) { public static void disguiseNextEntity(Disguise disguise) {
if (disguise == null) if (disguise == null)
return; return;
if (disguise.getEntity() != null || disguises.containsValue(disguise)) { if (disguise.getEntity() != null || DisguiseUtilities.getDisguises().containsValue(disguise)) {
disguise = disguise.clone(); disguise = disguise.clone();
} }
try { try {
Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount"); Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount");
field.setAccessible(true); field.setAccessible(true);
int id = field.getInt(null); int id = field.getInt(null);
disguises.put(id, disguise); DisguiseUtilities.getDisguises().put(id, disguise);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -84,11 +70,11 @@ public class DisguiseAPI {
} // If there was a old disguise } // If there was a old disguise
Disguise oldDisguise = getDisguise(entity); Disguise oldDisguise = getDisguise(entity);
// Stick the disguise in the disguises bin // Stick the disguise in the disguises bin
disguises.put(entity.getEntityId(), disguise); DisguiseUtilities.getDisguises().put(entity.getEntityId(), disguise);
// Resend the disguised entity's packet // Resend the disguised entity's packet
refreshTrackers(entity); DisguiseUtilities.refreshTrackers(entity);
// If he is a player, then self disguise himself // If he is a player, then self disguise himself
setupPlayerFakeDisguise(disguise); DisguiseUtilities.setupFakeDisguise(disguise);
// Discard the disguise // Discard the disguise
if (oldDisguise != null) if (oldDisguise != null)
oldDisguise.removeDisguise(); oldDisguise.removeDisguise();
@ -97,28 +83,28 @@ public class DisguiseAPI {
/** /**
* Get the disguise of a entity * Get the disguise of a entity
*/ */
public static Disguise getDisguise(Entity disguiser) { public static Disguise getDisguise(Entity disguised) {
if (disguiser == null) if (disguised == null)
return null; return null;
if (disguises.containsKey(disguiser.getEntityId())) if (DisguiseUtilities.getDisguises().containsKey(disguised.getEntityId()))
return disguises.get(disguiser.getEntityId()); return DisguiseUtilities.getDisguises().get(disguised.getEntityId());
return null; return null;
} }
/** /**
* Get the ID of a fake disguise for a entityplayer * Get the ID of a fake disguise for a entityplayer
*/ */
public static int getFakeDisguise(int id) { public static int getFakeDisguise(int entityId) {
if (selfDisguisesIds.containsKey(id)) if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId))
return selfDisguisesIds.get(id); return DisguiseUtilities.getSelfDisguisesIds().get(entityId);
return -1; return -1;
} }
/** /**
* Is this entity disguised * Is this entity disguised
*/ */
public static boolean isDisguised(Entity disguiser) { public static boolean isDisguised(Entity disguised) {
return getDisguise(disguiser) != null; return getDisguise(disguised) != null;
} }
/** /**
@ -139,6 +125,10 @@ public class DisguiseAPI {
return PacketsManager.isInventoryListenerEnabled(); return PacketsManager.isInventoryListenerEnabled();
} }
public static boolean isSelfDisguisesSoundsReplaced() {
return hearSelfDisguise;
}
/** /**
* Is the sound packets caught and modified * Is the sound packets caught and modified
*/ */
@ -160,79 +150,6 @@ public class DisguiseAPI {
return PacketsManager.isViewDisguisesListenerEnabled(); return PacketsManager.isViewDisguisesListenerEnabled();
} }
/**
* @param Resends
* the entity to all the watching players, which is where the magic begins
*/
private static void refreshTrackers(Entity entity) {
try {
Object world = ReflectionManager.getWorld(entity.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, entity.getEntityId());
if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry);
Method getBukkitEntity = ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity");
Method clear = entityTrackerEntry.getClass().getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer"));
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object player : cloned) {
if (entity instanceof Player && !((Player) getBukkitEntity.invoke(player)).canSee((Player) entity))
continue;
clear.invoke(entityTrackerEntry, player);
updatePlayer.invoke(entityTrackerEntry, player);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void removeSelfDisguise(Player player) {
if (selfDisguisesIds.containsKey(player.getEntityId())) {
// Send a packet to destroy the fake entity
PacketContainer packet = new PacketContainer(Packets.Server.DESTROY_ENTITY);
packet.getModifier().write(0, new int[] { selfDisguisesIds.get(player.getEntityId()) });
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
} catch (Exception ex) {
ex.printStackTrace();
}
// Remove the fake entity ID from the disguise bin
selfDisguisesIds.remove(player.getEntityId());
// Get the entity tracker
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) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry);
// If the tracker exists. Remove himself from his tracker
trackedPlayers.remove(ReflectionManager.getNmsEntity(player));
}
} catch (Exception ex) {
ex.printStackTrace();
}// Resend entity metadata else he will be invisible to himself until its resent
PacketContainer packetMetadata = new PacketContainer(Packets.Server.ENTITY_METADATA);
StructureModifier<Object> mods = packetMetadata.getModifier();
mods.write(0, player.getEntityId());
packetMetadata.getWatchableCollectionModifier().write(0,
WrappedDataWatcher.getEntityWatcher(player).getWatchableObjects());
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packetMetadata);
} catch (Exception ex) {
ex.printStackTrace();
}
player.updateInventory();
}
}
/** /**
* Can players hear their own disguises * Can players hear their own disguises
*/ */
@ -273,38 +190,6 @@ public class DisguiseAPI {
PacketsManager.setHearDisguisesListener(isSoundsEnabled); PacketsManager.setHearDisguisesListener(isSoundsEnabled);
} }
/**
* Setup it so he can see himself when disguised
*/
private static void setupPlayerFakeDisguise(final Disguise disguise) {
// If the disguises entity is null, or the disguised entity isn't a player return
if (disguise.getEntity() == null || !(disguise.getEntity() instanceof Player) || !disguises.containsValue(disguise))
return;
Player player = (Player) disguise.getEntity();
// Remove the old disguise, else we have weird disguises around the place
removeSelfDisguise(player);
// If the disguised player can't see himself. Return
if (!disguise.viewSelfDisguise() || !PacketsManager.isViewDisguisesListenerEnabled() || player.getVehicle() != null)
return;
try {
// Grab the entity ID the fake disguise will use
Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount");
field.setAccessible(true);
int id = field.getInt(null);
// Set the entitycount plus one so we don't have the id being reused
field.set(null, id + 1);
selfDisguisesIds.put(player.getEntityId(), id);
} catch (Exception ex) {
ex.printStackTrace();
}
PacketsManager.sendSelfDisguise(player);
if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) {
if (PacketsManager.isInventoryListenerEnabled()) {
player.updateInventory();
}
}
}
/** /**
* Disable velocity packets being sent for w/e reason. Maybe you want every ounce of performance you can get? * Disable velocity packets being sent for w/e reason. Maybe you want every ounce of performance you can get?
*/ */
@ -331,19 +216,6 @@ public class DisguiseAPI {
disguise.removeDisguise(); disguise.removeDisguise();
} }
public HashMap<Integer, Disguise> getDisguises() { private DisguiseAPI() {
return disguises;
}
public void refreshWatchingPlayers(Entity entity) {
refreshTrackers(entity);
}
public void removeVisibleDisguise(Player player) {
removeSelfDisguise(player);
}
public void setupFakeDisguise(Disguise disguise) {
setupPlayerFakeDisguise(disguise);
} }
} }

View File

@ -3,6 +3,8 @@ package me.libraryaddict.disguise;
import java.util.HashMap; import java.util.HashMap;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utils.DisguiseUtilities;
import me.libraryaddict.disguise.utils.UpdateChecker;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -19,7 +21,6 @@ import org.bukkit.scheduler.BukkitRunnable;
public class DisguiseListener implements Listener { public class DisguiseListener implements Listener {
private String currentVersion; private String currentVersion;
private DisguiseAPI disguiseAPI = new DisguiseAPI();
private HashMap<String, BukkitRunnable> disguiseRunnable = new HashMap<String, BukkitRunnable>(); private HashMap<String, BukkitRunnable> disguiseRunnable = new HashMap<String, BukkitRunnable>();
private HashMap<String, Disguise> disguiseSlap = new HashMap<String, Disguise>(); private HashMap<String, Disguise> disguiseSlap = new HashMap<String, Disguise>();
private String latestVersion; private String latestVersion;
@ -93,7 +94,7 @@ public class DisguiseListener implements Listener {
return; return;
Disguise disguise = DisguiseAPI.getDisguise(event.getEntered()); Disguise disguise = DisguiseAPI.getDisguise(event.getEntered());
if (disguise != null && event.getEntered() instanceof Player) { if (disguise != null && event.getEntered() instanceof Player) {
disguiseAPI.removeVisibleDisguise((Player) event.getEntered()); DisguiseUtilities.removeSelfDisguise((Player) event.getEntered());
((Player) event.getEntered()).updateInventory(); ((Player) event.getEntered()).updateInventory();
} }
} }
@ -106,7 +107,7 @@ public class DisguiseListener implements Listener {
if (disguise != null && event.getExited() instanceof Player) { if (disguise != null && event.getExited() instanceof Player) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() { public void run() {
disguiseAPI.setupFakeDisguise(disguise); DisguiseUtilities.setupFakeDisguise(disguise);
((Player) disguise.getEntity()).updateInventory(); ((Player) disguise.getEntity()).updateInventory();
} }
}); });

View File

@ -17,6 +17,8 @@ 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.PacketsManager;
import me.libraryaddict.disguise.utils.ReflectionManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -122,11 +124,11 @@ public class LibsDisguises extends JavaPlugin {
break; break;
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
// There is no explict watcher for this entity. // There is no explicit watcher for this entity.
Class c = disguiseType.getEntityType().getEntityClass(); Class entityClass = disguiseType.getEntityType().getEntityClass();
if (Ageable.class.isAssignableFrom(c)) { if (Ageable.class.isAssignableFrom(entityClass)) {
watcherClass = AgeableWatcher.class; watcherClass = AgeableWatcher.class;
} else if (LivingEntity.class.isAssignableFrom(c)) { } else if (LivingEntity.class.isAssignableFrom(entityClass)) {
watcherClass = LivingWatcher.class; watcherClass = LivingWatcher.class;
} else { } else {
watcherClass = FlagWatcher.class; watcherClass = FlagWatcher.class;
@ -176,19 +178,19 @@ public class LibsDisguises extends JavaPlugin {
Object entity = ReflectionManager.createEntityInstance(name); Object entity = ReflectionManager.createEntityInstance(name);
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity") Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity")
.invoke(entity); .invoke(entity);
int size = 0; int enumEntitySize = 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 a = (Enum) field.get(entity);
size = a.ordinal(); enumEntitySize = a.ordinal();
break; break;
} }
} }
Values value = new Values(disguiseType, entity.getClass(), size); Values disguiseValues = new Values(disguiseType, entity.getClass(), enumEntitySize);
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)
value.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(entity);

View File

@ -1,9 +1,10 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utils.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;

View File

@ -1,9 +1,10 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseListener; import me.libraryaddict.disguise.DisguiseListener;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utils.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;

View File

@ -3,9 +3,10 @@ package me.libraryaddict.disguise.commands;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utils.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -15,8 +16,8 @@ import org.bukkit.potion.PotionEffectType;
public class DisguiseHelpCommand extends BaseDisguiseCommand { public class DisguiseHelpCommand extends BaseDisguiseCommand {
private class EnumHelp { private class EnumHelp {
private String enumName;
private String enumDescription; private String enumDescription;
private String enumName;
private String[] enums; private String[] enums;
private String readableEnum; private String readableEnum;
@ -38,21 +39,21 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand {
this.readableEnum = enumReadable; this.readableEnum = enumReadable;
} }
public String getEnumName() {
return enumName;
}
public String getReadableEnum() {
return readableEnum;
}
public String getEnumDescription() { public String getEnumDescription() {
return enumDescription; return enumDescription;
} }
public String getEnumName() {
return enumName;
}
public String[] getEnums() { public String[] getEnums() {
return enums; return enums;
} }
public String getReadableEnum() {
return readableEnum;
}
} }
private ArrayList<EnumHelp> enumHelp = new ArrayList<EnumHelp>(); private ArrayList<EnumHelp> enumHelp = new ArrayList<EnumHelp>();

View File

@ -1,9 +1,10 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utils.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;

View File

@ -1,9 +1,10 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import java.util.ArrayList; import java.util.ArrayList;
import me.libraryaddict.disguise.BaseDisguiseCommand;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utils.BaseDisguiseCommand;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;

View File

@ -8,11 +8,13 @@ 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.PacketsManager;
import me.libraryaddict.disguise.ReflectionManager;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.utils.PacketsManager;
import me.libraryaddict.disguise.utils.ReflectionManager;
import me.libraryaddict.disguise.utils.DisguiseUtilities;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Horse.Variant; import org.bukkit.entity.Horse.Variant;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -26,18 +28,14 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
public abstract class Disguise { public abstract class Disguise {
/**
* Incase I forget, this is used to access normally hidden api calls.
*/
private static DisguiseAPI disguiseAPI = new DisguiseAPI();
private static JavaPlugin plugin; private static JavaPlugin plugin;
private DisguiseType disguiseType; private DisguiseType disguiseType;
private org.bukkit.entity.Entity entity; private org.bukkit.entity.Entity entity;
private boolean hearSelfDisguise = DisguiseAPI.canHearSelfDisguise(); private boolean hearSelfDisguise = DisguiseAPI.isSelfDisguisesSoundsReplaced();
private boolean hideArmorFromSelf = DisguiseAPI.isHidingArmorFromSelf(); private boolean hideArmorFromSelf = DisguiseAPI.isHidingArmorFromSelf();
private boolean hideHeldItemFromSelf = DisguiseAPI.isHidingHeldItemFromSelf(); private boolean hideHeldItemFromSelf = DisguiseAPI.isHidingHeldItemFromSelf();
private boolean replaceSounds = DisguiseAPI.isSoundEnabled(); private boolean replaceSounds = DisguiseAPI.isSoundEnabled();
private BukkitRunnable runnable; private BukkitRunnable velocityRunnable;
private boolean velocitySent = DisguiseAPI.isVelocitySent(); private boolean velocitySent = DisguiseAPI.isVelocitySent();
private boolean viewSelfDisguise = DisguiseAPI.isViewDisguises(); private boolean viewSelfDisguise = DisguiseAPI.isViewDisguises();
private FlagWatcher watcher; private FlagWatcher watcher;
@ -175,7 +173,7 @@ public abstract class Disguise {
final double vectorY = fallSpeed; final double vectorY = fallSpeed;
final boolean alwaysSendVelocity = alwaysSend; final boolean alwaysSendVelocity = alwaysSend;
// A scheduler to clean up any unused disguises. // A scheduler to clean up any unused disguises.
runnable = new BukkitRunnable() { velocityRunnable = new BukkitRunnable() {
private int i = 0; private int i = 0;
public void run() { public void run() {
@ -220,7 +218,7 @@ public abstract class Disguise {
PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_VELOCITY); PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_VELOCITY);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
if (entity == player) { if (entity == player) {
if (!viewSelfDisguise()) if (!isSelfDisguiseVisible())
continue; continue;
if (selfLookPacket != null) { if (selfLookPacket != null) {
try { try {
@ -334,6 +332,17 @@ public abstract class Disguise {
return this instanceof PlayerDisguise; return this instanceof PlayerDisguise;
} }
/**
* Can the disguised view himself as the disguise
*/
public boolean isSelfDisguiseVisible() {
return viewSelfDisguise;
}
public boolean isSoundsReplaced() {
return replaceSounds;
}
public boolean isVelocitySent() { public boolean isVelocitySent() {
return velocitySent; return velocitySent;
} }
@ -344,10 +353,10 @@ public abstract class Disguise {
public void removeDisguise() { public void removeDisguise() {
// Why the hell can't I safely check if its running?!?! // Why the hell can't I safely check if its running?!?!
try { try {
runnable.cancel(); velocityRunnable.cancel();
} catch (Exception ex) { } catch (Exception ex) {
} }
HashMap<Integer, Disguise> disguises = disguiseAPI.getDisguises(); HashMap<Integer, Disguise> disguises = DisguiseUtilities.getDisguises();
// If this disguise has a entity set // If this disguise has a entity set
if (getEntity() != null) { if (getEntity() != null) {
// If the entity is valid // If the entity is valid
@ -359,10 +368,10 @@ public abstract class Disguise {
// Gotta do reflection, copy code or open up calls. // Gotta do reflection, copy code or open up calls.
// Reflection is the cleanest? // Reflection is the cleanest?
if (getEntity() instanceof Player) { if (getEntity() instanceof Player) {
disguiseAPI.removeVisibleDisguise((Player) entity); DisguiseUtilities.removeSelfDisguise((Player) entity);
} }
// Better refresh the entity to undisguise it // Better refresh the entity to undisguise it
disguiseAPI.refreshWatchingPlayers(getEntity()); DisguiseUtilities.refreshTrackers(getEntity());
} }
} }
} else { } else {
@ -377,6 +386,7 @@ public abstract class Disguise {
} }
} }
@Deprecated
public boolean replaceSounds() { public boolean replaceSounds() {
return replaceSounds; return replaceSounds;
} }
@ -389,7 +399,7 @@ public abstract class Disguise {
throw new RuntimeException("This disguise is already in use! Try .clone()"); throw new RuntimeException("This disguise is already in use! Try .clone()");
this.entity = entity; this.entity = entity;
setupWatcher(); setupWatcher();
runnable.runTaskTimer(plugin, 1, 1); velocityRunnable.runTaskTimer(plugin, 1, 1);
} }
public void setHearSelfDisguise(boolean hearSelfDisguise) { public void setHearSelfDisguise(boolean hearSelfDisguise) {
@ -529,9 +539,9 @@ public abstract class Disguise {
if (getEntity() != null && getEntity() instanceof Player) { if (getEntity() != null && getEntity() instanceof Player) {
if (DisguiseAPI.getDisguise(getEntity()) == this) { if (DisguiseAPI.getDisguise(getEntity()) == this) {
if (viewSelfDisguise) { if (viewSelfDisguise) {
disguiseAPI.setupFakeDisguise(this); DisguiseUtilities.setupFakeDisguise(this);
} else } else
disguiseAPI.removeVisibleDisguise((Player) getEntity()); DisguiseUtilities.removeSelfDisguise((Player) getEntity());
} }
} }
} }
@ -541,9 +551,7 @@ public abstract class Disguise {
watcher = newWatcher; watcher = newWatcher;
} }
/** @Deprecated
* Can the disguised view himself as the disguise
*/
public boolean viewSelfDisguise() { public boolean viewSelfDisguise() {
return viewSelfDisguise; return viewSelfDisguise;
} }

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.disguisetypes;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import me.libraryaddict.disguise.ReflectionManager; import me.libraryaddict.disguise.utils.ReflectionManager;
import org.bukkit.Sound; import org.bukkit.Sound;

View File

@ -174,10 +174,6 @@ public enum DisguiseType {
private EntityType entityType; private EntityType entityType;
private Class watcherClass; private Class watcherClass;
private void setEntityType(EntityType entityType) {
this.entityType = entityType;
}
private DisguiseType(int... obj) { private DisguiseType(int... obj) {
for (int i = 0; i < obj.length; i++) { for (int i = 0; i < obj.length; i++) {
int value = obj[i]; int value = obj[i];
@ -229,6 +225,10 @@ public enum DisguiseType {
return entityType == EntityType.PLAYER; return entityType == EntityType.PLAYER;
} }
private void setEntityType(EntityType entityType) {
this.entityType = entityType;
}
public void setWatcherClass(Class c) { public void setWatcherClass(Class c) {
watcherClass = c; watcherClass = c;
} }

View File

@ -19,8 +19,8 @@ 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.PacketsManager; import me.libraryaddict.disguise.utils.PacketsManager;
import me.libraryaddict.disguise.ReflectionManager; import me.libraryaddict.disguise.utils.ReflectionManager;
public class FlagWatcher { public class FlagWatcher {
public enum SlotType { public enum SlotType {
@ -110,7 +110,7 @@ public class FlagWatcher {
} }
} }
// Here we check for if there is a health packet that says they died. // Here we check for if there is a health packet that says they died.
if (disguise.viewSelfDisguise() && disguise.getEntity() != null && disguise.getEntity() instanceof Player) { if (disguise.isSelfDisguiseVisible() && disguise.getEntity() != null && disguise.getEntity() instanceof Player) {
for (WrappedWatchableObject watch : newList) { for (WrappedWatchableObject watch : newList) {
// Its a health packet // Its a health packet
if (watch.getIndex() == 6) { if (watch.getIndex() == 6) {

View File

@ -83,8 +83,8 @@ public class MiscDisguise extends Disguise {
@Override @Override
public MiscDisguise clone() { public MiscDisguise clone() {
MiscDisguise disguise = new MiscDisguise(getType(), replaceSounds(), getData()); MiscDisguise disguise = new MiscDisguise(getType(), isSoundsReplaced(), getData());
disguise.setViewSelfDisguise(viewSelfDisguise()); disguise.setViewSelfDisguise(isSelfDisguiseVisible());
disguise.setHearSelfDisguise(canHearSelfDisguise()); disguise.setHearSelfDisguise(canHearSelfDisguise());
disguise.setHideArmorFromSelf(isHidingArmorFromSelf()); disguise.setHideArmorFromSelf(isHidingArmorFromSelf());
disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf()); disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf());

View File

@ -40,8 +40,8 @@ public class MobDisguise extends Disguise {
@Override @Override
public MobDisguise clone() { public MobDisguise clone() {
MobDisguise disguise = new MobDisguise(getType(), isAdult(), replaceSounds()); MobDisguise disguise = new MobDisguise(getType(), isAdult(), isSoundsReplaced());
disguise.setViewSelfDisguise(viewSelfDisguise()); disguise.setViewSelfDisguise(isSelfDisguiseVisible());
disguise.setHearSelfDisguise(canHearSelfDisguise()); disguise.setHearSelfDisguise(canHearSelfDisguise());
disguise.setHideArmorFromSelf(isHidingArmorFromSelf()); disguise.setHideArmorFromSelf(isHidingArmorFromSelf());
disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf()); disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf());

View File

@ -16,8 +16,8 @@ public class PlayerDisguise extends Disguise {
@Override @Override
public PlayerDisguise clone() { public PlayerDisguise clone() {
PlayerDisguise disguise = new PlayerDisguise(getName(), replaceSounds()); PlayerDisguise disguise = new PlayerDisguise(getName(), isSoundsReplaced());
disguise.setViewSelfDisguise(viewSelfDisguise()); disguise.setViewSelfDisguise(isSelfDisguiseVisible());
disguise.setHearSelfDisguise(canHearSelfDisguise()); disguise.setHearSelfDisguise(canHearSelfDisguise());
disguise.setHideArmorFromSelf(isHidingArmorFromSelf()); disguise.setHideArmorFromSelf(isHidingArmorFromSelf());
disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf()); disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf());

View File

@ -1,8 +1,8 @@
package me.libraryaddict.disguise.disguisetypes.watchers; package me.libraryaddict.disguise.disguisetypes.watchers;
import me.libraryaddict.disguise.ReflectionManager;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.utils.ReflectionManager;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;

View File

@ -1,8 +1,8 @@
package me.libraryaddict.disguise.disguisetypes.watchers; package me.libraryaddict.disguise.disguisetypes.watchers;
import me.libraryaddict.disguise.ReflectionManager;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.utils.ReflectionManager;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;

View File

@ -2,15 +2,14 @@ package me.libraryaddict.disguise.disguisetypes.watchers;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashSet; import java.util.HashSet;
import me.libraryaddict.disguise.ReflectionManager;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.utils.ReflectionManager;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
public class LivingWatcher extends FlagWatcher { public class LivingWatcher extends FlagWatcher {
private HashSet<Integer> potionEffects = new HashSet<Integer>();
static Object[] list; static Object[] list;
static Method potionNo; static Method potionNo;
static { static {
@ -33,6 +32,7 @@ public class LivingWatcher extends FlagWatcher {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
private HashSet<Integer> potionEffects = new HashSet<Integer>();
public LivingWatcher(Disguise disguise) { public LivingWatcher(Disguise disguise) {
super(disguise); super(disguise);
@ -72,35 +72,6 @@ public class LivingWatcher extends FlagWatcher {
return (Byte) getValue(8, (byte) 0) == 1; return (Byte) getValue(8, (byte) 0) == 1;
} }
public boolean hasCustomName() {
return getCustomName().length() > 0;
}
public boolean hasPotionEffect(PotionEffectType type) {
return potionEffects.contains(type.getId());
}
public boolean isCustomNameVisible() {
return (Byte) getValue(11, (byte) 0) == 1;
}
public void removePotionEffect(PotionEffectType type) {
if (potionEffects.contains(type.getId())) {
potionEffects.remove(type.getId());
sendPotionEffects();
}
}
public void removePotionParticles(boolean particles) {
setValue(8, (byte) (particles ? 1 : 0));
sendData(8);
}
private void sendPotionEffects() {
setValue(7, getPotions());
sendData(7);
}
private int getPotions() { private int getPotions() {
int m = 3694022; int m = 3694022;
@ -131,6 +102,35 @@ public class LivingWatcher extends FlagWatcher {
return (int) f1 << 16 | (int) f2 << 8 | (int) f3; return (int) f1 << 16 | (int) f2 << 8 | (int) f3;
} }
public boolean hasCustomName() {
return getCustomName().length() > 0;
}
public boolean hasPotionEffect(PotionEffectType type) {
return potionEffects.contains(type.getId());
}
public boolean isCustomNameVisible() {
return (Byte) getValue(11, (byte) 0) == 1;
}
public void removePotionEffect(PotionEffectType type) {
if (potionEffects.contains(type.getId())) {
potionEffects.remove(type.getId());
sendPotionEffects();
}
}
public void removePotionParticles(boolean particles) {
setValue(8, (byte) (particles ? 1 : 0));
sendData(8);
}
private void sendPotionEffects() {
setValue(7, getPotions());
sendData(7);
}
public void setCustomName(String name) { public void setCustomName(String name) {
if (name.length() > 64) if (name.length() > 64)
name = name.substring(0, 64); name = name.substring(0, 64);

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise.utils;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -0,0 +1,138 @@
package me.libraryaddict.disguise.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class DisguiseUtilities {
// 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>();
// 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
private static HashMap<Integer, Integer> selfDisguisesIds = new HashMap<Integer, Integer>();
public static HashMap<Integer, Disguise> getDisguises() {
return disguises;
}
public static HashMap<Integer, Integer> getSelfDisguisesIds() {
return selfDisguisesIds;
}
/**
* @param Resends
* the entity to all the watching players, which is where the magic begins
*/
public static void refreshTrackers(Entity entity) {
try {
Object world = ReflectionManager.getWorld(entity.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, entity.getEntityId());
if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry);
Method getBukkitEntity = ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity");
Method clear = entityTrackerEntry.getClass().getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer"));
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object player : cloned) {
if (entity instanceof Player && !((Player) getBukkitEntity.invoke(player)).canSee((Player) entity))
continue;
clear.invoke(entityTrackerEntry, player);
updatePlayer.invoke(entityTrackerEntry, player);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void removeSelfDisguise(Player player) {
if (selfDisguisesIds.containsKey(player.getEntityId())) {
// Send a packet to destroy the fake entity
PacketContainer packet = new PacketContainer(Packets.Server.DESTROY_ENTITY);
packet.getModifier().write(0, new int[] { selfDisguisesIds.get(player.getEntityId()) });
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
} catch (Exception ex) {
ex.printStackTrace();
}
// Remove the fake entity ID from the disguise bin
selfDisguisesIds.remove(player.getEntityId());
// Get the entity tracker
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) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry);
// If the tracker exists. Remove himself from his tracker
trackedPlayers.remove(ReflectionManager.getNmsEntity(player));
}
} catch (Exception ex) {
ex.printStackTrace();
}// Resend entity metadata else he will be invisible to himself until its resent
PacketContainer packetMetadata = new PacketContainer(Packets.Server.ENTITY_METADATA);
StructureModifier<Object> mods = packetMetadata.getModifier();
mods.write(0, player.getEntityId());
packetMetadata.getWatchableCollectionModifier().write(0,
WrappedDataWatcher.getEntityWatcher(player).getWatchableObjects());
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packetMetadata);
} catch (Exception ex) {
ex.printStackTrace();
}
player.updateInventory();
}
}
/**
* Setup it so he can see himself when disguised
*/
public static void setupFakeDisguise(final Disguise disguise) {
// If the disguises entity is null, or the disguised entity isn't a player return
if (disguise.getEntity() == null || !(disguise.getEntity() instanceof Player) || !disguises.containsValue(disguise))
return;
Player player = (Player) disguise.getEntity();
// Remove the old disguise, else we have weird disguises around the place
DisguiseUtilities.removeSelfDisguise(player);
// If the disguised player can't see himself. Return
if (!disguise.isSelfDisguiseVisible() || !PacketsManager.isViewDisguisesListenerEnabled() || player.getVehicle() != null)
return;
try {
// Grab the entity ID the fake disguise will use
Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount");
field.setAccessible(true);
int id = field.getInt(null);
// Set the entitycount plus one so we don't have the id being reused
field.set(null, id + 1);
selfDisguisesIds.put(player.getEntityId(), id);
} catch (Exception ex) {
ex.printStackTrace();
}
PacketsManager.sendSelfDisguise(player);
if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) {
if (PacketsManager.isInventoryListenerEnabled()) {
player.updateInventory();
}
}
}
}

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise.utils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -8,6 +8,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseSound; import me.libraryaddict.disguise.disguisetypes.DisguiseSound;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
@ -50,7 +52,6 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject;
public class PacketsManager { public class PacketsManager {
private static boolean cancelSound; private static boolean cancelSound;
private static DisguiseAPI disguiseAPI = new DisguiseAPI();
private static PacketListener inventoryListenerClient; private static PacketListener inventoryListenerClient;
private static PacketListener inventoryListenerServer; private static PacketListener inventoryListenerServer;
private static boolean inventoryModifierEnabled; private static boolean inventoryModifierEnabled;
@ -60,7 +61,7 @@ public class PacketsManager {
private static PacketListener viewDisguisesListener; private static PacketListener viewDisguisesListener;
private static boolean viewDisguisesListenerEnabled; private static boolean viewDisguisesListenerEnabled;
protected static void addPacketListeners(final JavaPlugin libsDisguises) { public static void addPacketListeners(JavaPlugin libsDisguises) {
ProtocolManager manager = ProtocolLibrary.getProtocolManager(); ProtocolManager manager = ProtocolLibrary.getProtocolManager();
manager.addPacketListener(new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGH, manager.addPacketListener(new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGH,
Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ENTITY_METADATA, Packets.Server.ARM_ANIMATION, Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ENTITY_METADATA, Packets.Server.ARM_ANIMATION,
@ -385,7 +386,7 @@ public class PacketsManager {
/** /**
* Creates the packet listeners * Creates the packet listeners
*/ */
protected static void init(LibsDisguises plugin) { public static void init(LibsDisguises plugin) {
libsDisguises = plugin; libsDisguises = plugin;
soundsListener = new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, soundsListener = new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL,
Packets.Server.NAMED_SOUND_EFFECT, Packets.Server.ENTITY_STATUS) { Packets.Server.NAMED_SOUND_EFFECT, Packets.Server.ENTITY_STATUS) {
@ -451,7 +452,7 @@ public class PacketsManager {
Disguise disguise = DisguiseAPI.getDisguise(disguisedEntity); Disguise disguise = DisguiseAPI.getDisguise(disguisedEntity);
if (disguise != null) { if (disguise != null) {
if (disguise.canHearSelfDisguise() || disguisedEntity != event.getPlayer()) { if (disguise.canHearSelfDisguise() || disguisedEntity != event.getPlayer()) {
if (disguise.replaceSounds()) { if (disguise.isSoundsReplaced()) {
String sound = null; String sound = null;
DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name());
if (dSound != null && soundType != null) if (dSound != null && soundType != null)
@ -694,7 +695,7 @@ public class PacketsManager {
if (event.getPlayer().getVehicle() == null && event.getPacket().getIntegers().read(0) == 0) { if (event.getPlayer().getVehicle() == null && event.getPacket().getIntegers().read(0) == 0) {
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer()); Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer());
// If the player is disguised, views self disguises and is hiding a item. // If the player is disguised, views self disguises and is hiding a item.
if (disguise != null && disguise.viewSelfDisguise() if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
switch (event.getPacketID()) { switch (event.getPacketID()) {
// If the server is setting the slot // If the server is setting the slot
@ -785,7 +786,7 @@ public class PacketsManager {
if (event.getPlayer().getVehicle() == null) { if (event.getPlayer().getVehicle() == null) {
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer()); Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer());
// If player is disguised, views self disguises and has a inventory modifier // If player is disguised, views self disguises and has a inventory modifier
if (disguise != null && disguise.viewSelfDisguise() if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
switch (event.getPacketID()) { switch (event.getPacketID()) {
// If they are in creative and clicked on a slot // If they are in creative and clicked on a slot
@ -1089,7 +1090,7 @@ public class PacketsManager {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
Disguise disguise = DisguiseAPI.getDisguise(player); Disguise disguise = DisguiseAPI.getDisguise(player);
if (disguise != null) { if (disguise != null) {
if (viewDisguisesListenerEnabled && disguise.viewSelfDisguise() if (viewDisguisesListenerEnabled && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
player.updateInventory(); player.updateInventory();
} }
@ -1109,11 +1110,11 @@ public class PacketsManager {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
Disguise disguise = DisguiseAPI.getDisguise(player); Disguise disguise = DisguiseAPI.getDisguise(player);
if (disguise != null) { if (disguise != null) {
if (disguise.viewSelfDisguise()) { if (disguise.isSelfDisguiseVisible()) {
if (enabled) { if (enabled) {
disguiseAPI.setupFakeDisguise(disguise); DisguiseUtilities.setupFakeDisguise(disguise);
} else { } else {
disguiseAPI.removeVisibleDisguise(player); DisguiseUtilities.removeSelfDisguise(player);
} }
if (inventoryModifierEnabled && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { if (inventoryModifierEnabled && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
player.updateInventory(); player.updateInventory();

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise.utils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -45,40 +45,6 @@ public class ReflectionManager {
} }
} }
public static String getEnumArt(Art art) {
try {
Class craftArt = Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".CraftArt");
Object enumArt = craftArt.getMethod("BukkitToNotch", Art.class).invoke(null, art);
for (Field field : enumArt.getClass().getFields()) {
if (field.getType() == String.class) {
return (String) field.get(enumArt);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Object getNmsEntity(Entity entity) {
try {
return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String getCraftSound(Sound sound) {
try {
Class c = getCraftClass("CraftSound");
return (String) c.getMethod("getSound", Sound.class).invoke(null, sound);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Object createEntityInstance(String entityName) { public static Object createEntityInstance(String entityName) {
try { try {
Class entityClass = getNmsClass("Entity" + entityName); Class entityClass = getNmsClass("Entity" + entityName);
@ -101,6 +67,49 @@ public class ReflectionManager {
return null; return null;
} }
public static ItemStack getBukkitItem(Object nmsItem) {
try {
return (ItemStack) itemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Class getCraftClass(String className) {
try {
return Class.forName("org.bukkit.craftbukkit." + bukkitVersion + "." + className);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getCraftSound(Sound sound) {
try {
Class c = getCraftClass("CraftSound");
return (String) c.getMethod("getSound", Sound.class).invoke(null, sound);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String getEnumArt(Art art) {
try {
Class craftArt = Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".CraftArt");
Object enumArt = craftArt.getMethod("BukkitToNotch", Art.class).invoke(null, art);
for (Field field : enumArt.getClass().getFields()) {
if (field.getType() == String.class) {
return (String) field.get(enumArt);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Class getNmsClass(String className) { public static Class getNmsClass(String className) {
try { try {
return Class.forName("net.minecraft.server." + bukkitVersion + "." + className); return Class.forName("net.minecraft.server." + bukkitVersion + "." + className);
@ -110,9 +119,18 @@ public class ReflectionManager {
return null; return null;
} }
public static Class getCraftClass(String className) { public static Object getNmsEntity(Entity entity) {
try { try {
return Class.forName("org.bukkit.craftbukkit." + bukkitVersion + "." + className); return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Object getNmsItem(ItemStack itemstack) {
try {
return itemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -137,22 +155,4 @@ public class ReflectionManager {
return null; return null;
} }
public static Object getNmsItem(ItemStack itemstack) {
try {
return itemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static ItemStack getBukkitItem(Object nmsItem) {
try {
return (ItemStack) itemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} }

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;