Move classes into packages, clean up packet handling into classes, yaw/pitch should be consistent

This commit is contained in:
libraryaddict 2019-01-03 15:13:03 +13:00
parent 43701c1fe8
commit cada0f4f91
68 changed files with 4009 additions and 3625 deletions

View File

@ -6,7 +6,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.AbstractHorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.DyeColor; import org.bukkit.DyeColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.*; import org.bukkit.entity.*;

View File

@ -3,8 +3,8 @@ package me.libraryaddict.disguise;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.LibsPremium;
import me.libraryaddict.disguise.utilities.PacketsManager; import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;

View File

@ -14,7 +14,7 @@ import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.UpdateChecker; import me.libraryaddict.disguise.utilities.UpdateChecker;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;

View File

@ -4,9 +4,16 @@ import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.commands.*; import me.libraryaddict.disguise.commands.*;
import me.libraryaddict.disguise.disguisetypes.*; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.disguisetypes.watchers.*; import me.libraryaddict.disguise.disguisetypes.watchers.*;
import me.libraryaddict.disguise.utilities.*; import me.libraryaddict.disguise.utilities.*;
import me.libraryaddict.disguise.utilities.metrics.MetricsInitalizer;
import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import me.libraryaddict.disguise.utilities.reflection.DisguiseValues;
import me.libraryaddict.disguise.utilities.reflection.FakeBoundingBox;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -21,9 +28,6 @@ import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
public class LibsDisguises extends JavaPlugin { public class LibsDisguises extends JavaPlugin {
private static LibsDisguises instance; private static LibsDisguises instance;
@ -89,7 +93,7 @@ public class LibsDisguises extends JavaPlugin {
getLogger().info("Commands has been disabled, as per config"); getLogger().info("Commands has been disabled, as per config");
} }
infectWithMetrics(); new MetricsInitalizer();
} }
@Override @Override
@ -101,208 +105,6 @@ public class LibsDisguises extends JavaPlugin {
} }
} }
private void infectWithMetrics() {
String version = getDescription().getVersion();
// If a release build, attach build number
if (!isReleaseBuild() || !LibsPremium.isPremium()) {
version += "-";
// 9.7.0-SNAPSHOT-b30
if (isNumberedBuild()) {
version += "b";
}
// else 9.7.0-SNAPSHOT-unknown
version += getBuildNo();
}
Metrics metrics = new Metrics(this, version);
final String premium = LibsPremium.isPremium() ?
getDescription().getVersion().contains("SNAPSHOT") ? "Paid Builds" : "Paid Plugin" : "Free Builds";
metrics.addCustomChart(new Metrics.SimplePie("premium") {
@Override
public String getValue() {
return premium;
}
});
metrics.addCustomChart(new Metrics.SimplePie("translations") {
@Override
public String getValue() {
return LibsPremium.isPremium() && DisguiseConfig.isUseTranslations() ? "Yes" : "No";
}
});
metrics.addCustomChart(new Metrics.SimplePie("custom_disguises") {
@Override
public String getValue() {
HashMap map = DisguiseConfig.getCustomDisguises();
return map.size() + (map.containsKey("libraryaddict") ? -1 : 0) > 0 ? "Yes" : "No";
}
});
metrics.addCustomChart(new Metrics.MultiLineChart("disguised_entities") {
@Override
public HashMap<String, Integer> getValues(HashMap<String, Integer> hashMap) {
for (HashSet<TargetedDisguise> list : DisguiseUtilities.getDisguises().values()) {
for (Disguise disg : list) {
if (disg.getEntity() == null || !disg.isDisguiseInUse())
continue;
String name = disg.getEntity().getType().name();
hashMap.put(name, hashMap.containsKey(name) ? hashMap.get(name) + 1 : 1);
}
}
return hashMap;
}
});
metrics.addCustomChart(new Metrics.MultiLineChart("disguises_used") {
@Override
public HashMap<String, Integer> getValues(HashMap<String, Integer> hashMap) {
for (HashSet<TargetedDisguise> list : DisguiseUtilities.getDisguises().values()) {
for (Disguise disg : list) {
if (disg.getEntity() == null || !disg.isDisguiseInUse())
continue;
String name = disg.getType().name();
hashMap.put(name, hashMap.containsKey(name) ? hashMap.get(name) + 1 : 1);
}
}
return hashMap;
}
});
metrics.addCustomChart(new Metrics.SimplePie("disguised_using") {
@Override
public String getValue() {
if (DisguiseUtilities.isPluginsUsed()) {
if (DisguiseUtilities.isCommandsUsed()) {
return "Plugins and Commands";
}
return "Plugins";
} else if (DisguiseUtilities.isCommandsUsed()) {
return "Commands";
}
return "Unknown";
}
});
metrics.addCustomChart(new Metrics.SimplePie("active_disguises") {
@Override
public String getValue() {
int disgs = 0;
for (HashSet set : DisguiseUtilities.getDisguises().values()) {
disgs += set.size();
}
if (disgs == 0)
return "0";
if (disgs <= 5)
return "1 to 5";
else if (disgs <= 15)
return "6 to 15";
else if (disgs <= 30)
return "16 to 30";
else if (disgs <= 60)
return "30 to 60";
else if (disgs <= 100)
return "60 to 100";
else if (disgs <= 200)
return "100 to 200";
return "More than 200";
}
});
metrics.addCustomChart(new Metrics.SimplePie("self_disguises") {
@Override
public String getValue() {
return DisguiseConfig.isViewDisguises() ? "Yes" : "No";
}
});
metrics.addCustomChart(new Metrics.SimplePie("commands") {
@Override
public String getValue() {
return DisguiseConfig.isDisableCommands() ? "Enabled" : "Disabled";
}
});
metrics.addCustomChart(new Metrics.SimplePie("spigot") {
@Override
public String getValue() {
try {
Class.forName("org.spigotmc.SpigotConfig");
return "Yes";
}
catch (Exception ex) {
return "No";
}
}
});
final boolean updates = getConfig().getBoolean("NotifyUpdate");
metrics.addCustomChart(new Metrics.SimplePie("updates") {
@Override
public String getValue() {
return updates ? "Enabled" : "Disabled";
}
});
if (getBuildNo() != null) {
metrics.addCustomChart(new Metrics.SimplePie("build_number") {
@Override
public String getValue() {
return getBuildNo();
}
});
}
metrics.addCustomChart(new Metrics.SimplePie("targeted_disguises") {
/**
* Store value just to minimize amount of times it's called, and to persist even when not using anymore
*/
private boolean targetedDisguises;
@Override
public String getValue() {
if (targetedDisguises) {
return "Yes";
}
Collection<HashSet<TargetedDisguise>> list = DisguiseUtilities.getDisguises().values();
if (list.isEmpty())
return "Unknown";
for (HashSet<TargetedDisguise> dList : list) {
for (TargetedDisguise disg : dList) {
if (disg.getObservers().isEmpty())
continue;
targetedDisguises = true;
return "Yes";
}
}
return "No";
}
});
}
public boolean isReleaseBuild() { public boolean isReleaseBuild() {
return !getDescription().getVersion().contains("-SNAPSHOT"); return !getDescription().getVersion().contains("-SNAPSHOT");
} }

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions; import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;

View File

@ -6,7 +6,7 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -5,7 +5,7 @@ import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -1,7 +1,7 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser; import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm; import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions; import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;

View File

@ -4,7 +4,7 @@ import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -3,8 +3,7 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm; import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions; import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.parser.ParamInfoManager; import me.libraryaddict.disguise.utilities.parser.ParamInfoManager;

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.commands;
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.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -4,8 +4,8 @@ import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -6,7 +6,7 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -5,10 +5,10 @@ import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.ClassGetter; import me.libraryaddict.disguise.utilities.reflection.ClassGetter;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.*; import me.libraryaddict.disguise.utilities.parser.*;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -1,9 +1,7 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType;
import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;

View File

@ -2,7 +2,7 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.LibsPremium;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;

View File

@ -1,9 +1,7 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType;
import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;

View File

@ -1,9 +1,7 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType;
import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;

View File

@ -2,7 +2,7 @@ package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;

View File

@ -1,9 +1,7 @@
package me.libraryaddict.disguise.commands; package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType;
import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;

View File

@ -19,8 +19,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
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.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.PacketsManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.ReflectionManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -233,11 +232,10 @@ public abstract class Disguise {
lookPacket.getIntegers().write(0, getEntity().getEntityId()); lookPacket.getIntegers().write(0, getEntity().getEntityId());
Location loc = getEntity().getLocation(); Location loc = getEntity().getLocation();
mods.write(4, PacketsManager.getYaw(getType(), getEntity().getType(), mods.write(4, DisguiseUtilities.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, PacketsManager mods.write(5, DisguiseUtilities.getPitch(getType(), getEntity().getType(),
.getPitch(getType(), DisguiseType.getType(getEntity().getType()), (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)));
(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();

View File

@ -1,6 +1,6 @@
package me.libraryaddict.disguise.disguisetypes; package me.libraryaddict.disguise.disguisetypes;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;

View File

@ -13,7 +13,7 @@ import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;

View File

@ -11,8 +11,8 @@ import com.comphenix.protocol.wrappers.WrappedGameProfile;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsProfileLookup; import me.libraryaddict.disguise.utilities.reflection.LibsProfileLookup;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;

View File

@ -9,7 +9,7 @@ import com.comphenix.protocol.wrappers.PlayerInfoData;
import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedChatComponent;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View File

@ -3,7 +3,7 @@ package me.libraryaddict.disguise.disguisetypes.watchers;
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.disguisetypes.MetaIndex; import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Sound; import org.bukkit.Sound;
import java.util.Arrays; import java.util.Arrays;

View File

@ -6,10 +6,7 @@ import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager; 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.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.*;
import com.comphenix.protocol.wrappers.WrappedBlockData;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
@ -22,8 +19,14 @@ import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
import me.libraryaddict.disguise.utilities.json.*; import me.libraryaddict.disguise.utilities.json.*;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import me.libraryaddict.disguise.utilities.reflection.DisguiseValues;
import me.libraryaddict.disguise.utilities.reflection.FakeBoundingBox;
import me.libraryaddict.disguise.utilities.reflection.LibsProfileLookup;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.util.Strings; import org.apache.logging.log4j.util.Strings;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -31,10 +34,7 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.Ageable; import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
@ -555,7 +555,7 @@ public class DisguiseUtilities {
teleport.getIntegers().write(0, entity); teleport.getIntegers().write(0, entity);
doubles.write(0, sleepingLocation.getX()); doubles.write(0, sleepingLocation.getX());
doubles.write(1, PacketsManager.getYModifier(disguise.getEntity(), disguise) + sleepingLocation.getY()); doubles.write(1, DisguiseUtilities.getYModifier(disguise.getEntity(), disguise) + sleepingLocation.getY());
doubles.write(2, sleepingLocation.getZ()); doubles.write(2, sleepingLocation.getZ());
return new PacketContainer[]{setBed, teleport}; return new PacketContainer[]{setBed, teleport};
@ -1649,7 +1649,7 @@ public class DisguiseUtilities {
return; return;
} }
LibsPackets transformed = PacketsManager.transformPacket(packet, disguise, player, player); LibsPackets transformed = PacketsManager.getPacketsHandler().transformPacket(packet, disguise, player, player);
try { try {
if (transformed.isUnhandled()) if (transformed.isUnhandled())
@ -1712,4 +1712,159 @@ public class DisguiseUtilities {
} }
} }
} }
/**
* Create a new datawatcher but with the 'correct' values
*/
public static WrappedDataWatcher createSanitizedDataWatcher(WrappedDataWatcher entityWatcher,
FlagWatcher disguiseWatcher) {
WrappedDataWatcher newWatcher = new WrappedDataWatcher();
try {
List<WrappedWatchableObject> list = DisguiseConfig.isMetadataPacketsEnabled() ?
disguiseWatcher.convert(entityWatcher.getWatchableObjects()) :
disguiseWatcher.getWatchableObjects();
for (WrappedWatchableObject watchableObject : list) {
if (watchableObject == null)
continue;
if (watchableObject.getValue() == null)
continue;
WrappedDataWatcher.WrappedDataWatcherObject obj = ReflectionManager
.createDataWatcherObject(watchableObject.getIndex(), watchableObject.getValue());
newWatcher.setObject(obj, watchableObject.getValue());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return newWatcher;
}
public static byte getPitch(DisguiseType disguiseType, EntityType entityType, byte value) {
return getPitch(disguiseType, getPitch(DisguiseType.getType(entityType), value));
}
public static byte getPitch(DisguiseType disguiseType, DisguiseType entityType, byte value) {
return getPitch(disguiseType, getPitch(entityType, value));
}
public static byte getPitch(DisguiseType disguiseType, byte value) {
if (disguiseType.isMisc()) {
return (byte) -value;
}
switch (disguiseType) {
case PHANTOM:
return (byte) -value;
default:
return value;
}
}
public static byte getYaw(DisguiseType disguiseType, EntityType entityType, byte value) {
return getYaw(disguiseType, getYaw(DisguiseType.getType(entityType), value));
}
public static byte getYaw(DisguiseType disguiseType, DisguiseType entityType, byte value) {
return getYaw(disguiseType, getYaw(entityType, value));
}
/**
* Add the yaw for the disguises
*/
public static byte getYaw(DisguiseType disguiseType, byte value) {
switch (disguiseType) {
case MINECART:
case MINECART_CHEST:
case MINECART_COMMAND:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
return (byte) (value + 64);
case BOAT:
case ENDER_DRAGON:
case WITHER_SKULL:
return (byte) (value - 128);
case ARROW:
case TIPPED_ARROW:
case SPECTRAL_ARROW:
return (byte) -value;
case PAINTING:
case ITEM_FRAME:
return (byte) -(value + 128);
default:
if (disguiseType.isMisc() && disguiseType != DisguiseType.ARMOR_STAND) {
return (byte) (value - 64);
}
return value;
}
}
/**
* Get the Y level to add to the disguise for realism.
*/
public static double getYModifier(Entity entity, Disguise disguise) {
double yMod = 0;
if ((disguise.getType() != DisguiseType.PLAYER || !((PlayerWatcher) disguise.getWatcher()).isSleeping()) &&
entity.getType() == EntityType.DROPPED_ITEM) {
yMod -= 0.13;
}
switch (disguise.getType()) {
case BAT:
if (entity instanceof LivingEntity)
return yMod + ((LivingEntity) entity).getEyeHeight();
case MINECART:
case MINECART_COMMAND:
case MINECART_CHEST:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
switch (entity.getType()) {
case MINECART:
case MINECART_CHEST:
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
return yMod;
default:
return yMod + 0.4;
}
case TIPPED_ARROW:
case SPECTRAL_ARROW:
case BOAT:
case EGG:
case ENDER_PEARL:
case ENDER_SIGNAL:
case FIREWORK:
case PAINTING:
case SMALL_FIREBALL:
case SNOWBALL:
case SPLASH_POTION:
case THROWN_EXP_BOTTLE:
case WITHER_SKULL:
return yMod + 0.7;
case PLAYER:
if (DisguiseConfig.isBedPacketsEnabled() && ((PlayerWatcher) disguise.getWatcher()).isSleeping()) {
return yMod + 0.35;
}
break;
case DROPPED_ITEM:
return yMod + 0.13;
default:
break;
}
return yMod;
}
} }

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File; import java.io.File;

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;

View File

@ -0,0 +1,232 @@
package me.libraryaddict.disguise.utilities.metrics;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class MetricsInitalizer {
private void infectWithMetrics() {
LibsDisguises plugin = LibsDisguises.getInstance();
String version = plugin.getDescription().getVersion();
// If a release build, attach build number
if (!plugin.isReleaseBuild() || !LibsPremium.isPremium()) {
version += "-";
// 9.7.0-SNAPSHOT-b30
if (plugin.isNumberedBuild()) {
version += "b";
}
// else 9.7.0-SNAPSHOT-unknown
version += plugin.getBuildNo();
}
Metrics metrics = new Metrics(plugin, version);
final String premiumType;
if (LibsPremium.isPremium()) {
if (plugin.isReleaseBuild()) {
premiumType = "Paid Plugin";
} else {
premiumType = "Paid Builds";
}
} else {
premiumType = "Free Builds";
}
metrics.addCustomChart(new Metrics.SimplePie("premium") {
@Override
public String getValue() {
return premiumType;
}
});
metrics.addCustomChart(new Metrics.SimplePie("translations") {
@Override
public String getValue() {
return LibsPremium.isPremium() && DisguiseConfig.isUseTranslations() ? "Yes" : "No";
}
});
metrics.addCustomChart(new Metrics.SimplePie("custom_disguises") {
@Override
public String getValue() {
HashMap map = DisguiseConfig.getCustomDisguises();
return map.size() + (map.containsKey("libraryaddict") ? -1 : 0) > 0 ? "Yes" : "No";
}
});
metrics.addCustomChart(new Metrics.MultiLineChart("disguised_entities") {
@Override
public HashMap<String, Integer> getValues(HashMap<String, Integer> hashMap) {
for (HashSet<TargetedDisguise> list : DisguiseUtilities.getDisguises().values()) {
for (Disguise disg : list) {
if (disg.getEntity() == null || !disg.isDisguiseInUse())
continue;
String name = disg.getEntity().getType().name();
hashMap.put(name, hashMap.containsKey(name) ? hashMap.get(name) + 1 : 1);
}
}
return hashMap;
}
});
metrics.addCustomChart(new Metrics.MultiLineChart("disguises_used") {
@Override
public HashMap<String, Integer> getValues(HashMap<String, Integer> hashMap) {
for (HashSet<TargetedDisguise> list : DisguiseUtilities.getDisguises().values()) {
for (Disguise disg : list) {
if (disg.getEntity() == null || !disg.isDisguiseInUse())
continue;
String name = disg.getType().name();
hashMap.put(name, hashMap.containsKey(name) ? hashMap.get(name) + 1 : 1);
}
}
return hashMap;
}
});
metrics.addCustomChart(new Metrics.SimplePie("disguised_using") {
@Override
public String getValue() {
if (DisguiseUtilities.isPluginsUsed()) {
if (DisguiseUtilities.isCommandsUsed()) {
return "Plugins and Commands";
}
return "Plugins";
} else if (DisguiseUtilities.isCommandsUsed()) {
return "Commands";
}
return "Unknown";
}
});
metrics.addCustomChart(new Metrics.SimplePie("active_disguises") {
@Override
public String getValue() {
int disgs = 0;
for (HashSet set : DisguiseUtilities.getDisguises().values()) {
disgs += set.size();
}
if (disgs == 0)
return "0";
if (disgs <= 5)
return "1 to 5";
else if (disgs <= 15)
return "6 to 15";
else if (disgs <= 30)
return "16 to 30";
else if (disgs <= 60)
return "30 to 60";
else if (disgs <= 100)
return "60 to 100";
else if (disgs <= 200)
return "100 to 200";
return "More than 200";
}
});
metrics.addCustomChart(new Metrics.SimplePie("self_disguises") {
@Override
public String getValue() {
return DisguiseConfig.isViewDisguises() ? "Yes" : "No";
}
});
metrics.addCustomChart(new Metrics.SimplePie("commands") {
@Override
public String getValue() {
return DisguiseConfig.isDisableCommands() ? "Enabled" : "Disabled";
}
});
metrics.addCustomChart(new Metrics.SimplePie("spigot") {
@Override
public String getValue() {
try {
Class.forName("org.spigotmc.SpigotConfig");
return "Yes";
}
catch (Exception ex) {
return "No";
}
}
});
final boolean updates = plugin.getConfig().getBoolean("NotifyUpdate");
metrics.addCustomChart(new Metrics.SimplePie("updates") {
@Override
public String getValue() {
return updates ? "Enabled" : "Disabled";
}
});
if (plugin.getBuildNo() != null) {
// Initalize final variable so we don't need to hold onto the plugin variable
final String buildNo = plugin.getBuildNo();
metrics.addCustomChart(new Metrics.SimplePie("build_number") {
@Override
public String getValue() {
return buildNo;
}
});
}
metrics.addCustomChart(new Metrics.SimplePie("targeted_disguises") {
/**
* Store value just to minimize amount of times it's called, and to persist even when not using anymore
*/
private boolean targetedDisguises;
@Override
public String getValue() {
if (targetedDisguises) {
return "Yes";
}
Collection<HashSet<TargetedDisguise>> list = DisguiseUtilities.getDisguises().values();
if (list.isEmpty())
return "Unknown";
for (HashSet<TargetedDisguise> dList : list) {
for (TargetedDisguise disg : dList) {
if (disg.getObservers().isEmpty())
continue;
targetedDisguises = true;
return "Yes";
}
}
return "No";
}
});
}
}

View File

@ -0,0 +1,16 @@
package me.libraryaddict.disguise.utilities.packets;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public interface IPacketHandler {
PacketType[] getHandledPackets();
void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity);
}

View File

@ -0,0 +1,94 @@
package me.libraryaddict.disguise.utilities.packets;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class LibsPackets {
private ArrayList<PacketContainer> packets = new ArrayList<>();
private HashMap<Integer, ArrayList<PacketContainer>> delayedPackets = new HashMap<>();
private boolean isSpawnPacket;
private Disguise disguise;
private boolean doNothing;
public LibsPackets(Disguise disguise) {
this.disguise = disguise;
}
public void setUnhandled() {
doNothing = true;
}
public boolean isUnhandled() {
return doNothing;
}
public Disguise getDisguise() {
return disguise;
}
public void setSpawnPacketCheck(PacketType type) {
isSpawnPacket = type.name().contains("SPAWN") && type.name().contains("ENTITY");
}
public void addPacket(PacketContainer packet) {
packets.add(packet);
}
public void addDelayedPacket(PacketContainer packet) {
addDelayedPacket(packet, 2);
}
public void clear() {
getPackets().clear();
}
public void addDelayedPacket(PacketContainer packet, int ticksDelayed) {
if (!delayedPackets.containsKey(ticksDelayed))
delayedPackets.put(ticksDelayed, new ArrayList<>());
delayedPackets.get(ticksDelayed).add(packet);
}
public ArrayList<PacketContainer> getPackets() {
return packets;
}
public Collection<ArrayList<PacketContainer>> getDelayedPackets() {
return delayedPackets.values();
}
public void sendDelayed(final Player observer) {
for (final Map.Entry<Integer, ArrayList<PacketContainer>> entry : delayedPackets.entrySet()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), new Runnable() {
public void run() {
try {
for (PacketContainer packet : entry.getValue()) {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
if (isSpawnPacket) {
PacketsManager.getPacketsHandler().removeCancel(disguise, observer);
}
}
}, entry.getKey());
}
}
}

View File

@ -0,0 +1,98 @@
package me.libraryaddict.disguise.utilities.packets;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.packets.packethandlers.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.UUID;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketsHandler {
private HashMap<Disguise, ArrayList<UUID>> cancelMeta = new HashMap<>();
private Collection<IPacketHandler> packetHandlers;
public PacketsHandler() {
registerPacketHandlers();
}
private void registerPacketHandlers() {
packetHandlers = new ArrayList<>();
packetHandlers.add(new PacketHandlerAnimation());
packetHandlers.add(new PacketHandlerAttributes());
packetHandlers.add(new PacketHandlerBed());
packetHandlers.add(new PacketHandlerCollect());
packetHandlers.add(new PacketHandlerEntityStatus());
packetHandlers.add(new PacketHandlerEquipment());
packetHandlers.add(new PacketHandlerHeadRotation());
packetHandlers.add(new PacketHandlerMetadata(this));
packetHandlers.add(new PacketHandlerMovement());
packetHandlers.add(new PacketHandlerSpawn(this));
packetHandlers.add(new PacketHandlerVelocity());
}
public boolean isCancelMeta(Disguise disguise, Player observer) {
return cancelMeta.containsKey(disguise) && cancelMeta.get(disguise).contains(observer.getUniqueId());
}
public void addCancel(Disguise disguise, Player observer) {
if (!cancelMeta.containsKey(disguise)) {
cancelMeta.put(disguise, new ArrayList<UUID>());
}
cancelMeta.get(disguise).add(observer.getUniqueId());
}
public void removeCancel(Disguise disguise, Player observer) {
ArrayList<UUID> cancel;
if ((cancel = cancelMeta.get(disguise)) == null)
return;
cancel.remove(observer.getUniqueId());
if (!cancel.isEmpty())
return;
cancelMeta.remove(disguise);
}
/**
* Transform the packet magically into the one I have always dreamed off. My true luv!!! This will return null if
* its not
* transformed
*/
public LibsPackets transformPacket(PacketContainer sentPacket, Disguise disguise, Player observer, Entity entity) {
LibsPackets packets = new LibsPackets(disguise);
try {
packets.addPacket(sentPacket);
for (IPacketHandler packetHandler : packetHandlers) {
for (PacketType packetType : packetHandler.getHandledPackets()) {
if (packetType != sentPacket.getType()) {
continue;
}
packetHandler.handle(disguise, sentPacket, packets, observer, entity);
return packets;
}
}
packets.setUnhandled();
}
catch (Exception e) {
e.printStackTrace();
}
return packets;
}
}

View File

@ -0,0 +1,205 @@
package me.libraryaddict.disguise.utilities.packets;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketListener;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.packets.packetlisteners.*;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.ArrayList;
public class PacketsManager {
private static PacketListener clientInteractEntityListener;
private static PacketListener inventoryListener;
private static boolean inventoryModifierEnabled;
private static LibsDisguises libsDisguises;
private static PacketListener mainListener;
private static PacketListener soundsListener;
private static boolean soundsListenerEnabled;
private static PacketListener viewDisguisesListener;
private static boolean viewDisguisesListenerEnabled;
private static PacketsHandler packetsHandler;
public static void addPacketListeners() {
// Add a client listener to cancel them interacting with uninteractable disguised entitys.
// You ain't supposed to be allowed to 'interact' with a item that cannot be clicked.
// Because it kicks you for hacking.
clientInteractEntityListener = new PacketListenerClientInteract(libsDisguises);
PacketListener tabListListener = new PacketListenerTabList(libsDisguises);
ProtocolLibrary.getProtocolManager().addPacketListener(clientInteractEntityListener);
ProtocolLibrary.getProtocolManager().addPacketListener(tabListListener);
// Now I call this and the main listener is registered!
setupMainPacketsListener();
}
/**
* Creates the packet listeners
*/
public static void init(LibsDisguises plugin) {
libsDisguises = plugin;
soundsListener = new PacketListenerSounds(libsDisguises);
// Self disguise (/vsd) listener
viewDisguisesListener = new PacketListenerViewSelfDisguise(libsDisguises);
inventoryListener = new PacketListenerInventory(libsDisguises);
packetsHandler = new PacketsHandler();
}
public static PacketsHandler getPacketsHandler() {
return packetsHandler;
}
public static boolean isHearDisguisesEnabled() {
return soundsListenerEnabled;
}
public static boolean isInventoryListenerEnabled() {
return inventoryModifierEnabled;
}
public static boolean isViewDisguisesListenerEnabled() {
return viewDisguisesListenerEnabled;
}
public static void setHearDisguisesListener(boolean enabled) {
if (soundsListenerEnabled != enabled) {
soundsListenerEnabled = enabled;
if (soundsListenerEnabled) {
ProtocolLibrary.getProtocolManager().addPacketListener(soundsListener);
} else {
ProtocolLibrary.getProtocolManager().removePacketListener(soundsListener);
}
}
}
public static void setInventoryListenerEnabled(boolean enabled) {
if (inventoryModifierEnabled != enabled) {
inventoryModifierEnabled = enabled;
if (inventoryModifierEnabled) {
ProtocolLibrary.getProtocolManager().addPacketListener(inventoryListener);
} else {
ProtocolLibrary.getProtocolManager().removePacketListener(inventoryListener);
}
for (Player player : Bukkit.getOnlinePlayers()) {
Disguise disguise = DisguiseAPI.getDisguise(player, player);
if (disguise != null) {
if (viewDisguisesListenerEnabled && disguise.isSelfDisguiseVisible() &&
(disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
player.updateInventory();
}
}
}
}
}
public static void setupMainPacketsListener() {
if (clientInteractEntityListener != null) {
if (mainListener != null) {
ProtocolLibrary.getProtocolManager().removePacketListener(mainListener);
}
ArrayList<PacketType> packetsToListen = new ArrayList<>();
// Add spawn packets
{
packetsToListen.add(Server.NAMED_ENTITY_SPAWN);
packetsToListen.add(Server.SPAWN_ENTITY_EXPERIENCE_ORB);
packetsToListen.add(Server.SPAWN_ENTITY);
packetsToListen.add(Server.SPAWN_ENTITY_LIVING);
packetsToListen.add(Server.SPAWN_ENTITY_PAINTING);
}
// Add packets that always need to be enabled to ensure safety
{
packetsToListen.add(Server.ENTITY_METADATA);
}
if (DisguiseConfig.isCollectPacketsEnabled()) {
packetsToListen.add(Server.COLLECT);
}
if (DisguiseConfig.isMiscDisguisesForLivingEnabled()) {
packetsToListen.add(Server.UPDATE_ATTRIBUTES);
}
// The bed packet.
if (DisguiseConfig.isBedPacketsEnabled()) {
packetsToListen.add(Server.BED);
}
// Add movement packets
if (DisguiseConfig.isMovementPacketsEnabled()) {
packetsToListen.add(Server.ENTITY_LOOK);
packetsToListen.add(Server.REL_ENTITY_MOVE_LOOK);
packetsToListen.add(Server.ENTITY_HEAD_ROTATION);
packetsToListen.add(Server.ENTITY_TELEPORT);
packetsToListen.add(Server.REL_ENTITY_MOVE);
packetsToListen.add(Server.ENTITY_VELOCITY);
}
// Add equipment packet
if (DisguiseConfig.isEquipmentPacketsEnabled()) {
packetsToListen.add(Server.ENTITY_EQUIPMENT);
}
// Add the packet that ensures if they are sleeping or not
if (DisguiseConfig.isAnimationPacketsEnabled()) {
packetsToListen.add(Server.ANIMATION);
}
// Add the packet that makes sure that entities with armor do not send unpickupable armor on death
if (DisguiseConfig.isEntityStatusPacketsEnabled()) {
packetsToListen.add(Server.ENTITY_STATUS);
}
mainListener = new PacketListenerMain(libsDisguises, packetsToListen);
ProtocolLibrary.getProtocolManager().addPacketListener(mainListener);
}
}
public static void setViewDisguisesListener(boolean enabled) {
if (viewDisguisesListenerEnabled != enabled) {
viewDisguisesListenerEnabled = enabled;
if (viewDisguisesListenerEnabled) {
ProtocolLibrary.getProtocolManager().addPacketListener(viewDisguisesListener);
} else {
ProtocolLibrary.getProtocolManager().removePacketListener(viewDisguisesListener);
}
for (Player player : Bukkit.getOnlinePlayers()) {
Disguise disguise = DisguiseAPI.getDisguise(player, player);
if (disguise != null) {
if (disguise.isSelfDisguiseVisible()) {
if (enabled) {
DisguiseUtilities.setupFakeDisguise(disguise);
} else {
DisguiseUtilities.removeSelfDisguise(player);
}
if (inventoryModifierEnabled &&
(disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
player.updateInventory();
}
}
}
}
}
}
}

View File

@ -0,0 +1,31 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerAnimation implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.ANIMATION};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
// Else if the disguise is attempting to send players a forbidden packet
if (disguise.getType().isMisc() || (sentPacket.getIntegers().read(1) == 2 && (!disguise.getType().isPlayer() ||
(DisguiseConfig.isBedPacketsEnabled() && ((PlayerWatcher) disguise.getWatcher()).isSleeping())))) {
packets.clear();
}
}
}

View File

@ -0,0 +1,71 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedAttribute;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.reflection.DisguiseValues;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerAttributes implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.UPDATE_ATTRIBUTES};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
if (disguise.isMiscDisguise()) {
packets.clear();
} else {
List<WrappedAttribute> attributes = new ArrayList<>();
for (WrappedAttribute attribute : sentPacket.getAttributeCollectionModifier().read(0)) {
if (attribute.getAttributeKey().equals("generic.maxHealth")) {
packets.clear();
PacketContainer updateAttributes = new PacketContainer(PacketType.Play.Server.UPDATE_ATTRIBUTES);
packets.addPacket(updateAttributes);
WrappedAttribute.Builder builder;
if (((LivingWatcher) disguise.getWatcher()).isMaxHealthSet()) {
builder = WrappedAttribute.newBuilder();
builder.attributeKey("generic.maxHealth");
builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth());
} else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity()) {
builder = WrappedAttribute.newBuilder(attribute);
} else {
builder = WrappedAttribute.newBuilder();
builder.attributeKey("generic.maxHealth");
builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth());
}
builder.packet(updateAttributes);
attributes.add(builder.build());
break;
}
}
if (!attributes.isEmpty()) {
packets.getPackets().get(0).getIntegers().write(0, entity.getEntityId());
packets.getPackets().get(0).getAttributeCollectionModifier().write(0, attributes);
} else {
packets.clear();
}
}
}
}

View File

@ -0,0 +1,28 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerBed implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.BED};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
// If the entity is going into a bed, stop everything but players from doing this
if (!disguise.getType().isPlayer()) {
packets.clear();
}
}
}

View File

@ -0,0 +1,42 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerCollect implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.COLLECT};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
if (disguise.getType().isMisc()) {
packets.clear();
} else if (DisguiseConfig.isBedPacketsEnabled() && disguise.getType().isPlayer() &&
((PlayerWatcher) disguise.getWatcher()).isSleeping()) {
PacketContainer newPacket = new PacketContainer(PacketType.Play.Server.ANIMATION);
StructureModifier<Integer> mods = newPacket.getIntegers();
mods.write(0, disguise.getEntity().getEntityId());
mods.write(1, 3);
packets.clear();
packets.addPacket(newPacket);
packets.addPacket(sentPacket);
}
}
}

View File

@ -0,0 +1,28 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerEntityStatus implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.ENTITY_STATUS};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
// If the entity is updating their status, stop them from showing death
if (packets.getPackets().get(0).getBytes().read(0) == (byte) 3) {
packets.clear();
}
}
}

View File

@ -0,0 +1,103 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerEquipment implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.ENTITY_EQUIPMENT};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
// Else if the disguise is updating equipment
EquipmentSlot slot = ReflectionManager.createEquipmentSlot(packets.getPackets().get(0).getModifier().read(1));
org.bukkit.inventory.ItemStack itemStack = disguise.getWatcher().getItemStack(slot);
if (itemStack != null) {
packets.clear();
PacketContainer equipPacket = sentPacket.shallowClone();
packets.addPacket(equipPacket);
equipPacket.getModifier()
.write(2, ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack));
}
if (disguise.getWatcher().isRightClicking() && slot == EquipmentSlot.HAND) {
ItemStack heldItem = packets.getPackets().get(0).getItemModifier().read(0);
if (heldItem != null && heldItem.getType() != Material.AIR) {
// Convert the datawatcher
List<WrappedWatchableObject> list = new ArrayList<>();
if (DisguiseConfig.isMetadataPacketsEnabled()) {
WrappedWatchableObject watch = ReflectionManager
.createWatchable(0, WrappedDataWatcher.getEntityWatcher(entity).getByte(0));
if (watch != null)
list.add(watch);
list = disguise.getWatcher().convert(list);
} else {
for (WrappedWatchableObject obj : disguise.getWatcher().getWatchableObjects()) {
if (obj.getIndex() == 0) {
list.add(obj);
break;
}
}
}
// Construct the packets to return
PacketContainer packetBlock = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
packetBlock.getModifier().write(0, entity.getEntityId());
packetBlock.getWatchableCollectionModifier().write(0, list);
PacketContainer packetUnblock = packetBlock.deepClone();
// Make a packet to send the 'unblock'
for (WrappedWatchableObject watcher : packetUnblock.getWatchableCollectionModifier().read(0)) {
watcher.setValue((byte) ((byte) watcher.getValue() & ~(1 << 4)));
}
// Send the unblock before the itemstack change so that the 2nd metadata packet works. Why?
// Scheduler
// delay.
PacketContainer packet1 = packets.getPackets().get(0);
packets.clear();
packets.addPacket(packetUnblock);
packets.addPacket(packet1);
packets.addPacket(packetBlock);
// Silly mojang made the right clicking datawatcher value only valid for one use. So I have
// to reset
// it.
}
}
}
}

View File

@ -0,0 +1,53 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerHeadRotation implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.ENTITY_HEAD_ROTATION};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
if (disguise.getType().isPlayer() && entity.getType() != EntityType.PLAYER) {
Location loc = entity.getLocation();
byte pitch = DisguiseUtilities
.getPitch(disguise.getType(), entity.getType(), (byte) (int) (loc.getPitch() * 256.0F / 360.0F));
byte yaw = DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), sentPacket.getBytes().read(0));
PacketContainer rotation = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
StructureModifier<Object> mods = rotation.getModifier();
mods.write(0, entity.getEntityId());
mods.write(1, yaw);
PacketContainer look = new PacketContainer(PacketType.Play.Server.ENTITY_LOOK);
look.getIntegers().write(0, entity.getEntityId());
look.getBytes().write(0, yaw);
look.getBytes().write(1, pitch);
packets.clear();
packets.addPacket(look);
packets.addPacket(rotation);
}
}
}

View File

@ -0,0 +1,53 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.packets.PacketsHandler;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.List;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerMetadata implements IPacketHandler {
private PacketsHandler packetsHandler;
public PacketHandlerMetadata(PacketsHandler packetsHandler) {
this.packetsHandler = packetsHandler;
}
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.ENTITY_METADATA};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
packets.clear();
if (DisguiseConfig.isMetadataPacketsEnabled() && packetsHandler.isCancelMeta(disguise, observer)) {
List<WrappedWatchableObject> watchableObjects = disguise.getWatcher()
.convert(sentPacket.getWatchableCollectionModifier().read(0));
PacketContainer metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
packets.addPacket(metaPacket);
StructureModifier<Object> newMods = metaPacket.getModifier();
newMods.write(0, entity.getEntityId());
metaPacket.getWatchableCollectionModifier().write(0, watchableObjects);
}
}
}

View File

@ -0,0 +1,103 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerMovement implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.REL_ENTITY_MOVE_LOOK, PacketType.Play.Server.ENTITY_LOOK,
PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.REL_ENTITY_MOVE};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
if (disguise.getType() == DisguiseType.RABBIT &&
(sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE ||
sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE_LOOK)) {
// When did the rabbit disguise last hop
long lastHop = 999999;
// If hop meta exists, set the last hop time
if (!entity.getMetadata("LibsRabbitHop").isEmpty()) {
// Last hop was 3 minutes ago, so subtract current time with the last hop time and get 3
// minutes ago in milliseconds
lastHop = System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong();
}
// If last hop was less than 0.1 or more than 0.5 seconds ago
if (lastHop < 100 || lastHop > 500) {
if (lastHop > 500) {
entity.removeMetadata("LibsRabbitHop", LibsDisguises.getInstance());
entity.setMetadata("LibsRabbitHop",
new FixedMetadataValue(LibsDisguises.getInstance(), System.currentTimeMillis()));
}
PacketContainer statusPacket = new PacketContainer(PacketType.Play.Server.ENTITY_STATUS);
packets.addPacket(statusPacket);
statusPacket.getIntegers().write(0, entity.getEntityId());
statusPacket.getBytes().write(0, (byte) 1);
}
}
// Stop wither skulls from looking
if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK &&
disguise.getType() == DisguiseType.WITHER_SKULL) {
packets.clear();
} else if (sentPacket.getType() != PacketType.Play.Server.REL_ENTITY_MOVE) {
packets.clear();
PacketContainer movePacket = sentPacket.shallowClone();
packets.addPacket(movePacket);
StructureModifier<Byte> bytes = movePacket.getBytes();
byte yawValue = bytes.read(0);
byte pitchValue = bytes.read(1);
bytes.write(0, DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), yawValue));
bytes.write(1, DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), pitchValue));
if (sentPacket.getType() == PacketType.Play.Server.ENTITY_TELEPORT &&
disguise.getType() == DisguiseType.ITEM_FRAME) {
StructureModifier<Double> doubles = movePacket.getDoubles();
Location loc = entity.getLocation();
double data = (((loc.getYaw() % 360) + 720 + 45) / 90) % 4;
if (data % 2 == 0) {
if (data % 2 == 0) {
doubles.write(3, loc.getZ());
} else {
doubles.write(1, loc.getZ());
}
}
double y = DisguiseUtilities.getYModifier(entity, disguise);
if (y != 0) {
doubles.write(2, doubles.read(2) + y);
}
}
}
}
}

View File

@ -0,0 +1,403 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedAttribute;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.*;
import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.packets.PacketsHandler;
import me.libraryaddict.disguise.utilities.reflection.DisguiseValues;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Art;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerSpawn implements IPacketHandler {
private PacketsHandler packetsHandler;
public PacketHandlerSpawn(PacketsHandler packetsHandler) {
this.packetsHandler = packetsHandler;
}
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.NAMED_ENTITY_SPAWN, PacketType.Play.Server.SPAWN_ENTITY_LIVING,
PacketType.Play.Server.SPAWN_ENTITY_EXPERIENCE_ORB, PacketType.Play.Server.SPAWN_ENTITY,
PacketType.Play.Server.SPAWN_ENTITY_PAINTING};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
packets.clear();
constructSpawnPackets(observer, packets, entity);
}
/**
* Construct the packets I need to spawn in the disguise
*/
private void constructSpawnPackets(final Player observer, LibsPackets packets, Entity disguisedEntity) {
Disguise disguise = packets.getDisguise();
if (disguise.getEntity() == null) {
disguise.setEntity(disguisedEntity);
}
// This sends the armor packets so that the player isn't naked.
// Please note it only sends the packets that wouldn't be sent normally
if (DisguiseConfig.isEquipmentPacketsEnabled()) {
for (EquipmentSlot slot : EquipmentSlot.values()) {
ItemStack itemstack = disguise.getWatcher().getItemStack(slot);
if (itemstack == null || itemstack.getType() == Material.AIR) {
continue;
}
if (disguisedEntity instanceof LivingEntity) {
ItemStack item = ReflectionManager.getEquipment(slot, disguisedEntity);
if (item != null && item.getType() != Material.AIR) {
continue;
}
}
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_EQUIPMENT);
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, ReflectionManager.createEnumItemSlot(slot));
mods.write(2, ReflectionManager.getNmsItem(itemstack));
packets.addDelayedPacket(packet);
}
}
if (DisguiseConfig.isMiscDisguisesForLivingEnabled()) {
if (disguise.getWatcher() instanceof LivingWatcher) {
ArrayList<WrappedAttribute> attributes = new ArrayList<>();
WrappedAttribute.Builder builder = WrappedAttribute.newBuilder().attributeKey("generic.maxHealth");
if (((LivingWatcher) disguise.getWatcher()).isMaxHealthSet()) {
builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth());
} else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity() &&
disguisedEntity instanceof Damageable) {
builder.baseValue(((Damageable) disguisedEntity).getMaxHealth());
} else {
builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth());
}
PacketContainer packet = new PacketContainer(PacketType.Play.Server.UPDATE_ATTRIBUTES);
builder.packet(packet);
attributes.add(builder.build());
packet.getIntegers().write(0, disguisedEntity.getEntityId());
packet.getAttributeCollectionModifier().write(0, attributes);
packets.addPacket(packet);
}
}
Location loc = disguisedEntity.getLocation().clone()
.add(0, DisguiseUtilities.getYModifier(disguisedEntity, disguise), 0);
byte yaw = (byte) (int) (loc.getYaw() * 256.0F / 360.0F);
byte pitch = (byte) (int) (loc.getPitch() * 256.0F / 360.0F);
if (DisguiseConfig.isMovementPacketsEnabled()) {
yaw = DisguiseUtilities.getYaw(disguise.getType(), disguisedEntity.getType(), yaw);
pitch = DisguiseUtilities.getPitch(disguise.getType(), disguisedEntity.getType(), pitch);
}
if (disguise.getType() == DisguiseType.EXPERIENCE_ORB) {
PacketContainer spawnOrb = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_EXPERIENCE_ORB);
packets.addPacket(spawnOrb);
StructureModifier<Object> mods = spawnOrb.getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, loc.getX());
mods.write(2, loc.getY() + 0.06);
mods.write(3, loc.getZ());
mods.write(4, 1);
} else if (disguise.getType() == DisguiseType.PAINTING) {
PacketContainer spawnPainting = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_PAINTING);
packets.addPacket(spawnPainting);
StructureModifier<Object> mods = spawnPainting.getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, disguisedEntity.getUniqueId());
mods.write(2, ReflectionManager.getBlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
mods.write(3, ReflectionManager.getEnumDirection(((int) loc.getYaw()) % 4));
int id = ((MiscDisguise) disguise).getData();
mods.write(4, ReflectionManager.getEnumArt(Art.values()[id]));
// Make the teleport packet to make it visible..
PacketContainer teleportPainting = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT);
packets.addPacket(teleportPainting);
mods = teleportPainting.getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, loc.getX());
mods.write(2, loc.getY());
mods.write(3, loc.getZ());
mods.write(4, yaw);
mods.write(5, pitch);
} else if (disguise.getType().isPlayer()) {
PlayerDisguise playerDisguise = (PlayerDisguise) disguise;
String name = playerDisguise.getName();
WrappedGameProfile gameProfile = playerDisguise.getGameProfile();
int entityId = disguisedEntity.getEntityId();
// Send player info along with the disguise
PacketContainer sendTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
if (!((PlayerDisguise) disguise).isDisplayedInTab()) {
// Add player to the list, necessary to spawn them
sendTab.getModifier().write(0, ReflectionManager.getEnumPlayerInfoAction(0));
List playerList = Collections
.singletonList(ReflectionManager.getPlayerInfoData(sendTab.getHandle(), gameProfile));
sendTab.getModifier().write(1, playerList);
packets.addPacket(sendTab);
}
// Spawn the player
PacketContainer spawnPlayer = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
spawnPlayer.getIntegers().write(0, entityId); // Id
spawnPlayer.getModifier().write(1, gameProfile.getUUID());
Location spawnAt = disguisedEntity.getLocation();
boolean selfDisguise = observer == disguisedEntity;
WrappedDataWatcher newWatcher;
if (selfDisguise) {
newWatcher = DisguiseUtilities
.createSanitizedDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity),
disguise.getWatcher());
} else {
newWatcher = new WrappedDataWatcher();
spawnAt = observer.getLocation();
spawnAt.add(spawnAt.getDirection().normalize().multiply(20));
}
// Spawn him in front of the observer
StructureModifier<Double> doubles = spawnPlayer.getDoubles();
doubles.write(0, spawnAt.getX());
doubles.write(1, spawnAt.getY());
doubles.write(2, spawnAt.getZ());
StructureModifier<Byte> bytes = spawnPlayer.getBytes();
bytes.write(0, yaw);
bytes.write(1, pitch);
spawnPlayer.getDataWatcherModifier().write(0, newWatcher);
// Make him invisible
newWatcher.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(MetaIndex.ENTITY_META.getIndex(),
WrappedDataWatcher.Registry.get(Byte.class)), (byte) 32);
packets.addPacket(spawnPlayer);
if (DisguiseConfig.isBedPacketsEnabled() && ((PlayerWatcher) disguise.getWatcher()).isSleeping()) {
PacketContainer[] bedPackets = DisguiseUtilities.getBedPackets(
loc.clone().subtract(0, DisguiseUtilities.getYModifier(disguisedEntity, disguise), 0),
observer.getLocation(), ((PlayerDisguise) disguise));
for (PacketContainer packet : bedPackets) {
packets.addPacket(packet);
}
} else if (!selfDisguise) {
// Teleport the player back to where he's supposed to be
PacketContainer teleportPacket = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT);
doubles = teleportPacket.getDoubles();
teleportPacket.getIntegers().write(0, entityId); // Id
doubles.write(0, loc.getX());
doubles.write(1, loc.getY());
doubles.write(2, loc.getZ());
bytes = teleportPacket.getBytes();
bytes.write(0, yaw);
bytes.write(1, pitch);
packets.addPacket(teleportPacket);
}
if (!selfDisguise) {
// Send a metadata packet
PacketContainer metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
newWatcher = DisguiseUtilities
.createSanitizedDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity),
disguise.getWatcher());
metaPacket.getIntegers().write(0, entityId); // Id
metaPacket.getWatchableCollectionModifier().write(0, newWatcher.getWatchableObjects());
packetsHandler.addCancel(disguise, observer);
// Add a delay to remove the entry from 'cancelMeta'
packets.addDelayedPacket(metaPacket, 4);
}
// Remove player from the list
PacketContainer deleteTab = sendTab.shallowClone();
deleteTab.getModifier().write(0, ReflectionManager.getEnumPlayerInfoAction(4));
if (!((PlayerDisguise) disguise).isDisplayedInTab()) {
packets.addDelayedPacket(deleteTab, 40);
}
} else if (disguise.getType().isMob() || disguise.getType() == DisguiseType.ARMOR_STAND) {
Vector vec = disguisedEntity.getVelocity();
PacketContainer spawnEntity = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
packets.addPacket(spawnEntity);
StructureModifier<Object> mods = spawnEntity.getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, disguisedEntity.getUniqueId());
mods.write(2, disguise.getType().getTypeId());
// region Vector calculations
double d1 = 3.9D;
double d2 = vec.getX();
double d3 = vec.getY();
double d4 = vec.getZ();
if (d2 < -d1)
d2 = -d1;
if (d3 < -d1)
d3 = -d1;
if (d4 < -d1)
d4 = -d1;
if (d2 > d1)
d2 = d1;
if (d3 > d1)
d3 = d1;
if (d4 > d1)
d4 = d1;
// endregion
mods.write(3, loc.getX());
mods.write(4, loc.getY());
mods.write(5, loc.getZ());
mods.write(6, (int) (d2 * 8000.0D));
mods.write(7, (int) (d3 * 8000.0D));
mods.write(8, (int) (d4 * 8000.0D));
mods.write(9, yaw);
mods.write(10, pitch);
mods.write(11, yaw);
spawnEntity.getDataWatcherModifier().write(0, DisguiseUtilities
.createSanitizedDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity),
disguise.getWatcher()));
} else if (disguise.getType().isMisc()) {
int objectId = disguise.getType().getObjectId();
int data = ((MiscDisguise) disguise).getData();
if (disguise.getType() == DisguiseType.FALLING_BLOCK) {
ItemStack block = ((FallingBlockWatcher) disguise.getWatcher()).getBlock();
data = ReflectionManager.getCombinedIdByItemStack(block);
} else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == -1) {
// If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners
// entity id
data = observer.getEntityId();
} else if (disguise.getType() == DisguiseType.ITEM_FRAME) {
data = ((((int) loc.getYaw() % 360) + 720 + 45) / 90) % 4;
}
Object nmsEntity = ReflectionManager.getNmsEntity(disguisedEntity);
PacketContainer spawnEntity = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(PacketType.Play.Server.SPAWN_ENTITY, nmsEntity, objectId, data)
.createPacket(nmsEntity, objectId, data);
packets.addPacket(spawnEntity);
// If it's not the same type, then highly likely they have different velocity settings which we'd want to
// cancel
if (DisguiseType.getType(disguisedEntity) != disguise.getType()) {
StructureModifier<Integer> ints = spawnEntity.getIntegers();
ints.write(1, 0);
ints.write(2, 0);
ints.write(3, 0);
}
spawnEntity.getModifier().write(8, pitch);
spawnEntity.getModifier().write(9, yaw);
if (disguise.getType() == DisguiseType.ITEM_FRAME) {
if (data % 2 == 0) {
spawnEntity.getModifier().write(4, loc.getZ() + (data == 0 ? -1 : 1));
} else {
spawnEntity.getModifier().write(2, loc.getX() + (data == 3 ? -1 : 1));
}
}
}
if (packets.getPackets().size() <= 1 || disguise.isPlayerDisguise()) {
PacketContainer rotateHead = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION);
packets.addPacket(rotateHead);
StructureModifier<Object> mods = rotateHead.getModifier();
mods.write(0, disguisedEntity.getEntityId());
mods.write(1, yaw);
}
if (disguise.getType() == DisguiseType.EVOKER_FANGS) {
PacketContainer newPacket = new PacketContainer(PacketType.Play.Server.ENTITY_STATUS);
StructureModifier<Object> mods = newPacket.getModifier();
mods.write(0, disguise.getEntity().getEntityId());
mods.write(1, (byte) 4);
packets.addPacket(newPacket);
}
}
}

View File

@ -0,0 +1,29 @@
package me.libraryaddict.disguise.utilities.packets.packethandlers;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Created by libraryaddict on 3/01/2019.
*/
public class PacketHandlerVelocity implements IPacketHandler {
@Override
public PacketType[] getHandledPackets() {
return new PacketType[]{PacketType.Play.Server.ENTITY_VELOCITY};
}
@Override
public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer,
Entity entity) {
// If the disguise is a misc and the disguised is not the same type
if (disguise.getType().isMisc() && DisguiseType.getType(entity) != disguise.getType()) {
packets.clear();
}
}
}

View File

@ -1,112 +1,112 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.EnumWrappers; import com.comphenix.protocol.wrappers.EnumWrappers;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher;
import me.libraryaddict.disguise.events.DisguiseInteractEvent; import me.libraryaddict.disguise.events.DisguiseInteractEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public class PacketListenerClientInteract extends PacketAdapter { public class PacketListenerClientInteract extends PacketAdapter {
public PacketListenerClientInteract(LibsDisguises plugin) { public PacketListenerClientInteract(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY); super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY);
} }
@Override @Override
public void onPacketReceiving(PacketEvent event) { public void onPacketReceiving(PacketEvent event) {
if (event.isCancelled()) if (event.isCancelled())
return; return;
Player observer = event.getPlayer(); Player observer = event.getPlayer();
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
return; return;
PacketContainer packet = event.getPacket(); PacketContainer packet = event.getPacket();
StructureModifier<Entity> entityModifer = packet.getEntityModifier(observer.getWorld()); StructureModifier<Entity> entityModifer = packet.getEntityModifier(observer.getWorld());
Entity entity = entityModifer.read(0); Entity entity = entityModifer.read(0);
if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow ||
entity == observer) { entity == observer) {
event.setCancelled(true); event.setCancelled(true);
} else if (packet.getIntegers().read(0) == DisguiseAPI.getSelfDisguiseId()) { } else if (packet.getIntegers().read(0) == DisguiseAPI.getSelfDisguiseId()) {
// If it's a self-interact // If it's a self-interact
event.setCancelled(true); event.setCancelled(true);
Disguise disguise = DisguiseAPI.getDisguise(observer, observer); Disguise disguise = DisguiseAPI.getDisguise(observer, observer);
if (disguise != null) { if (disguise != null) {
// The type of interact, we don't care the difference with "Interact_At" however as it's not useful // The type of interact, we don't care the difference with "Interact_At" however as it's not useful
// for self disguises // for self disguises
EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0); EnumWrappers.EntityUseAction interactType = packet.getEntityUseActions().read(0);
EquipmentSlot handUsed = EquipmentSlot.HAND; EquipmentSlot handUsed = EquipmentSlot.HAND;
// Attack has a null hand, which throws an error if you attempt to fetch // Attack has a null hand, which throws an error if you attempt to fetch
if (interactType != EnumWrappers.EntityUseAction.ATTACK) { if (interactType != EnumWrappers.EntityUseAction.ATTACK) {
// If the hand used wasn't their main hand // If the hand used wasn't their main hand
if (packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) { if (packet.getHands().read(0) == EnumWrappers.Hand.OFF_HAND) {
handUsed = EquipmentSlot.OFF_HAND; handUsed = EquipmentSlot.OFF_HAND;
} }
} }
DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed, DisguiseInteractEvent selfEvent = new DisguiseInteractEvent((TargetedDisguise) disguise, handUsed,
interactType == EnumWrappers.EntityUseAction.ATTACK); interactType == EnumWrappers.EntityUseAction.ATTACK);
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
Bukkit.getPluginManager().callEvent(selfEvent); Bukkit.getPluginManager().callEvent(selfEvent);
} }
}.runTask(LibsDisguises.getInstance()); }.runTask(LibsDisguises.getInstance());
} }
} }
for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(), for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(),
observer.getInventory().getItemInOffHand()}) { observer.getInventory().getItemInOffHand()}) {
if (item == null) { if (item == null) {
continue; continue;
} }
AnimalColor color = AnimalColor.getColorByMaterial(item.getType()); AnimalColor color = AnimalColor.getColorByMaterial(item.getType());
if (color == null) { if (color == null) {
continue; continue;
} }
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
if (disguise == null || if (disguise == null ||
(disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF)) { (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF)) {
continue; continue;
} }
if (disguise.getType() == DisguiseType.SHEEP) { if (disguise.getType() == DisguiseType.SHEEP) {
SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); SheepWatcher watcher = (SheepWatcher) disguise.getWatcher();
watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor());
} else { } else {
WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); WolfWatcher watcher = (WolfWatcher) disguise.getWatcher();
watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor());
} }
} }
} }
} }

View File

@ -1,341 +1,341 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
public class PacketListenerInventory extends PacketAdapter { public class PacketListenerInventory extends PacketAdapter {
private LibsDisguises libsDisguises; private LibsDisguises libsDisguises;
public PacketListenerInventory(LibsDisguises plugin) { public PacketListenerInventory(LibsDisguises plugin) {
super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, PacketType.Play.Client.HELD_ITEM_SLOT, super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, PacketType.Play.Client.HELD_ITEM_SLOT,
PacketType.Play.Client.SET_CREATIVE_SLOT, PacketType.Play.Client.WINDOW_CLICK); PacketType.Play.Client.SET_CREATIVE_SLOT, PacketType.Play.Client.WINDOW_CLICK);
libsDisguises = plugin; libsDisguises = plugin;
} }
@Override @Override
public void onPacketReceiving(final PacketEvent event) { public void onPacketReceiving(final PacketEvent event) {
if (event.isCancelled()) if (event.isCancelled())
return; return;
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (player.getName().contains("UNKNOWN[")) // If the player is temporary if (player.getName().contains("UNKNOWN[")) // If the player is temporary
return; return;
if (player instanceof com.comphenix.net.sf.cglib.proxy.Factory || player.getVehicle() != null) { if (player instanceof com.comphenix.net.sf.cglib.proxy.Factory || player.getVehicle() != null) {
return; return;
} }
Disguise disguise = DisguiseAPI.getDisguise(player, player); Disguise disguise = DisguiseAPI.getDisguise(player, player);
// 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.isSelfDisguiseVisible() if (disguise != null && disguise.isSelfDisguiseVisible()
&& (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
// If they are in creative and clicked on a slot // If they are in creative and clicked on a slot
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) { if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) {
int slot = event.getPacket().getIntegers().read(0); int slot = event.getPacket().getIntegers().read(0);
if (slot >= 5 && slot <= 8) { if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) { if (disguise.isHidingArmorFromSelf()) {
int armorSlot = Math.abs((slot - 5) - 3); int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = player.getInventory().getArmorContents()[armorSlot]; org.bukkit.inventory.ItemStack item = player.getInventory().getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
else if (slot >= 36 && slot <= 45) { else if (slot >= 36 && slot <= 45) {
if (disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingHeldItemFromSelf()) {
int currentSlot = player.getInventory().getHeldItemSlot(); int currentSlot = player.getInventory().getHeldItemSlot();
if (slot + 36 == currentSlot || slot == 45) { if (slot + 36 == currentSlot || slot == 45) {
org.bukkit.inventory.ItemStack item = player.getInventory().getItemInMainHand(); org.bukkit.inventory.ItemStack item = player.getInventory().getItemInMainHand();
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
} }
} }
// If the player switched item, aka he moved from slot 1 to slot 2 // If the player switched item, aka he moved from slot 1 to slot 2
else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT) { else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT) {
if (disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingHeldItemFromSelf()) {
// From logging, it seems that both bukkit and nms uses the same thing for the slot switching. // From logging, it seems that both bukkit and nms uses the same thing for the slot switching.
// 0 1 2 3 - 8 // 0 1 2 3 - 8
// If the packet is coming, then I need to replace the item they are switching to // If the packet is coming, then I need to replace the item they are switching to
// As for the old item, I need to restore it. // As for the old item, I need to restore it.
org.bukkit.inventory.ItemStack currentlyHeld = player.getItemInHand(); org.bukkit.inventory.ItemStack currentlyHeld = player.getItemInHand();
// If his old weapon isn't air // If his old weapon isn't air
if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR) { if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR) {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, player.getInventory().getHeldItemSlot() + 36); mods.write(1, player.getInventory().getHeldItemSlot() + 36);
mods.write(2, ReflectionManager.getNmsItem(currentlyHeld)); mods.write(2, ReflectionManager.getNmsItem(currentlyHeld));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
org.bukkit.inventory.ItemStack newHeld = player.getInventory() org.bukkit.inventory.ItemStack newHeld = player.getInventory()
.getItem(event.getPacket().getIntegers().read(0)); .getItem(event.getPacket().getIntegers().read(0));
// If his new weapon isn't air either! // If his new weapon isn't air either!
if (newHeld != null && newHeld.getType() != Material.AIR) { if (newHeld != null && newHeld.getType() != Material.AIR) {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, event.getPacket().getIntegers().read(0) + 36); mods.write(1, event.getPacket().getIntegers().read(0) + 36);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) { else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) {
int slot = event.getPacket().getIntegers().read(1); int slot = event.getPacket().getIntegers().read(1);
org.bukkit.inventory.ItemStack clickedItem; org.bukkit.inventory.ItemStack clickedItem;
if (event.getPacket().getShorts().read(0) == 1) { if (event.getPacket().getShorts().read(0) == 1) {
// Its a shift click // Its a shift click
clickedItem = event.getPacket().getItemModifier().read(0); clickedItem = event.getPacket().getItemModifier().read(0);
if (clickedItem != null && clickedItem.getType() != Material.AIR) { if (clickedItem != null && clickedItem.getType() != Material.AIR) {
// Rather than predict the clients actions // Rather than predict the clients actions
// Lets just update the entire inventory.. // Lets just update the entire inventory..
Bukkit.getScheduler().runTask(libsDisguises, new Runnable() { Bukkit.getScheduler().runTask(libsDisguises, new Runnable() {
public void run() { public void run() {
player.updateInventory(); player.updateInventory();
} }
}); });
} }
return; return;
} }
else { else {
// If its not a player inventory click // If its not a player inventory click
// Shift clicking is exempted for the item in hand.. // Shift clicking is exempted for the item in hand..
if (event.getPacket().getIntegers().read(0) != 0) { if (event.getPacket().getIntegers().read(0) != 0) {
return; return;
} }
clickedItem = player.getItemOnCursor(); clickedItem = player.getItemOnCursor();
} }
if (clickedItem != null && clickedItem.getType() != Material.AIR) { if (clickedItem != null && clickedItem.getType() != Material.AIR) {
// If the slot is a armor slot // If the slot is a armor slot
if (slot >= 5 && slot <= 8) { if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) { if (disguise.isHidingArmorFromSelf()) {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
// Else if its a hotbar slot // Else if its a hotbar slot
} }
else if (slot >= 36 && slot <= 45) { else if (slot >= 36 && slot <= 45) {
if (disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingHeldItemFromSelf()) {
int currentSlot = player.getInventory().getHeldItemSlot(); int currentSlot = player.getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting // Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36 || slot == 45) { if (slot == currentSlot + 36 || slot == 45) {
PacketContainer packet = new PacketContainer(Server.SET_SLOT); PacketContainer packet = new PacketContainer(Server.SET_SLOT);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
} }
} }
} }
} }
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
// If the inventory is the players inventory // If the inventory is the players inventory
if (player instanceof com.comphenix.net.sf.cglib.proxy.Factory || player.getVehicle() != null if (player instanceof com.comphenix.net.sf.cglib.proxy.Factory || player.getVehicle() != null
|| event.getPacket().getIntegers().read(0) != 0) { || event.getPacket().getIntegers().read(0) != 0) {
return; return;
} }
Disguise disguise = DisguiseAPI.getDisguise(player, player); Disguise disguise = DisguiseAPI.getDisguise(player, player);
if (disguise == null || !disguise.isSelfDisguiseVisible() if (disguise == null || !disguise.isSelfDisguiseVisible()
|| (!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf())) { || (!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf())) {
return; return;
} }
// 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 the server is setting the slot // If the server is setting the slot
// Need to set it to air if its in a place it shouldn't be. // Need to set it to air if its in a place it shouldn't be.
// Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this // Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this
/** /**
* Done * Done
*/ */
if (event.getPacketType() == Server.SET_SLOT) { if (event.getPacketType() == Server.SET_SLOT) {
// The raw slot // The raw slot
// nms code has the start of the hotbar being 36. // nms code has the start of the hotbar being 36.
int slot = event.getPacket().getIntegers().read(1); int slot = event.getPacket().getIntegers().read(1);
// If the slot is a armor slot // If the slot is a armor slot
if (slot >= 5 && slot <= 8) { if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) { if (disguise.isHidingArmorFromSelf()) {
// Get the bukkit armor slot! // Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3); int armorSlot = Math.abs((slot - 5) - 3);
org.bukkit.inventory.ItemStack item = player.getInventory().getArmorContents()[armorSlot]; org.bukkit.inventory.ItemStack item = player.getInventory().getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
event.setPacket(event.getPacket().shallowClone()); event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2, event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
} }
} }
// Else if its a hotbar slot // Else if its a hotbar slot
} }
else if (slot >= 36 && slot <= 45) { else if (slot >= 36 && slot <= 45) {
if (disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingHeldItemFromSelf()) {
int currentSlot = player.getInventory().getHeldItemSlot(); int currentSlot = player.getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting // Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36 || slot == 45) { if (slot == currentSlot + 36 || slot == 45) {
org.bukkit.inventory.ItemStack item = player.getInventory().getItemInMainHand(); org.bukkit.inventory.ItemStack item = player.getInventory().getItemInMainHand();
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
event.setPacket(event.getPacket().shallowClone()); event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2, event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
} }
} }
} }
} }
} }
else if (event.getPacketType() == Server.WINDOW_ITEMS) { else if (event.getPacketType() == Server.WINDOW_ITEMS) {
event.setPacket(event.getPacket().shallowClone()); event.setPacket(event.getPacket().shallowClone());
StructureModifier<List<ItemStack>> mods = event.getPacket().getItemListModifier(); StructureModifier<List<ItemStack>> mods = event.getPacket().getItemListModifier();
List<ItemStack> items = new ArrayList<>(mods.read(0)); List<ItemStack> items = new ArrayList<>(mods.read(0));
for (int slot = 0; slot < items.size(); slot++) { for (int slot = 0; slot < items.size(); slot++) {
if (slot >= 5 && slot <= 8) { if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) { if (disguise.isHidingArmorFromSelf()) {
// Get the bukkit armor slot! // Get the bukkit armor slot!
int armorSlot = Math.abs((slot - 5) - 3); int armorSlot = Math.abs((slot - 5) - 3);
ItemStack item = player.getInventory().getArmorContents()[armorSlot]; ItemStack item = player.getInventory().getArmorContents()[armorSlot];
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
items.set(slot, new ItemStack(Material.AIR)); items.set(slot, new ItemStack(Material.AIR));
} }
} }
// Else if its a hotbar slot // Else if its a hotbar slot
} }
else if (slot >= 36 && slot <= 45) { else if (slot >= 36 && slot <= 45) {
if (disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingHeldItemFromSelf()) {
int currentSlot = player.getInventory().getHeldItemSlot(); int currentSlot = player.getInventory().getHeldItemSlot();
// Check if the player is on the same slot as the slot that its setting // Check if the player is on the same slot as the slot that its setting
if (slot == currentSlot + 36 || slot == 45) { if (slot == currentSlot + 36 || slot == 45) {
ItemStack item = player.getInventory().getItemInMainHand(); ItemStack item = player.getInventory().getItemInMainHand();
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
items.set(slot, new ItemStack(Material.AIR)); items.set(slot, new ItemStack(Material.AIR));
} }
} }
} }
} }
} }
mods.write(0, items); mods.write(0, items);
} }
} }
} }

View File

@ -1,86 +1,82 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import java.lang.reflect.InvocationTargetException; import com.comphenix.protocol.PacketType;
import java.util.ArrayList; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary;
import org.bukkit.entity.Entity; import com.comphenix.protocol.events.ListenerPriority;
import org.bukkit.entity.Player; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.ProtocolLibrary; import me.libraryaddict.disguise.DisguiseAPI;
import com.comphenix.protocol.events.ListenerPriority; import me.libraryaddict.disguise.LibsDisguises;
import com.comphenix.protocol.events.PacketAdapter; import me.libraryaddict.disguise.disguisetypes.Disguise;
import com.comphenix.protocol.events.PacketContainer; import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import com.comphenix.protocol.events.PacketEvent; import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import com.comphenix.protocol.reflect.StructureModifier; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import java.lang.reflect.InvocationTargetException;
import me.libraryaddict.disguise.disguisetypes.Disguise; import java.util.ArrayList;
import me.libraryaddict.disguise.utilities.PacketsManager;
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets; public class PacketListenerMain extends PacketAdapter {
public PacketListenerMain(LibsDisguises plugin, ArrayList<PacketType> packetsToListen) {
public class PacketListenerMain extends PacketAdapter { super(plugin, ListenerPriority.HIGH, packetsToListen);
public PacketListenerMain(LibsDisguises plugin, ArrayList<PacketType> packetsToListen) { }
super(plugin, ListenerPriority.HIGH, packetsToListen);
} @Override
public void onPacketSending(final PacketEvent event) {
@Override if (event.isCancelled())
public void onPacketSending(final PacketEvent event) { return;
if (event.isCancelled())
return; final Player observer = event.getPlayer();
final Player observer = event.getPlayer(); if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
return;
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
return; // First get the entity, the one sending this packet
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
// First get the entity, the one sending this packet
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); org.bukkit.entity.Entity entity = entityModifer.read((Server.COLLECT == event.getPacketType() ? 1 : 0));
org.bukkit.entity.Entity entity = entityModifer.read((Server.COLLECT == event.getPacketType() ? 1 : 0)); // If the entity is the same as the sender. Don't disguise!
// Prevents problems and there is no advantage to be gained.
// If the entity is the same as the sender. Don't disguise! if (entity == observer)
// Prevents problems and there is no advantage to be gained. return;
if (entity == observer)
return; final Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
final Disguise disguise = DisguiseAPI.getDisguise(observer, entity); if (disguise == null)
return;
if (disguise == null)
return; LibsPackets packets;
LibsPackets packets; try {
packets = PacketsManager.getPacketsHandler().transformPacket(event.getPacket(), disguise, observer, entity);
try { }
packets = PacketsManager.transformPacket(event.getPacket(), disguise, observer, entity); catch (Exception ex) {
} ex.printStackTrace();
catch (Exception ex) { event.setCancelled(true);
ex.printStackTrace(); return;
event.setCancelled(true); }
return;
} if (packets.isUnhandled()) {
return;
if (packets.isUnhandled()) { }
return;
} packets.setSpawnPacketCheck(event.getPacketType());
packets.setSpawnPacketCheck(event.getPacketType()); event.setCancelled(true);
event.setCancelled(true); try {
for (PacketContainer packet : packets.getPackets()) {
try { ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
for (PacketContainer packet : packets.getPackets()) { }
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
} packets.sendDelayed(observer);
}
packets.sendDelayed(observer); catch (InvocationTargetException ex) {
} ex.printStackTrace();
catch (InvocationTargetException ex) { }
ex.printStackTrace(); }
} }
}
}

View File

@ -1,347 +1,347 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.utilities.DisguiseSound; import me.libraryaddict.disguise.utilities.DisguiseSound;
import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType; import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
public class PacketListenerSounds extends PacketAdapter { public class PacketListenerSounds extends PacketAdapter {
/** /**
* This is a fix for the stupidity that is * This is a fix for the stupidity that is
* "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard" * "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard"
*/ */
private static boolean cancelSound; private static boolean cancelSound;
private Object stepSoundEffect; private Object stepSoundEffect;
public PacketListenerSounds(LibsDisguises plugin) { public PacketListenerSounds(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS); super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS);
stepSoundEffect = ReflectionManager.getCraftSound(Sound.BLOCK_GRASS_STEP); stepSoundEffect = ReflectionManager.getCraftSound(Sound.BLOCK_GRASS_STEP);
} }
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
if (event.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
if (event.isAsync()) { if (event.isAsync()) {
return; return;
} }
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
return; return;
event.setPacket(event.getPacket().deepClone()); event.setPacket(event.getPacket().deepClone());
StructureModifier<Object> mods = event.getPacket().getModifier(); StructureModifier<Object> mods = event.getPacket().getModifier();
Player observer = event.getPlayer(); Player observer = event.getPlayer();
if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) { if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) {
SoundType soundType = null; SoundType soundType = null;
int[] soundCords = new int[]{(Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4)}; int[] soundCords = new int[]{(Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4)};
int chunkX = (int) Math.floor((soundCords[0] / 8D) / 16D); int chunkX = (int) Math.floor((soundCords[0] / 8D) / 16D);
int chunkZ = (int) Math.floor((soundCords[2] / 8D) / 16D); int chunkZ = (int) Math.floor((soundCords[2] / 8D) / 16D);
if (!observer.getWorld().isChunkLoaded(chunkX, chunkZ)) { if (!observer.getWorld().isChunkLoaded(chunkX, chunkZ)) {
return; return;
} }
Entity disguisedEntity = null; Entity disguisedEntity = null;
DisguiseSound entitySound = null; DisguiseSound entitySound = null;
Disguise disguise = null; Disguise disguise = null;
Object soundEffectObj = mods.read(0); Object soundEffectObj = mods.read(0);
Entity[] entities = observer.getWorld().getChunkAt(chunkX, chunkZ).getEntities(); Entity[] entities = observer.getWorld().getChunkAt(chunkX, chunkZ).getEntities();
for (Entity entity : entities) { for (Entity entity : entities) {
Disguise entityDisguise = DisguiseAPI.getDisguise(observer, entity); Disguise entityDisguise = DisguiseAPI.getDisguise(observer, entity);
if (entityDisguise != null) { if (entityDisguise != null) {
Location loc = entity.getLocation(); Location loc = entity.getLocation();
int[] entCords = new int[]{(int) (loc.getX() * 8), (int) (loc.getY() * 8), (int) (loc.getZ() * 8)}; int[] entCords = new int[]{(int) (loc.getX() * 8), (int) (loc.getY() * 8), (int) (loc.getZ() * 8)};
if (soundCords[0] != entCords[0] || soundCords[1] != entCords[1] || soundCords[2] != entCords[2]) { if (soundCords[0] != entCords[0] || soundCords[1] != entCords[1] || soundCords[2] != entCords[2]) {
continue; continue;
} }
entitySound = DisguiseSound.getType(entity.getType().name()); entitySound = DisguiseSound.getType(entity.getType().name());
if (entitySound == null) { if (entitySound == null) {
continue; continue;
} }
Object obj = null; Object obj = null;
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
try { try {
// Use reflection so that this works for either int or double methods // Use reflection so that this works for either int or double methods
obj = LivingEntity.class.getMethod("getHealth").invoke(entity); obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) { if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) {
soundType = SoundType.DEATH; soundType = SoundType.DEATH;
} else { } else {
obj = null; obj = null;
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if (obj == null) { if (obj == null) {
boolean hasInvun = false; boolean hasInvun = false;
Object nmsEntity = ReflectionManager.getNmsEntity(entity); Object nmsEntity = ReflectionManager.getNmsEntity(entity);
try { try {
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt(nmsEntity) == hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt(nmsEntity) ==
ReflectionManager.getNmsField("EntityLiving", "maxNoDamageTicks") ReflectionManager.getNmsField("EntityLiving", "maxNoDamageTicks")
.getInt(nmsEntity); .getInt(nmsEntity);
} else { } else {
Class clazz = ReflectionManager.getNmsClass("DamageSource"); Class clazz = ReflectionManager.getNmsClass("DamageSource");
hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", clazz) hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", clazz)
.invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC").get(null)); .invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC").get(null));
} }
} }
catch (Exception ex) { catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
soundType = entitySound.getType(soundEffectObj, !hasInvun); soundType = entitySound.getType(soundEffectObj, !hasInvun);
} }
if (soundType != null) { if (soundType != null) {
disguise = entityDisguise; disguise = entityDisguise;
disguisedEntity = entity; disguisedEntity = entity;
break; break;
} }
} }
} }
if (disguise != null && disguise.isSoundsReplaced() && if (disguise != null && disguise.isSoundsReplaced() &&
(disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != observer)) { (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != observer)) {
Object sound = null; Object sound = null;
DisguiseSound disguiseSound = DisguiseSound.getType(disguise.getType().name()); DisguiseSound disguiseSound = DisguiseSound.getType(disguise.getType().name());
if (disguiseSound != null) { if (disguiseSound != null) {
sound = disguiseSound.getSound(soundType); sound = disguiseSound.getSound(soundType);
} }
if (sound == null) { if (sound == null) {
event.setCancelled(true); event.setCancelled(true);
} else { } else {
if (sound.equals("step.grass")) { if (sound.equals("step.grass")) {
try { try {
Block block = observer.getWorld().getBlockAt((int) Math.floor(soundCords[0] / 8D), Block block = observer.getWorld().getBlockAt((int) Math.floor(soundCords[0] / 8D),
(int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D)); (int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D));
if (block != null) { if (block != null) {
Object nmsBlock = ReflectionManager.getCraftMethod("block.CraftBlock", "getNMSBlock") Object nmsBlock = ReflectionManager.getCraftMethod("block.CraftBlock", "getNMSBlock")
.invoke(block); .invoke(block);
Object step = ReflectionManager.getNmsMethod("Block", "getStepSound").invoke(nmsBlock); Object step = ReflectionManager.getNmsMethod("Block", "getStepSound").invoke(nmsBlock);
mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "d").invoke(step)); mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "d").invoke(step));
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
} }
} }
catch (Exception ex) { catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
// There is no else statement. Because seriously. This should never be null. Unless // There is no else statement. Because seriously. This should never be null. Unless
// someone is // someone is
// sending fake sounds. In which case. Why cancel it. // sending fake sounds. In which case. Why cancel it.
} else { } else {
mods.write(0, sound); mods.write(0, sound);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
// Time to change the pitch and volume // Time to change the pitch and volume
if (soundType == SoundType.HURT || soundType == SoundType.DEATH || if (soundType == SoundType.HURT || soundType == SoundType.DEATH ||
soundType == SoundType.IDLE) { soundType == SoundType.IDLE) {
// If the volume is the default // If the volume is the default
if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) { if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) {
mods.write(5, disguiseSound.getDamageAndIdleSoundVolume()); mods.write(5, disguiseSound.getDamageAndIdleSoundVolume());
} }
// Here I assume its the default pitch as I can't calculate if its real. // Here I assume its the default pitch as I can't calculate if its real.
if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity && if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity &&
((MobDisguise) disguise).doesDisguiseAge()) { ((MobDisguise) disguise).doesDisguiseAge()) {
boolean baby = false; boolean baby = false;
if (disguisedEntity instanceof Zombie) { if (disguisedEntity instanceof Zombie) {
baby = ((Zombie) disguisedEntity).isBaby(); baby = ((Zombie) disguisedEntity).isBaby();
} else if (disguisedEntity instanceof Ageable) { } else if (disguisedEntity instanceof Ageable) {
baby = !((Ageable) disguisedEntity).isAdult(); baby = !((Ageable) disguisedEntity).isAdult();
} }
if (((MobDisguise) disguise).isAdult() == baby) { if (((MobDisguise) disguise).isAdult() == baby) {
float pitch = (Float) mods.read(6); float pitch = (Float) mods.read(6);
if (baby) { if (baby) {
// If the pitch is not the expected // If the pitch is not the expected
if (pitch < 1.5 || pitch > 1.7) if (pitch < 1.5 || pitch > 1.7)
return; return;
pitch = (DisguiseUtilities.random.nextFloat() - pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F; DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F;
// Min = 1.5 // Min = 1.5
// Cap = 97.5 // Cap = 97.5
// Max = 1.7 // Max = 1.7
// Cap = 110.5 // Cap = 110.5
} else { } else {
// If the pitch is not the expected // If the pitch is not the expected
if (pitch < 1 || pitch > 1.2) if (pitch < 1 || pitch > 1.2)
return; return;
pitch = (DisguiseUtilities.random.nextFloat() - pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F;
// Min = 1 // Min = 1
// Cap = 63 // Cap = 63
// Max = 1.2 // Max = 1.2
// Cap = 75.6 // Cap = 75.6
} }
/*pitch *= 63; /*pitch *= 63;
if (pitch < 0) if (pitch < 0)
pitch = 0; pitch = 0;
if (pitch > 255) if (pitch > 255)
pitch = 255;*/ pitch = 255;*/
mods.write(6, pitch); mods.write(6, pitch);
} }
} }
} }
} }
} }
} }
} else if (event.getPacketType() == Server.ENTITY_STATUS) { } else if (event.getPacketType() == Server.ENTITY_STATUS) {
if ((byte) mods.read(1) != 2) { if ((byte) mods.read(1) != 2) {
return; return;
} }
// It made a damage animation // It made a damage animation
Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0);
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
if (disguise != null && !disguise.getType().isPlayer() && if (disguise != null && !disguise.getType().isPlayer() &&
(disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) { (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) {
DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); DisguiseSound disSound = DisguiseSound.getType(entity.getType().name());
if (disSound == null) if (disSound == null)
return; return;
SoundType soundType = null; SoundType soundType = null;
Object obj = null; Object obj = null;
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
try { try {
obj = LivingEntity.class.getMethod("getHealth").invoke(entity); obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) { if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) {
soundType = SoundType.DEATH; soundType = SoundType.DEATH;
} else { } else {
obj = null; obj = null;
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if (obj == null) { if (obj == null) {
soundType = SoundType.HURT; soundType = SoundType.HURT;
} }
if (disSound.getSound(soundType) == null || if (disSound.getSound(soundType) == null ||
(disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) { (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) {
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) { if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) {
cancelSound = !cancelSound; cancelSound = !cancelSound;
if (cancelSound) if (cancelSound)
return; return;
} }
disSound = DisguiseSound.getType(disguise.getType().name()); disSound = DisguiseSound.getType(disguise.getType().name());
if (disSound != null) { if (disSound != null) {
Object sound = disSound.getSound(soundType); Object sound = disSound.getSound(soundType);
if (sound != null) { if (sound != null) {
Location loc = entity.getLocation(); Location loc = entity.getLocation();
PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT); PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT);
mods = packet.getModifier(); mods = packet.getModifier();
mods.write(0, sound); mods.write(0, sound);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh
mods.write(2, (int) (loc.getX() * 8D)); mods.write(2, (int) (loc.getX() * 8D));
mods.write(3, (int) (loc.getY() * 8D)); mods.write(3, (int) (loc.getY() * 8D));
mods.write(4, (int) (loc.getZ() * 8D)); mods.write(4, (int) (loc.getZ() * 8D));
mods.write(5, disSound.getDamageAndIdleSoundVolume()); mods.write(5, disSound.getDamageAndIdleSoundVolume());
float pitch; float pitch;
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) {
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) *
0.2F + 1.5F; 0.2F + 1.5F;
} else } else
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) *
0.2F + 1.0F; 0.2F + 1.0F;
if (disguise.getType() == DisguiseType.BAT) if (disguise.getType() == DisguiseType.BAT)
pitch *= 0.95F; pitch *= 0.95F;
/* pitch *= 63; /* pitch *= 63;
if (pitch < 0) if (pitch < 0)
pitch = 0; pitch = 0;
if (pitch > 255) if (pitch > 255)
pitch = 255;*/ pitch = 255;*/
mods.write(6, pitch); mods.write(6, pitch);
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
} }
} }
} }
} }

View File

@ -1,65 +1,65 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction; import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction;
import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.PlayerInfoData;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
public class PacketListenerTabList extends PacketAdapter { public class PacketListenerTabList extends PacketAdapter {
public PacketListenerTabList(LibsDisguises plugin) { public PacketListenerTabList(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.PLAYER_INFO); super(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.PLAYER_INFO);
} }
@Override @Override
public void onPacketSending(final PacketEvent event) { public void onPacketSending(final PacketEvent event) {
if (event.isCancelled()) if (event.isCancelled())
return; return;
Player observer = event.getPlayer(); Player observer = event.getPlayer();
if (event.getPacket().getPlayerInfoAction().read(0) != PlayerInfoAction.ADD_PLAYER) if (event.getPacket().getPlayerInfoAction().read(0) != PlayerInfoAction.ADD_PLAYER)
return; return;
List<PlayerInfoData> list = event.getPacket().getPlayerInfoDataLists().read(0); List<PlayerInfoData> list = event.getPacket().getPlayerInfoDataLists().read(0);
Iterator<PlayerInfoData> itel = list.iterator(); Iterator<PlayerInfoData> itel = list.iterator();
while (itel.hasNext()) { while (itel.hasNext()) {
PlayerInfoData data = itel.next(); PlayerInfoData data = itel.next();
Player player = Bukkit.getPlayer(data.getProfile().getUUID()); Player player = Bukkit.getPlayer(data.getProfile().getUUID());
if (player == null) if (player == null)
continue; continue;
Disguise disguise = DisguiseAPI.getDisguise(observer, player); Disguise disguise = DisguiseAPI.getDisguise(observer, player);
if (disguise == null) if (disguise == null)
continue; continue;
if (!disguise.isHidePlayer()) if (!disguise.isHidePlayer())
continue; continue;
itel.remove(); itel.remove();
} }
if (list.isEmpty()) { if (list.isEmpty()) {
event.setCancelled(true); event.setCancelled(true);
} }
else { else {
event.getPacket().getPlayerInfoDataLists().write(0, list); event.getPacket().getPlayerInfoDataLists().write(0, list);
} }
} }
} }

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
@ -12,9 +12,9 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.PacketsManager; import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets; import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -67,7 +67,8 @@ public class PacketListenerViewSelfDisguise extends PacketAdapter {
} }
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them. // Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
LibsPackets transformed = PacketsManager.transformPacket(packet, disguise, observer, observer); LibsPackets transformed = PacketsManager.getPacketsHandler()
.transformPacket(packet, disguise, observer, observer);
if (transformed.isUnhandled()) { if (transformed.isUnhandled()) {
transformed.getPackets().add(packet); transformed.getPackets().add(packet);

View File

@ -1,6 +1,6 @@
package me.libraryaddict.disguise.utilities.parser; package me.libraryaddict.disguise.utilities.parser;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
/** /**
* Created by libraryaddict on 7/09/2018. * Created by libraryaddict on 7/09/2018.

View File

@ -3,8 +3,8 @@ package me.libraryaddict.disguise.utilities.parser;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.*; import me.libraryaddict.disguise.disguisetypes.*;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;

View File

@ -1,6 +1,6 @@
package me.libraryaddict.disguise.utilities.parser.params; package me.libraryaddict.disguise.utilities.parser.params;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import java.util.HashMap; import java.util.HashMap;

View File

@ -1,6 +1,6 @@
package me.libraryaddict.disguise.utilities.parser.params.types.base; package me.libraryaddict.disguise.utilities.parser.params.types.base;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import java.util.List; import java.util.List;

View File

@ -1,6 +1,6 @@
package me.libraryaddict.disguise.utilities.parser.params.types.custom; package me.libraryaddict.disguise.utilities.parser.params.types.custom;
import me.libraryaddict.disguise.utilities.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import me.libraryaddict.disguise.utilities.parser.params.types.ParamInfoEnum; import me.libraryaddict.disguise.utilities.parser.params.types.ParamInfoEnum;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;

View File

@ -2,7 +2,7 @@ package me.libraryaddict.disguise.utilities.parser.params.types.custom;
import com.comphenix.protocol.wrappers.WrappedBlockData; import com.comphenix.protocol.wrappers.WrappedBlockData;
import com.comphenix.protocol.wrappers.WrappedParticle; import com.comphenix.protocol.wrappers.WrappedParticle;
import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.params.types.ParamInfoEnum; import me.libraryaddict.disguise.utilities.parser.params.types.ParamInfoEnum;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;

View File

@ -1,95 +1,95 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.reflection;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.security.CodeSource; import java.security.CodeSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
/** /**
* User: Austin Date: 4/22/13 Time: 11:47 PM (c) lazertester * User: Austin Date: 4/22/13 Time: 11:47 PM (c) lazertester
*/ */
// Code for this taken and slightly modified from // Code for this taken and slightly modified from
// https://github.com/ddopson/java-class-enumerator // https://github.com/ddopson/java-class-enumerator
public class ClassGetter public class ClassGetter
{ {
public static ArrayList<Class<?>> getClassesForPackage(String pkgname) public static ArrayList<Class<?>> getClassesForPackage(String pkgname)
{ {
ArrayList<Class<?>> classes = new ArrayList<>(); ArrayList<Class<?>> classes = new ArrayList<>();
// String relPath = pkgname.replace('.', '/'); // String relPath = pkgname.replace('.', '/');
// Get a File object for the package // Get a File object for the package
CodeSource src = Entity.class.getProtectionDomain().getCodeSource(); CodeSource src = Entity.class.getProtectionDomain().getCodeSource();
if (src != null) if (src != null)
{ {
URL resource = src.getLocation(); URL resource = src.getLocation();
resource.getPath(); resource.getPath();
processJarfile(resource, pkgname, classes); processJarfile(resource, pkgname, classes);
} }
return classes; return classes;
} }
private static Class<?> loadClass(String className) private static Class<?> loadClass(String className)
{ {
try try
{ {
return Class.forName(className); return Class.forName(className);
} }
catch (ClassNotFoundException e) catch (ClassNotFoundException e)
{ {
throw new RuntimeException("Unexpected ClassNotFoundException loading class '" + className + "'"); throw new RuntimeException("Unexpected ClassNotFoundException loading class '" + className + "'");
} }
catch (NoClassDefFoundError e) catch (NoClassDefFoundError e)
{ {
return null; return null;
} }
} }
private static void processJarfile(URL resource, String pkgname, ArrayList<Class<?>> classes) private static void processJarfile(URL resource, String pkgname, ArrayList<Class<?>> classes)
{ {
try try
{ {
String relPath = pkgname.replace('.', '/'); String relPath = pkgname.replace('.', '/');
String resPath = URLDecoder.decode(resource.getPath(), "UTF-8"); String resPath = URLDecoder.decode(resource.getPath(), "UTF-8");
String jarPath = resPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", ""); String jarPath = resPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
JarFile jarFile = new JarFile(jarPath); JarFile jarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = jarFile.entries(); Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) while (entries.hasMoreElements())
{ {
JarEntry entry = entries.nextElement(); JarEntry entry = entries.nextElement();
String entryName = entry.getName(); String entryName = entry.getName();
String className = null; String className = null;
if (entryName.endsWith(".class") && entryName.startsWith(relPath) if (entryName.endsWith(".class") && entryName.startsWith(relPath)
&& entryName.length() > (relPath.length() + "/".length())) && entryName.length() > (relPath.length() + "/".length()))
{ {
className = entryName.replace('/', '.').replace('\\', '.').replace(".class", ""); className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
} }
if (className != null) if (className != null)
{ {
Class<?> c = loadClass(className); Class<?> c = loadClass(className);
if (c != null) if (c != null)
{ {
classes.add(c); classes.add(c);
} }
} }
} }
jarFile.close(); jarFile.close();
} }
catch (Exception ex) catch (Exception ex)
{ {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
} }

View File

@ -1,62 +1,62 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.reflection;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import java.util.HashMap; import java.util.HashMap;
public class DisguiseValues { public class DisguiseValues {
private static HashMap<DisguiseType, DisguiseValues> values = new HashMap<>(); private static HashMap<DisguiseType, DisguiseValues> values = new HashMap<>();
public static DisguiseValues getDisguiseValues(DisguiseType type) { public static DisguiseValues getDisguiseValues(DisguiseType type) {
return values.get(type); return values.get(type);
} }
public static Class getNmsEntityClass(DisguiseType type) { public static Class getNmsEntityClass(DisguiseType type) {
return getDisguiseValues(type).getNmsEntityClass(); return getDisguiseValues(type).getNmsEntityClass();
} }
private FakeBoundingBox adultBox; private FakeBoundingBox adultBox;
private FakeBoundingBox babyBox; private FakeBoundingBox babyBox;
private float[] entitySize; private float[] entitySize;
private double maxHealth; private double maxHealth;
private Class nmsEntityClass; private Class nmsEntityClass;
public DisguiseValues(DisguiseType type, Class classType, int entitySize, double maxHealth) { public DisguiseValues(DisguiseType type, Class classType, int entitySize, double maxHealth) {
values.put(type, this); values.put(type, this);
nmsEntityClass = classType; nmsEntityClass = classType;
this.maxHealth = maxHealth; this.maxHealth = maxHealth;
} }
public FakeBoundingBox getAdultBox() { public FakeBoundingBox getAdultBox() {
return adultBox; return adultBox;
} }
public FakeBoundingBox getBabyBox() { public FakeBoundingBox getBabyBox() {
return babyBox; return babyBox;
} }
public float[] getEntitySize() { public float[] getEntitySize() {
return entitySize; return entitySize;
} }
public double getMaxHealth() { public double getMaxHealth() {
return maxHealth; return maxHealth;
} }
public Class getNmsEntityClass() { public Class getNmsEntityClass() {
return nmsEntityClass; return nmsEntityClass;
} }
public void setAdultBox(FakeBoundingBox newBox) { public void setAdultBox(FakeBoundingBox newBox) {
adultBox = newBox; adultBox = newBox;
} }
public void setBabyBox(FakeBoundingBox newBox) { public void setBabyBox(FakeBoundingBox newBox) {
babyBox = newBox; babyBox = newBox;
} }
public void setEntitySize(float[] size) { public void setEntitySize(float[] size) {
this.entitySize = size; this.entitySize = size;
} }
} }

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.reflection;
public class FakeBoundingBox { public class FakeBoundingBox {

View File

@ -1,9 +1,9 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.reflection;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
public interface LibsProfileLookup { public interface LibsProfileLookup {
void onLookup(WrappedGameProfile gameProfile); void onLookup(WrappedGameProfile gameProfile);
} }

View File

@ -1,4 +1,4 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.reflection;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.translations;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;

View File

@ -1,6 +1,7 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.translations;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.reflection.ClassGetter;
import me.libraryaddict.disguise.utilities.parser.ParamInfoManager; import me.libraryaddict.disguise.utilities.parser.ParamInfoManager;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -1,6 +1,8 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities.translations;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium;
import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringEscapeUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;