From 314655ca7612d82c37dec4ab5db3c4e337971eb0 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Thu, 6 Jul 2023 13:38:30 -0700 Subject: [PATCH] added tazer --- .../fun/ogre/ogredupealias/OgreDupeAlias.java | 4 ++ .../commands/commands/GiveCustomCommand.java | 7 +- .../ogredupealias/plugin/ItemPresets.java | 10 +++ .../plugin/custom/items/CustomItem.java | 27 +++++++ .../items/CustomItemInteractionCallback.java | 11 +++ .../plugin/custom/items/CustomItems.java | 42 +++++++++++ .../custom/items/customitems/TazerItem.java | 70 +++++++++++++++++++ .../ogre/ogredupealias/utils/Randomizer.java | 6 +- .../fun/ogre/ogredupealias/utils/Text.java | 9 +++ 9 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItem.java create mode 100644 src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItemInteractionCallback.java create mode 100644 src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItems.java create mode 100644 src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/TazerItem.java diff --git a/src/main/java/fun/ogre/ogredupealias/OgreDupeAlias.java b/src/main/java/fun/ogre/ogredupealias/OgreDupeAlias.java index 73b6d01..a7c6658 100644 --- a/src/main/java/fun/ogre/ogredupealias/OgreDupeAlias.java +++ b/src/main/java/fun/ogre/ogredupealias/OgreDupeAlias.java @@ -4,6 +4,7 @@ import fun.ogre.ogredupealias.commands.commands.*; import fun.ogre.ogredupealias.data.Config; import fun.ogre.ogredupealias.events.*; import fun.ogre.ogredupealias.plugin.custom.forging.CraftingKeys; +import fun.ogre.ogredupealias.plugin.custom.items.CustomItems; import fun.ogre.ogredupealias.plugin.funitems.AdminUtility; import fun.ogre.ogredupealias.plugin.funitems.Pickler; import fun.ogre.ogredupealias.plugin.funitems.PotatoCannon; @@ -27,6 +28,7 @@ public final class OgreDupeAlias extends JavaPlugin { instance = this; this.init(); this.initConfig(); + CustomItems.init(); CraftingKeys.initRecipes(); } @@ -51,6 +53,8 @@ public final class OgreDupeAlias extends JavaPlugin { pm.registerEvents(new AdminUtility(), this); pm.registerEvents(new Pickler(), this); pm.registerEvents(new SPBEventListener(), this); + // event for custom items + pm.registerEvents(new CustomItems(), this); // Commands getCommand("forcefield").setExecutor(new ForceFieldCommand()); diff --git a/src/main/java/fun/ogre/ogredupealias/commands/commands/GiveCustomCommand.java b/src/main/java/fun/ogre/ogredupealias/commands/commands/GiveCustomCommand.java index 8163432..35a2357 100644 --- a/src/main/java/fun/ogre/ogredupealias/commands/commands/GiveCustomCommand.java +++ b/src/main/java/fun/ogre/ogredupealias/commands/commands/GiveCustomCommand.java @@ -64,6 +64,10 @@ public class GiveCustomCommand implements TabExecutor { p.getInventory().addItem(ItemPresets.PICKLER); p.sendMessage(Text.ofAll("&bGiven you a Pickler")); } + case "Tazer" -> { + p.getInventory().addItem(ItemPresets.TAZER); + p.sendMessage(Text.ofAll("&bGiven you a Tazer")); + } } } catch (Exception ex) { @@ -86,7 +90,8 @@ public class GiveCustomCommand implements TabExecutor { "VoidCharm", "Spleefer", "PotatoCannon", - "LaserPointer" + "LaserPointer", + "Tazer" }) .build(); } diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java b/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java index 652b2a9..4de1c50 100644 --- a/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java +++ b/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java @@ -153,4 +153,14 @@ public abstract class ItemPresets { .lore(Text.color("&2▪ &aRight-Click:&7 Pickle-ify Someone")) .customModelData(1111) .build(); + + public static ItemStack TAZER = ItemBuilder.create() + .material(Material.NETHERITE_AXE) + .name(Text.color("&7{&b&lTazer&7}")) + .unbreakable(true) + .enchant(Enchantment.LURE, 1) + .flag(ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE) + .customModelData(1111) + .build(); + } diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItem.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItem.java new file mode 100644 index 0000000..d164345 --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItem.java @@ -0,0 +1,27 @@ +package fun.ogre.ogredupealias.plugin.custom.items; + +import org.bukkit.inventory.ItemStack; + +public abstract class CustomItem { + + private final ItemStack item; + + public CustomItem(ItemStack item) { + this.item = item; + } + + public abstract CustomItemInteractionCallback getCallback(); + + public void registerThis() { + ItemStack item = getItem(); + CustomItemInteractionCallback callback = getCallback(); + + if (item != null && callback != null) { + CustomItems.register(item, callback); + } + } + + public ItemStack getItem() { + return item; + } +} diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItemInteractionCallback.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItemInteractionCallback.java new file mode 100644 index 0000000..aa46585 --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItemInteractionCallback.java @@ -0,0 +1,11 @@ +package fun.ogre.ogredupealias.plugin.custom.items; + +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +@FunctionalInterface +public interface CustomItemInteractionCallback { + + void handleInteraction(Player player, ItemStack item, PlayerInteractEvent event); +} diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItems.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItems.java new file mode 100644 index 0000000..c694b4a --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItems.java @@ -0,0 +1,42 @@ +package fun.ogre.ogredupealias.plugin.custom.items; + +import fun.ogre.ogredupealias.plugin.custom.items.customitems.TazerItem; +import fun.ogre.ogredupealias.utils.ItemUtils; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; +import java.util.Map; + +public final class CustomItems implements Listener { + + private static final Map callbackList = new HashMap<>(); + + public static void init() { + register(new TazerItem()); + } + + public static ItemStack register(ItemStack item, CustomItemInteractionCallback interactionCallback) { + callbackList.put(ItemUtils.nbtOf(item), interactionCallback); + return item; + } + + public static ItemStack register(CustomItem item) { + item.registerThis(); + return register(item.getItem(), item.getCallback()); + } + + public static void onInteract(ItemStack item, PlayerInteractEvent event) { + String nbt = ItemUtils.nbtOf(item); + if (callbackList.containsKey(nbt)) { + callbackList.get(nbt).handleInteraction(event.getPlayer(), item, event); + } + } + + @EventHandler + private void onPlayerInteraction(PlayerInteractEvent e) { + onInteract(e.getItem(), e); + } +} diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/TazerItem.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/TazerItem.java new file mode 100644 index 0000000..872b890 --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/TazerItem.java @@ -0,0 +1,70 @@ +package fun.ogre.ogredupealias.plugin.custom.items.customitems; + +import fun.ogre.ogredupealias.plugin.ItemPresets; +import fun.ogre.ogredupealias.plugin.custom.items.CustomItem; +import fun.ogre.ogredupealias.plugin.custom.items.CustomItemInteractionCallback; +import fun.ogre.ogredupealias.utils.Randomizer; +import fun.ogre.ogredupealias.utils.RaycastUtils; +import fun.ogre.ogredupealias.utils.SoundPlayer; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.entity.LivingEntity; +import org.bukkit.util.Vector; + +import java.util.function.Predicate; + +public class TazerItem extends CustomItem { + + public TazerItem() { + super(ItemPresets.TAZER); + } + + @Override + public CustomItemInteractionCallback getCallback() { + return (player, item, event) -> { + if (!event.getAction().name().contains("RIGHT_CLICK")) return; + + Location loc = player.getEyeLocation(); + Vector vec = player.getLocation().getDirection().normalize(); + + Location target = RaycastUtils.raycast(loc, vec, 64.0, point -> { + boolean hitBlock = !point.getBlock().isPassable(); + boolean hitEntity = point.getWorld().getNearbyEntities(point, 2, 2, 2).stream().anyMatch(e -> e != player && !e.isDead() && e instanceof LivingEntity); + return hitBlock || hitEntity; + }); + int maxSections = 10; + int delta = 5; + double maxDist = loc.distance(target); + double sectionDist = maxDist / maxSections; + Location prevLoc = loc.clone(); + + Predicate hitCondition = (point) -> { + Particle.DustOptions dust = new Particle.DustOptions(Color.AQUA, 0.7F); + point.getWorld().spawnParticle(Particle.REDSTONE, point, 1, 0, 0, 0, 0, dust); + point.getWorld().getNearbyEntities(point, 2, 2, 2).stream() + .filter(e -> e != player && !e.isDead() && e instanceof LivingEntity) + .forEach(entity -> { + ((LivingEntity) entity).damage(6, player); + entity.setFireTicks(100); + SoundPlayer zap = new SoundPlayer(entity.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1, 1.5F); + zap.playWithin(20); + }); + return false; + }; + + for (int i = 0; i < maxSections - 1; i ++) { + prevLoc = RaycastUtils.raycast(prevLoc, vec, sectionDist, 0.2, hitCondition); + vec = randomizeVector(vec, delta); + SoundPlayer zap = new SoundPlayer(prevLoc, Sound.ENTITY_BEE_HURT, 1, 10); + zap.playWithin(20); + } + RaycastUtils.raycast(prevLoc, target, 0.2, hitCondition); + }; + } + + private Vector randomizeVector(Vector vec, int delta) { + return vec.clone().add(new Vector(Randomizer.rand(-delta, delta), Randomizer.rand(-delta, delta), Randomizer.rand(-delta, delta))).normalize(); + } +} diff --git a/src/main/java/fun/ogre/ogredupealias/utils/Randomizer.java b/src/main/java/fun/ogre/ogredupealias/utils/Randomizer.java index 74817d6..8903f9a 100644 --- a/src/main/java/fun/ogre/ogredupealias/utils/Randomizer.java +++ b/src/main/java/fun/ogre/ogredupealias/utils/Randomizer.java @@ -50,8 +50,7 @@ public class Randomizer { * @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); + return (int)(Math.random() * max); } /** @@ -61,8 +60,7 @@ public class Randomizer { * @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)); + return min + (int)(Math.random() * (max - min + 1)); } } diff --git a/src/main/java/fun/ogre/ogredupealias/utils/Text.java b/src/main/java/fun/ogre/ogredupealias/utils/Text.java index 9bf3ce4..faa2d71 100644 --- a/src/main/java/fun/ogre/ogredupealias/utils/Text.java +++ b/src/main/java/fun/ogre/ogredupealias/utils/Text.java @@ -47,6 +47,15 @@ public class Text { return builder(""); } + public static String removeColors(String msg) { + String s = msg; + while (s.length() >= 2 && s.contains("§")) { + int index = s.indexOf("§"); + s = s.replaceAll(s.substring(index, index + 2), ""); + } + return s; + } + public static class TextBuilder { private String s;