Improve ImmutablePosition and related classes

This commit is contained in:
filoghost 2021-09-15 11:54:09 +02:00
parent 3935af2bf9
commit b4c8c6ede4
8 changed files with 41 additions and 46 deletions

View File

@ -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;

View File

@ -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

View File

@ -55,7 +55,7 @@ public class DefaultHolographicDisplaysAPIProvider extends HolographicDisplaysAP
@Override
public Position getPosition(Location location) {
return new ImmutablePosition(location);
return ImmutablePosition.of(location);
}
@Override

View File

@ -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

View File

@ -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) {

View File

@ -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()
+ "}";

View File

@ -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 {

View File

@ -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