From 09406d1289d249b894a5d791732d1c2cbca11c98 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Sat, 29 Apr 2023 18:34:26 -0700 Subject: [PATCH] attack cooldown bypass --- .../ogredupealias/OgreDupeAlias.java | 3 + .../commands/AttackCooldownCommand.java | 42 +++ .../events/EntityDamageListener.java | 44 +++ .../ogredupealias/utils/SoundPlayer.java | 256 ++++++++++++++++++ .../ogredupealias/utils/SoundUtils.java | 30 ++ src/main/resources/plugin.yml | 10 +- 6 files changed, 384 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/commands/commands/AttackCooldownCommand.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/SoundPlayer.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/SoundUtils.java diff --git a/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java b/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java index 196cc6b..9871d01 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java @@ -41,6 +41,7 @@ public final class OgreDupeAlias extends JavaPlugin { pm.registerEvents(new EntityDeathListener(),this); pm.registerEvents(new InteractionListener(),this); pm.registerEvents(new InventoryActionListener(),this); + pm.registerEvents(new EntityDamageListener(),this); // Commands getCommand("config").setExecutor(new ConfigCommand()); @@ -59,6 +60,8 @@ public final class OgreDupeAlias extends JavaPlugin { getCommand("irepair").setTabCompleter(new IRepairCommand()); getCommand("message").setExecutor(new MessageCommand()); getCommand("message").setTabCompleter(new MessageCommand()); + getCommand("attackcooldown").setExecutor(new AttackCooldownCommand()); + getCommand("attackcooldown").setTabCompleter(new AttackCooldownCommand()); } public void initConfig() { diff --git a/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/AttackCooldownCommand.java b/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/AttackCooldownCommand.java new file mode 100644 index 0000000..6e80ce5 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/AttackCooldownCommand.java @@ -0,0 +1,42 @@ +package io.github.itzispyder.ogredupealias.commands.commands; + +import io.github.itzispyder.ogredupealias.commands.CmdExHandler; +import io.github.itzispyder.ogredupealias.events.EntityDamageListener; +import io.github.itzispyder.ogredupealias.utils.Text; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class AttackCooldownCommand implements TabExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + try { + Player p = (Player) sender; + boolean isRecipient = EntityDamageListener.attackCooldownBypassers.isRecipient(p); + if (isRecipient) EntityDamageListener.attackCooldownBypassers.removeRecipient(p); + else EntityDamageListener.attackCooldownBypassers.addRecipient(p); + isRecipient = EntityDamageListener.attackCooldownBypassers.isRecipient(p); + + sender.sendMessage(Text.builder() + .message("&7[&bAttackCooldown&7] &8>> &3You are " + (isRecipient ? "&anow" : "&cno longer") + " &3a an attack cooldown bypasser!") + .prefix() + .color() + .build()); + } + catch (Exception ex) { + CmdExHandler handler = new CmdExHandler(ex,command); + sender.sendMessage(handler.getHelp()); + } + return true; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + return new ArrayList<>(); + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java new file mode 100644 index 0000000..86416fd --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java @@ -0,0 +1,44 @@ +package io.github.itzispyder.ogredupealias.events; + +import io.github.itzispyder.ogredupealias.plugin.ItemPresets; +import io.github.itzispyder.ogredupealias.plugin.RecipientList; +import io.github.itzispyder.ogredupealias.utils.ItemUtils; +import io.github.itzispyder.ogredupealias.utils.SoundPlayer; +import org.bukkit.Sound; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.inventory.ItemStack; + +public class EntityDamageListener implements Listener { + + public static final RecipientList attackCooldownBypassers = new RecipientList(); + + @EventHandler + public void onDamageByEntity(EntityDamageByEntityEvent e) { + this.processPenisSword(e); + } + + public void processPenisSword(EntityDamageByEntityEvent e) { + final Entity victim = e.getEntity(); + final Entity damager = e.getDamager(); + + if (damager instanceof Player pDamager && victim instanceof LivingEntity vLiving) { + final ItemStack item = pDamager.getInventory().getItemInMainHand(); + final SoundPlayer sticky = new SoundPlayer(vLiving.getLocation(), Sound.BLOCK_SLIME_BLOCK_BREAK, 1, 10); + + if (ItemUtils.nbtMatches(item, ItemPresets.TROLL_SWORD) || attackCooldownBypassers.isRecipient(pDamager)) { + vLiving.setNoDamageTicks(1); + vLiving.setMaximumNoDamageTicks(2); + sticky.playWithinAt(100); + } + else { + vLiving.setNoDamageTicks(19); + vLiving.setMaximumNoDamageTicks(20); + } + } + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/SoundPlayer.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/SoundPlayer.java new file mode 100644 index 0000000..239d33b --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/SoundPlayer.java @@ -0,0 +1,256 @@ +package io.github.itzispyder.ogredupealias.utils; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import static io.github.itzispyder.ogredupealias.OgreDupeAlias.instance; + +public class SoundPlayer { + + private Location location; + private Sound sound; + private float volume; + private float pitch; + + /** + * Constructs a new sound, this aims to add more methods to + * the Bukkit APIs Sound class, as they don't have many + * methods to use. + * + * @param location Location + * @param sound Sound + * @param volume float + * @param pitch float + */ + public SoundPlayer(Location location, Sound sound, float volume, float pitch) { + this.location = location; + this.sound = sound; + this.pitch = pitch; + this.volume = volume; + } + + + /** + * Plays a sound to a player but at the store location + * + * @param player Player + */ + public void play(Player player) { + player.playSound(this.location,this.sound,this.volume,this.pitch); + } + + /** + * Plays a sound to a player but at the player's location + * + * @param player Player + */ + public void playAt(Player player) { + player.playSound(player.getLocation(),this.sound,this.volume,this.pitch); + } + + /** + * Plays the sound to all players within a distance, but at the stored location. + * + * @param distance double + */ + public void playWithin(double distance) { + for (Player p : Bukkit.getOnlinePlayers()) { + if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) { + p.playSound(this.location,this.sound,this.volume,this.pitch); + } + } + } + + /** + * Plays the sound to all players within a distance, but at the players' location. + * + * @param distance double + */ + public void playWithinAt(double distance) { + for (Player p : Bukkit.getOnlinePlayers()) { + if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) { + p.playSound(p.getLocation(),this.sound,this.volume,this.pitch); + } + } + } + + + /** + * Plays the sound to all players on the server, but at the stored location. + */ + public void playAll() { + for (Player p : Bukkit.getOnlinePlayers()) p.playSound(this.location,this.sound,this.volume,this.pitch); + } + + /** + * Plays the sound to all players on the server, but at the players' location. + */ + public void playAllAt() { + for (Player p : Bukkit.getOnlinePlayers()) p.playSound(p.getLocation(),this.sound,this.volume,this.pitch); + } + + /** + * Repeats a sound to a player, but at the stored location. + * + * @param player Player + * @param times int + * @param tickDelay int + */ + public void repeat(Player player, int times, int tickDelay) { + new BukkitRunnable() { + int i = 0; + @Override + public void run() { + if (i < times) { + play(player); + i ++; + } else { + this.cancel(); + } + } + }.runTaskTimer(instance,0,tickDelay); + } + + /** + * Repeats a sound to a player, but at the player's location. + * + * @param player Player + * @param times int + * @param tickDelay int + */ + public void repeatAt(Player player, int times, int tickDelay) { + new BukkitRunnable() { + int i = 0; + @Override + public void run() { + if (i < times) { + playAt(player); + i ++; + } else { + this.cancel(); + } + } + }.runTaskTimer(instance,0,tickDelay); + } + + /** + * Repeats a sound to all players on the server, but at the stored location. + * + * @param times int + * @param tickDelay int + */ + public void repeatAll(int times, int tickDelay) { + new BukkitRunnable() { + int i = 0; + @Override + public void run() { + if (i < times) { + playAll(); + i ++; + } else { + this.cancel(); + } + } + }.runTaskTimer(instance,0,tickDelay); + } + + /** + * Repeats a sound to all players on the server, but at the players' location. + * + * @param times int + * @param tickDelay int + */ + public void repeatAllAt(int times, int tickDelay) { + new BukkitRunnable() { + int i = 0; + @Override + public void run() { + if (i < times) { + playAllAt(); + i ++; + } else { + this.cancel(); + } + } + }.runTaskTimer(instance,0,tickDelay); + } + + /** + * Repeats a sound to all players within a radius, but at the stored location. + * + * @param radius double + * @param times int + * @param tickDelay int + */ + public void repeatAll(double radius,int times, int tickDelay) { + new BukkitRunnable() { + int i = 0; + @Override + public void run() { + if (i < times) { + playWithin(radius); + i ++; + } else { + this.cancel(); + } + } + }.runTaskTimer(instance,0,tickDelay); + } + + /** + * Repeats a sound to all players within a radius, but at the players' location. + * + * @param distance double + * @param times int + * @param tickDelay int + */ + public void repeatAllAt(double distance, int times, int tickDelay) { + new BukkitRunnable() { + int i = 0; + @Override + public void run() { + if (i < times) { + playWithinAt(distance); + i ++; + } else { + this.cancel(); + } + } + }.runTaskTimer(instance,0,tickDelay); + } + + public Sound getSound() { + return sound; + } + + public float getPitch() { + return pitch; + } + + public float getVolume() { + return volume; + } + + public Location getLocation() { + return location; + } + + public void setPitch(float pitch) { + this.pitch = pitch; + } + + public void setVolume(float volume) { + this.volume = volume; + } + + public void setSound(Sound sound) { + this.sound = sound; + } + + public void setLocation(Location location) { + this.location = location; + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/SoundUtils.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/SoundUtils.java new file mode 100644 index 0000000..90bb09a --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/SoundUtils.java @@ -0,0 +1,30 @@ +package io.github.itzispyder.ipearlpvp.util; + +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; + +public abstract class SoundUtils { + + private static final Map cooldown = new HashMap<>(); + + /** + * For the parameters, use the format: "sound-name/sound-volume/sound-pitch" + * @param listener the player listening for the sounds + * @param p simplified parameters + */ + public static void play(Player listener, String p) { + if (cooldown.containsKey(listener.getName()) && cooldown.get(listener.getName()) > System.currentTimeMillis()) return; + cooldown.put(listener.getName(),System.currentTimeMillis() + 50); + try { + String[] params = p.split("/"); + Sound sound = Sound.valueOf(params[0].toUpperCase()); + float volume = Float.parseFloat(params[1]); + float pitch = Float.parseFloat(params[2]); + listener.playSound(listener.getLocation(),sound,volume,pitch); + } + catch (Exception ignore) {} + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4b00e5c..f69ed67 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -29,6 +29,9 @@ permissions: oda.commands.irepair: description: Access to irepair. default: op + oda.commands.attackcooldown: + description: Access to attackcooldown. + default: op oda.chat.bypass: description: Bypass chat restrictions default: op @@ -104,4 +107,9 @@ commands: - tell - whisper - w - - message \ No newline at end of file + - message + attackcooldown: + description: Attack cooldown bypass for entities. + usage: /attackcooldown + aliases: + - atkcool \ No newline at end of file