Refactor hologram location

This commit is contained in:
filoghost 2021-08-05 18:10:10 +02:00
parent f221f1ed72
commit a1bafc4366
13 changed files with 220 additions and 123 deletions

View File

@ -81,27 +81,27 @@ public class V2HologramAdapter implements Hologram {
@Override
public Location getLocation() {
return v3Hologram.getLocation();
return v3Hologram.getHologramLocation().toBukkitLocation();
}
@Override
public double getX() {
return v3Hologram.getX();
return v3Hologram.getHologramLocation().getX();
}
@Override
public double getY() {
return v3Hologram.getY();
return v3Hologram.getHologramLocation().getY();
}
@Override
public double getZ() {
return v3Hologram.getZ();
return v3Hologram.getHologramLocation().getZ();
}
@Override
public World getWorld() {
return v3Hologram.getWorld();
return v3Hologram.getHologramLocation().getWorld();
}
@Override

View File

@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.commands.HologramSubCommand;
import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor;
import me.filoghost.holographicdisplays.plugin.event.InternalHologramChangeEvent.ChangeType;
import me.filoghost.holographicdisplays.plugin.format.ColorScheme;
import me.filoghost.holographicdisplays.plugin.hologram.base.HologramLocation;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologram;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
@ -36,18 +37,19 @@ public class AlignCommand extends HologramSubCommand {
CommandValidate.check(hologram != referenceHologram, "The holograms must not be the same.");
Location newLocation = hologram.getLocation();
HologramLocation referenceLocation = referenceHologram.getHologramLocation();
Location newLocation = hologram.getHologramLocation().toBukkitLocation();
String axis = args[0];
if (axis.equalsIgnoreCase("x")) {
newLocation.setX(referenceHologram.getX());
newLocation.setX(referenceLocation.getX());
} else if (axis.equalsIgnoreCase("y")) {
newLocation.setY(referenceHologram.getY());
newLocation.setY(referenceLocation.getY());
} else if (axis.equalsIgnoreCase("z")) {
newLocation.setZ(referenceHologram.getZ());
newLocation.setZ(referenceLocation.getZ());
} else if (axis.equalsIgnoreCase("xz")) {
newLocation.setX(referenceHologram.getX());
newLocation.setZ(referenceHologram.getZ());
newLocation.setX(referenceLocation.getX());
newLocation.setZ(referenceLocation.getZ());
} else {
throw new CommandException("You must specify either X, Y, Z or XZ, " + axis + " is not a valid axis.");
}

View File

@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.commands.HologramSubCommand;
import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor;
import me.filoghost.holographicdisplays.plugin.format.ColorScheme;
import me.filoghost.holographicdisplays.plugin.format.DisplayFormat;
import me.filoghost.holographicdisplays.plugin.hologram.base.HologramLocation;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologram;
import org.bukkit.command.CommandSender;
@ -54,11 +55,11 @@ public class ListCommand extends HologramSubCommand {
for (int i = fromIndex; i < toIndex; i++) {
if (i < holograms.size()) {
InternalHologram hologram = holograms.get(i);
HologramLocation location = hologram.getHologramLocation();
sender.sendMessage(ColorScheme.SECONDARY_DARKER + "- " + ColorScheme.SECONDARY_BOLD + hologram.getName()
+ " " + ColorScheme.SECONDARY_DARKER + "at x: " + (int) hologram.getX()
+ ", y: " + (int) hologram.getY()
+ ", z: " + (int) hologram.getZ()
+ " (lines: " + hologram.getLineCount() + ", world: \"" + hologram.getWorld().getName() + "\")");
+ " " + ColorScheme.SECONDARY_DARKER + "at"
+ " x: " + location.getBlockX() + ", y: " + location.getBlockY() + ", z: " + location.getBlockZ()
+ " (lines: " + hologram.getLineCount() + ", world: \"" + location.getWorld().getName() + "\")");
}
}

View File

@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.commands.HologramSubCommand;
import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor;
import me.filoghost.holographicdisplays.plugin.format.ColorScheme;
import me.filoghost.holographicdisplays.plugin.format.DisplayFormat;
import me.filoghost.holographicdisplays.plugin.hologram.base.HologramLocation;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologram;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
@ -40,11 +41,11 @@ public class NearCommand extends HologramSubCommand {
CommandValidate.check(radius > 0, "Radius must be at least 1.");
World world = player.getWorld();
int radiusSquared = radius * radius;
List<InternalHologram> nearHolograms = new ArrayList<>();
for (InternalHologram hologram : hologramEditor.getHolograms()) {
if (hologram.getWorld().equals(world) && hologram.getLocation().distanceSquared(player.getLocation()) <= radiusSquared) {
HologramLocation location = hologram.getHologramLocation();
if (location.getWorld().equals(world) && location.distance(player.getLocation()) <= radius) {
nearHolograms.add(hologram);
}
}
@ -53,11 +54,10 @@ public class NearCommand extends HologramSubCommand {
DisplayFormat.sendTitle(player, "Near holograms");
for (InternalHologram nearHologram : nearHolograms) {
HologramLocation location = nearHologram.getHologramLocation();
player.sendMessage(ColorScheme.SECONDARY_DARKER + "- "
+ ColorScheme.SECONDARY_BOLD + nearHologram.getName() + " " + ColorScheme.SECONDARY_DARKER + "at"
+ " x: " + (int) nearHologram.getX() + ","
+ " y: " + (int) nearHologram.getY() + ","
+ " z: " + (int) nearHologram.getZ()
+ " x: " + location.getBlockX() + ", y: " + location.getBlockY() + ", z: " + location.getBlockZ()
+ " (lines: " + nearHologram.getLineCount() + ")");
}
}

View File

@ -34,7 +34,7 @@ public class TeleportCommand extends HologramSubCommand {
Player player = CommandValidate.getPlayerSender(sender);
InternalHologram hologram = hologramEditor.getHologram(args[0]);
hologramEditor.teleportLookingDown(player, hologram.getLocation());
hologramEditor.teleportLookingDown(player, hologram.getHologramLocation().toBukkitLocation());
player.sendMessage(ColorScheme.PRIMARY + "You were teleported to the hologram named '" + hologram.getName() + "'.");
}

View File

@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.config;
import me.filoghost.fcommons.Strings;
import me.filoghost.fcommons.config.ConfigSection;
import me.filoghost.holographicdisplays.plugin.hologram.base.HologramLocation;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologram;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramLine;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramManager;
@ -42,7 +43,7 @@ public class HologramConfig {
serializedLines.add(line.getSerializedConfigValue());
}
this.serializedLocation = serializeLocation(hologram.getLocation());
this.serializedLocation = serializeLocation(hologram.getHologramLocation());
}
public ConfigSection toConfigSection() {
@ -77,11 +78,11 @@ public class HologramConfig {
return hologram;
}
private String serializeLocation(Location loc) {
return loc.getWorld().getName()
+ ", " + LOCATION_NUMBER_FORMAT.format(loc.getX())
+ ", " + LOCATION_NUMBER_FORMAT.format(loc.getY())
+ ", " + LOCATION_NUMBER_FORMAT.format(loc.getZ());
private String serializeLocation(HologramLocation location) {
return location.getWorld().getName()
+ ", " + LOCATION_NUMBER_FORMAT.format(location.getX())
+ ", " + LOCATION_NUMBER_FORMAT.format(location.getY())
+ ", " + LOCATION_NUMBER_FORMAT.format(location.getZ());
}
private Location deserializeLocation(String serializedLocation) throws HologramLoadException {

View File

@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.config.Settings;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
@ -133,6 +134,31 @@ public class APIHologram extends BaseHologram<APIHologramLine> implements Hologr
return height;
}
@Override
public @NotNull Location getLocation() {
return getHologramLocation().toBukkitLocation();
}
@Override
public @NotNull World getWorld() {
return getHologramLocation().getWorld();
}
@Override
public double getX() {
return getHologramLocation().getX();
}
@Override
public double getY() {
return getHologramLocation().getY();
}
@Override
public double getZ() {
return getHologramLocation().getZ();
}
@Override
public long getCreationTimestamp() {
return creationTimestamp;

View File

@ -8,8 +8,6 @@ package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.plugin.config.Settings;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import me.filoghost.holographicdisplays.plugin.util.CachedBoolean;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
@ -23,18 +21,17 @@ import java.util.List;
public abstract class BaseHologram<T extends EditableHologramLine> extends BaseHologramComponent {
private final LineTrackerManager lineTrackerManager;
private final HologramLocation location;
private final List<T> lines;
private final List<T> unmodifiableLinesView;
private final CachedBoolean isInLoadedChunk;
private final LineTrackerManager lineTrackerManager;
public BaseHologram(Location location, LineTrackerManager lineTrackerManager) {
Preconditions.notNull(location, "location");
this.setLocation(location);
this.lineTrackerManager = lineTrackerManager;
this.location = new HologramLocation(location);
this.lines = new ArrayList<>();
this.unmodifiableLinesView = Collections.unmodifiableList(lines);
this.isInLoadedChunk = new CachedBoolean(() -> getWorld().isChunkLoaded(getChunkX(), getChunkZ()));
this.lineTrackerManager = lineTrackerManager;
}
protected abstract boolean isVisibleTo(Player player);
@ -127,34 +124,19 @@ public abstract class BaseHologram<T extends EditableHologramLine> extends BaseH
}
public void teleport(@NotNull Location location) {
Preconditions.notNull(location, "location");
teleport(location.getWorld(), location.getX(), location.getY(), location.getZ());
checkNotDeleted();
this.location.set(location);
updateLineLocations();
}
public void teleport(@NotNull World world, double x, double y, double z) {
checkNotDeleted();
Preconditions.notNull(world, "world");
setLocation(world, x, y, z);
this.isInLoadedChunk.invalidate();
this.location.set(world, x, y, z);
updateLineLocations();
}
protected final void onChunkLoad(Chunk chunk) {
if (this.getWorld() == chunk.getWorld() && this.getChunkX() == chunk.getX() && this.getChunkZ() == chunk.getZ()) {
this.isInLoadedChunk.set(true);
}
}
protected final void onChunkUnload(Chunk chunk) {
if (this.getWorld() == chunk.getWorld() && this.getChunkX() == chunk.getX() && this.getChunkZ() == chunk.getZ()) {
this.isInLoadedChunk.set(false);
}
}
public boolean isInLoadedChunk() {
return isInLoadedChunk.get();
public HologramLocation getHologramLocation() {
return location;
}
/**
@ -162,7 +144,7 @@ public abstract class BaseHologram<T extends EditableHologramLine> extends BaseH
* The second line is below the first, and so on.
*/
private void updateLineLocations() {
double currentLineY = getY();
double currentLineY = location.getY();
for (int i = 0; i < lines.size(); i++) {
T line = lines.get(i);
@ -172,13 +154,13 @@ public abstract class BaseHologram<T extends EditableHologramLine> extends BaseH
currentLineY -= Settings.spaceBetweenLines;
}
line.setLocation(getWorld(), getX(), currentLineY, getZ());
line.setLocation(location.getX(), currentLineY, location.getZ());
}
}
@Override
public String toString() {
return "BaseHologram [location=" + getLocation() + ", lines=" + lines + ", deleted=" + isDeleted() + "]";
return "BaseHologram [location=" + location + ", lines=" + lines + ", deleted=" + isDeleted() + "]";
}
}

View File

@ -6,66 +6,12 @@
package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.fcommons.Preconditions;
import org.bukkit.Location;
import org.bukkit.World;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
public abstract class BaseHologramComponent {
private World world;
private double x, y, z;
private int chunkX, chunkZ;
private boolean deleted;
public final @NotNull Location getLocation() {
return new Location(world, x, y, z);
}
protected final void setLocation(Location location) {
setLocation(location.getWorld(), location.getX(), location.getY(), location.getZ());
}
@MustBeInvokedByOverriders
protected void setLocation(World world, double x, double y, double z) {
Preconditions.notNull(world, "world");
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.chunkX = getChunkCoord(x);
this.chunkZ = getChunkCoord(z);
}
private int getChunkCoord(double locationCoord) {
return Location.locToBlock(locationCoord) >> 4;
}
public final World getWorld() {
return world;
}
public final double getX() {
return x;
}
public final double getY() {
return y;
}
public final double getZ() {
return z;
}
public final int getChunkX() {
return chunkX;
}
public final int getChunkZ() {
return chunkZ;
}
public final boolean isDeleted() {
return deleted;
}

View File

@ -18,6 +18,8 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
private final BaseHologram<?> hologram;
private final LineTracker<?> tracker;
private double x, y, z;
protected BaseHologramLine(BaseHologram<?> hologram) {
Preconditions.notNull(hologram, "parent hologram");
this.hologram = hologram;
@ -35,11 +37,33 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
}
@Override
public final void setLocation(World world, double x, double y, double z) {
super.setLocation(world, x, y, z);
public final void setLocation(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
setChanged();
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public World getWorld() {
return hologram.getHologramLocation().getWorld();
}
public boolean isInLoadedChunk() {
return hologram.getHologramLocation().isInLoadedChunk();
}
public final boolean isVisibleTo(Player player) {
return hologram.isVisibleTo(player);
}
@ -56,8 +80,4 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
&& isVisibleTo(player);
}
public boolean isInLoadedChunk() {
return hologram.isInLoadedChunk();
}
}

View File

@ -41,13 +41,13 @@ public abstract class BaseHologramManager<H extends BaseHologram<?>> {
public void onChunkLoad(Chunk chunk) {
for (H hologram : holograms) {
hologram.onChunkLoad(chunk);
hologram.getHologramLocation().onChunkLoad(chunk);
}
}
public void onChunkUnload(Chunk chunk) {
for (H hologram : holograms) {
hologram.onChunkUnload(chunk);
hologram.getHologramLocation().onChunkUnload(chunk);
}
}

View File

@ -5,11 +5,9 @@
*/
package me.filoghost.holographicdisplays.plugin.hologram.base;
import org.bukkit.World;
public interface EditableHologramLine {
void setLocation(World world, double x, double y, double z);
void setLocation(double x, double y, double z);
double getHeight();

View File

@ -0,0 +1,121 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.plugin.util.CachedBoolean;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.NumberConversions;
public class HologramLocation {
private World world;
private double x, y, z;
private int chunkX, chunkZ;
private final CachedBoolean chunkLoaded;
public HologramLocation(Location location) {
this.chunkLoaded = new CachedBoolean(() -> world.isChunkLoaded(chunkX, chunkZ));
set(location);
}
public World getWorld() {
return world;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public int getBlockX() {
return Location.locToBlock(x);
}
public int getBlockY() {
return Location.locToBlock(y);
}
public int getBlockZ() {
return Location.locToBlock(z);
}
protected void set(Location location) {
Preconditions.notNull(location, "location");
set(location.getWorld(), location.getX(), location.getY(), location.getZ());
}
protected void set(World world, double x, double y, double z) {
Preconditions.notNull(world, "world");
this.world = world;
this.x = x;
this.y = y;
this.z = z;
int newChunkX = getChunkCoord(x);
int newChunkZ = getChunkCoord(z);
if (this.chunkX != newChunkX || this.chunkZ != newChunkZ) {
this.chunkX = newChunkX;
this.chunkZ = newChunkZ;
this.chunkLoaded.invalidate();
}
}
private int getChunkCoord(double locationCoord) {
return Location.locToBlock(locationCoord) >> 4;
}
public boolean isInLoadedChunk() {
return chunkLoaded.get();
}
public void onChunkLoad(Chunk chunk) {
if (world == chunk.getWorld() && chunkX == chunk.getX() && chunkZ == chunk.getZ()) {
chunkLoaded.set(true);
}
}
public void onChunkUnload(Chunk chunk) {
if (world == chunk.getWorld() && chunkX == chunk.getX() && chunkZ == chunk.getZ()) {
chunkLoaded.set(false);
}
}
public Location toBukkitLocation() {
return new Location(world, x, y, z);
}
public double distance(Location location) {
return Math.sqrt(distanceSquared(location));
}
public double distanceSquared(Location location) {
return NumberConversions.square(this.x - location.getX())
+ NumberConversions.square(this.y - location.getY())
+ NumberConversions.square(this.z - location.getZ());
}
@Override
public String toString() {
return "HologramLocation ["
+ "world=" + world
+ ", x=" + x
+ ", y=" + y
+ ", z=" + z
+ "]";
}
}