From b4c8c6ede4c1c2ae0bca4d7e811bb2a89c2b06b9 Mon Sep 17 00:00:00 2001 From: filoghost Date: Wed, 15 Sep 2021 11:54:09 +0200 Subject: [PATCH] Improve ImmutablePosition and related classes --- .../plugin/api/current/APIHologram.java | 6 ++++ .../DefaultHolographicDisplaysAPI.java | 4 +-- ...DefaultHolographicDisplaysAPIProvider.java | 2 +- .../plugin/api/v2/V2HologramsAPIProvider.java | 2 +- .../plugin/commands/subs/CreateCommand.java | 2 +- .../plugin/hologram/base/BaseHologram.java | 33 +++++++++---------- .../hologram/base/HologramPosition.java | 19 ++++------- .../hologram/base/ImmutablePosition.java | 19 +++++------ 8 files changed, 41 insertions(+), 46 deletions(-) diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologram.java index 25c104e6..a3e33372 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/APIHologram.java @@ -6,6 +6,7 @@ package me.filoghost.holographicdisplays.plugin.api.current; import me.filoghost.fcommons.Preconditions; +import me.filoghost.holographicdisplays.api.Position; import me.filoghost.holographicdisplays.api.hologram.Hologram; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; @@ -46,6 +47,11 @@ public class APIHologram extends BaseHologram implements Hologram { return visibilitySettings; } + @Override + public void setPosition(@NotNull Position position) { + super.setPosition(ImmutablePosition.of(position)); + } + @Override public boolean isAllowPlaceholders() { return allowPlaceholders; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPI.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPI.java index 5d352b69..3ad22578 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPI.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPI.java @@ -37,7 +37,7 @@ class DefaultHolographicDisplaysAPI implements HolographicDisplaysAPI { Preconditions.notNull(location.getWorld(), "location.getWorld()"); Preconditions.checkMainThread("async hologram creation"); - return apiHologramManager.createHologram(new ImmutablePosition(location), plugin); + return apiHologramManager.createHologram(ImmutablePosition.of(location), plugin); } @Override @@ -46,7 +46,7 @@ class DefaultHolographicDisplaysAPI implements HolographicDisplaysAPI { Preconditions.notNull(position.getWorldName(), "position.getWorldName()"); Preconditions.checkMainThread("async hologram creation"); - return apiHologramManager.createHologram(new ImmutablePosition(position), plugin); + return apiHologramManager.createHologram(ImmutablePosition.of(position), plugin); } @Override diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPIProvider.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPIProvider.java index 43339b5e..c9d78497 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPIProvider.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/current/DefaultHolographicDisplaysAPIProvider.java @@ -55,7 +55,7 @@ public class DefaultHolographicDisplaysAPIProvider extends HolographicDisplaysAP @Override public Position getPosition(Location location) { - return new ImmutablePosition(location); + return ImmutablePosition.of(location); } @Override diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java index 1cc18a8f..08560733 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java @@ -40,7 +40,7 @@ public class V2HologramsAPIProvider extends HologramsAPIProvider { Preconditions.notNull(source.getWorld(), "source.getWorld()"); Preconditions.checkMainThread("async hologram creation"); - return hologramManager.createHologram(new ImmutablePosition(source), plugin); + return hologramManager.createHologram(ImmutablePosition.of(source), plugin); } @Override diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java index 6719fef9..b53da0f0 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/commands/subs/CreateCommand.java @@ -46,7 +46,7 @@ public class CreateCommand extends HologramSubCommand { "The name must contain only alphanumeric characters, underscores and hyphens."); CommandValidate.check(!hologramEditor.hologramExists(hologramName), "A hologram with that name already exists."); - ImmutablePosition spawnPosition = new ImmutablePosition(player.getLocation()); + ImmutablePosition spawnPosition = ImmutablePosition.of(player.getLocation()); boolean moveUp = player.isOnGround(); if (moveUp) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java index cd07defe..21973207 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologram.java @@ -6,7 +6,6 @@ package me.filoghost.holographicdisplays.plugin.hologram.base; import me.filoghost.fcommons.Preconditions; -import me.filoghost.holographicdisplays.api.Position; import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; import org.bukkit.Chunk; import org.bukkit.Location; @@ -18,11 +17,11 @@ import org.jetbrains.annotations.Nullable; public abstract class BaseHologram extends BaseHologramComponent { - private final HologramPosition position; + private final HologramPosition hologramPosition; private final LineTrackerManager lineTrackerManager; public BaseHologram(ImmutablePosition position, LineTrackerManager lineTrackerManager) { - this.position = new HologramPosition(position); + this.hologramPosition = new HologramPosition(position); this.lineTrackerManager = lineTrackerManager; } @@ -43,16 +42,11 @@ public abstract class BaseHologram extends BaseHologramComponent { } public @NotNull ImmutablePosition getPosition() { - return position.getPosition(); + return hologramPosition.getPosition(); } public @Nullable World getWorldIfLoaded() { - return position.getWorldIfLoaded(); - } - - public void setPosition(@NotNull Position position) { - Preconditions.notNull(position, "position"); - setPosition(position.getWorldName(), position.getX(), position.getY(), position.getZ()); + return hologramPosition.getWorldIfLoaded(); } public void setPosition(@NotNull Location location) { @@ -68,36 +62,41 @@ public abstract class BaseHologram extends BaseHologramComponent { public void setPosition(@NotNull String worldName, double x, double y, double z) { Preconditions.notNull(worldName, "worldName"); + setPosition(new ImmutablePosition(worldName, x, y, z)); + } + + public void setPosition(@NotNull ImmutablePosition position) { + Preconditions.notNull(position, "position"); checkNotDeleted(); - position.set(worldName, x, y, z); + hologramPosition.set(position); getLines().updatePositions(); } protected void onWorldLoad(World world) { - position.onWorldLoad(world); + hologramPosition.onWorldLoad(world); } protected void onWorldUnload(World world) { - position.onWorldUnload(world); + hologramPosition.onWorldUnload(world); } protected void onChunkLoad(Chunk chunk) { - position.onChunkLoad(chunk); + hologramPosition.onChunkLoad(chunk); } protected void onChunkUnload(Chunk chunk) { - position.onChunkUnload(chunk); + hologramPosition.onChunkUnload(chunk); } protected boolean isInLoadedChunk() { - return position.isChunkLoaded(); + return hologramPosition.isChunkLoaded(); } @Override public String toString() { return "Hologram{" - + "position=" + position + + "position=" + hologramPosition.getPosition() + ", lines=" + getLines() + ", deleted=" + isDeleted() + "}"; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/HologramPosition.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/HologramPosition.java index 019c78bc..60a502e9 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/HologramPosition.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/HologramPosition.java @@ -5,7 +5,6 @@ */ package me.filoghost.holographicdisplays.plugin.hologram.base; -import me.filoghost.fcommons.Preconditions; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; @@ -21,7 +20,6 @@ class HologramPosition { private @NotNull ChunkLoadState chunkLoadState; HologramPosition(@NotNull ImmutablePosition position) { - Preconditions.notNull(position, "position"); this.position = position; this.world = Bukkit.getWorld(position.getWorldName()); this.chunkX = getChunkCoordinate(position.getX()); @@ -29,16 +27,16 @@ class HologramPosition { this.chunkLoadState = ChunkLoadState.UNKNOWN; } - final void set(String worldName, double x, double y, double z) { - boolean worldChanged = !position.isInWorld(worldName); - int chunkX = getChunkCoordinate(x); - int chunkZ = getChunkCoordinate(z); + final void set(@NotNull ImmutablePosition position) { + boolean worldChanged = !this.position.isInSameWorld(position); + int chunkX = getChunkCoordinate(position.getX()); + int chunkZ = getChunkCoordinate(position.getZ()); - position = new ImmutablePosition(worldName, x, y, z); + this.position = position; if (worldChanged || this.chunkX != chunkX || this.chunkZ != chunkZ) { if (worldChanged) { - this.world = Bukkit.getWorld(worldName); + this.world = position.getWorldIfLoaded(); } this.chunkX = chunkX; this.chunkZ = chunkZ; @@ -101,11 +99,6 @@ class HologramPosition { return position; } - @Override - public String toString() { - return position.toString(); - } - private enum ChunkLoadState { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/ImmutablePosition.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/ImmutablePosition.java index 6f43d8e4..43635efa 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/ImmutablePosition.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/ImmutablePosition.java @@ -31,22 +31,19 @@ public final class ImmutablePosition implements Position { this.z = z; } - public ImmutablePosition(@NotNull Location location) { + public static ImmutablePosition of(@NotNull Location location) { Preconditions.notNull(location, "location"); Preconditions.notNull(location.getWorld(), "location.getWorld()"); - this.worldName = location.getWorld().getName(); - this.x = location.getX(); - this.y = location.getY(); - this.z = location.getZ(); + return new ImmutablePosition(location.getWorld().getName(), location.getX(), location.getY(), location.getZ()); } - public ImmutablePosition(@NotNull Position position) { + public static ImmutablePosition of(@NotNull Position position) { Preconditions.notNull(position, "position"); - Preconditions.notNull(position.getWorldName(), "position.getWorldName()"); - this.worldName = position.getWorldName(); - this.x = position.getX(); - this.y = position.getY(); - this.z = position.getZ(); + if (position instanceof ImmutablePosition) { + return (ImmutablePosition) position; + } else { + return new ImmutablePosition(position.getWorldName(), position.getX(), position.getY(), position.getZ()); + } } @Override