|
| 1 | +package gg.nextforge.textblockitemdisplay; |
| 2 | + |
| 3 | +import gg.nextforge.NextCorePlugin; |
| 4 | +import gg.nextforge.command.CommandContext; |
| 5 | +import gg.nextforge.command.CommandManager; |
| 6 | +import gg.nextforge.text.TextManager; |
| 7 | +import org.bukkit.Location; |
| 8 | +import org.bukkit.Material; |
| 9 | +import org.bukkit.entity.Player; |
| 10 | +import org.bukkit.inventory.ItemStack; |
| 11 | + |
| 12 | +/** |
| 13 | + * Command handling for hologram operations. |
| 14 | + */ |
| 15 | +public class HologramCommand { |
| 16 | + private final NextCorePlugin plugin; |
| 17 | + private final HologramManager manager; |
| 18 | + private final TextManager text; |
| 19 | + |
| 20 | + public HologramCommand(NextCorePlugin plugin, HologramManager manager) { |
| 21 | + this.plugin = plugin; |
| 22 | + this.manager = manager; |
| 23 | + this.text = plugin.getTextManager(); |
| 24 | + register(); |
| 25 | + } |
| 26 | + |
| 27 | + private void register() { |
| 28 | + CommandManager cm = plugin.getCommandManager(); |
| 29 | + cm.command("hologram") |
| 30 | + .permission("nextforge.command.hologram") |
| 31 | + .description("Manage holograms") |
| 32 | + .executor(this::handleHelp) |
| 33 | + .subcommand("help", this::handleHelp) |
| 34 | + .subcommand("list", this::handleList) |
| 35 | + .subcommand("nearby", this::handleNearby) |
| 36 | + .subcommand("create", this::handleCreate) |
| 37 | + .subcommand("remove", this::handleRemove) |
| 38 | + .subcommand("copy", this::handleCopy) |
| 39 | + .subcommand("info", this::handleInfo) |
| 40 | + .register(); |
| 41 | + } |
| 42 | + |
| 43 | + private void handleHelp(CommandContext ctx) { |
| 44 | + text.send(ctx.sender(), "<gray>/hologram list</gray> - list holograms"); |
| 45 | + text.send(ctx.sender(), "<gray>/hologram create (type) (name)</gray>"); |
| 46 | + } |
| 47 | + |
| 48 | + private void handleList(CommandContext ctx) { |
| 49 | + for (Hologram h : manager.getHolograms()) { |
| 50 | + text.send(ctx.sender(), " - " + h.getName()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void handleNearby(CommandContext ctx) { |
| 55 | + if (!(ctx.sender() instanceof Player player)) { |
| 56 | + text.send(ctx.sender(), "Only players"); |
| 57 | + return; |
| 58 | + } |
| 59 | + double range = 10; |
| 60 | + if (ctx.args().length > 0) { |
| 61 | + try { |
| 62 | + range = Double.parseDouble(ctx.args()[0]); |
| 63 | + } catch (NumberFormatException e) { |
| 64 | + text.send(ctx.sender(), "Invalid range. Please provide a valid number."); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + Location loc = player.getLocation(); |
| 69 | + for (Hologram h : manager.getHolograms()) { |
| 70 | + if (h.getLocation().getWorld().equals(loc.getWorld()) && |
| 71 | + h.getLocation().distance(loc) <= range) { |
| 72 | + text.send(player, " - " + h.getName()); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void handleCreate(CommandContext ctx) { |
| 78 | + if (!(ctx.sender() instanceof Player player)) { |
| 79 | + text.send(ctx.sender(), "Only players"); |
| 80 | + return; |
| 81 | + } |
| 82 | + if (ctx.args().length < 2) { |
| 83 | + text.send(ctx.sender(), "Usage: /hologram create (type) (name)"); |
| 84 | + return; |
| 85 | + } |
| 86 | + String type = ctx.args()[0].toLowerCase(); |
| 87 | + String name = ctx.args()[1]; |
| 88 | + Location loc = player.getLocation(); |
| 89 | + switch (type) { |
| 90 | + case "text" -> manager.createTextHologram(name, loc); |
| 91 | + case "item" -> manager.createItemHologram(name, loc, player.getInventory().getItemInMainHand()); |
| 92 | + case "block" -> manager.createBlockHologram(name, loc, Material.STONE); |
| 93 | + default -> { |
| 94 | + text.send(ctx.sender(), "Unknown type"); |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + text.send(ctx.sender(), "Created hologram " + name); |
| 99 | + } |
| 100 | + |
| 101 | + private void handleRemove(CommandContext ctx) { |
| 102 | + if (ctx.args().length < 1) { |
| 103 | + text.send(ctx.sender(), "Usage: /hologram remove (name)"); |
| 104 | + return; |
| 105 | + } |
| 106 | + manager.remove(ctx.args()[0]); |
| 107 | + text.send(ctx.sender(), "Removed hologram"); |
| 108 | + } |
| 109 | + |
| 110 | + private void handleCopy(CommandContext ctx) { |
| 111 | + if (ctx.args().length < 2) { |
| 112 | + text.send(ctx.sender(), "Usage: /hologram copy (src) (dest)"); |
| 113 | + return; |
| 114 | + } |
| 115 | + Hologram src = manager.get(ctx.args()[0]); |
| 116 | + if (src == null) { |
| 117 | + text.send(ctx.sender(), "Not found"); |
| 118 | + return; |
| 119 | + } |
| 120 | + Location loc = src.getLocation(); |
| 121 | + if (src instanceof TextHologram th) { |
| 122 | + TextHologram nh = manager.createTextHologram(ctx.args()[1], loc); |
| 123 | + th.getLines().forEach(nh::addLine); |
| 124 | + } else if (src instanceof ItemHologram ih) { |
| 125 | + manager.createItemHologram(ctx.args()[1], loc, ih.getItem()); |
| 126 | + } else if (src instanceof BlockHologram bh) { |
| 127 | + manager.createBlockHologram(ctx.args()[1], loc, bh.getBlockType()); |
| 128 | + } |
| 129 | + text.send(ctx.sender(), "Copied hologram"); |
| 130 | + } |
| 131 | + |
| 132 | + private void handleInfo(CommandContext ctx) { |
| 133 | + if (ctx.args().length < 1) { |
| 134 | + text.send(ctx.sender(), "Usage: /hologram info (name)"); |
| 135 | + return; |
| 136 | + } |
| 137 | + Hologram h = manager.get(ctx.args()[0]); |
| 138 | + if (h == null) { |
| 139 | + text.send(ctx.sender(), "Not found"); |
| 140 | + return; |
| 141 | + } |
| 142 | + text.send(ctx.sender(), "Name: " + h.getName()); |
| 143 | + text.send(ctx.sender(), "Location: " + h.getLocation().toVector()); |
| 144 | + if (h instanceof TextHologram th) { |
| 145 | + text.send(ctx.sender(), "Lines: " + th.getLines().size()); |
| 146 | + } |
| 147 | + if (h instanceof ItemHologram ih) { |
| 148 | + ItemStack item = ih.getItem(); |
| 149 | + text.send(ctx.sender(), "Item: " + item.getType()); |
| 150 | + } |
| 151 | + if (h instanceof BlockHologram bh) { |
| 152 | + text.send(ctx.sender(), "Block: " + bh.getBlockType()); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments