diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java index a5d2fd81..ab684810 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java @@ -9,6 +9,7 @@ import com.gmail.filoghost.holographicdisplays.api.internal.HologramsAPIProvider import me.filoghost.fcommons.FCommonsPlugin; import me.filoghost.fcommons.FeatureSupport; import me.filoghost.fcommons.logging.ErrorCollector; +import me.filoghost.holographicdisplays.api.beta.hologram.Hologram; import me.filoghost.holographicdisplays.api.beta.internal.HolographicDisplaysAPIProvider; import me.filoghost.holographicdisplays.nms.common.NMSManager; import me.filoghost.holographicdisplays.plugin.api.current.APIHologramManager; @@ -20,13 +21,17 @@ import me.filoghost.holographicdisplays.plugin.bridge.placeholderapi.Placeholder import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; import me.filoghost.holographicdisplays.plugin.config.ConfigManager; -import me.filoghost.holographicdisplays.plugin.config.HologramDatabase; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramLoadException; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramConfig; import me.filoghost.holographicdisplays.plugin.config.Settings; import me.filoghost.holographicdisplays.plugin.config.upgrade.AnimationsLegacyUpgrade; import me.filoghost.holographicdisplays.plugin.config.upgrade.DatabaseLegacyUpgrade; import me.filoghost.holographicdisplays.plugin.config.upgrade.SymbolsLegacyUpgrade; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram; +import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramManager; import me.filoghost.holographicdisplays.plugin.internal.placeholder.AnimationPlaceholderFactory; import me.filoghost.holographicdisplays.plugin.internal.placeholder.DefaultPlaceholders; @@ -47,7 +52,9 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.function.Function; public class HolographicDisplays extends FCommonsPlugin { @@ -108,9 +115,11 @@ public class HolographicDisplays extends FCommonsPlugin { ActivePlaceholderTracker placeholderTracker = new ActivePlaceholderTracker(placeholderRegistry, tickClock); LineClickListener lineClickListener = new LineClickListener(); lineTrackerManager = new LineTrackerManager(nmsManager, placeholderTracker, lineClickListener, tickClock); - internalHologramManager = new InternalHologramManager(lineTrackerManager); apiHologramManager = new APIHologramManager(lineTrackerManager); v2HologramManager = new V2HologramManager(lineTrackerManager); + Function hologramFactory = + (ImmutablePosition position) -> apiHologramManager.createHologram(position, this); + internalHologramManager = new InternalHologramManager(hologramFactory); // Run only once at startup, before loading the configuration new SymbolsLegacyUpgrade(configManager, errorCollector).tryRun(); @@ -136,7 +145,7 @@ public class HolographicDisplays extends FCommonsPlugin { // Listeners PlayerListener playerListener = new PlayerListener(nmsManager, lineClickListener, tickingTask); registerListener(playerListener); - registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager, v2HologramManager)); + registerListener(new ChunkListener(this, apiHologramManager, v2HologramManager)); UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener(); registerListener(updateNotificationListener); @@ -178,8 +187,18 @@ public class HolographicDisplays extends FCommonsPlugin { bungeeServerTracker.restart(Settings.bungeeRefreshSeconds, TimeUnit.SECONDS); - HologramDatabase hologramDatabase = configManager.loadHologramDatabase(errorCollector); - hologramDatabase.createHolograms(internalHologramManager, errorCollector); + // Load holograms from database + List hologramConfigs = configManager.readHologramDatabase(errorCollector); + for (InternalHologramConfig hologramConfig : hologramConfigs) { + try { + List lines = hologramConfig.deserializeLines(); + ImmutablePosition position = hologramConfig.deserializePosition(); + InternalHologram hologram = internalHologramManager.createHologram(hologramConfig.getName(), position); + hologram.addLines(lines); + } catch (InternalHologramLoadException e) { + errorCollector.add(e, "error while loading hologram \"" + hologramConfig.getName() + "\""); + } + } for (BaseHologram hologram : apiHologramManager.getHolograms()) { hologram.getLines().updatePositions(); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologramManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologramManager.java index 3402068a..8a7b8867 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologramManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologramManager.java @@ -24,7 +24,7 @@ public class APIHologramManager extends BaseHologramManager { this.lineTrackerManager = lineTrackerManager; } - APIHologram createHologram(ImmutablePosition position, Plugin plugin) { + public APIHologram createHologram(ImmutablePosition position, Plugin plugin) { APIHologram hologram = new APIHologram(position, plugin, this, lineTrackerManager); super.addHologram(hologram); return hologram; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/InternalHologramEditor.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/InternalHologramEditor.java index 7056954d..6f32eb6b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/InternalHologramEditor.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/InternalHologramEditor.java @@ -9,16 +9,14 @@ import me.filoghost.fcommons.Strings; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.plugin.config.ConfigManager; -import me.filoghost.holographicdisplays.plugin.config.HologramLineParser; -import me.filoghost.holographicdisplays.plugin.config.HologramLoadException; -import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramLineParser; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramLoadException; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramManager; import me.filoghost.holographicdisplays.plugin.util.FileUtils; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -38,10 +36,10 @@ public class InternalHologramEditor { this.configManager = configManager; } - public InternalHologramLine parseHologramLine(InternalHologram hologram, String serializedLine) throws CommandException { + public InternalHologramLine parseHologramLine(String serializedLine) throws CommandException { try { - return HologramLineParser.parseLine(hologram, serializedLine); - } catch (HologramLoadException e) { + return InternalHologramLineParser.parseLine(serializedLine); + } catch (InternalHologramLoadException e) { throw new CommandException(formatExceptionMessage(e)); } } @@ -73,7 +71,7 @@ public class InternalHologramEditor { return internalHologramManager.getHolograms(); } - public InternalHologram create(ImmutablePosition spawnPosition, String hologramName) { + public InternalHologram create(String hologramName, ImmutablePosition spawnPosition) { return internalHologramManager.createHologram(hologramName, spawnPosition); } @@ -82,8 +80,8 @@ public class InternalHologramEditor { } public void saveChanges(InternalHologram hologram, ChangeType changeType) { - configManager.saveHologramDatabase(internalHologramManager); - Bukkit.getPluginManager().callEvent(new InternalHologramChangeEvent(hologram, changeType)); + configManager.saveHologramDatabase(internalHologramManager.getHolograms()); + hologram.callChangeEvent(changeType); } public void teleportLookingDown(Player player, Location location) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/AddLineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/AddLineCommand.java index 233be75c..f7369c32 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/AddLineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/AddLineCommand.java @@ -10,10 +10,10 @@ import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import org.bukkit.command.CommandSender; public class AddLineCommand extends LineEditingCommand implements QuickEditCommand { @@ -36,9 +36,9 @@ public class AddLineCommand extends LineEditingCommand implements QuickEditComma InternalHologram hologram = hologramEditor.getExistingHologram(args[0]); String serializedLine = Strings.joinFrom(" ", args, 1); - InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); + InternalHologramLine line = hologramEditor.parseHologramLine(serializedLine); - hologram.getLines().add(line); + hologram.addLine(line); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); sender.sendMessage(ColorScheme.PRIMARY + "Line added."); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CopyCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CopyCommand.java index df382bcd..354f2f87 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CopyCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CopyCommand.java @@ -12,12 +12,8 @@ import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import org.bukkit.command.CommandSender; -import java.util.ArrayList; -import java.util.List; - public class CopyCommand extends HologramSubCommand { private final InternalHologramEditor hologramEditor; @@ -36,12 +32,7 @@ public class CopyCommand extends HologramSubCommand { InternalHologram fromHologram = hologramEditor.getExistingHologram(args[0]); InternalHologram toHologram = hologramEditor.getExistingHologram(args[1]); - List clonedLines = new ArrayList<>(); - for (InternalHologramLine line : fromHologram.getLines()) { - clonedLines.add(hologramEditor.parseHologramLine(toHologram, line.getSerializedConfigValue())); - } - - toHologram.getLines().setAll(clonedLines); + toHologram.setLines(fromHologram.getLines()); hologramEditor.saveChanges(toHologram, ChangeType.EDIT_LINES); sender.sendMessage(ColorScheme.PRIMARY + "Lines of hologram \"" + fromHologram.getName() + "\"" diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java index b53da0f0..7457c5dd 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java @@ -11,11 +11,11 @@ import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.plugin.commands.HologramSubCommand; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -53,21 +53,21 @@ public class CreateCommand extends HologramSubCommand { spawnPosition = spawnPosition.add(0, 1.2, 0); } - InternalHologram hologram = hologramEditor.create(spawnPosition, hologramName); + InternalHologram hologram = hologramEditor.create(hologramName, spawnPosition); InternalHologramLine line; if (args.length > 1) { String text = Strings.joinFrom(" ", args, 1); - line = hologramEditor.parseHologramLine(hologram, text); + line = hologramEditor.parseHologramLine(text); player.sendMessage(ColorScheme.SECONDARY_DARK + "(Change the lines with /" + context.getRootLabel() + " edit " + hologram.getName() + ")"); } else { - String defaultText = "Default hologram. Change it with " - + ColorScheme.PRIMARY + "/" + context.getRootLabel() + " edit " + hologram.getName(); - line = hologram.createTextLine(defaultText, defaultText.replace(ChatColor.COLOR_CHAR, '&')); + line = hologramEditor.parseHologramLine("Default hologram. Change it with " + + ColorScheme.PRIMARY.toString().replace(ChatColor.COLOR_CHAR, '&') + + "/" + context.getRootLabel() + " edit " + hologram.getName()); } - hologram.getLines().add(line); + hologram.addLine(line); hologramEditor.saveChanges(hologram, ChangeType.CREATE); hologramEditor.teleportLookingDown(player, player.getLocation()); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InfoCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InfoCommand.java index f90971a9..dd38a60c 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InfoCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InfoCommand.java @@ -9,10 +9,10 @@ import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.format.DisplayFormat; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import org.bukkit.command.CommandSender; public class InfoCommand extends LineEditingCommand implements QuickEditCommand { @@ -40,7 +40,7 @@ public class InfoCommand extends LineEditingCommand implements QuickEditCommand for (InternalHologramLine line : hologram.getLines()) { index++; sender.sendMessage(ColorScheme.SECONDARY_BOLD + index + ColorScheme.SECONDARY_DARK + ". " - + ColorScheme.SECONDARY + line.getSerializedConfigValue()); + + ColorScheme.SECONDARY + line.getSerializedString()); } commandManager.sendQuickEditCommands(context, hologram); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InsertLineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InsertLineCommand.java index a25ea40d..1936d457 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InsertLineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/InsertLineCommand.java @@ -11,11 +11,11 @@ import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.format.DisplayFormat; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import org.bukkit.command.CommandSender; public class InsertLineCommand extends LineEditingCommand implements QuickEditCommand { @@ -46,9 +46,9 @@ public class InsertLineCommand extends LineEditingCommand implements QuickEditCo CommandValidate.check(insertAfterIndex >= 0 && insertAfterIndex <= oldLinesAmount, "The line number must be between 0 and " + oldLinesAmount + "."); - InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); + InternalHologramLine line = hologramEditor.parseHologramLine(serializedLine); - hologram.getLines().insert(insertAfterIndex, line); + hologram.insertLine(insertAfterIndex, line); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); if (insertAfterIndex == 0) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/MoveHereCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/MoveHereCommand.java index 65111316..d891618b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/MoveHereCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/MoveHereCommand.java @@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.commands.HologramSubCommand; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; +import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -34,7 +35,7 @@ public class MoveHereCommand extends HologramSubCommand { Player player = CommandValidate.getPlayerSender(sender); InternalHologram hologram = hologramEditor.getExistingHologram(args[0]); - hologram.setPosition(player.getLocation()); + hologram.setPosition(ImmutablePosition.of(player.getLocation())); hologramEditor.saveChanges(hologram, ChangeType.EDIT_POSITION); hologramEditor.teleportLookingDown(player, player.getLocation()); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadImageCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadImageCommand.java index 8d767200..a52c12b4 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadImageCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadImageCommand.java @@ -12,6 +12,7 @@ import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.fcommons.logging.Log; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.format.DisplayFormat; @@ -19,7 +20,6 @@ import me.filoghost.holographicdisplays.plugin.image.ImageMessage; import me.filoghost.holographicdisplays.plugin.image.ImageReadException; import me.filoghost.holographicdisplays.plugin.image.ImageReader; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalTextHologramLine; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; @@ -103,9 +103,9 @@ public class ReadImageCommand extends LineEditingCommand { } ImageMessage imageMessage = new ImageMessage(image, width); - List newLines = new ArrayList<>(); + List newLines = new ArrayList<>(); for (String newLine : imageMessage.getLines()) { - newLines.add(hologram.createTextLine(newLine, Colors.uncolorize(newLine))); + newLines.add(hologramEditor.parseHologramLine(Colors.uncolorize(newLine))); } if (newLines.size() < 5) { @@ -113,10 +113,11 @@ public class ReadImageCommand extends LineEditingCommand { + " You can increase it by increasing the width, it will scale automatically."); } - if (!append) { - hologram.getLines().clear(); + if (append) { + hologram.addLines(newLines); + } else { + hologram.setLines(newLines); } - hologram.getLines().addAll(newLines); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); if (append) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadTextCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadTextCommand.java index 1470aaef..b45cd29c 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadTextCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/ReadTextCommand.java @@ -10,13 +10,13 @@ import me.filoghost.fcommons.command.sub.SubCommandContext; import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.logging.Log; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; -import me.filoghost.holographicdisplays.plugin.config.HologramLineParser; -import me.filoghost.holographicdisplays.plugin.config.HologramLoadException; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramLineParser; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramLoadException; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.format.DisplayFormat; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.util.FileUtils; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; @@ -78,14 +78,14 @@ public class ReadTextCommand extends LineEditingCommand { List newLines = new ArrayList<>(); for (int i = 0; i < linesAmount; i++) { try { - InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLines.get(i)); + InternalHologramLine line = InternalHologramLineParser.parseLine(serializedLines.get(i)); newLines.add(line); - } catch (HologramLoadException e) { + } catch (InternalHologramLoadException e) { throw new CommandException("Error at line " + (i + 1) + ": " + e.getMessage()); } } - hologram.getLines().setAll(newLines); + hologram.setLines(newLines); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); if (FileUtils.hasFileExtension(fileToRead, "jpg", "png", "jpeg", "gif")) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/RemoveLineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/RemoveLineCommand.java index 9d6ffb3a..005f7a90 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/RemoveLineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/RemoveLineCommand.java @@ -44,7 +44,7 @@ public class RemoveLineCommand extends LineEditingCommand implements QuickEditCo CommandValidate.check(linesAmount >= 2, "A hologram must always have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete."); - hologram.getLines().remove(index); + hologram.removeLine(index); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); sender.sendMessage(ColorScheme.PRIMARY + "Line " + lineNumber + " removed."); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/SetLineCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/SetLineCommand.java index 6bdece61..a08dcc92 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/SetLineCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/SetLineCommand.java @@ -11,10 +11,10 @@ import me.filoghost.fcommons.command.validation.CommandException; import me.filoghost.fcommons.command.validation.CommandValidate; import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager; import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.format.ColorScheme; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; import org.bukkit.command.CommandSender; public class SetLineCommand extends LineEditingCommand implements QuickEditCommand { @@ -44,9 +44,9 @@ public class SetLineCommand extends LineEditingCommand implements QuickEditComma "The line number must be between 1 and " + linesAmount + "."); int index = lineNumber - 1; - InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); + InternalHologramLine line = hologramEditor.parseHologramLine(serializedLine); - hologram.getLines().set(index, line); + hologram.setLine(index, line); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); sender.sendMessage(ColorScheme.PRIMARY + "Line " + lineNumber + " changed."); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java index df924532..09f4652d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/ConfigManager.java @@ -10,6 +10,9 @@ import me.filoghost.fcommons.config.BaseConfigManager; import me.filoghost.fcommons.config.Config; import me.filoghost.fcommons.config.ConfigErrors; import me.filoghost.fcommons.config.ConfigLoader; +import me.filoghost.fcommons.config.ConfigPath; +import me.filoghost.fcommons.config.ConfigSection; +import me.filoghost.fcommons.config.ConfigType; import me.filoghost.fcommons.config.FileConfig; import me.filoghost.fcommons.config.exception.ConfigException; import me.filoghost.fcommons.config.exception.ConfigLoadException; @@ -19,7 +22,7 @@ import me.filoghost.fcommons.config.mapped.MappedConfigLoader; import me.filoghost.fcommons.logging.ErrorCollector; import me.filoghost.fcommons.logging.Log; import me.filoghost.holographicdisplays.plugin.format.DisplayFormat; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramManager; +import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; import me.filoghost.holographicdisplays.plugin.internal.placeholder.AnimationPlaceholder; import me.filoghost.holographicdisplays.plugin.internal.placeholder.AnimationPlaceholderFactory; import me.filoghost.holographicdisplays.plugin.util.FileUtils; @@ -27,8 +30,12 @@ import me.filoghost.holographicdisplays.plugin.util.FileUtils; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.stream.Stream; public class ConfigManager extends BaseConfigManager { @@ -57,24 +64,41 @@ public class ConfigManager extends BaseConfigManager { Settings.load(mainConfig, errorCollector); } - public HologramDatabase loadHologramDatabase(ErrorCollector errorCollector) { - Config databaseConfig; - + public List readHologramDatabase(ErrorCollector errorCollector) { + Config config; try { - databaseConfig = databaseConfigLoader.init(); + config = databaseConfigLoader.init(); } catch (ConfigException e) { logConfigException(errorCollector, databaseConfigLoader.getFile(), e); - databaseConfig = new Config(); // Fallback: empty config + return Collections.emptyList(); } - HologramDatabase hologramDatabase = new HologramDatabase(); - hologramDatabase.loadFromConfig(databaseConfig); - return hologramDatabase; + List hologramConfigs = new ArrayList<>(); + + for (Entry entry : config.toMap(ConfigType.SECTION).entrySet()) { + String hologramName = entry.getKey().asRawKey(); + ConfigSection hologramConfigSection = entry.getValue(); + hologramConfigs.add(new InternalHologramConfig(hologramName, hologramConfigSection)); + } + + return hologramConfigs; } - public void saveHologramDatabase(InternalHologramManager hologramManager) { + public void saveHologramDatabase(List holograms) { + Config config = new Config(); + config.setHeader( + "", + "Please do NOT edit this file manually if possible.", + "" + ); + + for (InternalHologram hologram : holograms) { + InternalHologramConfig hologramConfig = new InternalHologramConfig(hologram); + config.setConfigSection(hologram.getName(), hologramConfig.getSerializedConfigSection()); + } + try { - databaseConfigLoader.save(HologramDatabase.exportToConfig(hologramManager)); + databaseConfigLoader.save(config); } catch (ConfigException e) { Log.severe("Error while saving holograms database file \"" + formatPath(databaseConfigLoader.getFile()) + "\"", e); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramDatabase.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramDatabase.java deleted file mode 100644 index 7f8f092f..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramDatabase.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.config; - -import me.filoghost.fcommons.config.Config; -import me.filoghost.fcommons.config.ConfigPath; -import me.filoghost.fcommons.config.ConfigSection; -import me.filoghost.fcommons.config.ConfigType; -import me.filoghost.fcommons.logging.ErrorCollector; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramManager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; - -public class HologramDatabase { - - private static List hologramConfigs; - - public void loadFromConfig(Config config) { - hologramConfigs = new ArrayList<>(); - - for (Entry entry : config.toMap(ConfigType.SECTION).entrySet()) { - String hologramName = entry.getKey().asRawKey(); - ConfigSection hologramSection = entry.getValue(); - - HologramConfig hologramConfig = new HologramConfig(hologramName, hologramSection); - hologramConfigs.add(hologramConfig); - } - } - - public void createHolograms(InternalHologramManager internalHologramManager, ErrorCollector errorCollector) { - for (HologramConfig hologramConfig : hologramConfigs) { - try { - hologramConfig.createHologram(internalHologramManager); - } catch (HologramLoadException e) { - errorCollector.add(e, "error while loading hologram \"" + hologramConfig.getName() + "\""); - } catch (Exception e) { - errorCollector.add(e, "unexpected exception while loading hologram \"" + hologramConfig.getName() + "\""); - } - } - } - - public static Config exportToConfig(InternalHologramManager hologramManager) { - Config config = new Config(); - - for (InternalHologram hologram : hologramManager.getHolograms()) { - config.setConfigSection(hologram.getName(), new HologramConfig(hologram).toConfigSection()); - } - - config.setHeader( - "", - "Please do NOT edit this file manually if possible.", - "" - ); - - return config; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramConfig.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramConfig.java similarity index 50% rename from plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramConfig.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramConfig.java index da070062..38774b20 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramConfig.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramConfig.java @@ -5,75 +5,85 @@ */ package me.filoghost.holographicdisplays.plugin.config; +import me.filoghost.fcommons.collection.CollectionUtils; import me.filoghost.fcommons.config.ConfigSection; import me.filoghost.fcommons.config.exception.ConfigValueException; +import me.filoghost.holographicdisplays.api.beta.Position; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramManager; import java.util.ArrayList; import java.util.List; -public class HologramConfig { +public class InternalHologramConfig { private final String name; - private final List serializedLines; - private final ConfigSection positionConfigSection; + private final ConfigSection configSection; - public HologramConfig(String name, ConfigSection configSection) { + public InternalHologramConfig(String name, ConfigSection configSection) { this.name = name; - this.serializedLines = configSection.getStringList("lines"); - this.positionConfigSection = configSection.getConfigSection("position"); + this.configSection = configSection; } - public HologramConfig(InternalHologram hologram) { + public InternalHologramConfig(InternalHologram hologram) { this.name = hologram.getName(); - this.serializedLines = new ArrayList<>(); - for (InternalHologramLine line : hologram.getLines()) { - serializedLines.add(line.getSerializedConfigValue()); + this.configSection = new ConfigSection(); + + List serializedLines = serializeLines(hologram); + ConfigSection positionConfigSection = serializePosition(hologram.getPosition()); + + configSection.setStringList("lines", serializedLines); + configSection.setConfigSection("position", positionConfigSection); + } + + public String getName() { + return name; + } + + public ConfigSection getSerializedConfigSection() { + return configSection; + } + + private List serializeLines(InternalHologram hologram) { + return CollectionUtils.toArrayList(hologram.getLines(), InternalHologramLine::getSerializedString); + } + + public List deserializeLines() throws InternalHologramLoadException { + List serializedLines = configSection.getStringList("lines"); + + if (serializedLines == null || serializedLines.size() == 0) { + throw new InternalHologramLoadException("at least one line is required"); } - ImmutablePosition position = hologram.getPosition(); - this.positionConfigSection = new ConfigSection(); + List lines = new ArrayList<>(); + for (String serializedLine : serializedLines) { + try { + lines.add(InternalHologramLineParser.parseLine(serializedLine)); + } catch (InternalHologramLoadException e) { + // Rethrow with more details + throw new InternalHologramLoadException("invalid line: " + e.getMessage(), e); + } + } + return lines; + } + + private ConfigSection serializePosition(Position position) { + ConfigSection positionConfigSection = new ConfigSection(); positionConfigSection.setString("world", position.getWorldName()); positionConfigSection.setDouble("x", position.getX()); positionConfigSection.setDouble("y", position.getY()); positionConfigSection.setDouble("z", position.getZ()); + return positionConfigSection; } - public ConfigSection toConfigSection() { - ConfigSection configSection = new ConfigSection(); - configSection.setStringList("lines", serializedLines); - configSection.setConfigSection("position", positionConfigSection); - return configSection; - } + public ImmutablePosition deserializePosition() throws InternalHologramLoadException { + ConfigSection positionConfigSection = configSection.getConfigSection("position"); - public void createHologram(InternalHologramManager internalHologramManager) throws HologramLoadException { - if (serializedLines == null || serializedLines.size() == 0) { - throw new HologramLoadException("at least one line is required"); - } if (positionConfigSection == null) { - throw new HologramLoadException("no position set"); + throw new InternalHologramLoadException("no position set"); } - ImmutablePosition position = parsePosition(); - InternalHologram hologram = internalHologramManager.createHologram(name, position); - List lines = new ArrayList<>(); - - for (String serializedLine : serializedLines) { - try { - lines.add(HologramLineParser.parseLine(hologram, serializedLine)); - } catch (HologramLoadException e) { - // Rethrow with more details - throw new HologramLoadException("invalid line: " + e.getMessage(), e); - } - } - - hologram.getLines().addAll(lines); - } - - private ImmutablePosition parsePosition() throws HologramLoadException { try { String worldName = positionConfigSection.getRequiredString("world"); double x = positionConfigSection.getRequiredDouble("x"); @@ -81,12 +91,8 @@ public class HologramConfig { double z = positionConfigSection.getRequiredDouble("z"); return new ImmutablePosition(worldName, x, y, z); } catch (ConfigValueException e) { - throw new HologramLoadException("invalid position attribute \"" + e.getConfigPath() + "\"", e); + throw new InternalHologramLoadException("invalid position attribute \"" + e.getConfigPath() + "\"", e); } } - public String getName() { - return name; - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramLineParser.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramLineParser.java similarity index 78% rename from plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramLineParser.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramLineParser.java index c8027430..344843b9 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramLineParser.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramLineParser.java @@ -9,8 +9,9 @@ import me.filoghost.fcommons.Colors; import me.filoghost.fcommons.MaterialsHelper; import me.filoghost.fcommons.Strings; import me.filoghost.holographicdisplays.plugin.format.DisplayFormat; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologram; import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramLine; +import me.filoghost.holographicdisplays.plugin.internal.hologram.ItemInternalHologramLine; +import me.filoghost.holographicdisplays.plugin.internal.hologram.TextInternalHologramLine; import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParseException; import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParser; import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders; @@ -20,31 +21,26 @@ import org.bukkit.inventory.ItemStack; import java.util.Locale; -public class HologramLineParser { +public class InternalHologramLineParser { private static final String ICON_PREFIX = "icon:"; - public static InternalHologramLine parseLine(InternalHologram hologram, String serializedLine) throws HologramLoadException { - InternalHologramLine hologramLine; - + public static InternalHologramLine parseLine(String serializedLine) throws InternalHologramLoadException { if (serializedLine.toLowerCase(Locale.ROOT).startsWith(ICON_PREFIX)) { String serializedIcon = serializedLine.substring(ICON_PREFIX.length()); ItemStack icon = parseItemStack(serializedIcon); - hologramLine = hologram.createItemLine(icon, serializedLine); + return new ItemInternalHologramLine(serializedLine, icon); } else { String displayText = DisplayFormat.apply(serializedLine, false); // Apply colors only outside placeholders displayText = StringWithPlaceholders.withEscapes(displayText).replaceStrings(Colors::colorize); - hologramLine = hologram.createTextLine(displayText, serializedLine); + return new TextInternalHologramLine(serializedLine, displayText); } - - return hologramLine; } - @SuppressWarnings("deprecation") - private static ItemStack parseItemStack(String serializedItem) throws HologramLoadException { + private static ItemStack parseItemStack(String serializedItem) throws InternalHologramLoadException { serializedItem = serializedItem.trim(); // Parse json @@ -71,7 +67,7 @@ public class HologramLineParser { try { dataValue = (short) Integer.parseInt(materialAndDataValue[1]); } catch (NumberFormatException e) { - throw new HologramLoadException("data value \"" + materialAndDataValue[1] + "\" is not a valid number"); + throw new InternalHologramLoadException("data value \"" + materialAndDataValue[1] + "\" is not a valid number"); } materialName = materialAndDataValue[0]; } else { @@ -80,7 +76,7 @@ public class HologramLineParser { Material material = MaterialsHelper.matchMaterial(materialName); if (material == null) { - throw new HologramLoadException("\"" + materialName + "\" is not a valid material"); + throw new InternalHologramLoadException("\"" + materialName + "\" is not a valid material"); } ItemStack itemStack = new ItemStack(material, 1, dataValue); @@ -91,9 +87,9 @@ public class HologramLineParser { MojangsonParser.parse(nbtString); Bukkit.getUnsafe().modifyItemStack(itemStack, nbtString); } catch (MojangsonParseException e) { - throw new HologramLoadException("invalid NBT data, " + e.getMessage()); + throw new InternalHologramLoadException("invalid NBT data, " + e.getMessage()); } catch (Exception e) { - throw new HologramLoadException("unexpected exception while parsing NBT data", e); + throw new InternalHologramLoadException("unexpected exception while parsing NBT data", e); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramLoadException.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramLoadException.java similarity index 53% rename from plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramLoadException.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramLoadException.java index 75699ba2..e73eb451 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramLoadException.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/InternalHologramLoadException.java @@ -5,13 +5,13 @@ */ package me.filoghost.holographicdisplays.plugin.config; -public class HologramLoadException extends Exception { +public class InternalHologramLoadException extends Exception { - public HologramLoadException(String message) { + public InternalHologramLoadException(String message) { super(message); } - public HologramLoadException(String message, Throwable cause) { + public InternalHologramLoadException(String message, Throwable cause) { super(message, cause); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologram.java index ee4ea5a7..78b275a1 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologram.java @@ -5,66 +5,118 @@ */ package me.filoghost.holographicdisplays.plugin.internal.hologram; -import me.filoghost.holographicdisplays.plugin.HolographicDisplays; -import me.filoghost.holographicdisplays.plugin.api.current.DefaultVisibilitySettings; -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram; -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLines; +import me.filoghost.holographicdisplays.api.beta.hologram.Hologram; +import me.filoghost.holographicdisplays.api.beta.hologram.PlaceholderSetting; +import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent; +import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; -import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; +import org.bukkit.Bukkit; -public class InternalHologram extends BaseHologram { +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; - private final BaseHologramLines lines; +public class InternalHologram { + + private final Hologram renderedHologram; private final String name; - private final DefaultVisibilitySettings visibilitySettings; + private ImmutablePosition position; + private final List lines; + private final List unmodifiableLinesView; + private boolean deleted; - protected InternalHologram(ImmutablePosition position, String name, LineTrackerManager lineTrackerManager) { - super(position, lineTrackerManager); - this.lines = new BaseHologramLines<>(this); + public InternalHologram(Function hologramFactory, String name, ImmutablePosition position) { + this.renderedHologram = hologramFactory.apply(position); + this.renderedHologram.setPlaceholderSetting(PlaceholderSetting.ENABLE_ALL); this.name = name; - this.visibilitySettings = new DefaultVisibilitySettings(); + this.position = position; + this.lines = new ArrayList<>(); + this.unmodifiableLinesView = Collections.unmodifiableList(lines); } - @Override - public BaseHologramLines getLines() { - return lines; - } - - public InternalTextHologramLine createTextLine(String text, String serializedConfigValue) { - return new InternalTextHologramLine(this, text, serializedConfigValue); - } - - public InternalItemHologramLine createItemLine(ItemStack icon, String serializedConfigValue) { - return new InternalItemHologramLine(this, icon, serializedConfigValue); + public Hologram getRenderedHologram() { + return renderedHologram; } public String getName() { return name; } - public DefaultVisibilitySettings getVisibilitySettings() { - return visibilitySettings; + public ImmutablePosition getPosition() { + return position; } - @Override - public Plugin getCreatorPlugin() { - return HolographicDisplays.getInstance(); + public void setPosition(ImmutablePosition position) { + checkNotDeleted(); + this.position = position; + updateRendering(); } - @Override - public boolean isVisibleTo(Player player) { - return visibilitySettings.isVisibleTo(player); + public List getLines() { + return unmodifiableLinesView; } - @Override - public String toString() { - return "InternalHologram{" - + "name=" + name - + ", super=" + super.toString() - + "}"; + public void addLine(InternalHologramLine line) { + checkNotDeleted(); + lines.add(line); + updateRendering(); + } + + public void addLines(List lines) { + checkNotDeleted(); + this.lines.addAll(lines); + updateRendering(); + } + + public void setLine(int index, InternalHologramLine line) { + checkNotDeleted(); + lines.set(index, line); + updateRendering(); + } + + public void setLines(List lines) { + checkNotDeleted(); + this.lines.clear(); + this.lines.addAll(lines); + updateRendering(); + } + + public void insertLine(int beforeIndex, InternalHologramLine line) { + checkNotDeleted(); + lines.add(beforeIndex, line); + updateRendering(); + } + + public void removeLine(int index) { + checkNotDeleted(); + lines.remove(index); + updateRendering(); + } + + void delete() { + if (!deleted) { + deleted = true; + renderedHologram.delete(); + } + } + + private void updateRendering() { + renderedHologram.setPosition(position); + renderedHologram.getLines().clear(); + for (InternalHologramLine serializedLine : lines) { + serializedLine.appendTo(renderedHologram); + } + } + + private void checkNotDeleted() { + if (deleted) { + throw new IllegalStateException("already deleted"); + } + } + + public void callChangeEvent(ChangeType changeType) { + Bukkit.getPluginManager().callEvent(new InternalHologramChangeEvent(this, changeType)); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramLine.java index 282f3b48..9390add3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramLine.java @@ -5,10 +5,20 @@ */ package me.filoghost.holographicdisplays.plugin.internal.hologram; -import me.filoghost.holographicdisplays.plugin.hologram.base.EditableHologramLine; +import me.filoghost.holographicdisplays.api.beta.hologram.Hologram; -public interface InternalHologramLine extends EditableHologramLine { +public abstract class InternalHologramLine { - String getSerializedConfigValue(); + private final String serializedString; + + public InternalHologramLine(String serializedString) { + this.serializedString = serializedString; + } + + public String getSerializedString() { + return serializedString; + } + + public abstract void appendTo(Hologram hologram); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramManager.java index c901c5b7..8b9ded78 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalHologramManager.java @@ -5,30 +5,27 @@ */ package me.filoghost.holographicdisplays.plugin.internal.hologram; -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramManager; +import me.filoghost.holographicdisplays.api.beta.hologram.Hologram; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; -import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; import org.jetbrains.annotations.Nullable; -public class InternalHologramManager extends BaseHologramManager { +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; - private final LineTrackerManager lineTrackerManager; +public class InternalHologramManager { - public InternalHologramManager(LineTrackerManager lineTrackerManager) { - this.lineTrackerManager = lineTrackerManager; - } + private final Function hologramFactory; + private final List holograms; - public InternalHologram createHologram(String name, ImmutablePosition position) { - if (getHologramByName(name) != null) { - throw new IllegalStateException("hologram named \"" + name + "\" already exists"); - } - InternalHologram hologram = new InternalHologram(position, name, lineTrackerManager); - super.addHologram(hologram); - return hologram; + public InternalHologramManager(Function hologramFactory) { + this.hologramFactory = hologramFactory; + this.holograms = new ArrayList<>(); } public @Nullable InternalHologram getHologramByName(String name) { - for (InternalHologram hologram : getHolograms()) { + for (InternalHologram hologram : holograms) { if (hologram.getName().equalsIgnoreCase(name)) { return hologram; } @@ -36,4 +33,29 @@ public class InternalHologramManager extends BaseHologramManager getHolograms() { + return Collections.unmodifiableList(holograms); + } + + public void deleteHologram(InternalHologram hologram) { + holograms.remove(hologram); + hologram.delete(); + } + + public void deleteHolograms() { + for (InternalHologram hologram : holograms) { + hologram.delete(); + } + holograms.clear(); + } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalItemHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalItemHologramLine.java deleted file mode 100644 index f977b7d6..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalItemHologramLine.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.internal.hologram; - -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemHologramLine; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class InternalItemHologramLine extends BaseItemHologramLine implements InternalHologramLine { - - private final String serializedConfigValue; - - protected InternalItemHologramLine(InternalHologram hologram, ItemStack itemStack, String serializedConfigValue) { - super(hologram, itemStack); - this.serializedConfigValue = serializedConfigValue; - } - - @Override - public String getSerializedConfigValue() { - return serializedConfigValue; - } - - @Override - public boolean hasClickCallback() { - return false; - } - - @Override - public void invokeClickCallback(Player player) {} - - @Override - public boolean hasPickupCallback() { - return false; - } - - @Override - public void invokePickupCallback(Player player) {} - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalTextHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalTextHologramLine.java deleted file mode 100644 index 4e4f9d0b..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/InternalTextHologramLine.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.internal.hologram; - -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLine; -import org.bukkit.entity.Player; - -public class InternalTextHologramLine extends BaseTextHologramLine implements InternalHologramLine { - - private final String serializedConfigValue; - - protected InternalTextHologramLine(InternalHologram hologram, String text, String serializedConfigValue) { - super(hologram, text); - this.serializedConfigValue = serializedConfigValue; - } - - @Override - public boolean isAllowPlaceholders() { - return true; - } - - @Override - public String getSerializedConfigValue() { - return serializedConfigValue; - } - - @Override - public boolean hasClickCallback() { - return false; - } - - @Override - public void invokeClickCallback(Player player) {} - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/ItemInternalHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/ItemInternalHologramLine.java new file mode 100644 index 00000000..2f1e568d --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/ItemInternalHologramLine.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.internal.hologram; + +import me.filoghost.holographicdisplays.api.beta.hologram.Hologram; +import org.bukkit.inventory.ItemStack; + +public class ItemInternalHologramLine extends InternalHologramLine { + + private final ItemStack itemStack; + + public ItemInternalHologramLine(String serializedString, ItemStack itemStack) { + super(serializedString); + this.itemStack = itemStack; + } + + @Override + public void appendTo(Hologram hologram) { + hologram.getLines().appendItem(itemStack); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/TextInternalHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/TextInternalHologramLine.java new file mode 100644 index 00000000..54e2d00c --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/internal/hologram/TextInternalHologramLine.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.internal.hologram; + +import me.filoghost.holographicdisplays.api.beta.hologram.Hologram; + +public class TextInternalHologramLine extends InternalHologramLine { + + private final String text; + + public TextInternalHologramLine(String serializedString, String text) { + super(serializedString); + this.text = text; + } + + @Override + public void appendTo(Hologram hologram) { + hologram.getLines().appendText(text); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java index 1d53cc3d..59bcb117 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java @@ -7,7 +7,6 @@ package me.filoghost.holographicdisplays.plugin.listener; import me.filoghost.holographicdisplays.plugin.api.current.APIHologramManager; import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramManager; -import me.filoghost.holographicdisplays.plugin.internal.hologram.InternalHologramManager; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.World; @@ -23,17 +22,11 @@ import org.bukkit.plugin.Plugin; public class ChunkListener implements Listener { private final Plugin plugin; - private final InternalHologramManager internalHologramManager; private final APIHologramManager apiHologramManager; private final V2HologramManager v2HologramManager; - public ChunkListener( - Plugin plugin, - InternalHologramManager internalHologramManager, - APIHologramManager apiHologramManager, - V2HologramManager v2HologramManager) { + public ChunkListener(Plugin plugin, APIHologramManager apiHologramManager, V2HologramManager v2HologramManager) { this.plugin = plugin; - this.internalHologramManager = internalHologramManager; this.apiHologramManager = apiHologramManager; this.v2HologramManager = v2HologramManager; } @@ -41,7 +34,6 @@ public class ChunkListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onWorldUnload(WorldUnloadEvent event) { World world = event.getWorld(); - internalHologramManager.onWorldUnload(world); apiHologramManager.onWorldUnload(world); v2HologramManager.onWorldUnload(world); } @@ -49,7 +41,6 @@ public class ChunkListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onWorldLoad(WorldLoadEvent event) { World world = event.getWorld(); - internalHologramManager.onWorldLoad(world); apiHologramManager.onWorldLoad(world); v2HologramManager.onWorldLoad(world); } @@ -57,7 +48,6 @@ public class ChunkListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onChunkUnload(ChunkUnloadEvent event) { Chunk chunk = event.getChunk(); - internalHologramManager.onChunkUnload(chunk); apiHologramManager.onChunkUnload(chunk); v2HologramManager.onChunkUnload(chunk); } @@ -80,7 +70,6 @@ public class ChunkListener implements Listener { } private void onChunkLoad(Chunk chunk) { - internalHologramManager.onChunkLoad(chunk); apiHologramManager.onChunkLoad(chunk); v2HologramManager.onChunkLoad(chunk); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/log/PrintableErrorCollector.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/log/PrintableErrorCollector.java index 52dad8d6..2ec442b0 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/log/PrintableErrorCollector.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/log/PrintableErrorCollector.java @@ -11,7 +11,7 @@ import me.filoghost.fcommons.config.exception.ConfigException; import me.filoghost.fcommons.config.exception.ConfigSyntaxException; import me.filoghost.fcommons.logging.ErrorCollector; import me.filoghost.fcommons.logging.ErrorLog; -import me.filoghost.holographicdisplays.plugin.config.HologramLoadException; +import me.filoghost.holographicdisplays.plugin.config.InternalHologramLoadException; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -58,7 +58,7 @@ public class PrintableErrorCollector extends ErrorCollector { details = ((ConfigSyntaxException) exception).getSyntaxErrorDetails(); break; - } else if (exception instanceof ConfigException || exception instanceof HologramLoadException) { + } else if (exception instanceof ConfigException || exception instanceof InternalHologramLoadException) { // Known exceptions, add the message and inspect the cause messageParts.add(exception.getMessage()); exception = exception.getCause();