Refactor hologram-related objects, simplify editing

This commit is contained in:
filoghost 2021-03-06 18:44:52 +01:00
parent a8a44bec90
commit c332547f38
25 changed files with 167 additions and 155 deletions

View File

@ -18,14 +18,14 @@ public interface StandardHologram {
boolean isInChunk(Chunk chunk); boolean isInChunk(Chunk chunk);
List<? extends StandardHologramLine> getLines();
int getLinesAmount();
Plugin getOwnerPlugin(); Plugin getOwnerPlugin();
List<? extends StandardHologramLine> getLinesUnsafe();
boolean isVisibleTo(Player player); boolean isVisibleTo(Player player);
String toFormattedString();
void refresh(); void refresh();
void refresh(boolean forceRespawn); void refresh(boolean forceRespawn);
@ -34,12 +34,10 @@ public interface StandardHologram {
void despawnEntities(); void despawnEntities();
void removeLine(StandardHologramLine line);
int size();
boolean isDeleted(); boolean isDeleted();
void setDeleted(); void setDeleted();
String toFormattedString();
} }

View File

@ -39,7 +39,7 @@ class PacketSender {
public void sendDestroyEntitiesPacket(Player player, StandardHologram hologram) { public void sendDestroyEntitiesPacket(Player player, StandardHologram hologram) {
List<Integer> ids = new ArrayList<>(); List<Integer> ids = new ArrayList<>();
for (StandardHologramLine line : hologram.getLinesUnsafe()) { for (StandardHologramLine line : hologram.getLines()) {
line.collectEntityIDs(ids); line.collectEntityIDs(ids);
} }
@ -49,7 +49,7 @@ class PacketSender {
} }
public void sendCreateEntitiesPacket(Player player, StandardHologram hologram) { public void sendCreateEntitiesPacket(Player player, StandardHologram hologram) {
for (StandardHologramLine line : hologram.getLinesUnsafe()) { for (StandardHologramLine line : hologram.getLines()) {
sendCreateEntitiesPacket(player, line); sendCreateEntitiesPacket(player, line);
} }
} }

View File

@ -20,9 +20,9 @@ import java.nio.file.Path;
public class HologramCommandValidate { public class HologramCommandValidate {
public static InternalHologramLine parseHologramLine(InternalHologram hologram, String serializedLine, boolean validateMaterial) throws CommandException { public static InternalHologramLine parseHologramLine(InternalHologram hologram, String serializedLine) throws CommandException {
try { try {
return HologramLineParser.parseLine(hologram, serializedLine, validateMaterial); return HologramLineParser.parseLine(hologram, serializedLine);
} catch (HologramLoadException e) { } catch (HologramLoadException e) {
throw new CommandException(Utils.formatExceptionMessage(e)); throw new CommandException(Utils.formatExceptionMessage(e));
} }

View File

@ -41,9 +41,8 @@ public class AddlineCommand extends LineEditingCommand implements QuickEditComma
InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]);
String serializedLine = Utils.join(args, " ", 1, args.length); String serializedLine = Utils.join(args, " ", 1, args.length);
InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine);
hologram.getLinesUnsafe().add(line); hologram.addLine(line);
hologram.refresh();
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));

View File

@ -16,6 +16,9 @@ import me.filoghost.holographicdisplays.object.internal.InternalHologramLine;
import me.filoghost.holographicdisplays.object.internal.InternalHologramManager; import me.filoghost.holographicdisplays.object.internal.InternalHologramManager;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
public class CopyCommand extends HologramSubCommand { public class CopyCommand extends HologramSubCommand {
private final InternalHologramManager internalHologramManager; private final InternalHologramManager internalHologramManager;
@ -36,13 +39,12 @@ public class CopyCommand extends HologramSubCommand {
InternalHologram fromHologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); InternalHologram fromHologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]);
InternalHologram toHologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[1]); InternalHologram toHologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[1]);
toHologram.clearLines(); List<InternalHologramLine> clonedLines = new ArrayList<>();
for (InternalHologramLine line : fromHologram.getLinesUnsafe()) { for (InternalHologramLine line : fromHologram.getLines()) {
InternalHologramLine clonedLine = HologramCommandValidate.parseHologramLine(toHologram, line.getSerializedConfigValue(), false); clonedLines.add(HologramCommandValidate.parseHologramLine(toHologram, line.getSerializedConfigValue()));
toHologram.getLinesUnsafe().add(clonedLine);
} }
toHologram.refresh(); toHologram.setLines(clonedLines);
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);

View File

@ -65,7 +65,7 @@ public class CreateCommand extends HologramSubCommand {
String text = Utils.join(args, " ", 1, args.length); String text = Utils.join(args, " ", 1, args.length);
CommandValidate.check(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); CommandValidate.check(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty.");
line = HologramCommandValidate.parseHologramLine(hologram, text, true); line = HologramCommandValidate.parseHologramLine(hologram, text);
player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + context.getRootLabel() + " edit " + hologram.getName() + ")"); player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + context.getRootLabel() + " edit " + hologram.getName() + ")");
} else { } else {
String defaultText = "Default hologram. Change it with " String defaultText = "Default hologram. Change it with "
@ -73,8 +73,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.getLinesUnsafe().add(line); hologram.addLine(line);
hologram.refresh();
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);
Location look = player.getLocation(); Location look = player.getLocation();

View File

@ -66,7 +66,7 @@ public class DebugCommand extends HologramSubCommand {
for (Entry<StandardHologram, HologramDebugInfo> entry : hologramsDebugInfo.entrySet()) { for (Entry<StandardHologram, HologramDebugInfo> entry : hologramsDebugInfo.entrySet()) {
StandardHologram hologram = entry.getKey(); StandardHologram hologram = entry.getKey();
HologramDebugInfo debugInfo = entry.getValue(); HologramDebugInfo debugInfo = entry.getValue();
sender.sendMessage(Colors.PRIMARY_SHADOW + "- '" + hologram.toFormattedString() + "': " + hologram.size() + " lines, " sender.sendMessage(Colors.PRIMARY_SHADOW + "- '" + hologram.toFormattedString() + "': " + hologram.getLinesAmount() + " lines, "
+ debugInfo.getTotalEntities() + " entities (" + debugInfo.aliveEntities + " alive, " + debugInfo.deadEntities + " dead)"); + debugInfo.getTotalEntities() + " entities (" + debugInfo.aliveEntities + " alive, " + debugInfo.deadEntities + " dead)");
} }
} }

View File

@ -39,7 +39,7 @@ public class InfoCommand extends LineEditingCommand implements QuickEditCommand
Messages.sendTitle(sender, "Lines of the hologram '" + hologram.getName() + "'"); Messages.sendTitle(sender, "Lines of the hologram '" + hologram.getName() + "'");
int index = 0; int index = 0;
for (InternalHologramLine line : hologram.getLinesUnsafe()) { for (InternalHologramLine line : hologram.getLines()) {
sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + line.getSerializedConfigValue()); sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + line.getSerializedConfigValue());
} }
commandManager.sendQuickEditCommands(context, hologram); commandManager.sendQuickEditCommands(context, hologram);

View File

@ -44,28 +44,27 @@ public class InsertlineCommand extends LineEditingCommand implements QuickEditCo
@Override @Override
public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException { public void execute(CommandSender sender, String[] args, SubCommandContext context) throws CommandException {
InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]); InternalHologram hologram = HologramCommandValidate.getInternalHologram(internalHologramManager, args[0]);
int insertAfter = CommandValidate.parseInteger(args[1]); int insertAfterIndex = CommandValidate.parseInteger(args[1]);
String serializedLine = Utils.join(args, " ", 2, args.length); String serializedLine = Utils.join(args, " ", 2, args.length);
int oldLinesAmount = hologram.size(); int oldLinesAmount = hologram.getLinesAmount();
CommandValidate.check(insertAfter >= 0 && insertAfter <= oldLinesAmount, "The number must be between 0 and " + hologram.size() + "(amount of lines of the hologram)."); CommandValidate.check(insertAfterIndex >= 0 && insertAfterIndex <= oldLinesAmount, "The number must be between 0 and " + hologram.getLinesAmount() + "(amount of lines of the hologram).");
InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine);
hologram.getLinesUnsafe().add(insertAfter, line); hologram.insertLine(insertAfterIndex, line);
hologram.refresh();
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));
if (insertAfter == 0) { if (insertAfterIndex == 0) {
sender.sendMessage(Colors.PRIMARY + "Line inserted before first line."); sender.sendMessage(Colors.PRIMARY + "Line inserted before first line.");
} else if (insertAfter == oldLinesAmount) { } else if (insertAfterIndex == oldLinesAmount) {
sender.sendMessage(Colors.PRIMARY + "Line appended at the end."); sender.sendMessage(Colors.PRIMARY + "Line appended at the end.");
Messages.sendTip(sender, "You can use \"/" + context.getRootLabel() + " addline\" to append a line at the end."); Messages.sendTip(sender, "You can use \"/" + context.getRootLabel() + " addline\" to append a line at the end.");
} else { } else {
sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "."); sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfterIndex + " and " + (insertAfterIndex + 1) + ".");
} }
commandManager.sendQuickEditCommands(context, hologram); commandManager.sendQuickEditCommands(context, hologram);
} }

View File

@ -58,7 +58,7 @@ public class ListCommand extends HologramSubCommand {
+ " " + Colors.SECONDARY_SHADOW + "at x: " + (int) hologram.getX() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) hologram.getX()
+ ", y: " + (int) hologram.getY() + ", y: " + (int) hologram.getY()
+ ", z: " + (int) hologram.getZ() + ", z: " + (int) hologram.getZ()
+ " (lines: " + hologram.size() + ", world: \"" + hologram.getWorld().getName() + "\")"); + " (lines: " + hologram.getLinesAmount() + ", world: \"" + hologram.getWorld().getName() + "\")");
} }
} }

View File

@ -53,7 +53,7 @@ public class NearCommand extends HologramSubCommand {
Messages.sendTitle(player, "Near holograms"); Messages.sendTitle(player, "Near holograms");
for (InternalHologram nearHologram : nearHolograms) { for (InternalHologram nearHologram : nearHolograms) {
player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.size() + ")"); player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.getLinesAmount() + ")");
} }
} }

View File

@ -108,18 +108,17 @@ public class ReadimageCommand extends LineEditingCommand {
} }
ImageMessage imageMessage = new ImageMessage(image, width); ImageMessage imageMessage = new ImageMessage(image, width);
String[] newLines = imageMessage.getLines(); List<InternalTextLine> newLines = new ArrayList<>();
for (String newLine : imageMessage.getLines()) {
newLines.add(hologram.createTextLine(newLine, newLine));
}
if (!append) { if (!append) {
hologram.clearLines(); hologram.clearLines();
} }
for (String newLine : newLines) { hologram.addLines(newLines);
InternalTextLine line = hologram.createTextLine(newLine, newLine);
hologram.getLinesUnsafe().add(line);
}
hologram.refresh();
if (newLines.length < 5) { if (newLines.size() < 5) {
Messages.sendTip(sender, "The image has a very low height. You can increase it by increasing the width, it will scale automatically."); Messages.sendTip(sender, "The image has a very low height. You can increase it by increasing the width, it will scale automatically.");
} }

View File

@ -76,16 +76,14 @@ public class ReadtextCommand extends LineEditingCommand {
List<InternalHologramLine> linesToAdd = new ArrayList<>(); List<InternalHologramLine> linesToAdd = new ArrayList<>();
for (int i = 0; i < linesAmount; i++) { for (int i = 0; i < linesAmount; i++) {
try { try {
InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLines.get(i), true); InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLines.get(i));
linesToAdd.add(line); linesToAdd.add(line);
} catch (HologramLoadException e) { } catch (HologramLoadException e) {
throw new CommandException("Error at line " + (i + 1) + ": " + e.getMessage()); throw new CommandException("Error at line " + (i + 1) + ": " + e.getMessage());
} }
} }
hologram.clearLines(); hologram.setLines(linesToAdd);
hologram.getLinesUnsafe().addAll(linesToAdd);
hologram.refresh();
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);

View File

@ -41,13 +41,12 @@ public class RemovelineCommand extends LineEditingCommand implements QuickEditCo
int lineNumber = CommandValidate.parseInteger(args[1]); int lineNumber = CommandValidate.parseInteger(args[1]);
CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.getLinesAmount(), "The line number must be between 1 and " + hologram.getLinesAmount() + ".");
int index = lineNumber - 1; int index = lineNumber - 1;
CommandValidate.check(hologram.size() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete."); CommandValidate.check(hologram.getLinesAmount() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + context.getRootLabel() + " delete.");
hologram.removeLine(index); hologram.removeLine(index);
hologram.refresh();
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));

View File

@ -43,14 +43,12 @@ public class SetlineCommand extends LineEditingCommand implements QuickEditComma
String serializedLine = Utils.join(args, " ", 2, args.length); String serializedLine = Utils.join(args, " ", 2, args.length);
int lineNumber = CommandValidate.parseInteger(args[1]); int lineNumber = CommandValidate.parseInteger(args[1]);
CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); CommandValidate.check(lineNumber >= 1 && lineNumber <= hologram.getLinesAmount(), "The line number must be between 1 and " + hologram.getLinesAmount() + ".");
int index = lineNumber - 1; int index = lineNumber - 1;
InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine, true); InternalHologramLine line = HologramCommandValidate.parseHologramLine(hologram, serializedLine);
InternalHologramLine prevLine = hologram.getLinesUnsafe().set(index, line); hologram.setLine(index, line);
prevLine.despawn();
hologram.refresh();
configManager.saveHologramDatabase(internalHologramManager); configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram)); Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));

View File

@ -38,7 +38,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.getLinesUnsafe()) { for (InternalHologramLine line : hologram.getLines()) {
serializedLines.add(line.getSerializedConfigValue()); serializedLines.add(line.getSerializedConfigValue());
} }
@ -62,17 +62,18 @@ public class HologramConfig {
Location location = deserializeLocation(serializedLocation); Location location = deserializeLocation(serializedLocation);
InternalHologram hologram = internalHologramManager.createHologram(location, name); InternalHologram hologram = internalHologramManager.createHologram(location, name);
List<InternalHologramLine> lines = new ArrayList<>();
for (String serializedLine : serializedLines) { for (String serializedLine : serializedLines) {
try { try {
InternalHologramLine line = HologramLineParser.parseLine(hologram, serializedLine, false); lines.add(HologramLineParser.parseLine(hologram, serializedLine));
hologram.getLinesUnsafe().add(line);
} catch (HologramLoadException e) { } catch (HologramLoadException e) {
// Rethrow with more details // Rethrow with more details
throw new HologramLoadException("hologram \"" + hologram.getName() + "\" has an invalid line: " + e.getMessage(), e); throw new HologramLoadException("hologram \"" + hologram.getName() + "\" has an invalid line: " + e.getMessage(), e);
} }
} }
hologram.setLines(lines);
return hologram; return hologram;
} }

View File

@ -36,7 +36,6 @@ public class HologramDatabase {
} }
public void createHolograms(CommandSender sender, InternalHologramManager internalHologramManager) { public void createHolograms(CommandSender sender, InternalHologramManager internalHologramManager) {
// Create all the holograms
for (HologramConfig hologramConfig : hologramConfigs) { for (HologramConfig hologramConfig : hologramConfigs) {
try { try {
hologramConfig.createHologram(internalHologramManager); hologramConfig.createHologram(internalHologramManager);
@ -50,11 +49,6 @@ public class HologramDatabase {
Log.warning("Unexpected exception while loading the hologram \"" + hologramConfig.getName() + "\".", e); Log.warning("Unexpected exception while loading the hologram \"" + hologramConfig.getName() + "\".", e);
} }
} }
// Then trigger a refresh for all of them
for (InternalHologram hologram : internalHologramManager.getHolograms()) {
hologram.refresh();
}
} }
public static Config exportToConfig(InternalHologramManager hologramManager) { public static Config exportToConfig(InternalHologramManager hologramManager) {

View File

@ -20,12 +20,12 @@ public class HologramLineParser {
private static final String ICON_PREFIX = "icon:"; private static final String ICON_PREFIX = "icon:";
private static final String EMPTY_LINE_PLACEHOLDER = "{empty}"; private static final String EMPTY_LINE_PLACEHOLDER = "{empty}";
public static InternalHologramLine parseLine(InternalHologram hologram, String serializedLine, boolean checkMaterialValidity) throws HologramLoadException { public static InternalHologramLine parseLine(InternalHologram hologram, String serializedLine) throws HologramLoadException {
InternalHologramLine hologramLine; InternalHologramLine hologramLine;
if (serializedLine.toLowerCase().startsWith(ICON_PREFIX)) { if (serializedLine.toLowerCase().startsWith(ICON_PREFIX)) {
String serializedIcon = serializedLine.substring(ICON_PREFIX.length()); String serializedIcon = serializedLine.substring(ICON_PREFIX.length());
ItemStack icon = parseItemStack(serializedIcon, checkMaterialValidity); ItemStack icon = parseItemStack(serializedIcon);
hologramLine = hologram.createItemLine(icon, serializedLine); hologramLine = hologram.createItemLine(icon, serializedLine);
} else { } else {
@ -44,7 +44,7 @@ public class HologramLineParser {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private static ItemStack parseItemStack(String serializedItem, boolean checkMaterialValidity) throws HologramLoadException { private static ItemStack parseItemStack(String serializedItem) throws HologramLoadException {
serializedItem = serializedItem.trim(); serializedItem = serializedItem.trim();
// Parse json // Parse json
@ -80,10 +80,7 @@ public class HologramLineParser {
Material material = MaterialsHelper.matchMaterial(materialName); Material material = MaterialsHelper.matchMaterial(materialName);
if (material == null) { if (material == null) {
if (checkMaterialValidity) { throw new HologramLoadException("\"" + materialName + "\" is not a valid material");
throw new HologramLoadException("\"" + materialName + "\" is not a valid material");
}
material = Material.BEDROCK;
} }
ItemStack itemStack = new ItemStack(material, 1, dataValue); ItemStack itemStack = new ItemStack(material, 1, dataValue);

View File

@ -190,8 +190,8 @@ public class ImageMessage {
return bestColorMatch; return bestColorMatch;
} }
public String[] getLines() { public String[] getLines() {
return lines; return lines;
} }
} }

View File

@ -12,23 +12,21 @@ import me.filoghost.holographicdisplays.api.line.HologramLine;
import me.filoghost.holographicdisplays.api.line.ItemLine; import me.filoghost.holographicdisplays.api.line.ItemLine;
import me.filoghost.holographicdisplays.api.line.TextLine; import me.filoghost.holographicdisplays.api.line.TextLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.object.base.BaseHologram;
import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.Configuration;
import me.filoghost.holographicdisplays.object.base.BaseHologram;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class APIHologram extends BaseHologram implements Hologram { public class APIHologram extends BaseHologram<APIHologramLine> implements Hologram {
private final Plugin plugin; private final Plugin plugin;
private final APIHologramManager apiHologramManager; private final APIHologramManager apiHologramManager;
private final VisibilityManager visibilityManager; private final VisibilityManager visibilityManager;
private final long creationTimestamp; private final long creationTimestamp;
private final List<APIHologramLine> lines;
private boolean allowPlaceholders; private boolean allowPlaceholders;
@ -39,8 +37,6 @@ public class APIHologram extends BaseHologram implements Hologram {
this.apiHologramManager = apiHologramManager; this.apiHologramManager = apiHologramManager;
this.visibilityManager = new DefaultVisibilityManager(this); this.visibilityManager = new DefaultVisibilityManager(this);
this.creationTimestamp = System.currentTimeMillis(); this.creationTimestamp = System.currentTimeMillis();
this.lines = new ArrayList<>();
} }
@Override @Override
@ -48,50 +44,35 @@ public class APIHologram extends BaseHologram implements Hologram {
return plugin; return plugin;
} }
@Override
public List<APIHologramLine> getLinesUnsafe() {
return lines;
}
@Override @Override
public TextLine appendTextLine(String text) { public TextLine appendTextLine(String text) {
checkState();
APITextLine line = createTextLine(text); APITextLine line = createTextLine(text);
lines.add(line); addLine(line);
refresh();
return line; return line;
} }
@Override @Override
public ItemLine appendItemLine(ItemStack itemStack) { public ItemLine appendItemLine(ItemStack itemStack) {
checkState();
Preconditions.notNull(itemStack, "itemStack"); Preconditions.notNull(itemStack, "itemStack");
APIItemLine line = createItemLine(itemStack); APIItemLine line = createItemLine(itemStack);
lines.add(line); addLine(line);
refresh();
return line; return line;
} }
@Override @Override
public TextLine insertTextLine(int index, String text) { public TextLine insertTextLine(int index, String text) {
checkState();
APITextLine line = createTextLine(text); APITextLine line = createTextLine(text);
lines.add(index, line); addLine(line);
refresh();
return line; return line;
} }
@Override @Override
public ItemLine insertItemLine(int index, ItemStack itemStack) { public ItemLine insertItemLine(int index, ItemStack itemStack) {
checkState();
Preconditions.notNull(itemStack, "itemStack"); Preconditions.notNull(itemStack, "itemStack");
APIItemLine line = createItemLine(itemStack); APIItemLine line = createItemLine(itemStack);
lines.add(index, line); addLine(line);
refresh();
return line; return line;
} }
@ -105,7 +86,12 @@ public class APIHologram extends BaseHologram implements Hologram {
@Override @Override
public HologramLine getLine(int index) { public HologramLine getLine(int index) {
return lines.get(index); return getLines().get(index);
}
@Override
public int size() {
return getLinesAmount();
} }
@Override @Override
@ -130,6 +116,7 @@ public class APIHologram extends BaseHologram implements Hologram {
@Override @Override
public double getHeight() { public double getHeight() {
List<APIHologramLine> lines = getLines();
if (lines.isEmpty()) { if (lines.isEmpty()) {
return 0; return 0;
} }

View File

@ -10,9 +10,12 @@ import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
public interface APIHologramLine extends HologramLine, StandardHologramLine { public interface APIHologramLine extends HologramLine, StandardHologramLine {
@Override
APIHologram getParent();
@Override @Override
default void removeLine() { default void removeLine() {
getHologram().removeLine(this); getParent().removeLine(this);
} }
} }

View File

@ -1,6 +1,5 @@
package me.filoghost.holographicdisplays.object.api; package me.filoghost.holographicdisplays.object.api;
import me.filoghost.holographicdisplays.api.Hologram;
import me.filoghost.holographicdisplays.api.line.ItemLine; import me.filoghost.holographicdisplays.api.line.ItemLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.object.base.BaseItemLine; import me.filoghost.holographicdisplays.object.base.BaseItemLine;
@ -16,7 +15,7 @@ public class APIItemLine extends BaseItemLine implements ItemLine, APIHologramLi
} }
@Override @Override
public Hologram getParent() { public APIHologram getParent() {
return parent; return parent;
} }

View File

@ -1,6 +1,5 @@
package me.filoghost.holographicdisplays.object.api; package me.filoghost.holographicdisplays.object.api;
import me.filoghost.holographicdisplays.api.Hologram;
import me.filoghost.holographicdisplays.api.line.TextLine; import me.filoghost.holographicdisplays.api.line.TextLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.object.base.BaseTextLine; import me.filoghost.holographicdisplays.object.base.BaseTextLine;
@ -15,7 +14,7 @@ public class APITextLine extends BaseTextLine implements TextLine, APIHologramLi
} }
@Override @Override
public Hologram getParent() { public APIHologram getParent() {
return parent; return parent;
} }

View File

@ -6,18 +6,23 @@
package me.filoghost.holographicdisplays.object.base; package me.filoghost.holographicdisplays.object.base;
import me.filoghost.fcommons.Preconditions; import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.hologram.StandardHologram; import me.filoghost.holographicdisplays.core.hologram.StandardHologram;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine; import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.Configuration;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
public abstract class BaseHologram extends BaseHologramComponent implements StandardHologram { public abstract class BaseHologram<T extends StandardHologramLine> extends BaseHologramComponent implements StandardHologram {
private final NMSManager nmsManager; private final NMSManager nmsManager;
private final List<T> lines;
private final List<T> unmodifiableLinesView;
private boolean deleted; private boolean deleted;
@ -25,6 +30,12 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan
Preconditions.notNull(location, "location"); Preconditions.notNull(location, "location");
this.setLocation(location); this.setLocation(location);
this.nmsManager = nmsManager; this.nmsManager = nmsManager;
this.lines = new ArrayList<>();
this.unmodifiableLinesView = Collections.unmodifiableList(lines);
}
protected final NMSManager getNMSManager() {
return nmsManager;
} }
@Override @Override
@ -36,53 +47,93 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan
public void setDeleted() { public void setDeleted() {
if (!deleted) { if (!deleted) {
deleted = true; deleted = true;
clearLines(); despawnEntities();
} }
} }
protected final NMSManager getNMSManager() { @Override
return nmsManager; public List<T> getLines() {
return unmodifiableLinesView;
} }
public void removeLine(int index) { public void addLine(T line) {
checkState(); checkNotDeleted();
getLinesUnsafe().remove(index).despawn(); lines.add(line);
refresh(); refresh();
} }
@Override public void addLines(List<? extends T> newLines) {
public void removeLine(StandardHologramLine line) { checkNotDeleted();
checkState();
getLinesUnsafe().remove(line); lines.addAll(newLines);
refresh();
}
public void insertLine(int afterIndex, T line) {
checkNotDeleted();
lines.add(afterIndex, line);
refresh();
}
public void setLine(int index, T line) {
checkNotDeleted();
T previousLine = lines.set(index, line);
previousLine.despawn();
refresh();
}
public void setLines(List<T> newLines) {
checkNotDeleted();
clearLines();
lines.addAll(newLines);
refresh();
}
public void removeLine(int index) {
checkNotDeleted();
lines.remove(index).despawn();
refresh();
}
public void removeLine(T line) {
checkNotDeleted();
lines.remove(line);
line.despawn(); line.despawn();
refresh(); refresh();
} }
public void clearLines() { public void clearLines() {
Iterator<? extends StandardHologramLine> iterator = getLinesUnsafe().iterator(); checkNotDeleted();
Iterator<T> iterator = lines.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
StandardHologramLine line = iterator.next(); T line = iterator.next();
iterator.remove(); iterator.remove();
line.despawn(); line.despawn();
} }
// No need to refresh, since there are no lines
} }
@Override @Override
public int size() { public int getLinesAmount() {
return getLinesUnsafe().size(); return lines.size();
} }
public void teleport(Location location) { public void teleport(Location location) {
checkState();
Preconditions.notNull(location, "location"); Preconditions.notNull(location, "location");
teleport(location.getWorld(), location.getX(), location.getY(), location.getZ()); teleport(location.getWorld(), location.getX(), location.getY(), location.getZ());
} }
public void teleport(World world, double x, double y, double z) { public void teleport(World world, double x, double y, double z) {
checkState(); checkNotDeleted();
Preconditions.notNull(world, "world"); Preconditions.notNull(world, "world");
setLocation(world, x, y, z); setLocation(world, x, y, z);
@ -101,7 +152,7 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan
@Override @Override
public void refresh(boolean forceRespawn, boolean isChunkLoaded) { public void refresh(boolean forceRespawn, boolean isChunkLoaded) {
checkState(); checkNotDeleted();
if (isChunkLoaded) { if (isChunkLoaded) {
respawnEntities(forceRespawn); respawnEntities(forceRespawn);
@ -117,8 +168,8 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan
private void respawnEntities(boolean forceRespawn) { private void respawnEntities(boolean forceRespawn) {
double currentLineY = getY(); double currentLineY = getY();
for (int i = 0; i < getLinesUnsafe().size(); i++) { for (int i = 0; i < lines.size(); i++) {
StandardHologramLine line = getLinesUnsafe().get(i); T line = lines.get(i);
currentLineY -= line.getHeight(); currentLineY -= line.getHeight();
if (i > 0) { if (i > 0) {
@ -134,18 +185,18 @@ public abstract class BaseHologram extends BaseHologramComponent implements Stan
@Override @Override
public void despawnEntities() { public void despawnEntities() {
for (StandardHologramLine line : getLinesUnsafe()) { for (T line : lines) {
line.despawn(); line.despawn();
} }
} }
protected void checkState() { private void checkNotDeleted() {
Preconditions.checkState(!deleted, "hologram already deleted"); Preconditions.checkState(!deleted, "hologram is not usable after being deleted");
} }
@Override @Override
public String toString() { public String toString() {
return "BaseHologram [location=" + getLocation() + ", lines=" + getLinesUnsafe() + ", deleted=" + deleted + "]"; return "BaseHologram [location=" + getLocation() + ", lines=" + lines + ", deleted=" + deleted + "]";
} }
/* /*

View File

@ -13,22 +13,13 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.ArrayList; public class InternalHologram extends BaseHologram<InternalHologramLine> {
import java.util.List;
public class InternalHologram extends BaseHologram {
private final String name; private final String name;
private final List<InternalHologramLine> lines;
protected InternalHologram(Location source, String name, NMSManager nmsManager) { protected InternalHologram(Location source, String name, NMSManager nmsManager) {
super(source, nmsManager); super(source, nmsManager);
this.name = name; this.name = name;
this.lines = new ArrayList<>();
}
public String getName() {
return name;
} }
public InternalTextLine createTextLine(String text, String serializedConfigValue) { public InternalTextLine createTextLine(String text, String serializedConfigValue) {
@ -39,14 +30,13 @@ public class InternalHologram extends BaseHologram {
return new InternalItemLine(this, getNMSManager(), icon, serializedConfigValue); return new InternalItemLine(this, getNMSManager(), icon, serializedConfigValue);
} }
@Override public String getName() {
public Plugin getOwnerPlugin() { return name;
return HolographicDisplays.getInstance();
} }
@Override @Override
public List<InternalHologramLine> getLinesUnsafe() { public Plugin getOwnerPlugin() {
return lines; return HolographicDisplays.getInstance();
} }
@Override @Override