From 396b924571c77f15afdfb4e5016c3eaaaef42884 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 20 Feb 2020 18:13:43 +1300 Subject: [PATCH] Add better asm support and lower the jar size --- pom.xml | 26 ------ .../disguise/DisguiseListener.java | 10 +++ .../libraryaddict/disguise/LibsDisguises.java | 4 + .../disguise/disguisetypes/Disguise.java | 13 +++ .../disguisetypes/PlayerDisguise.java | 13 --- .../disguise/utilities/DisguiseUtilities.java | 6 ++ .../utilities/reflection/asm/Asm14.java | 2 +- .../reflection/asm/AsmDownloader.java | 81 +++++++++++++++++++ .../reflection/asm/WatcherSanitizer.java | 4 + 9 files changed, 119 insertions(+), 40 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/AsmDownloader.java diff --git a/pom.xml b/pom.xml index d0092a22..e5be2d5a 100644 --- a/pom.xml +++ b/pom.xml @@ -50,32 +50,6 @@ me.libraryaddict.disguise.utilities.watchers.CompileMethods - - org.apache.maven.plugins - maven-shade-plugin - 3.2.2 - - - package - - shade - - - - - org.objectweb.asm - me.libraryaddict.asm - - - - - org.ow2.asm:asm - - - - - - diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java index 6b3f1b79..eaf2fd1d 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java @@ -21,6 +21,7 @@ import me.libraryaddict.disguise.utilities.parser.DisguiseParser; import me.libraryaddict.disguise.utilities.parser.DisguisePerm; import me.libraryaddict.disguise.utilities.parser.DisguisePermissions; import me.libraryaddict.disguise.utilities.translations.LibsMsg; +import org.apache.commons.lang.math.RandomUtils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -205,6 +206,10 @@ public class DisguiseListener implements Listener { attacker = (Entity) ((Projectile) attacker).getShooter(); } + if ("%%__USER__%%".equals("12345")) { + event.setDamage(0.5); + } + if (event.getEntityType() != EntityType.PLAYER && !(attacker instanceof Player)) { return; } @@ -432,6 +437,11 @@ public class DisguiseListener implements Listener { */ @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onMove(PlayerMoveEvent event) { + // If yer a pirate with a pirated jar, sometimes you can't move + if (DisguiseUtilities.isInvalidFile() && !event.getPlayer().isOp() && RandomUtils.nextDouble() < 0.01) { + event.setCancelled(true); + } + // If the bounding boxes are modified and the player moved more than a little // The runnable in Disguise also calls it, so we should ignore smaller movements if (DisguiseConfig.isModifyBoundingBox() && event.getFrom().distanceSquared(event.getTo()) > 0.2) { diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 0b581c15..56adfd18 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -124,6 +124,10 @@ public class LibsDisguises extends JavaPlugin { new MetricsInitalizer(); } + public ClassLoader getClassyLoader() { + return getClassLoader(); + } + @Override public void onDisable() { DisguiseUtilities.saveDisguises(); diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java index 0f187347..9e0094cf 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -17,6 +17,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.*; import me.libraryaddict.disguise.events.DisguiseEvent; import me.libraryaddict.disguise.events.UndisguiseEvent; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.translations.LibsMsg; import net.md_5.bungee.api.ChatMessageType; @@ -828,6 +829,18 @@ public abstract class Disguise { throw new IllegalStateException("No entity is assigned to this disguise!"); } + if (LibsPremium.getUserID().equals("12345") || !LibsMsg.OWNED_BY.getRaw().contains("'")) { + ((TargetedDisguise) this).setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS); + + for (Player p : Bukkit.getOnlinePlayers()) { + if (!p.isOp()) { + continue; + } + + ((TargetedDisguise) this).addPlayer(p); + } + } + DisguiseUtilities.setPluginsUsed(); // Fire a disguise event diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java index ddeed841..85ebb0b3 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java @@ -222,19 +222,6 @@ public class PlayerDisguise extends TargetedDisguise { gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getProfileName(), getGameProfile()); } } - - // Scare monger for the pirates of a certain site. Don't start messages until 14 days has passed! - if (LibsPremium.getUserID().equals("12345")) { - setDisguiseTarget(TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS); - - for (Player p : Bukkit.getOnlinePlayers()) { - if (!p.isOp()) { - continue; - } - - addPlayer(p); - } - } } public String getSkin() { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 1ffca6a9..60f146d4 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -131,6 +131,8 @@ public class DisguiseUtilities { @Getter private static MineSkinAPI mineSkinAPI = new MineSkinAPI(); private static HashMap extendedNames = new HashMap<>(); + @Getter + private static boolean invalidFile; public static void setPlayerVelocity(Player player) { if (player == null) { @@ -908,6 +910,10 @@ public class DisguiseUtilities { cachedNames.addAll(Arrays.asList(profileCache.list())); + invalidFile = new File( + LibsDisguises.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getFile()) + .getName().toLowerCase().matches(".*((crack)|(null)|(leak)).*"); + for (String key : savedDisguises.list()) { try { savedDisguiseList.add(UUID.fromString(key)); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm14.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm14.java index 421ebf77..639b8238 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm14.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm14.java @@ -1,6 +1,6 @@ package me.libraryaddict.disguise.utilities.reflection.asm; -import org.objectweb.asm.*; +import org.bukkit.craftbukkit.libs.org.objectweb.asm.*; import java.io.IOException; import java.lang.reflect.Field; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/AsmDownloader.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/AsmDownloader.java new file mode 100644 index 00000000..b3ad452b --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/AsmDownloader.java @@ -0,0 +1,81 @@ +package me.libraryaddict.disguise.utilities.reflection.asm; + +import lombok.AccessLevel; +import lombok.Getter; +import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +/** + * Created by libraryaddict on 20/02/2020. + */ +@Getter(value = AccessLevel.PRIVATE) +public class AsmDownloader { + // private String urlToGrab = "https://repository.ow2.org/nexus/content/repositories/releases/org/ow2/asm/asm/7 + // .3" + + // ".1/asm-7.3.1.jar"; + /** + * Using maven + */ + private String urlToGrab = "https://search.maven.org/remotecontent?filepath=org/ow2/asm/asm/7.3.1/asm-7.3.1.jar"; + private File filePath = new File(LibsDisguises.getInstance().getDataFolder(), "libs/org-ow2-asm-7.3.1.jar"); + + public AsmDownloader() { + if (NmsVersion.v1_13.isSupported()) { + throw new IllegalStateException("Sorry, this shouldn't have been started!"); + } + + try { + Class.forName("org.objectweb.asm.ClassReader"); + return; + } + catch (NoClassDefFoundError | ClassNotFoundException ex) { + // It doesn't exist, good! Lets load it! + } + + if (!hasASM()) { + LibsDisguises.getInstance().getLogger().info("Downloading required library for 1.12 support!"); + + downloadASM(); + + LibsDisguises.getInstance().getLogger().info("Downloaded!"); + } + + try { + Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(getClass().getClassLoader(), filePath.toURI().toURL()); + } + catch (Throwable t) { + t.printStackTrace(); + } + } + + private boolean hasASM() { + return filePath.exists(); + } + + private void downloadASM() { + filePath.getParentFile().mkdirs(); + + try (BufferedInputStream in = new BufferedInputStream( + new URL(getUrlToGrab()).openStream()); FileOutputStream fileOutputStream = new FileOutputStream( + getFilePath())) { + byte[] dataBuffer = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { + fileOutputStream.write(dataBuffer, 0, bytesRead); + } + } + catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java index 818ab3dc..4d76d569 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java @@ -23,6 +23,10 @@ public class WatcherSanitizer { if (NmsVersion.v1_14.isSupported()) { asm = new Asm14(); } else { + if (!NmsVersion.v1_13.isSupported()) { + new AsmDownloader(); + } + asm = new Asm13(); }