diff --git a/build.gradle b/build.gradle index 5f3dc97..6e63c89 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,6 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar plugins { id 'java' - id 'eclipse' id 'com.github.johnrengelman.shadow' version '7.0.0' } @@ -14,6 +13,7 @@ allprojects { group = pluginGroup repositories { + mavenLocal() mavenCentral() maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots" } maven { url "https://oss.sonatype.org/content/repositories/snapshots" } @@ -34,7 +34,7 @@ dependencies { implementation 'com.github.TheDutchMC:HaroTorch:v1-SNAPSHOT' implementation 'commons-io:commons-io:2.6' - implementation 'dev.array21:bukkit-reflection-util:1.1.1' + implementation 'dev.array21:bukkit-reflection-util:1.2.0' implementation 'dev.array21:classvalidator:1.0.0' implementation 'dev.array21:httplib:1.2.2' } diff --git a/src/main/java/dev/array21/harotorch/commands/CommandCooldown.java b/src/main/java/dev/array21/harotorch/commands/CommandCooldown.java new file mode 100644 index 0000000..781b6ed --- /dev/null +++ b/src/main/java/dev/array21/harotorch/commands/CommandCooldown.java @@ -0,0 +1,28 @@ +package dev.array21.harotorch.commands; + +import dev.array21.harotorch.HaroTorch; +import dev.array21.harotorch.lang.LangHandler; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.UUID; + +public class CommandCooldown { + public static boolean checkCommandCooldown(HaroTorch plugin, CommandSender sender, HashMap lastCommandTimestamps) { + Integer commandCooldown = plugin.getConfigManifest().commandCooldown; + if(commandCooldown != null && commandCooldown > 0) { + Long lastCommandUseTimestamp = lastCommandTimestamps.get(((Player) sender).getUniqueId()); + if(lastCommandUseTimestamp != null) { + if(lastCommandUseTimestamp >= System.currentTimeMillis()) { + sender.sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + LangHandler.activeLang.getLangMessages().get("commandCooldown")); + return true; + } + } + + lastCommandTimestamps.put(((Player) sender).getUniqueId(), System.currentTimeMillis() + (commandCooldown * 1000)); + } + return false; + } +} diff --git a/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightAreaOfEffectExecutor.java b/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightAreaOfEffectExecutor.java index a167b23..f12958c 100644 --- a/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightAreaOfEffectExecutor.java +++ b/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightAreaOfEffectExecutor.java @@ -10,6 +10,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; +import dev.array21.harotorch.commands.CommandCooldown; import org.bukkit.Color; import org.bukkit.Location; import org.bukkit.Particle; @@ -35,14 +36,14 @@ public class HighlightAreaOfEffectExecutor implements SubCommand { private static final ExecutorService POOL = Executors.newFixedThreadPool(3); - private static HashMap lastCommandTimestamps = new HashMap<>(); - + private final static HashMap lastCommandTimestamps = new HashMap<>(); + private static Class packetPlayOutWorldParticleClass; private static Class packetPlayOutWorldParticleInterfaceClass; private static Class craftPlayerClass; private static Class craftParticleClass; private static Class particleParamClass; - + static { try { if(ReflectionUtil.isUseNewSpigotPackaging()) { @@ -50,12 +51,12 @@ public class HighlightAreaOfEffectExecutor implements SubCommand { } else { packetPlayOutWorldParticleClass = ReflectionUtil.getNmsClass("PacketPlayOutWorldParticles"); } - + packetPlayOutWorldParticleInterfaceClass = packetPlayOutWorldParticleClass.getInterfaces()[0]; - + craftPlayerClass = ReflectionUtil.getBukkitClass("entity.CraftPlayer"); craftParticleClass = ReflectionUtil.getBukkitClass("CraftParticle"); - + if(ReflectionUtil.isUseNewSpigotPackaging()) { particleParamClass = ReflectionUtil.getMinecraftClass("core.particles.ParticleParam"); } else { @@ -67,20 +68,9 @@ public class HighlightAreaOfEffectExecutor implements SubCommand { } public boolean run(HaroTorch plugin, CommandSender sender, String[] args) { - - Integer commandCooldown = plugin.getConfigManifest().commandCooldown; - if(commandCooldown != null && commandCooldown > 0) { - Long lastCommandUseTimestamp = lastCommandTimestamps.get(((Player) sender).getUniqueId()); - if(lastCommandUseTimestamp != null) { - if(lastCommandUseTimestamp >= System.currentTimeMillis()) { - sender.sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + LangHandler.activeLang.getLangMessages().get("commandCooldown")); - return true; - } - } - - lastCommandTimestamps.put(((Player) sender).getUniqueId(), System.currentTimeMillis() + (commandCooldown * 1000)); - } - + + if (CommandCooldown.checkCommandCooldown(plugin, sender, lastCommandTimestamps)) return true; + String msg = LangHandler.activeLang.getLangMessages().get("startingAoe").replaceAll("%SECONDS%", ChatColor.RED + String.valueOf(plugin.getConfigManifest().torchHighlightTime) + ChatColor.GOLD); sender.sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + msg); @@ -143,7 +133,7 @@ public void run() { return true; } - + /** * Get all TorchParticleObject's in a square range * @param nearbyTorches Locations of the Torches diff --git a/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightExecutor.java b/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightExecutor.java index b029778..39ff52a 100644 --- a/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightExecutor.java +++ b/src/main/java/dev/array21/harotorch/commands/torchSubCmds/HighlightExecutor.java @@ -1,24 +1,30 @@ package dev.array21.harotorch.commands.torchSubCmds; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.UUID; -import java.util.regex.Pattern; +import dev.array21.bukkitreflectionlib.abstractions.entity.Entity; +import dev.array21.bukkitreflectionlib.abstractions.entity.monster.EntityMagmaCube; +import dev.array21.bukkitreflectionlib.abstractions.entity.player.CraftPlayer; +import dev.array21.bukkitreflectionlib.abstractions.entity.player.PlayerConnection; +import dev.array21.bukkitreflectionlib.abstractions.packet.EntityDestroyPacket; +import dev.array21.bukkitreflectionlib.abstractions.packet.EntityMetadataPacket; +import dev.array21.bukkitreflectionlib.abstractions.packet.EntitySpawnPacket; +import dev.array21.bukkitreflectionlib.abstractions.world.CraftWorld; +import dev.array21.bukkitreflectionlib.exceptions.ReflectException; +import dev.array21.harotorch.commands.CommandCooldown; import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; -import dev.array21.bukkitreflectionlib.ReflectionUtil; import net.md_5.bungee.api.ChatColor; import dev.array21.harotorch.HaroTorch; import dev.array21.harotorch.commands.SubCommand; import dev.array21.harotorch.lang.LangHandler; import dev.array21.harotorch.torch.TorchHandler; -@SuppressWarnings("deprecation") public class HighlightExecutor implements SubCommand { /** @@ -27,85 +33,15 @@ public class HighlightExecutor implements SubCommand { * V = Miliseconds since Jan 1 1970 after which the Player may execute a command again */ private static final HashMap lastCommandTimestamps = new HashMap<>(); - - private static Class craftPlayerClass; - private static Class craftWorldClass; - private static Class worldClass; - private static Class entityTypesClass; - private static Class entityMagmaCubeClass; - private static Class entitySlimeClass; - private static Class entityClass; - private static Class entityLivingClass; - private static Class packetPlayOutEntityLivingClass; - private static Class packetPlayOutEntityLivingInterfaceClass; - private static Class packetPlayOutEntityMetadataClass; - private static Class packetPlayOutEntityMetadataInterfaceClass; - private static Class packetPlayOutEntityDestroyClass; - private static Class packetPlayOutEntityDestroyInterfaceClass; - private static Class dataWatcherClass; - private static Object entityMagmaCubeField; - static { - try { - craftPlayerClass = ReflectionUtil.getBukkitClass("entity.CraftPlayer"); - craftWorldClass = ReflectionUtil.getBukkitClass("CraftWorld"); - - if(ReflectionUtil.isUseNewSpigotPackaging()) { - worldClass = ReflectionUtil.getMinecraftClass("world.level.World"); - entityTypesClass = ReflectionUtil.getMinecraftClass("world.entity.EntityTypes"); - entityMagmaCubeClass = ReflectionUtil.getMinecraftClass("world.entity.monster.EntityMagmaCube"); - entitySlimeClass = ReflectionUtil.getMinecraftClass("world.entity.monster.EntitySlime"); - entityClass = ReflectionUtil.getMinecraftClass("world.entity.Entity"); - entityLivingClass = ReflectionUtil.getMinecraftClass("world.entity.EntityLiving"); - packetPlayOutEntityLivingClass = ReflectionUtil.getMinecraftClass("network.protocol.game.PacketPlayOutSpawnEntityLiving"); - packetPlayOutEntityMetadataClass = ReflectionUtil.getMinecraftClass("network.protocol.game.PacketPlayOutEntityMetadata"); - packetPlayOutEntityDestroyClass = ReflectionUtil.getMinecraftClass("network.protocol.game.PacketPlayOutEntityDestroy"); - dataWatcherClass = ReflectionUtil.getMinecraftClass("network.syncher.DataWatcher"); + public boolean run(HaroTorch plugin, CommandSender sender, String[] args) { - entityMagmaCubeField = ReflectionUtil.getObject(entityTypesClass, (Object) null, "X"); - } else { - worldClass = ReflectionUtil.getNmsClass("World"); - entityTypesClass = ReflectionUtil.getNmsClass("EntityTypes"); - entityMagmaCubeClass = ReflectionUtil.getNmsClass("EntityMagmaCube"); - entitySlimeClass = ReflectionUtil.getNmsClass("EntitySlime"); - entityClass = ReflectionUtil.getNmsClass("Entity"); - entityLivingClass = ReflectionUtil.getNmsClass("EntityLiving"); - packetPlayOutEntityLivingClass = ReflectionUtil.getNmsClass("PacketPlayOutSpawnEntityLiving"); - packetPlayOutEntityMetadataClass = ReflectionUtil.getNmsClass("PacketPlayOutEntityMetadata"); - packetPlayOutEntityDestroyClass = ReflectionUtil.getNmsClass("PacketPlayOutEntityDestroy"); - dataWatcherClass = ReflectionUtil.getNmsClass("DataWatcher"); - - entityMagmaCubeField = ReflectionUtil.getObject(entityTypesClass, (Object) null, "MAGMA_CUBE"); - } - - packetPlayOutEntityLivingInterfaceClass = packetPlayOutEntityLivingClass.getInterfaces()[0]; - packetPlayOutEntityMetadataInterfaceClass = packetPlayOutEntityMetadataClass.getInterfaces()[0]; - packetPlayOutEntityDestroyInterfaceClass = packetPlayOutEntityDestroyClass.getInterfaces()[0]; - - } catch(Exception e) { - e.printStackTrace(); - } - } + if (CommandCooldown.checkCommandCooldown(plugin, sender, lastCommandTimestamps)) return true; - public boolean run(HaroTorch plugin, CommandSender sender, String[] args) { - - Integer commandCooldown = plugin.getConfigManifest().commandCooldown; - if(commandCooldown != null && commandCooldown > 0) { - Long lastCommandUseTimestamp = lastCommandTimestamps.get(((Player) sender).getUniqueId()); - if(lastCommandUseTimestamp != null) { - if(lastCommandUseTimestamp >= System.currentTimeMillis()) { - sender.sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + LangHandler.activeLang.getLangMessages().get("commandCooldown")); - return true; - } - } - - lastCommandTimestamps.put(((Player) sender).getUniqueId(), System.currentTimeMillis() + (commandCooldown * 1000)); - } - List nearbyTorches = TorchHandler.getTorchLocationsNearPlayer((Player) sender, plugin.getConfigManifest().torchHighlightRange); Player p = (Player) sender; - - List returnedIds = spawnHighlight(p, nearbyTorches); + + Entity[] spawnedEntities = spawnHighlight(p, nearbyTorches.toArray(new Location[0])); String msg = LangHandler.activeLang.getLangMessages().get("startingHiglight").replaceAll("%SECONDS%", ChatColor.RED + String.valueOf(plugin.getConfigManifest().torchHighlightTime) + ChatColor.GOLD); p.sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + msg); @@ -114,7 +50,7 @@ public boolean run(HaroTorch plugin, CommandSender sender, String[] args) { @Override public void run() { - killHighlighted(returnedIds, p); + killHighlighted(spawnedEntities, p); p.sendMessage(HaroTorch.getMessagePrefix() + ChatColor.GOLD + LangHandler.activeLang.getLangMessages().get("endingHighlight")); } }.runTaskLater(plugin, plugin.getConfigManifest().torchHighlightTime * 20); @@ -129,175 +65,61 @@ public void run() { * @param locations The Locations * @return Returns a List of Entity IDs */ - public List spawnHighlight(Player player, List locations) { - - List result = new ArrayList<>(); - + public Entity[] spawnHighlight(Player player, Location[] locations) { + Entity[] spawnedEntities = new Entity[locations.length]; + try { - Object entityPlayerObject = ReflectionUtil.invokeMethod(craftPlayerClass, player, "getHandle"); - Object playerConnectionObject; - if(ReflectionUtil.isUseNewSpigotPackaging()) { - playerConnectionObject = ReflectionUtil.getObject(entityPlayerObject, "b"); - } else { - playerConnectionObject = ReflectionUtil.getObject(entityPlayerObject, "playerConnection"); - } - - for(Location loc : locations) { - Object nmsWorld = ReflectionUtil.invokeMethod(craftWorldClass, loc.getWorld(), "getHandle"); - Object entityMagmaCube = ReflectionUtil.invokeConstructor(entityMagmaCubeClass, new Class[] { entityTypesClass, worldClass }, new Object[] { entityMagmaCubeField, nmsWorld }); + CraftPlayer craftPlayer = CraftPlayer.getInstance(player); + PlayerConnection playerConnection = craftPlayer.getPlayerConnection(); + + for (int i = 0; i < locations.length; i++) { + Location location = locations[i]; + + CraftWorld craftWorld = CraftWorld.getInstance(location.getWorld()); - if(ReflectionUtil.getMajorVersion() >= 18) { - // setInvisible - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "j", new Class[] { boolean.class }, new Object[] { false }); - //entityLivingClass.getField("collides").set(entityMagmaCube, false); - // setSharedFlag; Glowing - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "b", new Class[] { int.class, boolean.class }, new Object[] { 6, true }); - // setSharedFlag; Invisibility - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "b", new Class[] { int.class, boolean.class }, new Object[] { 5, true }); - // setSize - ReflectionUtil.invokeMethod(entitySlimeClass, entityMagmaCube, "a", new Class[] { int.class, boolean.class }, new Object[] { 2, true }); - // setPosRaw - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "o", - new Class[] { double.class, double.class, double.class }, - new Object[] { loc.getBlockX() + 0.5D, loc.getBlockY(), loc.getBlockZ() + 0.5D }); - // setYRot - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "o", - new Class[] { float.class }, - new Object[] { 0f }); - // setXRot - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "p", - new Class[] { float.class }, - new Object[] { 0f }); - } else { - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "setInvisible", new Class[] { boolean.class }, new Object[] { false }); - entityLivingClass.getField("collides").set(entityMagmaCube, false); - //Glowing - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "setFlag", new Class[] { int.class, boolean.class }, new Object[] { 6, true }); - //Invisibility - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "setFlag", new Class[] { int.class, boolean.class }, new Object[] { 5, true }); - //Size - ReflectionUtil.invokeMethod(entitySlimeClass, entityMagmaCube, "setSize", new Class[] { int.class, boolean.class }, new Object[] { 2, true }); - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "setLocation", - new Class[] { double.class, double.class, double.class, float.class, float.class}, - new Object[] { loc.getBlockX() + 0.5D, loc.getBlockY(), loc.getBlockZ() + 0.5D, 0f, 0f}); - } - - Object spawnEntityLivingPacket = ReflectionUtil.invokeConstructor(packetPlayOutEntityLivingClass, new Class[] { entityLivingClass }, new Object[] { entityMagmaCube }); - Object entityMetadataPacket = ReflectionUtil.invokeConstructor(packetPlayOutEntityMetadataClass, - new Class[] { int.class, dataWatcherClass, boolean.class}, - new Object[] { - ReflectionUtil.getMajorVersion() >= 18 ? - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "ae") - : ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "getId"), - ReflectionUtil.getMajorVersion() >= 18 ? - ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "ai") - : ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "getDataWatcher"), - false - }); + EntityMagmaCube magmaCube = EntityMagmaCube.getInstance(craftWorld); + magmaCube.setInvisible(true); + magmaCube.setGlowing(true); + magmaCube.setSize(2); + magmaCube.setLocation( + location.getBlockX() + 0.5d, + location.getBlockY(), + location.getBlockZ() + 0.5d, + 0f, + 0f + ); - if(ReflectionUtil.getMajorVersion() >= 18) { - ReflectionUtil.invokeMethod(playerConnectionObject, "a", - new Class[] { packetPlayOutEntityLivingInterfaceClass }, - new Object[] { spawnEntityLivingPacket }); - ReflectionUtil.invokeMethod(playerConnectionObject, "a", - new Class[] { packetPlayOutEntityMetadataInterfaceClass }, - new Object[] { entityMetadataPacket }); - result.add((Integer) ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "ae")); - } else { - ReflectionUtil.invokeMethod(playerConnectionObject, "sendPacket", - new Class[] { packetPlayOutEntityLivingInterfaceClass }, - new Object[] { spawnEntityLivingPacket }); - ReflectionUtil.invokeMethod(playerConnectionObject, "sendPacket", - new Class[] { packetPlayOutEntityMetadataInterfaceClass }, - new Object[] { entityMetadataPacket }); - result.add((Integer) ReflectionUtil.invokeMethod(entityClass, entityMagmaCube, "getId")); - } + EntitySpawnPacket entitySpawnPacket = EntitySpawnPacket.getInstance(magmaCube); + entitySpawnPacket.send(playerConnection); + + EntityMetadataPacket entityMetadataPacket = EntityMetadataPacket.getInstance(magmaCube); + entityMetadataPacket.send(playerConnection); + + spawnedEntities[i] = magmaCube; } - } catch(Exception e) { + } catch (ReflectException e) { e.printStackTrace(); } - - return result; + + return spawnedEntities; } /** * Kill the spawned Magma cubes to end highlighting - * @param ids The Entity IDs of the Entities to remove + * @param entities The entities to destroy * @param player The Player for which the Entities are showing */ - public void killHighlighted(List ids, Player player) { + public void killHighlighted(Entity[] entities, Player player) { try { - Object entityPlayerObject = ReflectionUtil.invokeMethod(craftPlayerClass, player, "getHandle"); - Object playerConnectionObject; - if(ReflectionUtil.isUseNewSpigotPackaging()) { - playerConnectionObject = ReflectionUtil.getObject(entityPlayerObject, "b"); - } else { - playerConnectionObject = ReflectionUtil.getObject(entityPlayerObject, "playerConnection"); - } - - if(ReflectionUtil.isUseNewSpigotPackaging()) { - String[] vParts = ReflectionUtil.SERVER_VERSION.split(Pattern.quote("_")); - int minor = Integer.valueOf(vParts[1]); - int patch = Integer.valueOf(vParts[2].replace("R", "")); - - // In 1.17.0 (1_17_R0) the constructor only took a single integer - // As of 1.17.1 (1_17_R1) it takes an int[] - if(patch == 0 && minor == 17) { - for(int id : ids) { - Object destroyEntityPacket = ReflectionUtil.invokeConstructor(packetPlayOutEntityDestroyClass, new Class[] { int.class }, new Object[] { id }); - - if(ReflectionUtil.getMajorVersion() >= 18) { - ReflectionUtil.invokeMethod(playerConnectionObject, "a", - new Class[] { packetPlayOutEntityDestroyInterfaceClass }, - new Object[] { destroyEntityPacket }); - } else { - ReflectionUtil.invokeMethod(playerConnectionObject, "sendPacket", - new Class[] { packetPlayOutEntityDestroyInterfaceClass }, - new Object[] { destroyEntityPacket }); - } - } - } else { - Object destroyEntityPacket = ReflectionUtil.invokeConstructor(packetPlayOutEntityDestroyClass, new Class[] { int[].class }, new Object[] { toIntArray(ids) }); + CraftPlayer craftPlayer = CraftPlayer.getInstance(player); + PlayerConnection playerConnection = craftPlayer.getPlayerConnection(); - if(ReflectionUtil.getMajorVersion() >= 18) { - ReflectionUtil.invokeMethod(playerConnectionObject, "a", - new Class[] { packetPlayOutEntityDestroyInterfaceClass }, - new Object[] { destroyEntityPacket }); - } else { - ReflectionUtil.invokeMethod(playerConnectionObject, "sendPacket", - new Class[] { packetPlayOutEntityDestroyInterfaceClass }, - new Object[] { destroyEntityPacket }); - } - } - } else { - Object destroyEntityPacket = ReflectionUtil.invokeConstructor(packetPlayOutEntityDestroyClass, new Class[] { int[].class }, new Object[] { toIntArray(ids) }); - - if (ReflectionUtil.getMajorVersion() >= 18) { - ReflectionUtil.invokeMethod(playerConnectionObject, "a", - new Class[]{packetPlayOutEntityDestroyInterfaceClass}, - new Object[]{destroyEntityPacket}); - } else { - ReflectionUtil.invokeMethod(playerConnectionObject, "sendPacket", - new Class[]{packetPlayOutEntityDestroyInterfaceClass}, - new Object[]{destroyEntityPacket}); - } + for(Entity entity : entities) { + EntityDestroyPacket destroyPacket = EntityDestroyPacket.getInstance(new Entity[] { entity }); + destroyPacket.send(playerConnection); } - } catch(Exception e) { + } catch (ReflectException e) { e.printStackTrace(); } } - - /** - * Convert a List of Integers to an int[] - * @param a The List to convert - * @return The List as an int[] - */ - private int[] toIntArray(List a) { - int[] b = new int[a.size()]; - for(int i = 0; i < a.size(); i++) { - b[i] = a.get(i); - } - - return b; - } }