diff --git a/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/Hologram.java b/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/Hologram.java index f8becacf..f0d16e9d 100644 --- a/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/Hologram.java +++ b/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/Hologram.java @@ -6,119 +6,29 @@ package me.filoghost.holographicdisplays.api.hologram; import me.filoghost.holographicdisplays.api.HolographicDisplaysAPI; -import me.filoghost.holographicdisplays.api.hologram.line.HologramLine; -import me.filoghost.holographicdisplays.api.hologram.line.ItemHologramLine; -import me.filoghost.holographicdisplays.api.hologram.line.TextHologramLine; import org.bukkit.Location; import org.bukkit.World; -import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; /** - * An object made of various lines, that can be items or holograms. - * Holographic lines appear as a nametag without any entity below. - * To create one, please see {@link HolographicDisplaysAPI#createHologram(Location)}. + * Entity to manage a group of vertically aligned lines, which display floating text and items. + * To create one see {@link HolographicDisplaysAPI}. * * @since 1 */ public interface Hologram { /** - * Appends a text line to end of this hologram. - * - * @param text the content of the line, can be null for an empty line - * @return the new TextLine appended - * @since 1 - */ - @NotNull TextHologramLine appendTextLine(@Nullable String text); - - /** - * Appends an item line to end of this hologram. - * - * @param itemStack the content of the line - * @return the new ItemLine appended - * @since 1 - */ - @NotNull ItemHologramLine appendItemLine(@NotNull ItemStack itemStack); - - /** - * Inserts a text line in this hologram. - * - * @param index the line is inserted before this index. If 0, the new line will be inserted before the first line. - * @param text the content of the line, can be null for an empty line - * @return the new TextLine inserted - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - * @since 1 - */ - @NotNull TextHologramLine insertTextLine(int index, @Nullable String text); - - /** - * Inserts an item line in this hologram. - * - * @param index the line is inserted before this index. If 0, the new line will be inserted before the first line. - * @param itemStack the content of the line - * @return the new ItemLine inserted - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - * @since 1 - */ - @NotNull ItemHologramLine insertItemLine(int index, @NotNull ItemStack itemStack); - - /** - * Finds the element at a given index in the lines. - * - * @param index the index of the line to retrieve. - * @return the hologram line at the given index, can be an {@link ItemHologramLine} or a {@link TextHologramLine}. - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - * @since 1 - */ - @NotNull HologramLine getLine(int index); - - /** - * Removes a line at a given index. - * - * @param index the index of the line, that should be between 0 and size() - 1. - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - * @since 1 - */ - void removeLine(int index); - - /** - * Removes a line. - * - * @param line the line to be removed. - * @return if the hologram contained the line - * @since 1 - */ - boolean removeLine(@NotNull HologramLine line); - - /** - * Removes all the lines from this hologram. + * Returns the editable list of lines. * * @since 1 */ - void clearLines(); + @NotNull HologramLines lines(); /** - * Checks the amount of lines of the hologram. + * Returns the current position. * - * @return the amount of lines - * @since 1 - */ - int getLineCount(); - - /** - * The physical height of the hologram, counting all the lines. - * - * @return the height of the hologram, counting all the lines and the gaps between them - * @since 1 - */ - double getHeight(); - - /** - * Returns the hologram position. - * - * @return the hologram position + * @return the current position * @since 1 */ @NotNull HologramPosition getPosition(); diff --git a/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/HologramLines.java b/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/HologramLines.java new file mode 100644 index 00000000..237ca8a8 --- /dev/null +++ b/api/src/main/java/me/filoghost/holographicdisplays/api/hologram/HologramLines.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.api.hologram; + +import me.filoghost.holographicdisplays.api.hologram.line.HologramLine; +import me.filoghost.holographicdisplays.api.hologram.line.ItemHologramLine; +import me.filoghost.holographicdisplays.api.hologram.line.TextHologramLine; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * The editable list of lines of a hologram. + * + * @since 1 + */ +public interface HologramLines { + + /** + * Adds a new text line at the end. + * + * @param text the content of the line, see {@link TextHologramLine#setText(String)} + * @return the created line + * @since 1 + */ + @NotNull TextHologramLine appendText(@Nullable String text); + + /** + * Adds a new item line at the end. + * + * @param itemStack the content of the line, see {@link ItemHologramLine#setItemStack(ItemStack)} + * @return the created line + * @since 1 + */ + @NotNull ItemHologramLine appendItem(@NotNull ItemStack itemStack); + + /** + * Inserts a new text line before the given index. + * + * @param beforeIndex the index before which the line is inserted, 0 to insert as first + * @param text the content of the line, see {@link TextHologramLine#setText(String)} + * @return the created line + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + * @since 1 + */ + @NotNull TextHologramLine insertText(int beforeIndex, @Nullable String text); + + /** + * Inserts a new item line before the given index. + * + * @param beforeIndex the index before which the line is inserted, 0 to insert as first + * @param itemStack the content of the line, see {@link ItemHologramLine#setItemStack(ItemStack)} + * @return the created line + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + * @since 1 + */ + @NotNull ItemHologramLine insertItem(int beforeIndex, @NotNull ItemStack itemStack); + + /** + * Returns the line at the given index. + * + * @param index the index of the line to retrieve + * @return the line at the given index + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + * @since 1 + */ + @NotNull HologramLine get(int index); + + /** + * Removes the line at the given index. + * + * @param index the index of the line to remove + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + * @since 1 + */ + void remove(int index); + + /** + * Removes a line. + * + * @param line the line to be removed + * @return if the line was found and removed + * @since 1 + */ + boolean remove(@NotNull HologramLine line); + + /** + * Removes all the lines. + * + * @since 1 + */ + void clear(); + + /** + * Returns the amount of lines. + * + * @return the amount of lines + * @since 1 + */ + int size(); + + /** + * The total height of the lines, including the gaps between them. + * + * @return the total height of the lines + * @since 1 + */ + double getHeight(); + +} diff --git a/example/death-holograms/src/main/java/me/filoghost/example/deathholograms/DeathHolograms.java b/example/death-holograms/src/main/java/me/filoghost/example/deathholograms/DeathHolograms.java index 73c9a2ce..82416b47 100644 --- a/example/death-holograms/src/main/java/me/filoghost/example/deathholograms/DeathHolograms.java +++ b/example/death-holograms/src/main/java/me/filoghost/example/deathholograms/DeathHolograms.java @@ -41,8 +41,8 @@ public class DeathHolograms extends JavaPlugin implements Listener { public void onPlayerDeath(PlayerDeathEvent event) { Hologram hologram = holographicDisplaysAPI.createHologram(event.getEntity().getEyeLocation()); - hologram.appendTextLine(ChatColor.RED + "Player " + ChatColor.GOLD + event.getEntity().getName() + ChatColor.RED + " died here!"); - hologram.appendTextLine(ChatColor.GRAY + "Time of death: " + TIME_FORMATTER.format(Instant.now())); + hologram.lines().appendText(ChatColor.RED + "Player " + ChatColor.GOLD + event.getEntity().getName() + ChatColor.RED + " died here!"); + hologram.lines().appendText(ChatColor.GRAY + "Time of death: " + TIME_FORMATTER.format(Instant.now())); } } diff --git a/example/power-ups/src/main/java/me/filoghost/example/powerups/PowerUps.java b/example/power-ups/src/main/java/me/filoghost/example/powerups/PowerUps.java index 07679fcf..14b07912 100644 --- a/example/power-ups/src/main/java/me/filoghost/example/powerups/PowerUps.java +++ b/example/power-ups/src/main/java/me/filoghost/example/powerups/PowerUps.java @@ -49,10 +49,10 @@ public class PowerUps extends JavaPlugin implements Listener { // Spawn the floating item with a label Hologram hologram = holographicDisplaysAPI.createHologram(event.getEntity().getLocation().add(0.0, 0.9, 0.0)); - hologram.appendTextLine(ChatColor.AQUA + "" + ChatColor.BOLD + "Speed PowerUp"); - ItemHologramLine icon = hologram.appendItemLine(new ItemStack(Material.SUGAR)); + hologram.lines().appendText(ChatColor.AQUA + "" + ChatColor.BOLD + "Speed PowerUp"); + ItemHologramLine itemLine = hologram.lines().appendItem(new ItemStack(Material.SUGAR)); - icon.setPickupListener((Player player) -> { + itemLine.setPickupListener((Player player) -> { // Play an effect player.playEffect(hologram.getPosition().toLocation(), Effect.MOBSPAWNER_FLAMES, null); 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 5a7e5df6..d81cdea1 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java @@ -177,10 +177,10 @@ public class HolographicDisplays extends FCommonsPlugin { hologramDatabase.createHolograms(internalHologramManager, errorCollector); for (APIHologram hologram : apiHologramManager.getHolograms()) { - hologram.getLines().updateLinePositions(); + hologram.lines().updatePositions(); } for (V2Hologram hologram : v2HologramManager.getHolograms()) { - hologram.getLines().updateLinePositions(); + hologram.lines().updatePositions(); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java index d2fd48f9..853e0554 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java @@ -44,7 +44,7 @@ public class V2Hologram extends BaseHologram implements Hologram { } @Override - public BaseHologramLines getLines() { + public BaseHologramLines lines() { return lines; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java index 36366f3d..b1b02582 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java @@ -15,7 +15,7 @@ public interface V2HologramLine extends HologramLine, EditableHologramLine { @Override default void removeLine() { - getParent().getLines().remove(this); + getParent().lines().remove(this); } } 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 60da84c8..5c3899b7 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 @@ -38,7 +38,7 @@ public class AddLineCommand extends LineEditingCommand implements QuickEditComma InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); - hologram.getLines().add(line); + hologram.lines().add(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 1d8aebf9..4f8cd1c1 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 @@ -37,11 +37,11 @@ public class CopyCommand extends HologramSubCommand { InternalHologram toHologram = hologramEditor.getExistingHologram(args[1]); List clonedLines = new ArrayList<>(); - for (InternalHologramLine line : fromHologram.getLines()) { + for (InternalHologramLine line : fromHologram.lines()) { clonedLines.add(hologramEditor.parseHologramLine(toHologram, line.getSerializedConfigValue())); } - toHologram.getLines().setAll(clonedLines); + toHologram.lines().setAll(clonedLines); 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 2f102d07..31089b62 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 @@ -67,7 +67,7 @@ public class CreateCommand extends HologramSubCommand { line = hologram.createTextLine(defaultText, defaultText.replace(ChatColor.COLOR_CHAR, '&')); } - hologram.getLines().add(line); + hologram.lines().add(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 fb0ae0f2..57ea8f1e 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 @@ -37,7 +37,7 @@ public class InfoCommand extends LineEditingCommand implements QuickEditCommand DisplayFormat.sendTitle(sender, "Lines of the hologram \"" + hologram.getName() + "\""); int index = 0; - for (InternalHologramLine line : hologram.getLines()) { + for (InternalHologramLine line : hologram.lines()) { index++; sender.sendMessage(ColorScheme.SECONDARY_BOLD + index + ColorScheme.SECONDARY_DARK + ". " + ColorScheme.SECONDARY + line.getSerializedConfigValue()); 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 d4af2a94..739be29b 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 @@ -41,14 +41,14 @@ public class InsertLineCommand extends LineEditingCommand implements QuickEditCo int insertAfterIndex = CommandValidate.parseInteger(args[1]); String serializedLine = Strings.joinFrom(" ", args, 2); - int oldLinesAmount = hologram.getLines().size(); + int oldLinesAmount = hologram.lines().size(); CommandValidate.check(insertAfterIndex >= 0 && insertAfterIndex <= oldLinesAmount, "The line number must be between 0 and " + oldLinesAmount + "."); InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); - hologram.getLines().insert(insertAfterIndex, line); + hologram.lines().insert(insertAfterIndex, line); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); if (insertAfterIndex == 0) { 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 c6e21357..1abc5448 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 @@ -114,9 +114,9 @@ public class ReadImageCommand extends LineEditingCommand { } if (!append) { - hologram.getLines().clear(); + hologram.lines().clear(); } - hologram.getLines().addAll(newLines); + hologram.lines().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 ca3561e3..396d1a36 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 @@ -85,7 +85,7 @@ public class ReadTextCommand extends LineEditingCommand { } } - hologram.getLines().setAll(newLines); + hologram.lines().setAll(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 d7fc5df5..3f984d73 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 @@ -35,7 +35,7 @@ public class RemoveLineCommand extends LineEditingCommand implements QuickEditCo InternalHologram hologram = hologramEditor.getExistingHologram(args[0]); int lineNumber = CommandValidate.parseInteger(args[1]); - int linesAmount = hologram.getLines().size(); + int linesAmount = hologram.lines().size(); CommandValidate.check(lineNumber >= 1 && lineNumber <= linesAmount, "The line number must be between 1 and " + linesAmount + "."); @@ -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.lines().remove(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 c2899b2d..81b9de7b 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 @@ -38,7 +38,7 @@ public class SetLineCommand extends LineEditingCommand implements QuickEditComma String serializedLine = Strings.joinFrom(" ", args, 2); int lineNumber = CommandValidate.parseInteger(args[1]); - int linesAmount = hologram.getLines().size(); + int linesAmount = hologram.lines().size(); CommandValidate.check(lineNumber >= 1 && lineNumber <= linesAmount, "The line number must be between 1 and " + linesAmount + "."); @@ -46,7 +46,7 @@ public class SetLineCommand extends LineEditingCommand implements QuickEditComma InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); - hologram.getLines().set(index, line); + hologram.lines().set(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/HologramConfig.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramConfig.java index 3605f60e..3fcbce2b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramConfig.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/HologramConfig.java @@ -30,7 +30,7 @@ public class HologramConfig { public HologramConfig(InternalHologram hologram) { this.name = hologram.getName(); this.serializedLines = new ArrayList<>(); - for (InternalHologramLine line : hologram.getLines()) { + for (InternalHologramLine line : hologram.lines()) { serializedLines.add(line.getSerializedConfigValue()); } @@ -70,7 +70,7 @@ public class HologramConfig { } } - hologram.getLines().addAll(lines); + hologram.lines().addAll(lines); } private BaseHologramPosition parsePosition() throws HologramLoadException { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/format/DisplayFormat.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/format/DisplayFormat.java index ffc30b26..29bd3861 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/format/DisplayFormat.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/format/DisplayFormat.java @@ -89,7 +89,7 @@ public class DisplayFormat { public static void sendHologramSummary(CommandSender sender, InternalHologram hologram, boolean showWorld) { BaseHologramPosition position = hologram.getPosition(); sender.sendMessage(ColorScheme.SECONDARY_DARK + "- " + ColorScheme.SECONDARY_BOLD + hologram.getName() - + ColorScheme.SECONDARY_DARK + " (" + hologram.getLines().size() + " lines) at " + + ColorScheme.SECONDARY_DARK + " (" + hologram.lines().size() + " lines) at " + (showWorld ? "world: \"" + position.getWorldName() + "\", " : "") + "x: " + position.getBlockX() + ", " + "y: " + position.getBlockY() + ", " diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java index 9c698967..c5d0998b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java @@ -7,20 +7,16 @@ package me.filoghost.holographicdisplays.plugin.hologram.api; import me.filoghost.fcommons.Preconditions; import me.filoghost.holographicdisplays.api.hologram.Hologram; -import me.filoghost.holographicdisplays.api.hologram.line.HologramLine; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram; -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLines; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; 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.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; public class APIHologram extends BaseHologram implements Hologram { - private final BaseHologramLines lines; + private final APIHologramLines lines; private final Plugin plugin; private final APIHologramManager apiHologramManager; private final DefaultVisibilitySettings visibilitySettings; @@ -34,14 +30,14 @@ public class APIHologram extends BaseHologram implements Hologram { LineTrackerManager lineTrackerManager) { super(position, lineTrackerManager); Preconditions.notNull(plugin, "plugin"); - this.lines = new BaseHologramLines<>(this); + this.lines = new APIHologramLines(this); this.plugin = plugin; this.apiHologramManager = apiHologramManager; this.visibilitySettings = new DefaultVisibilitySettings(); } @Override - public BaseHologramLines getLines() { + public @NotNull APIHologramLines lines() { return lines; } @@ -50,79 +46,6 @@ public class APIHologram extends BaseHologram implements Hologram { return plugin; } - @Override - public @NotNull APIHologramLine getLine(int index) { - return lines.get(index); - } - - @Override - public @NotNull APITextLine appendTextLine(@Nullable String text) { - checkNotDeleted(); - - APITextLine line = new APITextLine(this, text); - lines.add(line); - return line; - } - - @Override - public @NotNull APIItemLine appendItemLine(@NotNull ItemStack itemStack) { - Preconditions.notNull(itemStack, "itemStack"); - checkNotDeleted(); - - APIItemLine line = new APIItemLine(this, itemStack); - lines.add(line); - return line; - } - - @Override - public @NotNull APITextLine insertTextLine(int index, @Nullable String text) { - checkNotDeleted(); - - APITextLine line = new APITextLine(this, text); - lines.add(line); - return line; - } - - @Override - public @NotNull APIItemLine insertItemLine(int index, @NotNull ItemStack itemStack) { - Preconditions.notNull(itemStack, "itemStack"); - checkNotDeleted(); - - APIItemLine line = new APIItemLine(this, itemStack); - lines.add(line); - return line; - } - - @Override - public void removeLine(int index) { - checkNotDeleted(); - - lines.remove(index); - } - - @Override - public boolean removeLine(@NotNull HologramLine line) { - checkNotDeleted(); - - if (line instanceof APIHologramLine) { - return lines.remove((APIHologramLine) line); - } else { - return false; - } - } - - @Override - public void clearLines() { - checkNotDeleted(); - - lines.clear(); - } - - @Override - public int getLineCount() { - return getLines().size(); - } - @Override public void setAllowPlaceholders(boolean allowPlaceholders) { checkNotDeleted(); @@ -147,11 +70,6 @@ public class APIHologram extends BaseHologram implements Hologram { return visibilitySettings.isVisibleTo(player); } - @Override - public double getHeight() { - return lines.getHeight(); - } - @Override public @NotNull DefaultVisibilitySettings getVisibilitySettings() { return visibilitySettings; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLines.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLines.java new file mode 100644 index 00000000..1c89863c --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLines.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.hologram.api; + +import me.filoghost.fcommons.Preconditions; +import me.filoghost.holographicdisplays.api.hologram.HologramLines; +import me.filoghost.holographicdisplays.api.hologram.line.HologramLine; +import me.filoghost.holographicdisplays.api.hologram.line.ItemHologramLine; +import me.filoghost.holographicdisplays.api.hologram.line.TextHologramLine; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLines; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class APIHologramLines extends BaseHologramLines implements HologramLines { + + private final APIHologram hologram; + + public APIHologramLines(APIHologram hologram) { + super(hologram); + this.hologram = hologram; + } + + @Override + public @NotNull TextHologramLine appendText(@Nullable String text) { + checkNotDeleted(); + + APITextLine line = new APITextLine(hologram, text); + super.add(line); + return line; + } + + @Override + public @NotNull ItemHologramLine appendItem(@NotNull ItemStack itemStack) { + Preconditions.notNull(itemStack, "itemStack"); + checkNotDeleted(); + + APIItemLine line = new APIItemLine(hologram, itemStack); + super.add(line); + return line; + } + + @Override + public @NotNull TextHologramLine insertText(int beforeIndex, @Nullable String text) { + checkNotDeleted(); + + APITextLine line = new APITextLine(hologram, text); + super.insert(beforeIndex, line); + return line; + } + + @Override + public @NotNull ItemHologramLine insertItem(int beforeIndex, @NotNull ItemStack itemStack) { + Preconditions.notNull(itemStack, "itemStack"); + checkNotDeleted(); + + APIItemLine line = new APIItemLine(hologram, itemStack); + super.insert(beforeIndex, line); + return line; + } + + @Override + public boolean remove(@NotNull HologramLine line) { + if (line instanceof APIHologramLine) { + return super.remove((APIHologramLine) line); + } else { + return false; + } + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java index 71b54451..0eeaa1d8 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java @@ -26,7 +26,7 @@ public abstract class BaseHologram extends BaseHologramComponent { this.lineTrackerManager = lineTrackerManager; } - public abstract BaseHologramLines getLines(); + public abstract BaseHologramLines lines(); protected abstract boolean isVisibleTo(Player player); @@ -39,7 +39,7 @@ public abstract class BaseHologram extends BaseHologramComponent { @Override public final void setDeleted() { super.setDeleted(); - getLines().setDeleted(); + lines().setDeleted(); } public @NotNull BaseHologramPosition getPosition() { @@ -71,7 +71,7 @@ public abstract class BaseHologram extends BaseHologramComponent { checkNotDeleted(); position.set(worldName, x, y, z); - getLines().updateLinePositions(); + lines().updatePositions(); } protected void onWorldLoad(World world) { @@ -98,7 +98,7 @@ public abstract class BaseHologram extends BaseHologramComponent { public String toString() { return "Hologram{" + "position=" + position - + ", lines=" + getLines() + + ", lines=" + lines() + ", deleted=" + isDeleted() + "}"; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java index 8cb8867a..306671e7 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java @@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.base; import me.filoghost.holographicdisplays.api.hologram.HologramPosition; import me.filoghost.holographicdisplays.plugin.config.Settings; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collections; @@ -38,7 +39,7 @@ public class BaseHologramLines implements Iterab return lines.isEmpty(); } - public T get(int index) { + public @NotNull T get(int index) { return lines.get(index); } @@ -46,21 +47,21 @@ public class BaseHologramLines implements Iterab checkNotDeleted(); lines.add(line); - updateLinePositions(); + updatePositions(); } public void addAll(List newLines) { checkNotDeleted(); lines.addAll(newLines); - updateLinePositions(); + updatePositions(); } - public void insert(int afterIndex, T line) { + public void insert(int beforeIndex, T line) { checkNotDeleted(); - lines.add(afterIndex, line); - updateLinePositions(); + lines.add(beforeIndex, line); + updatePositions(); } public void set(int index, T line) { @@ -68,7 +69,7 @@ public class BaseHologramLines implements Iterab T previousLine = lines.set(index, line); previousLine.setDeleted(); - updateLinePositions(); + updatePositions(); } public void setAll(List newLines) { @@ -76,14 +77,14 @@ public class BaseHologramLines implements Iterab clear(); lines.addAll(newLines); - updateLinePositions(); + updatePositions(); } public void remove(int index) { checkNotDeleted(); lines.remove(index).setDeleted(); - updateLinePositions(); + updatePositions(); } public boolean remove(T line) { @@ -92,7 +93,7 @@ public class BaseHologramLines implements Iterab boolean removed = lines.remove(line); if (removed) { line.setDeleted(); - updateLinePositions(); + updatePositions(); } return removed; } @@ -114,7 +115,7 @@ public class BaseHologramLines implements Iterab * The top part of the first line should be exactly on the Y position of the hologram. * The second line is below the first, and so on. */ - public void updateLinePositions() { + public void updatePositions() { HologramPosition hologramPosition = hologram.getPosition(); double currentLineY = hologramPosition.getY(); @@ -151,7 +152,7 @@ public class BaseHologramLines implements Iterab } } - private void checkNotDeleted() { + protected void checkNotDeleted() { hologram.checkNotDeleted(); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/internal/InternalHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/internal/InternalHologram.java index 3661b925..b4d4ef0d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/internal/InternalHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/internal/InternalHologram.java @@ -26,7 +26,7 @@ public class InternalHologram extends BaseHologram { } @Override - public BaseHologramLines getLines() { + public BaseHologramLines lines() { return lines; }