From 8b6a351ecff8a0ca0c7a0564bf6be3ca5afe2ae2 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Mon, 1 May 2023 20:47:00 -0700 Subject: [PATCH] attack cooldown bypass --- .../events/BlockActionListener.java | 13 ++-- .../events/ChatEventListener.java | 7 ++ .../events/CommandEventListener.java | 7 ++ .../events/EntityDamageListener.java | 43 ++++++++++-- .../events/EntityDeathListener.java | 9 ++- .../events/InteractionListener.java | 24 +++---- .../events/InventoryActionListener.java | 24 ++++--- .../ogredupealias/utils/Cooldown.java | 33 +++++++++ .../utils/FileValidationUtils.java | 21 ++++++ .../ogredupealias/utils/ManualMap.java | 41 +++++++++++ .../ogredupealias/utils/MathUtils.java | 21 ++++++ .../ogredupealias/utils/Randomizer.java | 68 +++++++++++++++++++ .../itzispyder/ogredupealias/utils/Text.java | 8 ++- 13 files changed, 280 insertions(+), 39 deletions(-) create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/Cooldown.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/FileValidationUtils.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/ManualMap.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/MathUtils.java create mode 100644 src/main/java/io/github/itzispyder/ogredupealias/utils/Randomizer.java diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/BlockActionListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/BlockActionListener.java index 8c1f6ec..d85e665 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/BlockActionListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/BlockActionListener.java @@ -1,7 +1,5 @@ package io.github.itzispyder.ogredupealias.events; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; @@ -11,15 +9,16 @@ public class BlockActionListener implements Listener { @EventHandler public void onPlace(BlockPlaceEvent e) { - final Player p = e.getPlayer(); - final Block b = e.getBlockPlaced(); - final Block f = e.getBlockAgainst(); + try { + } + catch (Exception ignore) {} } public void onBreak(BlockBreakEvent e) { - final Player p = e.getPlayer(); - final Block b = e.getBlock(); + try { + } + catch (Exception ignore) {} } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/ChatEventListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/ChatEventListener.java index 48e0249..9a6704e 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/ChatEventListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/ChatEventListener.java @@ -10,6 +10,13 @@ public class ChatEventListener implements Listener { @EventHandler public void onChat(AsyncPlayerChatEvent e) { + try { + this.handleChatConstraints(e); + } + catch (Exception ignore) {} + } + + private void handleChatConstraints(AsyncPlayerChatEvent e) { final Player p = e.getPlayer(); final String msg = e.getMessage(); final ChatConstraints cc = new ChatConstraints(p,msg); diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/CommandEventListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/CommandEventListener.java index c6aad83..c4fafdc 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/CommandEventListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/CommandEventListener.java @@ -32,6 +32,13 @@ public class CommandEventListener implements Listener { @EventHandler public void onPlayerCommand(PlayerCommandPreprocessEvent e) { + try { + this.handleChatCommands(e); + } + catch (Exception ignore) {} + } + + private void handleChatCommands(PlayerCommandPreprocessEvent e) { final String msg = e.getMessage(); final Player p = e.getPlayer(); final String[] words = msg.split(" "); diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java index 453d226..2403a78 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDamageListener.java @@ -9,31 +9,62 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.inventory.ItemStack; public class EntityDamageListener implements Listener { public static final RecipientList attackCooldownBypassers = new RecipientList(); + public static final int DEFAULT_NO_DAMAGE_TICKS = 9; + public static final int DEFAULT_MAX_NO_DAMAGE_TICKS = 10; + public static final int NO_DAMAGE_TICKS = 1; + public static final int MAX_NO_DAMAGE_TICKS = 2; @EventHandler public void onDamageByEntity(EntityDamageByEntityEvent e) { - this.processPenisSword(e); + try { + this.handleBurstMelee(e); + } + catch (Exception ignore) {} } - public void processPenisSword(EntityDamageByEntityEvent e) { + @EventHandler + public void onDamage(EntityDamageEvent e) { + try { + this.handleBurstMelee(e); + } + catch (Exception ignore) {} + } + + public void handleBurstMelee(EntityDamageEvent e) { + final Entity ent = e.getEntity(); + final EntityDamageEvent.DamageCause cause = e.getCause(); + + if (cause.name().contains("ENTITY")) return; + if (ent instanceof LivingEntity eLiving) { + eLiving.setNoDamageTicks(14); + eLiving.setMaximumNoDamageTicks(15); + } + } + + public void handleBurstMelee(EntityDamageByEntityEvent e) { final Entity victim = e.getEntity(); final Entity damager = e.getDamager(); + final double dist = victim.getLocation().distance(damager.getLocation()); if (damager instanceof Player pDamager && victim instanceof LivingEntity vLiving) { + if (dist > 3.5) return; + if (pDamager.isDead() || victim.isDead()) return; + final ItemStack item = pDamager.getInventory().getItemInMainHand(); if (ItemUtils.nbtMatches(item, ItemPresets.TROLL_SWORD) || attackCooldownBypassers.isRecipient(pDamager)) { - vLiving.setNoDamageTicks(1); - vLiving.setMaximumNoDamageTicks(2); + vLiving.setNoDamageTicks(NO_DAMAGE_TICKS); + vLiving.setMaximumNoDamageTicks(MAX_NO_DAMAGE_TICKS); } else { - vLiving.setNoDamageTicks(19); - vLiving.setMaximumNoDamageTicks(20); + vLiving.setNoDamageTicks(DEFAULT_NO_DAMAGE_TICKS); + vLiving.setMaximumNoDamageTicks(DEFAULT_MAX_NO_DAMAGE_TICKS); } } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDeathListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDeathListener.java index bc4c84a..480761d 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDeathListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/EntityDeathListener.java @@ -1,7 +1,6 @@ package io.github.itzispyder.ogredupealias.events; import io.github.itzispyder.ogredupealias.plugin.ItemPresets; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; @@ -15,9 +14,15 @@ public class EntityDeathListener implements Listener { @EventHandler public void onDeath(EntityDeathEvent e) { + try { + this.handleWardenDeath(e); + } + catch (Exception ignore) {} + } + + private void handleWardenDeath(EntityDeathEvent e) { final LivingEntity ent = e.getEntity(); final EntityType type = ent.getType(); - final Location loc = ent.getLocation(); if (type == EntityType.WARDEN) { e.getDrops().stream().filter(Objects::nonNull).filter(i -> i.getType() == Material.SCULK_CATALYST).forEach(i -> i.setItemMeta(ItemPresets.SCULK_CATALYST.getItemMeta())); diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/InteractionListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/InteractionListener.java index 499cfe2..aa788bb 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/InteractionListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/InteractionListener.java @@ -6,27 +6,25 @@ import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; public class InteractionListener implements Listener { @EventHandler public void onClick(PlayerInteractEvent e) { - final Player p = e.getPlayer(); - final Action a = e.getAction(); - try { - final Block b = e.getClickedBlock(); - final ItemStack item = e.getItem(); - - if (PlacedStructures.isCustomTable(b)) { - e.setCancelled(true); - p.openInventory(InventoryPresets.createCustomTable()); - return; - } + this.processTable(e); } catch (Exception ignore) {} } + + private void processTable(PlayerInteractEvent e) { + final Player p = e.getPlayer(); + final Block b = e.getClickedBlock(); + + if (PlacedStructures.isCustomTable(b)) { + e.setCancelled(true); + p.openInventory(InventoryPresets.createCustomTable()); + } + } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/events/InventoryActionListener.java b/src/main/java/io/github/itzispyder/ogredupealias/events/InventoryActionListener.java index 6c6aa4d..53e66ad 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/events/InventoryActionListener.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/events/InventoryActionListener.java @@ -12,24 +12,30 @@ public class InventoryActionListener implements Listener { @EventHandler public void onClick(InventoryClickEvent e) { - final Inventory inv = e.getClickedInventory(); - final String title = e.getView().getTitle(); - try { - if (inv == null) return; - - if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryAction(e); + this.handleForgeClick(e); } catch (Exception ignore) {} } @EventHandler public void onClose(InventoryCloseEvent e) { - final String title = e.getView().getTitle(); - try { - if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryClose(e); + this.handleForgeClose(e); } catch (Exception ignore) {} } + + private void handleForgeClick(InventoryClickEvent e) { + final Inventory inv = e.getClickedInventory(); + final String title = e.getView().getTitle(); + + if (inv == null) return; + if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryAction(e); + } + + private void handleForgeClose(InventoryCloseEvent e) { + final String title = e.getView().getTitle(); + if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryClose(e); + } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/Cooldown.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/Cooldown.java new file mode 100644 index 0000000..7a0deb2 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/Cooldown.java @@ -0,0 +1,33 @@ +package io.github.itzispyder.ogredupealias.utils; + +import java.util.HashMap; +import java.util.Map; + +public class Cooldown { + + private final Map timer; + + public Cooldown() { + this.timer = new HashMap<>(); + } + + private O getOrDefault(O value, O def) { + return value != null ? value : def; + } + + public long getCooldown(T obj) { + return Math.max(getOrDefault(timer.get(obj), 0L) - System.currentTimeMillis(), 0L); + } + + public boolean isOnCooldown(T obj) { + return getCooldown(obj) > 0L; + } + + public void setCooldown(T obj, long millis) { + timer.put(obj, System.currentTimeMillis() + millis); + } + + public void addCooldown(T obj, long millis) { + setCooldown(obj, getCooldown(obj) + millis); + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/FileValidationUtils.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/FileValidationUtils.java new file mode 100644 index 0000000..9afe982 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/FileValidationUtils.java @@ -0,0 +1,21 @@ +package io.github.itzispyder.ogredupealias.utils; + +import java.io.File; + +public final class FileValidationUtils { + + public static boolean validate(File file) { + try { + if (!file.getParentFile().exists()) + if (!file.getParentFile().mkdirs()) + return false; + if (!file.exists()) + if (!file.createNewFile()) + return false; + return true; + } + catch (Exception ex) { + return false; + } + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/ManualMap.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/ManualMap.java new file mode 100644 index 0000000..f796664 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/ManualMap.java @@ -0,0 +1,41 @@ +package io.github.itzispyder.ogredupealias.utils; + +import java.util.HashMap; +import java.util.Map; + +/** + * Make a map manually + * @param key + * @param value + */ +public class ManualMap { + + public Map map = new HashMap<>(); + + /** + * Manual map + * @param objects objects array + * @param list of objects + */ + public ManualMap (O... objects) { + if (objects.length % 2 != 0) + throw new IllegalArgumentException("objects amount must be even for each key to have a value pair!"); + for (int i = 0; i < objects.length; i += 2) { + try { + map.put((K) objects[i],(V) objects[i + 1]); + } + catch (Exception ex) { + map.clear(); + break; + } + } + } + + /** + * Returns the map + * @return map + */ + public Map getMap() { + return map; + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/MathUtils.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/MathUtils.java new file mode 100644 index 0000000..aebbeaa --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/MathUtils.java @@ -0,0 +1,21 @@ +package io.github.itzispyder.ogredupealias.utils; + +import java.util.*; + +public final class MathUtils { + + public static double avg(Integer... ints) { + final List list = Arrays.stream(ints).filter(Objects::nonNull).toList(); + return avg(list); + } + + public static double avg(List ints) { + double sum = 0.0; + for (Integer i : ints) sum += i; + return sum / ints.size(); + } + + public static double round(double value, int nthPlace) { + return Math.floor(value * nthPlace) / nthPlace; + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/Randomizer.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/Randomizer.java new file mode 100644 index 0000000..203d765 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/Randomizer.java @@ -0,0 +1,68 @@ +package io.github.itzispyder.ogredupealias.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * Randomize items from a list + * @param list of? + */ +public class Randomizer { + + private final List array; + + /** + * From array list + * @param array list + */ + public Randomizer(List array) { + this.array = array; + } + + /** + * From set + * @param array set + */ + public Randomizer(Set array) { + this.array = new ArrayList<>(array); + } + + /** + * From array + * @param array array + */ + public Randomizer(T[] array) { + this.array = List.of(array); + } + + /** + * Pick random from the array + * @return random of list of? + */ + public T pickRand() { + return array.get(rand(array.size()) - 1); + } + + /** + * Generates a random integer from 1 to (max) + * @param max max value + * @return random + */ + public static int rand(int max) { + if (max <= 0) throw new IllegalArgumentException("max cannot be less than 1!"); + return (int) Math.ceil(Math.random() * max); + } + + /** + * Generates a random integer from (min) to (max) + * @param min min value + * @param max max value + * @return random + */ + public static int rand(int min, int max) { + if (max <= 0 || min <= 0) throw new IllegalArgumentException("max or min cannot be less than 1!"); + if (max <= min) throw new IllegalArgumentException("max cannot be less than or equal to min!"); + return min + (int) Math.floor(Math.random() * (max - min + 1)); + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java index d039ef0..adab6f4 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java @@ -1,6 +1,6 @@ package io.github.itzispyder.ogredupealias.utils; -import static io.github.itzispyder.ogredupealias.OgreDupeAlias.prefix; +import io.github.itzispyder.ogredupealias.data.Config; public class Text { @@ -8,6 +8,10 @@ public class Text { return s; } + public static String ofAll(String s) { + return builder(s).prefix().color().build(); + } + /** * Replaces all & with ยง to color the text * @param s string @@ -32,7 +36,7 @@ public class Text { } public static String prefixed(String s) { - return prefix() + s; + return Config.Plugin.prefix() + s; } public static TextBuilder builder(String s) {