|
| 1 | +package com.fancyinnovations.fancyholograms.commands.hologram; |
| 2 | + |
| 3 | +import com.fancyinnovations.fancyholograms.api.data.BlockHologramData; |
| 4 | +import com.fancyinnovations.fancyholograms.api.events.HologramUpdateEvent; |
| 5 | +import com.fancyinnovations.fancyholograms.api.hologram.Hologram; |
| 6 | +import com.fancyinnovations.fancyholograms.commands.HologramCMD; |
| 7 | +import com.fancyinnovations.fancyholograms.commands.Subcommand; |
| 8 | +import com.fancyinnovations.fancyholograms.main.FancyHologramsPlugin; |
| 9 | +import de.oliver.fancylib.MessageHelper; |
| 10 | +import org.bukkit.Bukkit; |
| 11 | +import org.bukkit.block.data.BlockData; |
| 12 | +import org.bukkit.command.CommandSender; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +public class BlockStateCMD implements Subcommand { |
| 20 | + |
| 21 | + @Override |
| 22 | + public List<String> tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { |
| 23 | + if (hologram == null || !(hologram.getData() instanceof BlockHologramData blockData)) { |
| 24 | + return Collections.emptyList(); |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + BlockData defaultBlockData = blockData.getBlock().createBlockData(); |
| 29 | + |
| 30 | + if (args.length == 4) { |
| 31 | + return Arrays.stream(defaultBlockData.getAsString(true) |
| 32 | + .replaceAll("^[^\\[]*\\[", "") |
| 33 | + .replaceAll("\\]$", "") |
| 34 | + .split(",")) |
| 35 | + .map(s -> s.split("=")[0]) |
| 36 | + .filter(prop -> prop.toLowerCase().startsWith(args[3].toLowerCase())) |
| 37 | + .collect(Collectors.toList()); |
| 38 | + } else if (args.length == 5) { |
| 39 | + String propertyName = args[3]; |
| 40 | + String currentState = defaultBlockData.getAsString(true); |
| 41 | + |
| 42 | + for (String prop : currentState.replaceAll("^[^\\[]*\\[", "").replaceAll("\\]$", "").split(",")) { |
| 43 | + String[] parts = prop.split("="); |
| 44 | + if (parts[0].equals(propertyName)) { |
| 45 | + List<String> possibleValues = new ArrayList<>(); |
| 46 | + |
| 47 | + possibleValues.add("true"); |
| 48 | + possibleValues.add("false"); |
| 49 | + |
| 50 | + possibleValues.addAll(Arrays.asList("north", "south", "east", "west", "up", "down")); |
| 51 | + |
| 52 | + for (int i = 0; i <= 15; i++) { |
| 53 | + possibleValues.add(String.valueOf(i)); |
| 54 | + } |
| 55 | + |
| 56 | + return possibleValues.stream() |
| 57 | + .filter(val -> { |
| 58 | + try { |
| 59 | + Bukkit.createBlockData(blockData.getBlock(), propertyName + "=" + val); |
| 60 | + return true; |
| 61 | + } catch (IllegalArgumentException e) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + }) |
| 65 | + .filter(val -> val.toLowerCase().startsWith(args[4].toLowerCase())) |
| 66 | + .collect(Collectors.toList()); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } catch (Exception e) { |
| 71 | + return Collections.emptyList(); |
| 72 | + } |
| 73 | + |
| 74 | + return Collections.emptyList(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { |
| 79 | + |
| 80 | + if (!(player.hasPermission("fancyholograms.hologram.edit.block_state"))) { |
| 81 | + MessageHelper.error(player, "You don't have the required permission to change the block state of this hologram"); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + if (!(hologram.getData() instanceof BlockHologramData blockData)) { |
| 86 | + MessageHelper.error(player, "This command can only be used on block holograms"); |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // If no additional args, clear the block state |
| 91 | + if (args.length < 4) { |
| 92 | + final var copied = blockData.copy(blockData.getName()); |
| 93 | + copied.setBlockState(null); |
| 94 | + |
| 95 | + if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.BLOCK_STATE)) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + blockData.setBlockState(null); |
| 100 | + |
| 101 | + if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) { |
| 102 | + FancyHologramsPlugin.get().getStorage().save(hologram.getData()); |
| 103 | + } |
| 104 | + |
| 105 | + MessageHelper.success(player, "Cleared block state (using default)"); |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + // Parse property=value pairs from args |
| 110 | + // Format: /hologram edit <name> blockstate <property> <value> [property] [value] ... |
| 111 | + |
| 112 | + // Start with existing properties or create default |
| 113 | + Map<String, String> existingProperties = new HashMap<>(); |
| 114 | + |
| 115 | + // Parse existing block state if present |
| 116 | + if (blockData.getBlockState() != null && !blockData.getBlockState().isEmpty()) { |
| 117 | + String[] props = blockData.getBlockState().split(","); |
| 118 | + for (String prop : props) { |
| 119 | + String[] parts = prop.split("=", 2); |
| 120 | + if (parts.length == 2) { |
| 121 | + existingProperties.put(parts[0].trim(), parts[1].trim()); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Apply the new properties (overwriting existing ones) |
| 127 | + for (int i = 3; i < args.length; i += 2) { |
| 128 | + if (i + 1 >= args.length) { |
| 129 | + MessageHelper.error(player, "Missing value for property: " + args[i]); |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + String property = args[i]; |
| 134 | + String value = args[i + 1]; |
| 135 | + existingProperties.put(property, value); |
| 136 | + } |
| 137 | + |
| 138 | + // Build the block state string with brackets format |
| 139 | + String blockStateString = existingProperties.entrySet().stream() |
| 140 | + .sorted(Map.Entry.comparingByKey()) // Sort for consistency |
| 141 | + .map(e -> e.getKey() + "=" + e.getValue()) |
| 142 | + .collect(Collectors.joining(",")); |
| 143 | + |
| 144 | + // Validate the block state by creating block data with full string format |
| 145 | + try { |
| 146 | + String fullBlockString = blockData.getBlock().getKey().toString() + "[" + blockStateString + "]"; |
| 147 | + Bukkit.createBlockData(fullBlockString); |
| 148 | + } catch (IllegalArgumentException e) { |
| 149 | + MessageHelper.error(player, "Invalid block state: " + e.getMessage()); |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + if (java.util.Objects.equals(blockStateString, blockData.getBlockState())) { |
| 154 | + MessageHelper.warning(player, "This block state is already set"); |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + final var copied = blockData.copy(blockData.getName()); |
| 159 | + copied.setBlockState(blockStateString); |
| 160 | + |
| 161 | + if (!HologramCMD.callModificationEvent(hologram, player, copied, HologramUpdateEvent.HologramModification.BLOCK_STATE)) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + if (java.util.Objects.equals(blockStateString, blockData.getBlockState())) { |
| 166 | + MessageHelper.warning(player, "This block state is already set"); |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + blockData.setBlockState(blockStateString); |
| 171 | + |
| 172 | + if (FancyHologramsPlugin.get().getHologramConfiguration().isSaveOnChangedEnabled()) { |
| 173 | + FancyHologramsPlugin.get().getStorage().save(hologram.getData()); |
| 174 | + } |
| 175 | + |
| 176 | + MessageHelper.success(player, "Set block state: " + blockStateString); |
| 177 | + |
| 178 | + return true; |
| 179 | + } |
| 180 | +} |
0 commit comments