From c332547f38f61677e593c44047f68e5e41688cc8 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sat, 6 Mar 2021 18:44:52 +0100 Subject: [PATCH] Refactor hologram-related objects, simplify editing --- .../core/hologram/StandardHologram.java | 14 +-- .../bridge/protocollib/PacketSender.java | 4 +- .../commands/HologramCommandValidate.java | 4 +- .../commands/subs/AddlineCommand.java | 5 +- .../commands/subs/CopyCommand.java | 14 ++- .../commands/subs/CreateCommand.java | 7 +- .../commands/subs/DebugCommand.java | 2 +- .../commands/subs/InfoCommand.java | 2 +- .../commands/subs/InsertlineCommand.java | 17 ++- .../commands/subs/ListCommand.java | 2 +- .../commands/subs/NearCommand.java | 2 +- .../commands/subs/ReadimageCommand.java | 13 +- .../commands/subs/ReadtextCommand.java | 6 +- .../commands/subs/RemovelineCommand.java | 5 +- .../commands/subs/SetlineCommand.java | 8 +- .../disk/HologramConfig.java | 9 +- .../disk/HologramDatabase.java | 6 - .../disk/HologramLineParser.java | 11 +- .../image/ImageMessage.java | 4 +- .../object/api/APIHologram.java | 41 +++---- .../object/api/APIHologramLine.java | 5 +- .../object/api/APIItemLine.java | 3 +- .../object/api/APITextLine.java | 3 +- .../object/base/BaseHologram.java | 113 +++++++++++++----- .../object/internal/InternalHologram.java | 22 +--- 25 files changed, 167 insertions(+), 155 deletions(-) diff --git a/core/src/main/java/me/filoghost/holographicdisplays/core/hologram/StandardHologram.java b/core/src/main/java/me/filoghost/holographicdisplays/core/hologram/StandardHologram.java index 2467d359..ca6417e9 100644 --- a/core/src/main/java/me/filoghost/holographicdisplays/core/hologram/StandardHologram.java +++ b/core/src/main/java/me/filoghost/holographicdisplays/core/hologram/StandardHologram.java @@ -18,14 +18,14 @@ public interface StandardHologram { boolean isInChunk(Chunk chunk); + List getLines(); + + int getLinesAmount(); + Plugin getOwnerPlugin(); - List getLinesUnsafe(); - boolean isVisibleTo(Player player); - String toFormattedString(); - void refresh(); void refresh(boolean forceRespawn); @@ -34,12 +34,10 @@ public interface StandardHologram { void despawnEntities(); - void removeLine(StandardHologramLine line); - - int size(); - boolean isDeleted(); void setDeleted(); + String toFormattedString(); + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/bridge/protocollib/PacketSender.java b/plugin/src/main/java/me/filoghost/holographicdisplays/bridge/protocollib/PacketSender.java index ce59809e..c99d9dc0 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/bridge/protocollib/PacketSender.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/bridge/protocollib/PacketSender.java @@ -39,7 +39,7 @@ class PacketSender { public void sendDestroyEntitiesPacket(Player player, StandardHologram hologram) { List ids = new ArrayList<>(); - for (StandardHologramLine line : hologram.getLinesUnsafe()) { + for (StandardHologramLine line : hologram.getLines()) { line.collectEntityIDs(ids); } @@ -49,7 +49,7 @@ class PacketSender { } public void sendCreateEntitiesPacket(Player player, StandardHologram hologram) { - for (StandardHologramLine line : hologram.getLinesUnsafe()) { + for (StandardHologramLine line : hologram.getLines()) { sendCreateEntitiesPacket(player, line); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java index 49c8cc66..305ed44c 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/HologramCommandValidate.java @@ -20,9 +20,9 @@ import java.nio.file.Path; public class HologramCommandValidate { - public static InternalHologramLine parseHologramLine(InternalHologram hologram, String serializedLine, boolean validateMaterial) throws CommandException { + public static InternalHologramLine parseHologramLine(InternalHologram hologram, String serializedLine) throws CommandException { try { - return HologramLineParser.parseLine(hologram, serializedLine, validateMaterial); + return HologramLineParser.parseLine(hologram, serializedLine); } catch (HologramLoadException e) { throw new CommandException(Utils.formatExceptionMessage(e)); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java index 521874a1..bc948d6f 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/AddlineCommand.java @@ -41,9 +41,8 @@ public class AddlineCommand extends LineEditingCommand implements QuickEditComma InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); String serializedLine = Utils.join(args, " ", 1, args.length); - InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); - hologram.getLinesUnsafe().add(line); - hologram.refresh(); + InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine); + hologram.addLine(line); configManager.saveHologramDatabase(internalHologramManager); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java index f8fa4ab5..06c47450 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CopyCommand.java @@ -16,6 +16,9 @@ import me.filoghost.holographicdisplays.object.internal.InternalHologramLine; import me.filoghost.holographicdisplays.object.internal.InternalHologramManager; import org.bukkit.command.CommandSender; +import java.util.ArrayList; +import java.util.List; + public class CopyCommand extends HologramSubCommand { private final InternalHologramManager internalHologramManager; @@ -35,14 +38,13 @@ public class CopyCommand extends HologramSubCommand { public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { InternalHologram fromHologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); InternalHologram toHologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[1]); - - toHologram.clearLines(); - for (InternalHologramLine line : fromHologram.getLinesUnsafe()) { - InternalHologramLine clonedLine = HologramCommandValidate.parseHologramLine(toHologram, line.getSerializedConfigValue(), false); - toHologram.getLinesUnsafe().add(clonedLine); + + List clonedLines = new ArrayList<>(); + for (InternalHologramLine line : fromHologram.getLines()) { + clonedLines.add(HologramCommandValidate.parseHologramLine(toHologram, line.getSerializedConfigValue())); } - toHologram.refresh(); + toHologram.setLines(clonedLines); configManager.saveHologramDatabase(internalHologramManager); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java index 23356a4b..f220c57d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/CreateCommand.java @@ -65,16 +65,15 @@ public class CreateCommand extends HologramSubCommand { String text = Utils.join(args, " ", 1, args.length); CommandValidate.check(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); - line = HologramCommandValidate.parseHologramLine(hologram, text, true); + line = HologramCommandValidate.parseHologramLine(hologram, text); player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + context.getRootLabel() + " edit " + hologram.getName() + ")"); } else { String defaultText = "Default hologram. Change it with " + Colors.PRIMARY + "/" + context.getRootLabel() + " edit " + hologram.getName(); line = hologram.createTextLine(defaultText, defaultText.replace(ChatColor.COLOR_CHAR, '&')); } - - hologram.getLinesUnsafe().add(line); - hologram.refresh(); + + hologram.addLine(line); configManager.saveHologramDatabase(internalHologramManager); Location look = player.getLocation(); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java index a29b5758..4de3229a 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/DebugCommand.java @@ -66,7 +66,7 @@ public class DebugCommand extends HologramSubCommand { for (Entry entry : hologramsDebugInfo.entrySet()) { StandardHologram hologram = entry.getKey(); HologramDebugInfo debugInfo = entry.getValue(); - sender.sendMessage(Colors.PRIMARY_SHADOW + "- '" + hologram.toFormattedString() + "': " + hologram.size() + " lines, " + sender.sendMessage(Colors.PRIMARY_SHADOW + "- '" + hologram.toFormattedString() + "': " + hologram.getLinesAmount() + " lines, " + debugInfo.getTotalEntities() + " entities (" + debugInfo.aliveEntities + " alive, " + debugInfo.deadEntities + " dead)"); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java index 090a151b..aa0e1e50 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InfoCommand.java @@ -39,7 +39,7 @@ public class InfoCommand extends LineEditingCommand implements QuickEditCommand Messages.sendTitle(sender, "Lines of the hologram '" + hologram.getName() + "'"); int index = 0; - for (InternalHologramLine line : hologram.getLinesUnsafe()) { + for (InternalHologramLine line : hologram.getLines()) { sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + line.getSerializedConfigValue()); } commandManager.sendQuickEditCommands(context, hologram); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java index 00377392..6d89d16b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/InsertlineCommand.java @@ -44,28 +44,27 @@ public class InsertlineCommand extends LineEditingCommand implements QuickEditCo @Override public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); - int insertAfter = CommandValidate.parseInteger(args[1]); + int insertAfterIndex = CommandValidate.parseInteger(args[1]); String serializedLine = Utils.join(args, " ", 2, args.length); - int oldLinesAmount = hologram.size(); + int oldLinesAmount = hologram.getLinesAmount(); - CommandValidate.check(insertAfter >= 0 && insertAfter <= oldLinesAmount, "The number must be between 0 and " + hologram.size() + "(amount of lines of the hologram)."); + CommandValidate.check(insertAfterIndex >= 0 && insertAfterIndex <= oldLinesAmount, "The number must be between 0 and " + hologram.getLinesAmount() + "(amount of lines of the hologram)."); - InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); - hologram.getLinesUnsafe().add(insertAfter, line); - hologram.refresh(); + InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine); + hologram.insertLine(insertAfterIndex, line); configManager.saveHologramDatabase(internalHologramManager); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); - if (insertAfter == 0) { + if (insertAfterIndex == 0) { sender.sendMessage(Colors.PRIMARY + "Line inserted before first line."); - } else if (insertAfter == oldLinesAmount) { + } else if (insertAfterIndex == oldLinesAmount) { sender.sendMessage(Colors.PRIMARY + "Line appended at the end."); Messages.sendTip(sender, "You can use \"/" + context.getRootLabel() + " addline\" to append a line at the end."); } else { - sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "."); + sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfterIndex + " and " + (insertAfterIndex + 1) + "."); } commandManager.sendQuickEditCommands(context, hologram); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java index e3cfd10f..bcd3f7d9 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ListCommand.java @@ -58,7 +58,7 @@ public class ListCommand extends HologramSubCommand { + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) hologram.getX() + ", y: " + (int) hologram.getY() + ", z: " + (int) hologram.getZ() - + " (lines: " + hologram.size() + ", world: \"" + hologram.getWorld().getName() + "\")"); + + " (lines: " + hologram.getLinesAmount() + ", world: \"" + hologram.getWorld().getName() + "\")"); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java index 119b1f48..4ef6224e 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/NearCommand.java @@ -53,7 +53,7 @@ public class NearCommand extends HologramSubCommand { Messages.sendTitle(player, "Near holograms"); for (InternalHologram nearHologram : nearHolograms) { - player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.size() + ")"); + player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.getLinesAmount() + ")"); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java index afda714d..efd86893 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadimageCommand.java @@ -108,18 +108,17 @@ public class ReadimageCommand extends LineEditingCommand { } ImageMessage imageMessage = new ImageMessage(image, width); - String[] newLines = imageMessage.getLines(); + List newLines = new ArrayList<>(); + for (String newLine : imageMessage.getLines()) { + newLines.add(hologram.createTextLine(newLine, newLine)); + } if (!append) { hologram.clearLines(); } - for (String newLine : newLines) { - InternalTextLine line = hologram.createTextLine(newLine, newLine); - hologram.getLinesUnsafe().add(line); - } - hologram.refresh(); + hologram.addLines(newLines); - if (newLines.length < 5) { + if (newLines.size() < 5) { Messages.sendTip(sender, "The image has a very low height. You can increase it by increasing the width, it will scale automatically."); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java index d8b9edc6..53fbc948 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/ReadtextCommand.java @@ -76,16 +76,14 @@ public class ReadtextCommand extends LineEditingCommand { List linesToAdd = new ArrayList<>(); for (int i = 0; i < linesAmount; i++) { try { - InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLines.get(i), true); + InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLines.get(i)); linesToAdd.add(line); } catch (HologramLoadException e) { throw new CommandException("Error at line " + (i + 1) + ": " + e.getMessage()); } } - hologram.clearLines(); - hologram.getLinesUnsafe().addAll(linesToAdd); - hologram.refresh(); + hologram.setLines(linesToAdd); configManager.saveHologramDatabase(internalHologramManager); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java index eabbcb5b..2724b981 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/RemovelineCommand.java @@ -41,13 +41,12 @@ public class RemovelineCommand extends LineEditingCommand implements QuickEditCo int lineNumber = CommandValidate.parseInteger(args[1]); - CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); + CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.getLinesAmount(), "The line number must be between 1 and " + hologram.getLinesAmount() + "."); int index = lineNumber - 1; - CommandValidate.check(hologram.size() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete."); + CommandValidate.check(hologram.getLinesAmount() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete."); hologram.removeLine(index); - hologram.refresh(); configManager.saveHologramDatabase(internalHologramManager); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java index 2ac78165..183d453a 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/commands/subs/SetlineCommand.java @@ -43,14 +43,12 @@ public class SetlineCommand extends LineEditingCommand implements QuickEditComma String serializedLine = Utils.join(args, " ", 2, args.length); int lineNumber = CommandValidate.parseInteger(args[1]); - CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); + CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.getLinesAmount(), "The line number must be between 1 and " + hologram.getLinesAmount() + "."); int index = lineNumber - 1; - InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); + InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine); - InternalHologramLine prevLine = hologram.getLinesUnsafe().set(index, line); - prevLine.despawn(); - hologram.refresh(); + hologram.setLine(index, line); configManager.saveHologramDatabase(internalHologramManager); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramConfig.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramConfig.java index 65435730..7895efb5 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramConfig.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramConfig.java @@ -38,7 +38,7 @@ public class HologramConfig { public HologramConfig(InternalHologram hologram) { this.name = hologram.getName(); this.serializedLines = new ArrayList<>(); - for (InternalHologramLine line : hologram.getLinesUnsafe()) { + for (InternalHologramLine line : hologram.getLines()) { serializedLines.add(line.getSerializedConfigValue()); } @@ -62,17 +62,18 @@ public class HologramConfig { Location location = deserializeLocation(serializedLocation); InternalHologram hologram = internalHologramManager.createHologram(location, name); + List lines = new ArrayList<>(); for (String serializedLine : serializedLines) { try { - InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLine, false); - hologram.getLinesUnsafe().add(line); + lines.add(HologramLineParser.parseLine(hologram, serializedLine)); } catch (HologramLoadException e) { // Rethrow with more details throw new HologramLoadException("hologram \"" + hologram.getName() + "\" has an invalid line: " + e.getMessage(), e); } } - + + hologram.setLines(lines); return hologram; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java index 0e924396..be39bd7c 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramDatabase.java @@ -36,7 +36,6 @@ public class HologramDatabase { } public void createHolograms(CommandSender sender, InternalHologramManager internalHologramManager) { - // Create all the holograms for (HologramConfig hologramConfig : hologramConfigs) { try { hologramConfig.createHologram(internalHologramManager); @@ -50,11 +49,6 @@ public class HologramDatabase { Log.warning("Unexpected exception while loading the hologram \"" + hologramConfig.getName() + "\".", e); } } - - // Then trigger a refresh for all of them - for (InternalHologram hologram : internalHologramManager.getHolograms()) { - hologram.refresh(); - } } public static Config exportToConfig(InternalHologramManager hologramManager) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java index 14b5abb3..b4222103 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/disk/HologramLineParser.java @@ -20,12 +20,12 @@ public class HologramLineParser { private static final String ICON_PREFIX = "icon:"; private static final String EMPTY_LINE_PLACEHOLDER = "{empty}"; - public static InternalHologramLine parseLine(InternalHologram hologram, String serializedLine, boolean checkMaterialValidity) throws HologramLoadException { + public static InternalHologramLine parseLine(InternalHologram hologram, String serializedLine) throws HologramLoadException { InternalHologramLine hologramLine; if (serializedLine.toLowerCase().startsWith(ICON_PREFIX)) { String serializedIcon = serializedLine.substring(ICON_PREFIX.length()); - ItemStack icon = parseItemStack(serializedIcon, checkMaterialValidity); + ItemStack icon = parseItemStack(serializedIcon); hologramLine = hologram.createItemLine(icon, serializedLine); } else { @@ -44,7 +44,7 @@ public class HologramLineParser { @SuppressWarnings("deprecation") - private static ItemStack parseItemStack(String serializedItem, boolean checkMaterialValidity) throws HologramLoadException { + private static ItemStack parseItemStack(String serializedItem) throws HologramLoadException { serializedItem = serializedItem.trim(); // Parse json @@ -80,10 +80,7 @@ public class HologramLineParser { Material material = MaterialsHelper.matchMaterial(materialName); if (material == null) { - if (checkMaterialValidity) { - throw new HologramLoadException("\"" + materialName + "\" is not a valid material"); - } - material = Material.BEDROCK; + throw new HologramLoadException("\"" + materialName + "\" is not a valid material"); } ItemStack itemStack = new ItemStack(material, 1, dataValue); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java index a7316905..ccf190be 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/image/ImageMessage.java @@ -189,9 +189,9 @@ public class ImageMessage { // Minecraft has 15 colors return bestColorMatch; } - - + public String[] getLines() { return lines; } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java index fdfd8285..98d0d804 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java @@ -12,23 +12,21 @@ import me.filoghost.holographicdisplays.api.line.HologramLine; import me.filoghost.holographicdisplays.api.line.ItemLine; import me.filoghost.holographicdisplays.api.line.TextLine; import me.filoghost.holographicdisplays.core.nms.NMSManager; -import me.filoghost.holographicdisplays.object.base.BaseHologram; import me.filoghost.holographicdisplays.disk.Configuration; +import me.filoghost.holographicdisplays.object.base.BaseHologram; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; -import java.util.ArrayList; import java.util.List; -public class APIHologram extends BaseHologram implements Hologram { +public class APIHologram extends BaseHologram implements Hologram { private final Plugin plugin; private final APIHologramManager apiHologramManager; private final VisibilityManager visibilityManager; private final long creationTimestamp; - private final List lines; private boolean allowPlaceholders; @@ -39,59 +37,42 @@ public class APIHologram extends BaseHologram implements Hologram { this.apiHologramManager = apiHologramManager; this.visibilityManager = new DefaultVisibilityManager(this); this.creationTimestamp = System.currentTimeMillis(); - this.lines = new ArrayList<>(); - } @Override public Plugin getOwnerPlugin() { return plugin; } - - @Override - public List getLinesUnsafe() { - return lines; - } - + @Override public TextLine appendTextLine(String text) { - checkState(); - APITextLine line = createTextLine(text); - lines.add(line); - refresh(); + addLine(line); return line; } @Override public ItemLine appendItemLine(ItemStack itemStack) { - checkState(); Preconditions.notNull(itemStack, "itemStack"); APIItemLine line = createItemLine(itemStack); - lines.add(line); - refresh(); + addLine(line); return line; } @Override public TextLine insertTextLine(int index, String text) { - checkState(); - APITextLine line = createTextLine(text); - lines.add(index, line); - refresh(); + addLine(line); return line; } @Override public ItemLine insertItemLine(int index, ItemStack itemStack) { - checkState(); Preconditions.notNull(itemStack, "itemStack"); APIItemLine line = createItemLine(itemStack); - lines.add(index, line); - refresh(); + addLine(line); return line; } @@ -105,7 +86,12 @@ public class APIHologram extends BaseHologram implements Hologram { @Override public HologramLine getLine(int index) { - return lines.get(index); + return getLines().get(index); + } + + @Override + public int size() { + return getLinesAmount(); } @Override @@ -130,6 +116,7 @@ public class APIHologram extends BaseHologram implements Hologram { @Override public double getHeight() { + List lines = getLines(); if (lines.isEmpty()) { return 0; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java index 4f77ef86..2325f730 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java @@ -10,9 +10,12 @@ import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine; public interface APIHologramLine extends HologramLine, StandardHologramLine { + @Override + APIHologram getParent(); + @Override default void removeLine() { - getHologram().removeLine(this); + getParent().removeLine(this); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java index fc05f8ad..136107c3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java @@ -1,6 +1,5 @@ package me.filoghost.holographicdisplays.object.api; -import me.filoghost.holographicdisplays.api.Hologram; import me.filoghost.holographicdisplays.api.line.ItemLine; import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.object.base.BaseItemLine; @@ -16,7 +15,7 @@ public class APIItemLine extends BaseItemLine implements ItemLine, APIHologramLi } @Override - public Hologram getParent() { + public APIHologram getParent() { return parent; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java index b0fa709a..2fb8e730 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java @@ -1,6 +1,5 @@ package me.filoghost.holographicdisplays.object.api; -import me.filoghost.holographicdisplays.api.Hologram; import me.filoghost.holographicdisplays.api.line.TextLine; import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.object.base.BaseTextLine; @@ -15,7 +14,7 @@ public class APITextLine extends BaseTextLine implements TextLine, APIHologramLi } @Override - public Hologram getParent() { + public APIHologram getParent() { return parent; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologram.java index c4c7c21c..7b6b46fa 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseHologram.java @@ -6,18 +6,23 @@ package me.filoghost.holographicdisplays.object.base; import me.filoghost.fcommons.Preconditions; -import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.hologram.StandardHologram; import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine; +import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.disk.Configuration; import org.bukkit.Location; import org.bukkit.World; +import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; +import java.util.List; -public abstract class BaseHologram extends BaseHologramComponent implements StandardHologram { +public abstract class BaseHologram extends BaseHologramComponent implements StandardHologram { private final NMSManager nmsManager; + private final List lines; + private final List unmodifiableLinesView; private boolean deleted; @@ -25,64 +30,110 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan Preconditions.notNull(location, "location"); this.setLocation(location); this.nmsManager = nmsManager; + this.lines = new ArrayList<>(); + this.unmodifiableLinesView = Collections.unmodifiableList(lines); + } + + protected final NMSManager getNMSManager() { + return nmsManager; } @Override public boolean isDeleted() { return deleted; } - + @Override public void setDeleted() { if (!deleted) { deleted = true; - clearLines(); + despawnEntities(); } } - - protected final NMSManager getNMSManager() { - return nmsManager; - } - - public void removeLine(int index) { - checkState(); - - getLinesUnsafe().remove(index).despawn(); - refresh(); - } @Override - public void removeLine(StandardHologramLine line) { - checkState(); + public List getLines() { + return unmodifiableLinesView; + } - getLinesUnsafe().remove(line); + public void addLine(T line) { + checkNotDeleted(); + + lines.add(line); + refresh(); + } + + public void addLines(List newLines) { + checkNotDeleted(); + + lines.addAll(newLines); + refresh(); + } + + public void insertLine(int afterIndex, T line) { + checkNotDeleted(); + + lines.add(afterIndex, line); + refresh(); + } + + public void setLine(int index, T line) { + checkNotDeleted(); + + T previousLine = lines.set(index, line); + previousLine.despawn(); + refresh(); + } + + public void setLines(List newLines) { + checkNotDeleted(); + + clearLines(); + lines.addAll(newLines); + refresh(); + } + + public void removeLine(int index) { + checkNotDeleted(); + + lines.remove(index).despawn(); + refresh(); + } + + public void removeLine(T line) { + checkNotDeleted(); + + lines.remove(line); line.despawn(); refresh(); } public void clearLines() { - Iterator iterator = getLinesUnsafe().iterator(); + checkNotDeleted(); + + Iterator iterator = lines.iterator(); while (iterator.hasNext()) { - StandardHologramLine line = iterator.next(); + T line = iterator.next(); iterator.remove(); line.despawn(); } + + // No need to refresh, since there are no lines } @Override - public int size() { - return getLinesUnsafe().size(); + public int getLinesAmount() { + return lines.size(); } public void teleport(Location location) { - checkState(); Preconditions.notNull(location, "location"); teleport(location.getWorld(), location.getX(), location.getY(), location.getZ()); } public void teleport(World world, double x, double y, double z) { - checkState(); + checkNotDeleted(); Preconditions.notNull(world, "world"); setLocation(world, x, y, z); @@ -101,7 +152,7 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan @Override public void refresh(boolean forceRespawn, boolean isChunkLoaded) { - checkState(); + checkNotDeleted(); if (isChunkLoaded) { respawnEntities(forceRespawn); @@ -117,8 +168,8 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan private void respawnEntities(boolean forceRespawn) { double currentLineY = getY(); - for (int i = 0; i < getLinesUnsafe().size(); i++) { - StandardHologramLine line = getLinesUnsafe().get(i); + for (int i = 0; i < lines.size(); i++) { + T line = lines.get(i); currentLineY -= line.getHeight(); if (i > 0) { @@ -134,18 +185,18 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan @Override public void despawnEntities() { - for (StandardHologramLine line : getLinesUnsafe()) { + for (T line : lines) { line.despawn(); } } - protected void checkState() { - Preconditions.checkState(!deleted, "hologram already deleted"); + private void checkNotDeleted() { + Preconditions.checkState(!deleted, "hologram is not usable after being deleted"); } @Override public String toString() { - return "BaseHologram [location=" + getLocation() + ", lines=" + getLinesUnsafe() + ", deleted=" + deleted + "]"; + return "BaseHologram [location=" + getLocation() + ", lines=" + lines + ", deleted=" + deleted + "]"; } /* diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/internal/InternalHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/internal/InternalHologram.java index 96f09c94..1a414210 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/internal/InternalHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/internal/InternalHologram.java @@ -13,22 +13,13 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; -import java.util.ArrayList; -import java.util.List; - -public class InternalHologram extends BaseHologram { +public class InternalHologram extends BaseHologram { private final String name; - private final List lines; protected InternalHologram(Location source, String name, NMSManager nmsManager) { super(source, nmsManager); this.name = name; - this.lines = new ArrayList<>(); - } - - public String getName() { - return name; } public InternalTextLine createTextLine(String text, String serializedConfigValue) { @@ -39,16 +30,15 @@ public class InternalHologram extends BaseHologram { return new InternalItemLine(this, getNMSManager(), icon, serializedConfigValue); } + public String getName() { + return name; + } + @Override public Plugin getOwnerPlugin() { return HolographicDisplays.getInstance(); } - - @Override - public List getLinesUnsafe() { - return lines; - } - + @Override public boolean isVisibleTo(Player player) { return true;