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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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