Make HologramPosition immutable

This commit is contained in:
filoghost 2021-08-19 15:36:10 +02:00
parent 9e966ea185
commit 55f690cec9
17 changed files with 108 additions and 209 deletions

View File

@ -120,38 +120,6 @@ public interface Hologram {
*/
@NotNull HologramPosition getPosition();
/**
* Returns the world name of the hologram position.
*
* @return the world name of the hologram position
* @since 1
*/
@NotNull String getPositionWorldName();
/**
* Returns the X coordinate of the hologram position.
*
* @return the X coordinate of the hologram position
* @since 1
*/
double getPositionX();
/**
* Returns the Y coordinate of the hologram position.
*
* @return the Y coordinate of the hologram position
* @since 1
*/
double getPositionY();
/**
* Returns the Z coordinate of the hologram position.
*
* @return the Z coordinate of the hologram position
* @since 1
*/
double getPositionZ();
/**
* Moves the hologram to the given position.
*

View File

@ -27,25 +27,15 @@ public interface HologramPosition {
@NotNull String getWorldName();
void setWorldName(@NotNull String worldName);
@Nullable World getWorldIfLoaded();
void setWorld(@NotNull World world);
double getX();
void setX(double x);
double getY();
void setY(double y);
double getZ();
void setZ(double z);
HologramPosition add(double x, double y, double z);
@NotNull HologramPosition add(double x, double y, double z);
int getBlockX();

View File

@ -10,7 +10,7 @@ import me.filoghost.holographicdisplays.api.HolographicDisplaysAPI;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import me.filoghost.holographicdisplays.api.internal.HolographicDisplaysAPIProvider;
import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager;
import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramPosition;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition;
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
import org.bukkit.Location;
import org.bukkit.World;
@ -44,17 +44,17 @@ public class DefaultHolographicDisplaysAPIProvider extends HolographicDisplaysAP
@Override
public HologramPosition createHologramPosition(World world, double x, double y, double z) {
Preconditions.notNull(world, "world");
return new APIHologramPosition(world.getName(), x, y, z);
return new BaseHologramPosition(world.getName(), x, y, z);
}
@Override
public HologramPosition createHologramPosition(String worldName, double x, double y, double z) {
return new APIHologramPosition(worldName, x, y, z);
return new BaseHologramPosition(worldName, x, y, z);
}
@Override
public HologramPosition createHologramPosition(Location location) {
return new APIHologramPosition(location);
return new BaseHologramPosition(location);
}
}

View File

@ -81,31 +81,27 @@ public class V2HologramAdapter implements Hologram {
@Override
public Location getLocation() {
return new Location(
v3Hologram.getPositionWorldIfLoaded(),
v3Hologram.getPositionX(),
v3Hologram.getPositionY(),
v3Hologram.getPositionZ());
return v3Hologram.getPosition().toLocation();
}
@Override
public double getX() {
return v3Hologram.getPositionX();
return v3Hologram.getPosition().getX();
}
@Override
public double getY() {
return v3Hologram.getPositionY();
return v3Hologram.getPosition().getY();
}
@Override
public double getZ() {
return v3Hologram.getPositionZ();
return v3Hologram.getPosition().getZ();
}
@Override
public World getWorld() {
return v3Hologram.getPositionWorldIfLoaded();
return v3Hologram.getWorldIfLoaded();
}
@Override

View File

@ -36,19 +36,19 @@ public class AlignCommand extends HologramSubCommand {
CommandValidate.check(hologram != referenceHologram, "The holograms must not be the same.");
BaseHologramPosition referencePosition = referenceHologram.getBasePosition();
BaseHologramPosition newPosition = hologram.getBasePosition();
BaseHologramPosition referencePosition = referenceHologram.getPosition();
BaseHologramPosition newPosition = hologram.getPosition();
String axis = args[0];
if (axis.equalsIgnoreCase("x")) {
newPosition.setX(referencePosition.getX());
newPosition = newPosition.withX(referencePosition.getX());
} else if (axis.equalsIgnoreCase("y")) {
newPosition.setY(referencePosition.getY());
newPosition = newPosition.withY(referencePosition.getY());
} else if (axis.equalsIgnoreCase("z")) {
newPosition.setZ(referencePosition.getZ());
newPosition = newPosition.withZ(referencePosition.getZ());
} else if (axis.equalsIgnoreCase("xz")) {
newPosition.setX(referencePosition.getX());
newPosition.setZ(referencePosition.getZ());
newPosition = newPosition.withX(referencePosition.getX());
newPosition = newPosition.withZ(referencePosition.getZ());
} else {
throw new CommandException("You must specify either X, Y, Z or XZ, " + axis + " is not a valid axis.");
}

View File

@ -50,7 +50,7 @@ public class CreateCommand extends HologramSubCommand {
boolean moveUp = player.isOnGround();
if (moveUp) {
spawnPosition.add(0, 1.2, 0);
spawnPosition = spawnPosition.add(0, 1.2, 0);
}
InternalHologram hologram = hologramEditor.create(spawnPosition, hologramName);

View File

@ -43,7 +43,7 @@ public class NearCommand extends HologramSubCommand {
List<InternalHologram> nearHolograms = new ArrayList<>();
for (InternalHologram hologram : hologramEditor.getHolograms()) {
BaseHologramPosition position = hologram.getBasePosition();
BaseHologramPosition position = hologram.getPosition();
if (position.isInWorld(world) && position.distance(player.getLocation()) <= radius) {
nearHolograms.add(hologram);
}

View File

@ -34,7 +34,7 @@ public class TeleportCommand extends HologramSubCommand {
Player player = CommandValidate.getPlayerSender(sender);
InternalHologram hologram = hologramEditor.getExistingHologram(args[0]);
hologramEditor.teleportLookingDown(player, hologram.getBasePosition().toLocation());
hologramEditor.teleportLookingDown(player, hologram.getPosition().toLocation());
player.sendMessage(ColorScheme.PRIMARY + "Teleported to the hologram \"" + hologram.getName() + "\".");
}

View File

@ -34,7 +34,7 @@ public class HologramConfig {
serializedLines.add(line.getSerializedConfigValue());
}
BaseHologramPosition position = hologram.getBasePosition();
BaseHologramPosition position = hologram.getPosition();
this.positionConfigSection = new ConfigSection();
positionConfigSection.setString("world", position.getWorldName());
positionConfigSection.setDouble("x", position.getX());

View File

@ -87,7 +87,7 @@ public class DisplayFormat {
}
public static void sendHologramSummary(CommandSender sender, InternalHologram hologram, boolean showWorld) {
BaseHologramPosition position = hologram.getBasePosition();
BaseHologramPosition position = hologram.getPosition();
sender.sendMessage(ColorScheme.SECONDARY_DARK + "- " + ColorScheme.SECONDARY_BOLD + hologram.getName()
+ ColorScheme.SECONDARY_DARK + " (" + hologram.getLines().size() + " lines) at "
+ (showWorld ? "world: \"" + position.getWorldName() + "\", " : "")

View File

@ -8,7 +8,6 @@ package me.filoghost.holographicdisplays.plugin.hologram.api;
import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.api.hologram.Hologram;
import me.filoghost.holographicdisplays.api.hologram.HologramLine;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramAdapter;
import me.filoghost.holographicdisplays.plugin.config.Settings;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram;
@ -170,16 +169,6 @@ public class APIHologram extends BaseHologram implements Hologram {
return height;
}
@Override
public @NotNull HologramPosition getPosition() {
return new APIHologramPosition(getPositionWorldName(), getPositionX(), getPositionY(), getPositionZ());
}
@Override
public void setPosition(@NotNull HologramPosition position) {
super.setPosition(position.getWorldName(), position.getX(), position.getY(), position.getZ());
}
@Override
public long getCreationTimestamp() {
return creationTimestamp;

View File

@ -1,28 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.plugin.hologram.api;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition;
import org.bukkit.Location;
public class APIHologramPosition extends BaseHologramPosition implements HologramPosition {
public APIHologramPosition(String worldName, double x, double y, double z) {
super(worldName, x, y, z);
}
public APIHologramPosition(Location location) {
super(location);
}
@Override
public APIHologramPosition add(double x, double y, double z) {
super.add(x, y, z);
return this;
}
}

View File

@ -6,6 +6,7 @@
package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager;
import org.bukkit.Chunk;
import org.bukkit.Location;
@ -41,31 +42,15 @@ public abstract class BaseHologram extends BaseHologramComponent {
getLines().setDeleted();
}
public BaseHologramPosition getBasePosition() {
return position.toBasePosition();
public @NotNull BaseHologramPosition getPosition() {
return position.getPosition();
}
public @Nullable World getPositionWorldIfLoaded() {
public @Nullable World getWorldIfLoaded() {
return position.getWorldIfLoaded();
}
public @NotNull String getPositionWorldName() {
return position.getWorldName();
}
public double getPositionX() {
return position.getX();
}
public double getPositionY() {
return position.getY();
}
public double getPositionZ() {
return position.getZ();
}
public void setPosition(@NotNull BaseHologramPosition position) {
public void setPosition(@NotNull HologramPosition position) {
Preconditions.notNull(position, "position");
setPosition(position.getWorldName(), position.getX(), position.getY(), position.getZ());
}

View File

@ -53,7 +53,7 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
}
public @Nullable World getWorldIfLoaded() {
return hologram.getPositionWorldIfLoaded();
return hologram.getWorldIfLoaded();
}
public boolean isInLoadedChunk() {

View File

@ -5,6 +5,7 @@
*/
package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import me.filoghost.holographicdisplays.plugin.config.Settings;
import java.util.ArrayList;
@ -114,7 +115,8 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
* The second line is below the first, and so on.
*/
protected void updateLinePositions() {
double currentLineY = hologram.getPositionY();
HologramPosition hologramPosition = hologram.getPosition();
double currentLineY = hologramPosition.getY();
for (int i = 0; i < lines.size(); i++) {
T line = lines.get(i);
@ -124,7 +126,7 @@ public class BaseHologramLines<T extends EditableHologramLine> implements Iterab
currentLineY -= Settings.spaceBetweenLines;
}
line.setPosition(hologram.getPositionX(), currentLineY, hologram.getPositionZ());
line.setPosition(hologramPosition.getX(), currentLineY, hologramPosition.getZ());
}
}

View File

@ -6,6 +6,7 @@
package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.api.hologram.HologramPosition;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
@ -15,18 +16,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.Locale;
public class BaseHologramPosition {
public class BaseHologramPosition implements HologramPosition {
private @NotNull String worldName;
private double x, y, z;
public BaseHologramPosition(BaseHologramPosition position) {
Preconditions.notNull(position, "position");
this.worldName = position.worldName;
this.x = position.x;
this.y = position.y;
this.z = position.z;
}
private final @NotNull String worldName;
private final double x, y, z;
public BaseHologramPosition(@NotNull String worldName, double x, double y, double z) {
Preconditions.notNull(worldName, "worldName");
@ -36,7 +29,7 @@ public class BaseHologramPosition {
this.z = z;
}
public BaseHologramPosition(Location location) {
public BaseHologramPosition(@NotNull Location location) {
Preconditions.notNull(location, "location");
Preconditions.notNull(location.getWorld(), "location's world");
this.worldName = location.getWorld().getName();
@ -45,87 +38,78 @@ public class BaseHologramPosition {
this.z = location.getZ();
}
@Override
public @NotNull String getWorldName() {
return worldName;
}
public void setWorldName(@NotNull String worldName) {
Preconditions.notNull(worldName, "worldName");
this.worldName = worldName;
}
public boolean isInWorld(World world) {
public boolean isInWorld(@Nullable World world) {
return world != null && isInWorld(world.getName());
}
public boolean isInWorld(String worldName) {
public boolean isInWorld(@Nullable String worldName) {
// Use the same comparison used by Bukkit.getWorld(...)
return worldName != null && worldName.toLowerCase(Locale.ENGLISH).equals(this.worldName.toLowerCase(Locale.ENGLISH));
}
@Override
public @Nullable World getWorldIfLoaded() {
return Bukkit.getWorld(worldName);
}
public void setWorld(@NotNull World world) {
Preconditions.notNull(world, "world");
this.worldName = world.getName();
}
@Override
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
@Override
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public BaseHologramPosition add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
public void set(String worldName, double x, double y, double z) {
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
}
@Override
public int getBlockX() {
return Location.locToBlock(x);
}
@Override
public int getBlockY() {
return Location.locToBlock(y);
}
@Override
public int getBlockZ() {
return Location.locToBlock(z);
}
@Override
public @NotNull BaseHologramPosition add(double x, double y, double z) {
return new BaseHologramPosition(this.worldName, this.x + x, this.y + y, this.z + z);
}
public @NotNull BaseHologramPosition withX(double x) {
return new BaseHologramPosition(this.worldName, x, this.y, this.z);
}
public @NotNull BaseHologramPosition withY(double y) {
return new BaseHologramPosition(this.worldName, this.x, y, this.z);
}
public @NotNull BaseHologramPosition withZ(double z) {
return new BaseHologramPosition(this.worldName, this.x, this.y, z);
}
@Override
public double distance(@NotNull Location location) {
return Math.sqrt(distanceSquared(location));
}
@Override
public double distanceSquared(@NotNull Location location) {
Preconditions.notNull(location, "location");
return NumberConversions.square(this.x - location.getX())
@ -133,10 +117,37 @@ public class BaseHologramPosition {
+ NumberConversions.square(this.z - location.getZ());
}
@Override
public @NotNull Location toLocation() {
return new Location(getWorldIfLoaded(), x, y, z);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BaseHologramPosition other = (BaseHologramPosition) obj;
return this.worldName.equals(other.worldName)
&& Double.doubleToLongBits(this.x) == Double.doubleToLongBits(other.x)
&& Double.doubleToLongBits(this.y) == Double.doubleToLongBits(other.y)
&& Double.doubleToLongBits(this.z) == Double.doubleToLongBits(other.z);
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + worldName.hashCode();
result = 31 * result + Double.hashCode(x);
result = 31 * result + Double.hashCode(y);
result = 31 * result + Double.hashCode(z);
return result;
}
@Override
public String toString() {
return "HologramPosition{"

View File

@ -5,6 +5,7 @@
*/
package me.filoghost.holographicdisplays.plugin.hologram.base;
import me.filoghost.fcommons.Preconditions;
import me.filoghost.holographicdisplays.plugin.util.CachedBoolean;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
@ -15,25 +16,26 @@ import org.jetbrains.annotations.Nullable;
class WorldAwareHologramPosition {
private final BaseHologramPosition basePosition;
private @NotNull BaseHologramPosition position;
private @Nullable World world;
private int chunkX, chunkZ;
private final CachedBoolean chunkLoaded;
WorldAwareHologramPosition(BaseHologramPosition basePosition) {
this.basePosition = new BaseHologramPosition(basePosition);
this.world = Bukkit.getWorld(basePosition.getWorldName());
this.chunkX = getChunkCoordinate(basePosition.getX());
this.chunkZ = getChunkCoordinate(basePosition.getZ());
WorldAwareHologramPosition(@NotNull BaseHologramPosition position) {
Preconditions.notNull(position, "position");
this.position = position;
this.world = Bukkit.getWorld(position.getWorldName());
this.chunkX = getChunkCoordinate(position.getX());
this.chunkZ = getChunkCoordinate(position.getZ());
this.chunkLoaded = new CachedBoolean(() -> world != null && world.isChunkLoaded(chunkX, chunkZ));
}
final void set(String worldName, double x, double y, double z) {
boolean worldChanged = !basePosition.isInWorld(worldName);
boolean worldChanged = !position.isInWorld(worldName);
int chunkX = getChunkCoordinate(x);
int chunkZ = getChunkCoordinate(z);
basePosition.set(worldName, x, y, z);
position = new BaseHologramPosition(worldName, x, y, z);
if (worldChanged || this.chunkX != chunkX || this.chunkZ != chunkZ) {
if (worldChanged) {
@ -50,14 +52,14 @@ class WorldAwareHologramPosition {
}
void onWorldLoad(World world) {
if (basePosition.isInWorld(world)) {
if (position.isInWorld(world)) {
this.world = world;
chunkLoaded.invalidate();
}
}
void onWorldUnload(World world) {
if (basePosition.isInWorld(world)) {
if (position.isInWorld(world)) {
this.world = null;
chunkLoaded.set(false);
}
@ -83,29 +85,13 @@ class WorldAwareHologramPosition {
return world;
}
@NotNull String getWorldName() {
return basePosition.getWorldName();
}
double getX() {
return basePosition.getX();
}
double getY() {
return basePosition.getY();
}
double getZ() {
return basePosition.getZ();
}
BaseHologramPosition toBasePosition() {
return new BaseHologramPosition(basePosition);
@NotNull BaseHologramPosition getPosition() {
return position;
}
@Override
public String toString() {
return basePosition.toString();
return position.toString();
}
}