From 9ea5f1cf0e16cd4bfb6903fd86ac193163048621 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Sat, 8 Jul 2023 19:22:23 -0700 Subject: [PATCH] added laser pointer --- .../commands/commands/GiveCustomCommand.java | 6 +- .../fun/ogre/ogredupealias/data/Config.java | 4 ++ .../events/PlayerEventListener.java | 3 +- .../ogredupealias/plugin/ItemPresets.java | 7 ++ .../plugin/custom/items/CustomItem.java | 8 ++- .../plugin/custom/items/CustomItems.java | 6 +- .../custom/items/customitems/LazerItem.java | 69 +++++++++++++++++++ .../custom/items/customitems/TazerItem.java | 2 +- 8 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/LazerItem.java 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 35a2357..e8324cb 100644 --- a/src/main/java/fun/ogre/ogredupealias/commands/commands/GiveCustomCommand.java +++ b/src/main/java/fun/ogre/ogredupealias/commands/commands/GiveCustomCommand.java @@ -64,9 +64,11 @@ public class GiveCustomCommand implements TabExecutor { p.getInventory().addItem(ItemPresets.PICKLER); p.sendMessage(Text.ofAll("&bGiven you a Pickler")); } - case "Tazer" -> { + case "IMPROPER" -> { + p.getInventory().clear(); + p.updateInventory(); p.getInventory().addItem(ItemPresets.TAZER); - p.sendMessage(Text.ofAll("&bGiven you a Tazer")); + p.getInventory().addItem(ItemPresets.LAZER); } } } diff --git a/src/main/java/fun/ogre/ogredupealias/data/Config.java b/src/main/java/fun/ogre/ogredupealias/data/Config.java index 5c47cd1..1d7d123 100644 --- a/src/main/java/fun/ogre/ogredupealias/data/Config.java +++ b/src/main/java/fun/ogre/ogredupealias/data/Config.java @@ -37,6 +37,10 @@ public abstract class Config { return sections; } + public static T getOrDef(T val, T def) { + return val != null ? val : def; + } + public static class Plugin { private static final String path = "plugin."; public static String prefix() { diff --git a/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java b/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java index 42e4492..e0dda5b 100644 --- a/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java +++ b/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java @@ -1,10 +1,9 @@ package fun.ogre.ogredupealias.events; import fun.ogre.ogredupealias.data.Config; -import fun.ogre.ogredupealias.utils.ItemUtils; import fun.ogre.ogredupealias.plugin.ItemPresets; +import fun.ogre.ogredupealias.utils.ItemUtils; import org.bukkit.Material; -import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java b/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java index 4de1c50..c0e9c85 100644 --- a/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java +++ b/src/main/java/fun/ogre/ogredupealias/plugin/ItemPresets.java @@ -163,4 +163,11 @@ public abstract class ItemPresets { .customModelData(1111) .build(); + public static ItemStack LAZER = ItemBuilder.create() + .material(Material.REDSTONE_TORCH) + .name("Lazer Pointer") + .lore(Text.color("&7- A funny gadget!")) + .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 index d164345..be39d4e 100644 --- a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItem.java +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItem.java @@ -5,9 +5,11 @@ import org.bukkit.inventory.ItemStack; public abstract class CustomItem { private final ItemStack item; + private final String name; - public CustomItem(ItemStack item) { + public CustomItem(String name, ItemStack item) { this.item = item; + this.name = name; } public abstract CustomItemInteractionCallback getCallback(); @@ -24,4 +26,8 @@ public abstract class CustomItem { public ItemStack getItem() { return item; } + + public String getName() { + return name; + } } 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 index c694b4a..0133bc2 100644 --- a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItems.java +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/CustomItems.java @@ -1,5 +1,6 @@ package fun.ogre.ogredupealias.plugin.custom.items; +import fun.ogre.ogredupealias.plugin.custom.items.customitems.LazerItem; import fun.ogre.ogredupealias.plugin.custom.items.customitems.TazerItem; import fun.ogre.ogredupealias.utils.ItemUtils; import org.bukkit.event.EventHandler; @@ -13,9 +14,11 @@ import java.util.Map; public final class CustomItems implements Listener { private static final Map callbackList = new HashMap<>(); + private static final Map, String> namesList = new HashMap<>(); public static void init() { register(new TazerItem()); + register(new LazerItem()); } public static ItemStack register(ItemStack item, CustomItemInteractionCallback interactionCallback) { @@ -25,7 +28,8 @@ public final class CustomItems implements Listener { public static ItemStack register(CustomItem item) { item.registerThis(); - return register(item.getItem(), item.getCallback()); + namesList.put(item.getClass(), item.getName()); + return item.getItem(); } public static void onInteract(ItemStack item, PlayerInteractEvent event) { diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/LazerItem.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/LazerItem.java new file mode 100644 index 0000000..bc2b915 --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/items/customitems/LazerItem.java @@ -0,0 +1,69 @@ +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.RaycastUtils; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.block.Block; +import org.bukkit.util.Vector; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +public class LazerItem extends CustomItem { + + public LazerItem() { + super("lazer", ItemPresets.LAZER); + } + + @Override + public CustomItemInteractionCallback getCallback() { + return (player, item, event) -> { + event.setCancelled(true); + + Location loc = player.getLocation().add(0, 1.4, 0); + Vector dir = player.getLocation().getDirection().normalize(); + AtomicInteger attempts = new AtomicInteger(0); + + castInDirection(loc, dir, true, 10, attempts); + }; + } + + private void castInDirection(Location loc, Vector dir, boolean again, int maxAttempts, AtomicInteger attempts) { + AtomicBoolean hitSmth = new AtomicBoolean(false); + Location hit = RaycastUtils.raycast(loc, dir, 64, 0.1, point -> { + Particle.DustOptions dust = new Particle.DustOptions(Color.fromRGB(255, 0, 0), 0.5F); + point.getWorld().spawnParticle(Particle.REDSTONE, point, 1, 0, 0, 0, 0, dust); + hitSmth.set(!point.getBlock().isPassable() && loc.distance(point) > 1.0); + return hitSmth.get(); + }); + Vector incoming = dir.clone().multiply(-1).normalize(); + Vector bounce = getContactingFace(hit, hit.getBlock()); + Vector reflect = bounce.multiply(-1).subtract(incoming).normalize(); + + if (again && maxAttempts > attempts.getAndIncrement()) { + castInDirection(hit, reflect, hitSmth.get(), maxAttempts, attempts); + } + } + + private Vector getContactingFace(Location pointOfContact, Block block) { + Vector center = block.getLocation().add(0.5, 0.5, 0.5).toVector(); + Vector incoming = center.subtract(pointOfContact.toVector()); + double x = incoming.dot(new Vector(1, 0, 0)); + double y = incoming.dot(new Vector(0, 1, 0)); + double z = incoming.dot(new Vector(0, 0, 1)); + double ax = Math.abs(x); + double ay = Math.abs(y); + double az = Math.abs(z); + + if (ax > ay && ax > az) + return new Vector(x, 0, 0).normalize(); + else if (ay > ax && ay > az) + return new Vector(0, y, 0).normalize(); + else + return new Vector(0, 0, z).normalize(); + } +} 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 index 357ab8d..c04cd78 100644 --- 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 @@ -20,7 +20,7 @@ import java.util.function.Predicate; public class TazerItem extends CustomItem { public TazerItem() { - super(ItemPresets.TAZER); + super("tazer", ItemPresets.TAZER); } @Override