API changes

This commit is contained in:
filoghost 2021-08-23 19:28:15 +02:00
parent dba767dd79
commit 2da7d318df
23 changed files with 240 additions and 224 deletions

View File

@ -6,119 +6,29 @@
package me.filoghost.holographicdisplays.api.hologram; package me.filoghost.holographicdisplays.api.hologram;
import me.filoghost.holographicdisplays.api.HolographicDisplaysAPI; 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.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* An object made of various lines, that can be items or holograms. * Entity to manage a group of vertically aligned lines, which display floating text and items.
* Holographic lines appear as a nametag without any entity below. * To create one see {@link HolographicDisplaysAPI}.
* To create one, please see {@link HolographicDisplaysAPI#createHologram(Location)}.
* *
* @since 1 * @since 1
*/ */
public interface Hologram { public interface Hologram {
/** /**
* Appends a text line to end of this hologram. * Returns the editable list of lines.
*
* @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.
* *
* @since 1 * @since 1
*/ */
void clearLines(); @NotNull HologramLines lines();
/** /**
* Checks the amount of lines of the hologram. * Returns the current position.
* *
* @return the amount of lines * @return the current position
* @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
* @since 1 * @since 1
*/ */
@NotNull HologramPosition getPosition(); @NotNull HologramPosition getPosition();

View File

@ -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();
}

View File

@ -41,8 +41,8 @@ public class DeathHolograms extends JavaPlugin implements Listener {
public void onPlayerDeath(PlayerDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
Hologram hologram = holographicDisplaysAPI.createHologram(event.getEntity().getEyeLocation()); Hologram hologram = holographicDisplaysAPI.createHologram(event.getEntity().getEyeLocation());
hologram.appendTextLine(ChatColor.RED + "Player " + ChatColor.GOLD + event.getEntity().getName() + ChatColor.RED + " died here!"); hologram.lines().appendText(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.GRAY + "Time of death: " + TIME_FORMATTER.format(Instant.now()));
} }
} }

View File

@ -49,10 +49,10 @@ public class PowerUps extends JavaPlugin implements Listener {
// Spawn the floating item with a label // Spawn the floating item with a label
Hologram hologram = holographicDisplaysAPI.createHologram(event.getEntity().getLocation().add(0.0, 0.9, 0.0)); Hologram hologram = holographicDisplaysAPI.createHologram(event.getEntity().getLocation().add(0.0, 0.9, 0.0));
hologram.appendTextLine(ChatColor.AQUA + "" + ChatColor.BOLD + "Speed PowerUp"); hologram.lines().appendText(ChatColor.AQUA + "" + ChatColor.BOLD + "Speed PowerUp");
ItemHologramLine icon = hologram.appendItemLine(new ItemStack(Material.SUGAR)); ItemHologramLine itemLine = hologram.lines().appendItem(new ItemStack(Material.SUGAR));
icon.setPickupListener((Player player) -> { itemLine.setPickupListener((Player player) -> {
// Play an effect // Play an effect
player.playEffect(hologram.getPosition().toLocation(), Effect.MOBSPAWNER_FLAMES, null); player.playEffect(hologram.getPosition().toLocation(), Effect.MOBSPAWNER_FLAMES, null);

View File

@ -177,10 +177,10 @@ public class HolographicDisplays extends FCommonsPlugin {
hologramDatabase.createHolograms(internalHologramManager, errorCollector); hologramDatabase.createHolograms(internalHologramManager, errorCollector);
for (APIHologram hologram : apiHologramManager.getHolograms()) { for (APIHologram hologram : apiHologramManager.getHolograms()) {
hologram.getLines().updateLinePositions(); hologram.lines().updatePositions();
} }
for (V2Hologram hologram : v2HologramManager.getHolograms()) { for (V2Hologram hologram : v2HologramManager.getHolograms()) {
hologram.getLines().updateLinePositions(); hologram.lines().updatePositions();
} }
} }

View File

@ -44,7 +44,7 @@ public class V2Hologram extends BaseHologram implements Hologram {
} }
@Override @Override
public BaseHologramLines<V2HologramLine> getLines() { public BaseHologramLines<V2HologramLine> lines() {
return lines; return lines;
} }

View File

@ -15,7 +15,7 @@ public interface V2HologramLine extends HologramLine, EditableHologramLine {
@Override @Override
default void removeLine() { default void removeLine() {
getParent().getLines().remove(this); getParent().lines().remove(this);
} }
} }

View File

@ -38,7 +38,7 @@ public class AddLineCommand extends LineEditingCommand implements QuickEditComma
InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine);
hologram.getLines().add(line); hologram.lines().add(line);
hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
sender.sendMessage(ColorScheme.PRIMARY + "Line added."); sender.sendMessage(ColorScheme.PRIMARY + "Line added.");

View File

@ -37,11 +37,11 @@ public class CopyCommand extends HologramSubCommand {
InternalHologram toHologram = hologramEditor.getExistingHologram(args[1]); InternalHologram toHologram = hologramEditor.getExistingHologram(args[1]);
List<InternalHologramLine> clonedLines = new ArrayList<>(); List<InternalHologramLine> clonedLines = new ArrayList<>();
for (InternalHologramLine line : fromHologram.getLines()) { for (InternalHologramLine line : fromHologram.lines()) {
clonedLines.add(hologramEditor.parseHologramLine(toHologram, line.getSerializedConfigValue())); clonedLines.add(hologramEditor.parseHologramLine(toHologram, line.getSerializedConfigValue()));
} }
toHologram.getLines().setAll(clonedLines); toHologram.lines().setAll(clonedLines);
hologramEditor.saveChanges(toHologram, ChangeType.EDIT_LINES); hologramEditor.saveChanges(toHologram, ChangeType.EDIT_LINES);
sender.sendMessage(ColorScheme.PRIMARY + "Lines of hologram \"" + fromHologram.getName() + "\"" sender.sendMessage(ColorScheme.PRIMARY + "Lines of hologram \"" + fromHologram.getName() + "\""

View File

@ -67,7 +67,7 @@ public class CreateCommand extends HologramSubCommand {
line = hologram.createTextLine(defaultText, defaultText.replace(ChatColor.COLOR_CHAR, '&')); line = hologram.createTextLine(defaultText, defaultText.replace(ChatColor.COLOR_CHAR, '&'));
} }
hologram.getLines().add(line); hologram.lines().add(line);
hologramEditor.saveChanges(hologram, ChangeType.CREATE); hologramEditor.saveChanges(hologram, ChangeType.CREATE);
hologramEditor.teleportLookingDown(player, player.getLocation()); hologramEditor.teleportLookingDown(player, player.getLocation());

View File

@ -37,7 +37,7 @@ public class InfoCommand extends LineEditingCommand implements QuickEditCommand
DisplayFormat.sendTitle(sender, "Lines of the hologram \"" + hologram.getName() + "\""); DisplayFormat.sendTitle(sender, "Lines of the hologram \"" + hologram.getName() + "\"");
int index = 0; int index = 0;
for (InternalHologramLine line : hologram.getLines()) { for (InternalHologramLine line : hologram.lines()) {
index++; index++;
sender.sendMessage(ColorScheme.SECONDARY_BOLD + index + ColorScheme.SECONDARY_DARK + ". " sender.sendMessage(ColorScheme.SECONDARY_BOLD + index + ColorScheme.SECONDARY_DARK + ". "
+ ColorScheme.SECONDARY + line.getSerializedConfigValue()); + ColorScheme.SECONDARY + line.getSerializedConfigValue());

View File

@ -41,14 +41,14 @@ public class InsertLineCommand extends LineEditingCommand implements QuickEditCo
int insertAfterIndex = CommandValidate.parseInteger(args[1]); int insertAfterIndex = CommandValidate.parseInteger(args[1]);
String serializedLine = Strings.joinFrom(" ", args, 2); String serializedLine = Strings.joinFrom(" ", args, 2);
int oldLinesAmount = hologram.getLines().size(); int oldLinesAmount = hologram.lines().size();
CommandValidate.check(insertAfterIndex >= 0 && insertAfterIndex <= oldLinesAmount, CommandValidate.check(insertAfterIndex >= 0 && insertAfterIndex <= oldLinesAmount,
"The line number must be between 0 and " + oldLinesAmount + "."); "The line number must be between 0 and " + oldLinesAmount + ".");
InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine); InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine);
hologram.getLines().insert(insertAfterIndex, line); hologram.lines().insert(insertAfterIndex, line);
hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
if (insertAfterIndex == 0) { if (insertAfterIndex == 0) {

View File

@ -114,9 +114,9 @@ public class ReadImageCommand extends LineEditingCommand {
} }
if (!append) { if (!append) {
hologram.getLines().clear(); hologram.lines().clear();
} }
hologram.getLines().addAll(newLines); hologram.lines().addAll(newLines);
hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
if (append) { if (append) {

View File

@ -85,7 +85,7 @@ public class ReadTextCommand extends LineEditingCommand {
} }
} }
hologram.getLines().setAll(newLines); hologram.lines().setAll(newLines);
hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
if (FileUtils.hasFileExtension(fileToRead, "jpg", "png", "jpeg", "gif")) { if (FileUtils.hasFileExtension(fileToRead, "jpg", "png", "jpeg", "gif")) {

View File

@ -35,7 +35,7 @@ public class RemoveLineCommand extends LineEditingCommand implements QuickEditCo
InternalHologram hologram = hologramEditor.getExistingHologram(args[0]); InternalHologram hologram = hologramEditor.getExistingHologram(args[0]);
int lineNumber = CommandValidate.parseInteger(args[1]); int lineNumber = CommandValidate.parseInteger(args[1]);
int linesAmount = hologram.getLines().size(); int linesAmount = hologram.lines().size();
CommandValidate.check(lineNumber >= 1 && lineNumber <= linesAmount, CommandValidate.check(lineNumber >= 1 && lineNumber <= linesAmount,
"The line number must be between 1 and " + 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, CommandValidate.check(linesAmount >= 2,
"A hologram must always have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete."); "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); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
sender.sendMessage(ColorScheme.PRIMARY + "Line " + lineNumber + " removed."); sender.sendMessage(ColorScheme.PRIMARY + "Line " + lineNumber + " removed.");

View File

@ -38,7 +38,7 @@ public class SetLineCommand extends LineEditingCommand implements QuickEditComma
String serializedLine = Strings.joinFrom(" ", args, 2); String serializedLine = Strings.joinFrom(" ", args, 2);
int lineNumber = CommandValidate.parseInteger(args[1]); int lineNumber = CommandValidate.parseInteger(args[1]);
int linesAmount = hologram.getLines().size(); int linesAmount = hologram.lines().size();
CommandValidate.check(lineNumber >= 1 && lineNumber <= linesAmount, CommandValidate.check(lineNumber >= 1 && lineNumber <= linesAmount,
"The line number must be between 1 and " + 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); InternalHologramLine line = hologramEditor.parseHologramLine(hologram, serializedLine);
hologram.getLines().set(index, line); hologram.lines().set(index, line);
hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES); hologramEditor.saveChanges(hologram, ChangeType.EDIT_LINES);
sender.sendMessage(ColorScheme.PRIMARY + "Line " + lineNumber + " changed."); sender.sendMessage(ColorScheme.PRIMARY + "Line " + lineNumber + " changed.");

View File

@ -30,7 +30,7 @@ public class HologramConfig {
public HologramConfig(InternalHologram hologram) { public HologramConfig(InternalHologram hologram) {
this.name = hologram.getName(); this.name = hologram.getName();
this.serializedLines = new ArrayList<>(); this.serializedLines = new ArrayList<>();
for (InternalHologramLine line : hologram.getLines()) { for (InternalHologramLine line : hologram.lines()) {
serializedLines.add(line.getSerializedConfigValue()); serializedLines.add(line.getSerializedConfigValue());
} }
@ -70,7 +70,7 @@ public class HologramConfig {
} }
} }
hologram.getLines().addAll(lines); hologram.lines().addAll(lines);
} }
private BaseHologramPosition parsePosition() throws HologramLoadException { private BaseHologramPosition parsePosition() throws HologramLoadException {

View File

@ -89,7 +89,7 @@ public class DisplayFormat {
public static void sendHologramSummary(CommandSender sender, InternalHologram hologram, boolean showWorld) { public static void sendHologramSummary(CommandSender sender, InternalHologram hologram, boolean showWorld) {
BaseHologramPosition position = hologram.getPosition(); BaseHologramPosition position = hologram.getPosition();
sender.sendMessage(ColorScheme.SECONDARY_DARK + "- " + ColorScheme.SECONDARY_BOLD + hologram.getName() 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() + "\", " : "") + (showWorld ? "world: \"" + position.getWorldName() + "\", " : "")
+ "x: " + position.getBlockX() + ", " + "x: " + position.getBlockX() + ", "
+ "y: " + position.getBlockY() + ", " + "y: " + position.getBlockY() + ", "

View File

@ -7,20 +7,16 @@ package me.filoghost.holographicdisplays.plugin.hologram.api;
import me.filoghost.fcommons.Preconditions; import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.api.hologram.Hologram; 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.BaseHologram;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLines;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class APIHologram extends BaseHologram implements Hologram { public class APIHologram extends BaseHologram implements Hologram {
private final BaseHologramLines<APIHologramLine> lines; private final APIHologramLines lines;
private final Plugin plugin; private final Plugin plugin;
private final APIHologramManager apiHologramManager; private final APIHologramManager apiHologramManager;
private final DefaultVisibilitySettings visibilitySettings; private final DefaultVisibilitySettings visibilitySettings;
@ -34,14 +30,14 @@ public class APIHologram extends BaseHologram implements Hologram {
LineTrackerManager lineTrackerManager) { LineTrackerManager lineTrackerManager) {
super(position, lineTrackerManager); super(position, lineTrackerManager);
Preconditions.notNull(plugin, "plugin"); Preconditions.notNull(plugin, "plugin");
this.lines = new BaseHologramLines<>(this); this.lines = new APIHologramLines(this);
this.plugin = plugin; this.plugin = plugin;
this.apiHologramManager = apiHologramManager; this.apiHologramManager = apiHologramManager;
this.visibilitySettings = new DefaultVisibilitySettings(); this.visibilitySettings = new DefaultVisibilitySettings();
} }
@Override @Override
public BaseHologramLines<APIHologramLine> getLines() { public @NotNull APIHologramLines lines() {
return lines; return lines;
} }
@ -50,79 +46,6 @@ public class APIHologram extends BaseHologram implements Hologram {
return plugin; 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 @Override
public void setAllowPlaceholders(boolean allowPlaceholders) { public void setAllowPlaceholders(boolean allowPlaceholders) {
checkNotDeleted(); checkNotDeleted();
@ -147,11 +70,6 @@ public class APIHologram extends BaseHologram implements Hologram {
return visibilitySettings.isVisibleTo(player); return visibilitySettings.isVisibleTo(player);
} }
@Override
public double getHeight() {
return lines.getHeight();
}
@Override @Override
public @NotNull DefaultVisibilitySettings getVisibilitySettings() { public @NotNull DefaultVisibilitySettings getVisibilitySettings() {
return visibilitySettings; return visibilitySettings;

View File

@ -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<APIHologramLine> 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;
}
}
}

View File

@ -26,7 +26,7 @@ public abstract class BaseHologram extends BaseHologramComponent {
this.lineTrackerManager = lineTrackerManager; this.lineTrackerManager = lineTrackerManager;
} }
public abstract BaseHologramLines<? extends EditableHologramLine> getLines(); public abstract BaseHologramLines<? extends EditableHologramLine> lines();
protected abstract boolean isVisibleTo(Player player); protected abstract boolean isVisibleTo(Player player);
@ -39,7 +39,7 @@ public abstract class BaseHologram extends BaseHologramComponent {
@Override @Override
public final void setDeleted() { public final void setDeleted() {
super.setDeleted(); super.setDeleted();
getLines().setDeleted(); lines().setDeleted();
} }
public @NotNull BaseHologramPosition getPosition() { public @NotNull BaseHologramPosition getPosition() {
@ -71,7 +71,7 @@ public abstract class BaseHologram extends BaseHologramComponent {
checkNotDeleted(); checkNotDeleted();
position.set(worldName, x, y, z); position.set(worldName, x, y, z);
getLines().updateLinePositions(); lines().updatePositions();
} }
protected void onWorldLoad(World world) { protected void onWorldLoad(World world) {
@ -98,7 +98,7 @@ public abstract class BaseHologram extends BaseHologramComponent {
public String toString() { public String toString() {
return "Hologram{" return "Hologram{"
+ "position=" + position + "position=" + position
+ ", lines=" + getLines() + ", lines=" + lines()
+ ", deleted=" + isDeleted() + ", deleted=" + isDeleted()
+ "}"; + "}";
} }

View File

@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition; import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import me.filoghost.holographicdisplays.plugin.config.Settings; import me.filoghost.holographicdisplays.plugin.config.Settings;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -38,7 +39,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
return lines.isEmpty(); return lines.isEmpty();
} }
public T get(int index) { public @NotNull T get(int index) {
return lines.get(index); return lines.get(index);
} }
@ -46,21 +47,21 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
checkNotDeleted(); checkNotDeleted();
lines.add(line); lines.add(line);
updateLinePositions(); updatePositions();
} }
public void addAll(List<? extends T> newLines) { public void addAll(List<? extends T> newLines) {
checkNotDeleted(); checkNotDeleted();
lines.addAll(newLines); lines.addAll(newLines);
updateLinePositions(); updatePositions();
} }
public void insert(int afterIndex, T line) { public void insert(int beforeIndex, T line) {
checkNotDeleted(); checkNotDeleted();
lines.add(afterIndex, line); lines.add(beforeIndex, line);
updateLinePositions(); updatePositions();
} }
public void set(int index, T line) { public void set(int index, T line) {
@ -68,7 +69,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
T previousLine = lines.set(index, line); T previousLine = lines.set(index, line);
previousLine.setDeleted(); previousLine.setDeleted();
updateLinePositions(); updatePositions();
} }
public void setAll(List<T> newLines) { public void setAll(List<T> newLines) {
@ -76,14 +77,14 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
clear(); clear();
lines.addAll(newLines); lines.addAll(newLines);
updateLinePositions(); updatePositions();
} }
public void remove(int index) { public void remove(int index) {
checkNotDeleted(); checkNotDeleted();
lines.remove(index).setDeleted(); lines.remove(index).setDeleted();
updateLinePositions(); updatePositions();
} }
public boolean remove(T line) { public boolean remove(T line) {
@ -92,7 +93,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
boolean removed = lines.remove(line); boolean removed = lines.remove(line);
if (removed) { if (removed) {
line.setDeleted(); line.setDeleted();
updateLinePositions(); updatePositions();
} }
return removed; return removed;
} }
@ -114,7 +115,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
* The top part of the first line should be exactly on the Y position of the hologram. * 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. * The second line is below the first, and so on.
*/ */
public void updateLinePositions() { public void updatePositions() {
HologramPosition hologramPosition = hologram.getPosition(); HologramPosition hologramPosition = hologram.getPosition();
double currentLineY = hologramPosition.getY(); double currentLineY = hologramPosition.getY();
@ -151,7 +152,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
} }
} }
private void checkNotDeleted() { protected void checkNotDeleted() {
hologram.checkNotDeleted(); hologram.checkNotDeleted();
} }

View File

@ -26,7 +26,7 @@ public class InternalHologram extends BaseHologram {
} }
@Override @Override
public BaseHologramLines<InternalHologramLine> getLines() { public BaseHologramLines<InternalHologramLine> lines() {
return lines; return lines;
} }