fancyholograms-v3: Add worldName property

This commit is contained in:
Oliver
2025-05-17 21:20:21 +02:00
parent 59db4ca61e
commit f16b296347
13 changed files with 68 additions and 24 deletions

View File

@@ -25,6 +25,7 @@ public class HologramData implements YamlData {
private final HologramType type;
private String filePath;
private Location location;
private String worldName;
private boolean hasChanges = false;
private int visibilityDistance = DEFAULT_VISIBILITY_DISTANCE;
private Visibility visibility = DEFAULT_VISIBILITY;
@@ -41,6 +42,11 @@ public class HologramData implements YamlData {
this.name = name;
this.type = type;
this.location = location;
if (location != null && location.getWorld() != null) {
this.worldName = location.getWorld().getName();
} else {
this.worldName = null;
}
}
public @NotNull String getName() {
@@ -66,7 +72,32 @@ public class HologramData implements YamlData {
}
public HologramData setLocation(@Nullable Location location) {
this.location = location != null ? location.clone() : null;
if (location == null) {
this.location = null;
this.worldName = null;
} else {
this.location = location.clone();
if (this.location.getWorld() == null) {
this.worldName = null;
}
}
setHasChanges(true);
return this;
}
public String getWorldName() {
return worldName;
}
public HologramData setWorldName(String worldName) {
this.worldName = worldName;
if (this.location != null) {
World world = Bukkit.getWorld(worldName);
if (world != null) {
this.location.setWorld(world);
}
}
setHasChanges(true);
return this;
}

View File

@@ -185,7 +185,7 @@ public final class Hologram1_19_4 extends Hologram {
return; // could not be created, nothing to show
}
if (!data.getLocation().getWorld().getName().equals(player.getLocation().getWorld().getName())) {
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}

View File

@@ -185,7 +185,7 @@ public final class Hologram1_20_1 extends Hologram {
return; // could not be created, nothing to show
}
if (!data.getLocation().getWorld().getName().equals(player.getLocation().getWorld().getName())) {
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}

View File

@@ -185,7 +185,7 @@ public final class Hologram1_20_2 extends Hologram {
return; // could not be created, nothing to show
}
if (!data.getLocation().getWorld().getName().equals(player.getLocation().getWorld().getName())) {
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}

View File

@@ -185,7 +185,7 @@ public final class Hologram1_20_4 extends Hologram {
return; // could not be created, nothing to show
}
if (!data.getLocation().getWorld().getName().equals(player.getLocation().getWorld().getName())) {
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}

View File

@@ -1,7 +1,7 @@
package de.oliver.fancyholograms.commands.hologram;
import de.oliver.fancyholograms.api.hologram.Hologram;
import de.oliver.fancyholograms.api.data.*;
import de.oliver.fancyholograms.api.hologram.Hologram;
import de.oliver.fancyholograms.commands.Subcommand;
import de.oliver.fancylib.MessageHelper;
import org.bukkit.command.CommandSender;
@@ -31,7 +31,7 @@ public class InfoCMD implements Subcommand {
MessageHelper.info(player, "<b>Information about the " + hologram.getData().getName() + " hologram:");
MessageHelper.info(player, "Name: <gray>" + hologram.getData().getName());
MessageHelper.info(player, "Type: <gray>" + hologram.getData().getType().name());
MessageHelper.info(player, "Location: <gray>" + data.getLocation().getWorld().getName() + " " + data.getLocation().getX() + " / " + data.getLocation().getY() + " / " + data.getLocation().getZ());
MessageHelper.info(player, "Location: <gray>" + data.getWorldName() + " " + data.getLocation().getX() + " / " + data.getLocation().getY() + " / " + data.getLocation().getZ());
MessageHelper.info(player, "Visibility distance: <gray>" + data.getVisibilityDistance() + " blocks");
if (data instanceof DisplayHologramData displayData) {

View File

@@ -56,7 +56,7 @@ public class ListCMD implements Subcommand {
.limit(10)
.forEach(holo -> {
final var location = holo.getData().getLocation();
if (location == null || location.getWorld() == null) {
if (location == null || holo.getData().getWorldName() == null) {
return;
}
@@ -67,7 +67,7 @@ public class ListCMD implements Subcommand {
Formats.DECIMAL.format(location.x()),
Formats.DECIMAL.format(location.y()),
Formats.DECIMAL.format(location.z()),
location.getWorld().getName()
holo.getData().getWorldName()
));
});

View File

@@ -58,7 +58,7 @@ public class NearbyCMD implements Subcommand {
.getRegistry()
.getAllPersistent()
.stream()
.filter((holo) -> holo.getData().getLocation().getWorld() == playerLocation.getWorld())
.filter((holo) -> holo.getData().getWorldName().equals(playerLocation.getWorld().getName()))
.map((holo) -> Map.entry(holo, holo.getData().getLocation().distance(playerLocation)))
.filter((entry) -> entry.getValue() <= range.get())
.sorted(Comparator.comparingInt(a -> a.getValue().intValue()))

View File

@@ -9,6 +9,7 @@ import de.oliver.fancyholograms.api.hologram.Hologram;
import de.oliver.fancyholograms.main.FancyHologramsPlugin;
import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@@ -82,8 +83,8 @@ public class HologramControllerImpl implements HologramController {
}
private boolean isWithinVisibilityDistance(@NotNull final Hologram hologram, @NotNull final Player player) {
final var location = hologram.getData().getLocation();
if (!location.getWorld().equals(player.getWorld())) {
final Location location = hologram.getData().getLocation();
if (!hologram.getData().getWorldName().equals(player.getWorld().getName())) {
return false;
}

View File

@@ -44,7 +44,8 @@ public final class HologramImpl extends Hologram {
return; // could not be created, nothing to show
}
if (!data.getLocation().getWorld().getName().equals(player.getLocation().getWorld().getName())) {
// this implies that the world is loaded
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}
@@ -67,12 +68,16 @@ public final class HologramImpl extends Hologram {
@Override
public void despawnFrom(@NotNull final Player player) {
if (!new HologramDespawnEvent(this, player).callEvent()) {
if (fsDisplay == null) {
return; // doesn't exist, nothing to hide
}
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}
if (fsDisplay == null) {
return; // doesn't exist, nothing to hide
if (!new HologramDespawnEvent(this, player).callEvent()) {
return;
}
FS_RealPlayer fsPlayer = new FS_RealPlayer(player);
@@ -90,6 +95,10 @@ public final class HologramImpl extends Hologram {
return; // doesn't exist, nothing to refresh
}
if (!data.getWorldName().equals(player.getLocation().getWorld().getName())) {
return;
}
syncWithData();
if (!isViewer(player)) {
@@ -123,9 +132,6 @@ public final class HologramImpl extends Hologram {
// location data
final var location = data.getLocation();
if (location.getWorld() == null || !location.isWorldLoaded()) {
return;
}
fsDisplay.setLocation(location);
if (fsDisplay instanceof FS_TextDisplay textDisplay && data instanceof TextHologramData textData) {

View File

@@ -150,6 +150,7 @@ public class YamlHologramStorage implements HologramStorage {
case ITEM -> displayData = new ItemHologramData(name, new Location(null, 0, 0, 0));
case BLOCK -> displayData = new BlockHologramData(name, new Location(null, 0, 0, 0));
}
displayData.setWorldName(holoSection.getString("location.world"));
if (!displayData.read(holoSection, name)) {
FancyHologramsPlugin.get().getFancyLogger().warn("Could not read hologram data - skipping hologram");

View File

@@ -16,13 +16,14 @@ public class JsonAdapter {
data.getName(),
data.getType(),
new JsonLocation(
data.getLocation().getWorld().getName(),
data.getWorldName(),
data.getLocation().getX(),
data.getLocation().getY(),
data.getLocation().getZ(),
data.getLocation().getYaw(),
data.getLocation().getPitch()
),
data.getWorldName(),
data.getVisibilityDistance(),
data.getVisibility(),
data.getLinkedNpcName()
@@ -118,7 +119,7 @@ public class JsonAdapter {
public static HologramData fromJson(JsonDataUnion data) {
Location loc = new Location(
Bukkit.getWorld(data.hologram_data().location().world()),
Bukkit.getWorld(data.hologram_data().worldName()),
data.hologram_data().location().x(),
data.hologram_data().location().y(),
data.hologram_data().location().z()
@@ -156,7 +157,8 @@ public class JsonAdapter {
.setBrightness(brightness)
.setShadowRadius(data.display_data().shadow_radius())
.setShadowStrength(data.display_data().shadow_strength())
.setVisibilityDistance(data.hologram_data().visibilityDistance()) // hologram data
.setWorldName(data.hologram_data().worldName())// hologram data
.setVisibilityDistance(data.hologram_data().visibilityDistance())
.setVisibility(data.hologram_data().visibility())
.setLinkedNpcName(data.hologram_data().linkedNpcName());
@@ -168,7 +170,8 @@ public class JsonAdapter {
.setBrightness(brightness)
.setShadowRadius(data.display_data().shadow_radius())
.setShadowStrength(data.display_data().shadow_strength())
.setVisibilityDistance(data.hologram_data().visibilityDistance()) // hologram data
.setWorldName(data.hologram_data().worldName())// hologram data
.setVisibilityDistance(data.hologram_data().visibilityDistance())
.setVisibility(data.hologram_data().visibility())
.setLinkedNpcName(data.hologram_data().linkedNpcName());
case BLOCK -> new BlockHologramData(data.hologram_data().name(), loc)
@@ -179,7 +182,8 @@ public class JsonAdapter {
.setBrightness(brightness)
.setShadowRadius(data.display_data().shadow_radius())
.setShadowStrength(data.display_data().shadow_strength())
.setVisibilityDistance(data.hologram_data().visibilityDistance()) // hologram data
.setWorldName(data.hologram_data().worldName())// hologram data
.setVisibilityDistance(data.hologram_data().visibilityDistance())
.setVisibility(data.hologram_data().visibility())
.setLinkedNpcName(data.hologram_data().linkedNpcName());
};

View File

@@ -7,6 +7,7 @@ public record JsonHologramData(
String name,
HologramType type,
JsonLocation location,
String worldName,
Integer visibilityDistance,
Visibility visibility,
String linkedNpcName