From 63b95c93aa6351bd62e21914ca432d8205dd75de Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Thu, 23 May 2024 10:54:26 +0300 Subject: [PATCH 1/9] Added new property `Viewer` for hologram. #99 --- .../oliver/fancyholograms/api/Hologram.java | 30 ++++++++++++++++++- build.gradle.kts | 2 +- .../listeners/PlayerListener.java | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java index 364b0dbc..f64d5f21 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java @@ -39,6 +39,11 @@ public abstract class Hologram { @NotNull protected final Set shown = new HashSet<>(); + /** + * Set of UUIDs of player to whom the hologram will show without any checking. + */ + private final Set viewers = new HashSet<>(); + protected Hologram(@NotNull final HologramData data) { this.data = data; @@ -91,6 +96,16 @@ public final void showHologram(Collection players) { players.forEach(this::showHologram); } + /** + * Add player to permanent view of this hologram. + * + * @param playerId id of the player. + * @see #viewers + */ + public final void addViewer(UUID playerId) { + this.viewers.add(playerId); + } + /** * Must be called asynchronously */ @@ -105,6 +120,16 @@ public final void hideHologram(Collection players) { players.forEach(this::hideHologram); } + /** + * Remove player from a list of viewers this hologram. + * + * @param playerId id of the player. + * @see #viewers + */ + public final void removeViewer(UUID playerId) { + this.viewers.remove(playerId); + } + public final void updateHologram() { update(); } @@ -144,7 +169,10 @@ protected boolean shouldHologramBeShown(@NotNull final Player player) { return false; } - if (!getData().getDisplayData().isVisibleByDefault() && !player.hasPermission("fancyholograms.viewhologram." + data.getName())) { + if (!this.viewers.contains(player.getUniqueId()) && + !getData().getDisplayData().isVisibleByDefault() && + !player.hasPermission("fancyholograms.viewhologram." + data.getName()) + ) { return false; } diff --git a/build.gradle.kts b/build.gradle.kts index 5bc4d280..87927217 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ runPaper.folia.registerTask() allprojects { group = "de.oliver" val buildId = System.getenv("BUILD_ID") - version = "2.1.0" + (if (buildId != null) ".$buildId" else "") + version = "2.1.1" + (if (buildId != null) ".$buildId" else "") description = "Simple, lightweight and fast hologram plugin using display entities" diff --git a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java index b7694042..9c963d99 100644 --- a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java +++ b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java @@ -36,6 +36,7 @@ public void onQuit(@NotNull final PlayerQuitEvent event) { FancyHolograms.get().getScheduler().runTaskAsynchronously(() -> { for (final var hologram : this.plugin.getHologramsManager().getHolograms()) { hologram.hideHologram(event.getPlayer()); + hologram.removeViewer(event.getPlayer().getUniqueId()); } }); } From 388ab51958788cda06fea4aefc84d28a94b19d8a Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Thu, 23 May 2024 12:26:04 +0300 Subject: [PATCH 2/9] Revert "Added new property `Viewer` for hologram. #99" This reverts commit 63b95c93aa6351bd62e21914ca432d8205dd75de. --- .../oliver/fancyholograms/api/Hologram.java | 30 +------------------ build.gradle.kts | 2 +- .../listeners/PlayerListener.java | 1 - 3 files changed, 2 insertions(+), 31 deletions(-) diff --git a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java index f64d5f21..364b0dbc 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java @@ -39,11 +39,6 @@ public abstract class Hologram { @NotNull protected final Set shown = new HashSet<>(); - /** - * Set of UUIDs of player to whom the hologram will show without any checking. - */ - private final Set viewers = new HashSet<>(); - protected Hologram(@NotNull final HologramData data) { this.data = data; @@ -96,16 +91,6 @@ public final void showHologram(Collection players) { players.forEach(this::showHologram); } - /** - * Add player to permanent view of this hologram. - * - * @param playerId id of the player. - * @see #viewers - */ - public final void addViewer(UUID playerId) { - this.viewers.add(playerId); - } - /** * Must be called asynchronously */ @@ -120,16 +105,6 @@ public final void hideHologram(Collection players) { players.forEach(this::hideHologram); } - /** - * Remove player from a list of viewers this hologram. - * - * @param playerId id of the player. - * @see #viewers - */ - public final void removeViewer(UUID playerId) { - this.viewers.remove(playerId); - } - public final void updateHologram() { update(); } @@ -169,10 +144,7 @@ protected boolean shouldHologramBeShown(@NotNull final Player player) { return false; } - if (!this.viewers.contains(player.getUniqueId()) && - !getData().getDisplayData().isVisibleByDefault() && - !player.hasPermission("fancyholograms.viewhologram." + data.getName()) - ) { + if (!getData().getDisplayData().isVisibleByDefault() && !player.hasPermission("fancyholograms.viewhologram." + data.getName())) { return false; } diff --git a/build.gradle.kts b/build.gradle.kts index 87927217..5bc4d280 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ runPaper.folia.registerTask() allprojects { group = "de.oliver" val buildId = System.getenv("BUILD_ID") - version = "2.1.1" + (if (buildId != null) ".$buildId" else "") + version = "2.1.0" + (if (buildId != null) ".$buildId" else "") description = "Simple, lightweight and fast hologram plugin using display entities" diff --git a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java index 9c963d99..b7694042 100644 --- a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java +++ b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java @@ -36,7 +36,6 @@ public void onQuit(@NotNull final PlayerQuitEvent event) { FancyHolograms.get().getScheduler().runTaskAsynchronously(() -> { for (final var hologram : this.plugin.getHologramsManager().getHolograms()) { hologram.hideHologram(event.getPlayer()); - hologram.removeViewer(event.getPlayer().getUniqueId()); } }); } From fad7b0a7fa8e8fb8b129121b08c1d9d477964ff4 Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Thu, 23 May 2024 13:09:42 +0300 Subject: [PATCH 3/9] Added predicate for visibility --- .../oliver/fancyholograms/api/Hologram.java | 2 +- .../api/data/DisplayHologramData.java | 18 ++++++---- ...PermissionRequiredVisibilityPredicate.java | 11 ++++++ .../data/property/visibility/Visibility.java | 34 +++++++++++++++++++ .../visibility/VisibilityPredicate.java | 9 +++++ build.gradle.kts | 2 +- .../fancyholograms/commands/HologramCMD.java | 3 +- .../hologram/VisibleByDefaultCMD.java | 13 ++++--- .../storage/FlatFileHologramStorage.java | 5 ++- 9 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java create mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java create mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java diff --git a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java index 364b0dbc..d14cf701 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java @@ -144,7 +144,7 @@ protected boolean shouldHologramBeShown(@NotNull final Player player) { return false; } - if (!getData().getDisplayData().isVisibleByDefault() && !player.hasPermission("fancyholograms.viewhologram." + data.getName())) { + if (!this.getData().getDisplayData().getVisibleByDefault().canSee(player, this)) { return false; } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java index 14186f05..cd39091f 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java @@ -1,6 +1,7 @@ package de.oliver.fancyholograms.api.data; import de.oliver.fancyholograms.api.FancyHologramsPlugin; +import de.oliver.fancyholograms.api.data.property.visibility.Visibility; import de.oliver.fancylib.FancyLib; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -11,6 +12,7 @@ import org.joml.Vector3f; import java.util.Locale; +import java.util.Optional; public class DisplayHologramData implements Data { @@ -20,7 +22,7 @@ public class DisplayHologramData implements Data { public static final float DEFAULT_SHADOW_RADIUS = 0.0f; public static final float DEFAULT_SHADOW_STRENGTH = 1.0f; public static final int DEFAULT_VISIBILITY_DISTANCE = -1; - public static final boolean DEFAULT_IS_VISIBLE = true; + public static final Visibility DEFAULT_IS_VISIBLE = Visibility.ALL; private Location location; private Display.Billboard billboard = DEFAULT_BILLBOARD; @@ -30,12 +32,12 @@ public class DisplayHologramData implements Data { private float shadowRadius = DEFAULT_SHADOW_RADIUS; private float shadowStrength = DEFAULT_SHADOW_STRENGTH; private int visibilityDistance = DEFAULT_VISIBILITY_DISTANCE; - private boolean visibleByDefault = DEFAULT_IS_VISIBLE; + private Visibility visibleByDefault = DEFAULT_IS_VISIBLE; private String linkedNpcName; public DisplayHologramData(Location location, Display.Billboard billboard, Vector3f scale, Vector3f translation, Display.Brightness brightness, float shadowRadius, float shadowStrength, - int visibilityDistance, String linkedNpcName, boolean visibleByDefault) { + int visibilityDistance, String linkedNpcName, Visibility visibleByDefault) { this.location = location; this.billboard = billboard; this.scale = scale; @@ -81,7 +83,7 @@ public void write(ConfigurationSection section, String name) { section.set("shadow_radius", shadowRadius); section.set("shadow_strength", shadowStrength); section.set("visibility_distance", visibilityDistance); - section.set("visible_by_default", visibleByDefault); + section.set("visible_by_default", visibleByDefault.toString()); if (billboard == Display.Billboard.CENTER) { @@ -126,7 +128,9 @@ public void read(ConfigurationSection section, String name) { shadowStrength = (float) section.getDouble("shadow_strength", DEFAULT_SHADOW_STRENGTH); visibilityDistance = section.getInt("visibility_distance", DEFAULT_VISIBILITY_DISTANCE); linkedNpcName = section.getString("linkedNpc"); - visibleByDefault = section.getBoolean("visible_by_default", DEFAULT_IS_VISIBLE); + visibleByDefault = Optional.of(section.getString("visible_by_default", DEFAULT_IS_VISIBLE.toString())) + .map(Visibility::byString) + .orElse(DEFAULT_IS_VISIBLE); String billboardStr = section.getString("billboard", DisplayHologramData.DEFAULT_BILLBOARD.name()); billboard = switch (billboardStr.toLowerCase()) { @@ -213,11 +217,11 @@ public DisplayHologramData setVisibilityDistance(int visibilityDistance) { return this; } - public boolean isVisibleByDefault() { + public Visibility getVisibleByDefault() { return visibleByDefault; } - public void setVisibleByDefault(boolean visibleByDefault) { + public void setVisibleByDefault(Visibility visibleByDefault) { this.visibleByDefault = visibleByDefault; } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java new file mode 100644 index 00000000..e8b16bd3 --- /dev/null +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java @@ -0,0 +1,11 @@ +package de.oliver.fancyholograms.api.data.property.visibility; + +import de.oliver.fancyholograms.api.Hologram; +import org.bukkit.entity.Player; + +public class PermissionRequiredVisibilityPredicate implements VisibilityPredicate { + @Override + public boolean canSee(Player player, Hologram hologram) { + return player.hasPermission("fancyholograms.viewhologram." + hologram.getData().getName()); + } +} diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java new file mode 100644 index 00000000..10f577df --- /dev/null +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java @@ -0,0 +1,34 @@ +package de.oliver.fancyholograms.api.data.property.visibility; + +import de.oliver.fancyholograms.api.Hologram; +import org.bukkit.entity.Player; + +import javax.annotation.Nullable; +import java.util.Arrays; + +public enum Visibility { + ALL((player, hologram) -> true), + MANUAL((player, hologram) -> hologram.isShown(player)), + PERMISSION_REQUIRED(new PermissionRequiredVisibilityPredicate()), + ; + + + private final VisibilityPredicate predicate; + + + Visibility(VisibilityPredicate predicate) { + this.predicate = predicate; + } + + + public boolean canSee(Player player, Hologram hologram) { + return this.predicate.canSee(player, hologram); + } + + @Nullable + public static Visibility byString(String value) { + return Arrays.stream(Visibility.values()) + .filter(visibility -> visibility.toString().equalsIgnoreCase(value)) + .findFirst().orElse(null); + } +} diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java new file mode 100644 index 00000000..45531d03 --- /dev/null +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java @@ -0,0 +1,9 @@ +package de.oliver.fancyholograms.api.data.property.visibility; + +import de.oliver.fancyholograms.api.Hologram; +import org.bukkit.entity.Player; + +public interface VisibilityPredicate { + + boolean canSee(Player player, Hologram hologram); +} diff --git a/build.gradle.kts b/build.gradle.kts index 5bc4d280..87927217 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ runPaper.folia.registerTask() allprojects { group = "de.oliver" val buildId = System.getenv("BUILD_ID") - version = "2.1.0" + (if (buildId != null) ".$buildId" else "") + version = "2.1.1" + (if (buildId != null) ".$buildId" else "") description = "Simple, lightweight and fast hologram plugin using display entities" diff --git a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java index 396ca7b2..9321ea40 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java @@ -209,7 +209,8 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No yield FancyNpcsPlugin.get().getNpcManager().getAllNpcs().stream().map(npc -> npc.getData().getName()); } case "block" -> Arrays.stream(Material.values()).filter(Material::isBlock).map(Enum::name); - case "seethrough", "visiblebydefault" -> Stream.of("true", "false"); + case "seethrough" -> Stream.of("true", "false"); + case "visiblebydefault" -> new VisibleByDefaultCMD().tabcompletion(sender, hologram, args).stream(); default -> null; }; diff --git a/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java b/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java index a10bf2a7..f6064f2e 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java @@ -2,24 +2,29 @@ import de.oliver.fancyholograms.FancyHolograms; import de.oliver.fancyholograms.api.Hologram; +import de.oliver.fancyholograms.api.data.property.visibility.Visibility; import de.oliver.fancyholograms.commands.Subcommand; import de.oliver.fancylib.MessageHelper; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; import java.util.List; +import java.util.Objects; public class VisibleByDefaultCMD implements Subcommand { @Override public List tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { - return null; + return Arrays.stream( + Visibility.values() + ).map(Objects::toString).toList(); } @Override public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { - var visibleByDefault = Boolean.parseBoolean(args[3]); + var visibleByDefault = Visibility.byString(args[3]); if (hologram == null) { return false; } @@ -28,12 +33,12 @@ public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @ copied.getDisplayData().setVisibleByDefault(visibleByDefault); - if (hologram.getData().getDisplayData().isVisibleByDefault() == copied.getDisplayData().isVisibleByDefault()) { + if (hologram.getData().getDisplayData().getVisibleByDefault() == copied.getDisplayData().getVisibleByDefault()) { MessageHelper.warning(player, "This hologram already has visibility by default set to " + visibleByDefault); return false; } - hologram.getData().getDisplayData().setVisibleByDefault(copied.getDisplayData().isVisibleByDefault()); + hologram.getData().getDisplayData().setVisibleByDefault(copied.getDisplayData().getVisibleByDefault()); if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) { FancyHolograms.get().getHologramStorage().save(hologram); diff --git a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java index 754a64d8..e3bd3d9a 100644 --- a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java +++ b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java @@ -5,6 +5,7 @@ import de.oliver.fancyholograms.api.HologramStorage; import de.oliver.fancyholograms.api.HologramType; import de.oliver.fancyholograms.api.data.*; +import de.oliver.fancyholograms.api.data.property.visibility.Visibility; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import org.bukkit.Bukkit; @@ -232,7 +233,9 @@ public static HologramData readHologram(String name, ConfigurationSection config final var billboardName = config.getString("billboard", DisplayHologramData.DEFAULT_BILLBOARD.name()); final var textAlignmentName = config.getString("text_alignment", TextHologramData.DEFAULT_TEXT_ALIGNMENT.name()); final var linkedNpc = config.getString("linkedNpc"); - final var visibleByDefault = config.getBoolean("visible_by_default", DisplayHologramData.DEFAULT_IS_VISIBLE); + final var visibleByDefault = Optional.of(config.getString( + "visible_by_default", DisplayHologramData.DEFAULT_IS_VISIBLE.toString()) + ).map(Visibility::byString).orElse(DisplayHologramData.DEFAULT_IS_VISIBLE); final var billboard = switch (billboardName.toLowerCase(Locale.ROOT)) { case "fixed" -> Display.Billboard.FIXED; From e6b9ec556ea3b132bb3ecf2aef6bf7c224e3c8a5 Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Thu, 23 May 2024 21:38:25 +0300 Subject: [PATCH 4/9] Pr fixes. --- .../PermissionRequiredVisibilityPredicate.java | 11 ----------- .../api/data/property/visibility/Visibility.java | 10 ++++++++-- .../data/property/visibility/VisibilityPredicate.java | 9 --------- build.gradle.kts | 2 +- 4 files changed, 9 insertions(+), 23 deletions(-) delete mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java delete mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java deleted file mode 100644 index e8b16bd3..00000000 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/PermissionRequiredVisibilityPredicate.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.oliver.fancyholograms.api.data.property.visibility; - -import de.oliver.fancyholograms.api.Hologram; -import org.bukkit.entity.Player; - -public class PermissionRequiredVisibilityPredicate implements VisibilityPredicate { - @Override - public boolean canSee(Player player, Hologram hologram) { - return player.hasPermission("fancyholograms.viewhologram." + hologram.getData().getName()); - } -} diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java index 10f577df..444331c3 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java @@ -9,8 +9,9 @@ public enum Visibility { ALL((player, hologram) -> true), MANUAL((player, hologram) -> hologram.isShown(player)), - PERMISSION_REQUIRED(new PermissionRequiredVisibilityPredicate()), - ; + PERMISSION_REQUIRED( + (player, hologram) -> player.hasPermission("fancyholograms.viewhologram." + hologram.getData().getName()) + ); private final VisibilityPredicate predicate; @@ -31,4 +32,9 @@ public static Visibility byString(String value) { .filter(visibility -> visibility.toString().equalsIgnoreCase(value)) .findFirst().orElse(null); } + + public interface VisibilityPredicate { + + boolean canSee(Player player, Hologram hologram); + } } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java deleted file mode 100644 index 45531d03..00000000 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/VisibilityPredicate.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.oliver.fancyholograms.api.data.property.visibility; - -import de.oliver.fancyholograms.api.Hologram; -import org.bukkit.entity.Player; - -public interface VisibilityPredicate { - - boolean canSee(Player player, Hologram hologram); -} diff --git a/build.gradle.kts b/build.gradle.kts index 87927217..5bc4d280 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ runPaper.folia.registerTask() allprojects { group = "de.oliver" val buildId = System.getenv("BUILD_ID") - version = "2.1.1" + (if (buildId != null) ".$buildId" else "") + version = "2.1.0" + (if (buildId != null) ".$buildId" else "") description = "Simple, lightweight and fast hologram plugin using display entities" From 5561a46f221b992050f6ad7b0669b4caefe7158b Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Thu, 23 May 2024 22:38:11 +0300 Subject: [PATCH 5/9] Added migration for visibility to visible_by_default --- .../oliver/fancyholograms/api/Hologram.java | 2 +- .../api/data/DisplayHologramData.java | 44 +++++++++++++------ .../data/property/visibility/Visibility.java | 8 ++-- .../fancyholograms/commands/HologramCMD.java | 6 +-- ...leByDefaultCMD.java => VisibilityCMD.java} | 20 +++++---- .../storage/FlatFileHologramStorage.java | 22 ++++++++-- 6 files changed, 68 insertions(+), 34 deletions(-) rename src/main/java/de/oliver/fancyholograms/commands/hologram/{VisibleByDefaultCMD.java => VisibilityCMD.java} (67%) diff --git a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java index d14cf701..4636bd5e 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java @@ -144,7 +144,7 @@ protected boolean shouldHologramBeShown(@NotNull final Player player) { return false; } - if (!this.getData().getDisplayData().getVisibleByDefault().canSee(player, this)) { + if (!this.getData().getDisplayData().getVisibility().canSee(player, this)) { return false; } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java index cd39091f..8eb32ea4 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java @@ -22,7 +22,8 @@ public class DisplayHologramData implements Data { public static final float DEFAULT_SHADOW_RADIUS = 0.0f; public static final float DEFAULT_SHADOW_STRENGTH = 1.0f; public static final int DEFAULT_VISIBILITY_DISTANCE = -1; - public static final Visibility DEFAULT_IS_VISIBLE = Visibility.ALL; + public static final boolean DEFAULT_IS_VISIBLE = true; + public static final Visibility DEFAULT_VISIBILITY = Visibility.ALL; private Location location; private Display.Billboard billboard = DEFAULT_BILLBOARD; @@ -32,12 +33,12 @@ public class DisplayHologramData implements Data { private float shadowRadius = DEFAULT_SHADOW_RADIUS; private float shadowStrength = DEFAULT_SHADOW_STRENGTH; private int visibilityDistance = DEFAULT_VISIBILITY_DISTANCE; - private Visibility visibleByDefault = DEFAULT_IS_VISIBLE; + private Visibility visibility = DEFAULT_VISIBILITY; private String linkedNpcName; public DisplayHologramData(Location location, Display.Billboard billboard, Vector3f scale, Vector3f translation, Display.Brightness brightness, float shadowRadius, float shadowStrength, - int visibilityDistance, String linkedNpcName, Visibility visibleByDefault) { + int visibilityDistance, String linkedNpcName, Visibility visibility) { this.location = location; this.billboard = billboard; this.scale = scale; @@ -47,7 +48,7 @@ public DisplayHologramData(Location location, Display.Billboard billboard, Vecto this.shadowStrength = shadowStrength; this.visibilityDistance = visibilityDistance; this.linkedNpcName = linkedNpcName; - this.visibleByDefault = visibleByDefault; + this.visibility = visibility; } public DisplayHologramData() { @@ -64,7 +65,7 @@ public static DisplayHologramData getDefault(Location location) { DEFAULT_SHADOW_STRENGTH, DEFAULT_VISIBILITY_DISTANCE, null, - DEFAULT_IS_VISIBLE + DEFAULT_VISIBILITY ); } @@ -83,7 +84,7 @@ public void write(ConfigurationSection section, String name) { section.set("shadow_radius", shadowRadius); section.set("shadow_strength", shadowStrength); section.set("visibility_distance", visibilityDistance); - section.set("visible_by_default", visibleByDefault.toString()); + section.set("visibility", visibility.toString()); if (billboard == Display.Billboard.CENTER) { @@ -128,9 +129,24 @@ public void read(ConfigurationSection section, String name) { shadowStrength = (float) section.getDouble("shadow_strength", DEFAULT_SHADOW_STRENGTH); visibilityDistance = section.getInt("visibility_distance", DEFAULT_VISIBILITY_DISTANCE); linkedNpcName = section.getString("linkedNpc"); - visibleByDefault = Optional.of(section.getString("visible_by_default", DEFAULT_IS_VISIBLE.toString())) - .map(Visibility::byString) - .orElse(DEFAULT_IS_VISIBLE); + + visibility = Optional.ofNullable(section.getString("visibility")) + .flatMap(Visibility::byString) + .orElseGet(() -> { + final var visibleByDefault = section.getBoolean("visible_by_default", DEFAULT_IS_VISIBLE); + if (section.contains("visible_by_default")) { + section.set("visible_by_default", null); + } + if (visibleByDefault) { + return Visibility.ALL; + } else { + return Visibility.PERMISSION_REQUIRED; + } + }); + + if (section.contains("visible_by_default")) { + section.set("visible_by_default", null); + } String billboardStr = section.getString("billboard", DisplayHologramData.DEFAULT_BILLBOARD.name()); billboard = switch (billboardStr.toLowerCase()) { @@ -217,12 +233,12 @@ public DisplayHologramData setVisibilityDistance(int visibilityDistance) { return this; } - public Visibility getVisibleByDefault() { - return visibleByDefault; + public Visibility getVisibility() { + return this.visibility; } - public void setVisibleByDefault(Visibility visibleByDefault) { - this.visibleByDefault = visibleByDefault; + public void setVisibility(Visibility visibility) { + this.visibility = visibility; } public String getLinkedNpcName() { @@ -246,7 +262,7 @@ public Data copy() { shadowStrength, visibilityDistance, linkedNpcName, - visibleByDefault + visibility ); } } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java index 444331c3..c730ace4 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java @@ -3,8 +3,8 @@ import de.oliver.fancyholograms.api.Hologram; import org.bukkit.entity.Player; -import javax.annotation.Nullable; import java.util.Arrays; +import java.util.Optional; public enum Visibility { ALL((player, hologram) -> true), @@ -26,11 +26,11 @@ public boolean canSee(Player player, Hologram hologram) { return this.predicate.canSee(player, hologram); } - @Nullable - public static Visibility byString(String value) { + + public static Optional byString(String value) { return Arrays.stream(Visibility.values()) .filter(visibility -> visibility.toString().equalsIgnoreCase(value)) - .findFirst().orElse(null); + .findFirst(); } public interface VisibilityPredicate { diff --git a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java index 9321ea40..b825f444 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java @@ -149,7 +149,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No final var usingNpcs = FancyHolograms.isUsingFancyNpcs(); - List suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", "visibleByDefault", "shadowRadius", "shadowStrength", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : "")); + List suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", VisibilityCMD.VISIBILITY_COMMAND, "shadowRadius", "shadowStrength", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : "")); suggestions.addAll(type.getCommands()); return suggestions.stream().filter(input -> input.toLowerCase().startsWith(args[2].toLowerCase(Locale.ROOT))).toList(); @@ -210,7 +210,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No } case "block" -> Arrays.stream(Material.values()).filter(Material::isBlock).map(Enum::name); case "seethrough" -> Stream.of("true", "false"); - case "visiblebydefault" -> new VisibleByDefaultCMD().tabcompletion(sender, hologram, args).stream(); + case VisibilityCMD.VISIBILITY_COMMAND -> new VisibilityCMD().tabcompletion(sender, hologram, args).stream(); default -> null; }; @@ -298,7 +298,7 @@ private boolean edit(@NotNull final CommandSender player, @NotNull final Hologra case "scale" -> new ScaleCMD().run(player, hologram, args); case "updatetextinterval" -> new UpdateTextIntervalCMD().run(player, hologram, args); case "visibilitydistance" -> new VisibilityDistanceCMD().run(player, hologram, args); - case "visiblebydefault" -> new VisibleByDefaultCMD().run(player, hologram, args); + case VisibilityCMD.VISIBILITY_COMMAND -> new VisibilityCMD().run(player, hologram, args); case "linkwithnpc" -> new LinkWithNpcCMD().run(player, hologram, args); case "shadowradius" -> new ShadowRadiusCMD().run(player, hologram, args); case "shadowstrength" -> new ShadowStrengthCMD().run(player, hologram, args); diff --git a/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java b/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java similarity index 67% rename from src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java rename to src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java index f6064f2e..23659b85 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibleByDefaultCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java @@ -13,7 +13,10 @@ import java.util.List; import java.util.Objects; -public class VisibleByDefaultCMD implements Subcommand { +public class VisibilityCMD implements Subcommand { + + public static final String VISIBILITY_COMMAND = "visibility"; + @Override public List tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { @@ -24,27 +27,28 @@ public List tabcompletion(@NotNull CommandSender player, @Nullable Holog @Override public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { - var visibleByDefault = Visibility.byString(args[3]); - if (hologram == null) { + final var optionalVisibility = Visibility.byString(args[3]); + if (hologram == null || optionalVisibility.isEmpty()) { return false; } + final var visibility = optionalVisibility.get(); final var copied = hologram.getData().copy(); - copied.getDisplayData().setVisibleByDefault(visibleByDefault); + copied.getDisplayData().setVisibility(visibility); - if (hologram.getData().getDisplayData().getVisibleByDefault() == copied.getDisplayData().getVisibleByDefault()) { - MessageHelper.warning(player, "This hologram already has visibility by default set to " + visibleByDefault); + if (hologram.getData().getDisplayData().getVisibility() == copied.getDisplayData().getVisibility()) { + MessageHelper.warning(player, "This hologram already has visibility by default set to " + visibility); return false; } - hologram.getData().getDisplayData().setVisibleByDefault(copied.getDisplayData().getVisibleByDefault()); + hologram.getData().getDisplayData().setVisibility(copied.getDisplayData().getVisibility()); if (FancyHolograms.get().getHologramConfiguration().isSaveOnChangedEnabled()) { FancyHolograms.get().getHologramStorage().save(hologram); } - MessageHelper.success(player, "Changed visibility by default to " + visibleByDefault); + MessageHelper.success(player, "Changed visibility by default to " + visibility); return true; } } diff --git a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java index e3bd3d9a..99ea72fd 100644 --- a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java +++ b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java @@ -23,6 +23,8 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import static de.oliver.fancyholograms.api.data.DisplayHologramData.DEFAULT_IS_VISIBLE; + public class FlatFileHologramStorage implements HologramStorage { private static final ReadWriteLock lock = new ReentrantReadWriteLock(); @@ -233,9 +235,21 @@ public static HologramData readHologram(String name, ConfigurationSection config final var billboardName = config.getString("billboard", DisplayHologramData.DEFAULT_BILLBOARD.name()); final var textAlignmentName = config.getString("text_alignment", TextHologramData.DEFAULT_TEXT_ALIGNMENT.name()); final var linkedNpc = config.getString("linkedNpc"); - final var visibleByDefault = Optional.of(config.getString( - "visible_by_default", DisplayHologramData.DEFAULT_IS_VISIBLE.toString()) - ).map(Visibility::byString).orElse(DisplayHologramData.DEFAULT_IS_VISIBLE); + + + final var visibility = Optional.ofNullable(config.getString("visibility")) + .flatMap(Visibility::byString) + .orElseGet(() -> { + final var visibleByDefault = config.getBoolean("visible_by_default", DEFAULT_IS_VISIBLE); + if (config.contains("visible_by_default")) { + config.set("visible_by_default", null); + } + if (visibleByDefault) { + return Visibility.ALL; + } else { + return Visibility.PERMISSION_REQUIRED; + } + }); final var billboard = switch (billboardName.toLowerCase(Locale.ROOT)) { case "fixed" -> Display.Billboard.FIXED; @@ -262,7 +276,7 @@ public static HologramData readHologram(String name, ConfigurationSection config } - DisplayHologramData displayData = new DisplayHologramData(location, billboard, new Vector3f((float) scaleX, (float) scaleY, (float) scaleZ), DisplayHologramData.DEFAULT_TRANSLATION, null, (float) shadowRadius, (float) shadowStrength, visibilityDistance, linkedNpc, visibleByDefault); + DisplayHologramData displayData = new DisplayHologramData(location, billboard, new Vector3f((float) scaleX, (float) scaleY, (float) scaleZ), DisplayHologramData.DEFAULT_TRANSLATION, null, (float) shadowRadius, (float) shadowStrength, visibilityDistance, linkedNpc, visibility); TextHologramData textData = new TextHologramData(text, background, textAlignment, textHasShadow, isSeeThrough, textUpdateInterval); From 33f73a42f61102e194f4b902588e18e2ea00652d Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Fri, 24 May 2024 20:32:38 +0300 Subject: [PATCH 6/9] Pr fixes. --- .../de/oliver/fancyholograms/commands/HologramCMD.java | 6 +++--- .../fancyholograms/commands/hologram/VisibilityCMD.java | 7 ++----- .../fancyholograms/storage/FlatFileHologramStorage.java | 3 +-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java index b825f444..4f21fb79 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/HologramCMD.java @@ -149,7 +149,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No final var usingNpcs = FancyHolograms.isUsingFancyNpcs(); - List suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", VisibilityCMD.VISIBILITY_COMMAND, "shadowRadius", "shadowStrength", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : "")); + List suggestions = new ArrayList<>(Arrays.asList("position", "moveHere", "moveTo", "rotate", "rotatepitch", "billboard", "scale", "visibilityDistance", "visibility", "shadowRadius", "shadowStrength", usingNpcs ? "linkWithNpc" : "", usingNpcs ? "unlinkWithNpc" : "")); suggestions.addAll(type.getCommands()); return suggestions.stream().filter(input -> input.toLowerCase().startsWith(args[2].toLowerCase(Locale.ROOT))).toList(); @@ -210,7 +210,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No } case "block" -> Arrays.stream(Material.values()).filter(Material::isBlock).map(Enum::name); case "seethrough" -> Stream.of("true", "false"); - case VisibilityCMD.VISIBILITY_COMMAND -> new VisibilityCMD().tabcompletion(sender, hologram, args).stream(); + case "visibility" -> new VisibilityCMD().tabcompletion(sender, hologram, args).stream(); default -> null; }; @@ -298,7 +298,7 @@ private boolean edit(@NotNull final CommandSender player, @NotNull final Hologra case "scale" -> new ScaleCMD().run(player, hologram, args); case "updatetextinterval" -> new UpdateTextIntervalCMD().run(player, hologram, args); case "visibilitydistance" -> new VisibilityDistanceCMD().run(player, hologram, args); - case VisibilityCMD.VISIBILITY_COMMAND -> new VisibilityCMD().run(player, hologram, args); + case "visibility" -> new VisibilityCMD().run(player, hologram, args); case "linkwithnpc" -> new LinkWithNpcCMD().run(player, hologram, args); case "shadowradius" -> new ShadowRadiusCMD().run(player, hologram, args); case "shadowstrength" -> new ShadowStrengthCMD().run(player, hologram, args); diff --git a/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java b/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java index 23659b85..0ad35c2b 100644 --- a/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java +++ b/src/main/java/de/oliver/fancyholograms/commands/hologram/VisibilityCMD.java @@ -15,9 +15,6 @@ public class VisibilityCMD implements Subcommand { - public static final String VISIBILITY_COMMAND = "visibility"; - - @Override public List tabcompletion(@NotNull CommandSender player, @Nullable Hologram hologram, @NotNull String[] args) { return Arrays.stream( @@ -38,7 +35,7 @@ public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @ if (hologram.getData().getDisplayData().getVisibility() == copied.getDisplayData().getVisibility()) { - MessageHelper.warning(player, "This hologram already has visibility by default set to " + visibility); + MessageHelper.warning(player, "This hologram already has visibility set to " + visibility); return false; } @@ -48,7 +45,7 @@ public boolean run(@NotNull CommandSender player, @Nullable Hologram hologram, @ FancyHolograms.get().getHologramStorage().save(hologram); } - MessageHelper.success(player, "Changed visibility by default to " + visibility); + MessageHelper.success(player, "Changed visibility to " + visibility); return true; } } diff --git a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java index 99ea72fd..d4ef2c04 100644 --- a/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java +++ b/src/main/java/de/oliver/fancyholograms/storage/FlatFileHologramStorage.java @@ -23,7 +23,6 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import static de.oliver.fancyholograms.api.data.DisplayHologramData.DEFAULT_IS_VISIBLE; public class FlatFileHologramStorage implements HologramStorage { @@ -240,7 +239,7 @@ public static HologramData readHologram(String name, ConfigurationSection config final var visibility = Optional.ofNullable(config.getString("visibility")) .flatMap(Visibility::byString) .orElseGet(() -> { - final var visibleByDefault = config.getBoolean("visible_by_default", DEFAULT_IS_VISIBLE); + final var visibleByDefault = config.getBoolean("visible_by_default", DisplayHologramData.DEFAULT_IS_VISIBLE); if (config.contains("visible_by_default")) { config.set("visible_by_default", null); } From 46a37156501f986eb0fd7468e9329b344b3251df Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Fri, 24 May 2024 22:08:28 +0300 Subject: [PATCH 7/9] Added old API and improve javadocs. --- .../api/data/DisplayHologramData.java | 28 +++++++++++++++++++ .../data/property/visibility/Visibility.java | 9 ++++++ 2 files changed, 37 insertions(+) diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java index 8eb32ea4..c186122f 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/DisplayHologramData.java @@ -233,10 +233,38 @@ public DisplayHologramData setVisibilityDistance(int visibilityDistance) { return this; } + /** + * Returns to the default state of visibility. + * This method is deprecated, to control the visibility of the hologram, use {@link #getVisibility()}. + * + * @return {@code true} if hologram can see all players, else {@code false}. + */ + @Deprecated(forRemoval = true) + public boolean isVisibleByDefault() { + return this.getVisibility() == Visibility.ALL; + } + + /** + * Set the default state of visibility. + * This method is deprecated, to control the visibility of the hologram, use {@link #setVisibility(Visibility)}. + */ + @Deprecated(forRemoval = true) + public void setVisibleByDefault(boolean visibleByDefault) { + this.setVisibility(visibleByDefault ? Visibility.ALL : Visibility.PERMISSION_REQUIRED); + } + + /** + * Get the type of visibility for the hologram. + * + * @return type of visibility. + */ public Visibility getVisibility() { return this.visibility; } + /** + * Set the type of visibility for the hologram. + */ public void setVisibility(Visibility visibility) { this.visibility = visibility; } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java index c730ace4..5f990977 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java @@ -7,8 +7,17 @@ import java.util.Optional; public enum Visibility { + /** + * Everybody can see a hologram. + */ ALL((player, hologram) -> true), + /** + * Manual control. + */ MANUAL((player, hologram) -> hologram.isShown(player)), + /** + * The player needs permission to see a specific hologram. + */ PERMISSION_REQUIRED( (player, hologram) -> player.hasPermission("fancyholograms.viewhologram." + hologram.getData().getName()) ); From 9d2a7119bd622dd7114fdc7ac7d92a96764ac6fa Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Sat, 25 May 2024 05:17:51 +0300 Subject: [PATCH 8/9] Added list for Manual visibility. --- .../oliver/fancyholograms/api/Hologram.java | 29 +++++++++++++++++++ .../data/property/visibility/Visibility.java | 8 +++-- .../listeners/PlayerListener.java | 6 +++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java index 4636bd5e..e35b3f77 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java @@ -39,6 +39,13 @@ public abstract class Hologram { @NotNull protected final Set shown = new HashSet<>(); + /** + * Set of UUIDs of players to whom the hologram needs to show. + * Used for {@link de.oliver.fancyholograms.api.data.property.visibility.Visibility#MANUAL_VIEWER} visibility. + */ + @NotNull + private final Set manualViewer = new HashSet<>(); + protected Hologram(@NotNull final HologramData data) { this.data = data; @@ -91,6 +98,15 @@ public final void showHologram(Collection players) { players.forEach(this::showHologram); } + /** + * Add manual viewer. + * + * @param playerId target player id. + */ + public final void addManualViewer(@NotNull UUID playerId) { + this.manualViewer.add(playerId); + } + /** * Must be called asynchronously */ @@ -105,6 +121,15 @@ public final void hideHologram(Collection players) { players.forEach(this::hideHologram); } + /** + * Remove manual viewer. + * + * @param playerId target player id. + */ + public final void removeManualViewer(@NotNull UUID playerId) { + this.manualViewer.remove(playerId); + } + public final void updateHologram() { update(); } @@ -126,6 +151,10 @@ public final void refreshHologram(@NotNull final Collection pl return Collections.unmodifiableSet(this.shown); } + public final @NotNull @UnmodifiableView Set getManualViewer() { + return Collections.unmodifiableSet(this.manualViewer); + } + public final boolean isShown(@NotNull final UUID player) { return this.shown.contains(player); } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java index 5f990977..14e314ed 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Optional; +import java.util.UUID; public enum Visibility { /** @@ -12,9 +13,12 @@ public enum Visibility { */ ALL((player, hologram) -> true), /** - * Manual control. + * Only players from the list {@link Hologram#manualViewer} can see a hologram. + * + * @see Hologram#addManualViewer(UUID) to add player + * @see Hologram#removeManualViewer(UUID) to remote player */ - MANUAL((player, hologram) -> hologram.isShown(player)), + MANUAL_VIEWER((player, hologram) -> hologram.getManualViewer().contains(player.getUniqueId())), /** * The player needs permission to see a specific hologram. */ diff --git a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java index b7694042..400cfa7a 100644 --- a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java +++ b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java @@ -5,7 +5,10 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.player.*; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerTeleportEvent; import org.jetbrains.annotations.NotNull; public final class PlayerListener implements Listener { @@ -36,6 +39,7 @@ public void onQuit(@NotNull final PlayerQuitEvent event) { FancyHolograms.get().getScheduler().runTaskAsynchronously(() -> { for (final var hologram : this.plugin.getHologramsManager().getHolograms()) { hologram.hideHologram(event.getPlayer()); + hologram.removeManualViewer(event.getPlayer().getUniqueId()); } }); } From bce6c0d9266eaa583580a8d078dd5223ce8a012e Mon Sep 17 00:00:00 2001 From: Alexander Chapchuk Date: Sat, 25 May 2024 22:10:25 +0300 Subject: [PATCH 9/9] Revert "Added list for Manual visibility." This reverts commit 9d2a7119bd622dd7114fdc7ac7d92a96764ac6fa. --- .../oliver/fancyholograms/api/Hologram.java | 29 ------------------- .../data/property/visibility/Visibility.java | 8 ++--- .../listeners/PlayerListener.java | 6 +--- 3 files changed, 3 insertions(+), 40 deletions(-) diff --git a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java index e35b3f77..4636bd5e 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/Hologram.java @@ -39,13 +39,6 @@ public abstract class Hologram { @NotNull protected final Set shown = new HashSet<>(); - /** - * Set of UUIDs of players to whom the hologram needs to show. - * Used for {@link de.oliver.fancyholograms.api.data.property.visibility.Visibility#MANUAL_VIEWER} visibility. - */ - @NotNull - private final Set manualViewer = new HashSet<>(); - protected Hologram(@NotNull final HologramData data) { this.data = data; @@ -98,15 +91,6 @@ public final void showHologram(Collection players) { players.forEach(this::showHologram); } - /** - * Add manual viewer. - * - * @param playerId target player id. - */ - public final void addManualViewer(@NotNull UUID playerId) { - this.manualViewer.add(playerId); - } - /** * Must be called asynchronously */ @@ -121,15 +105,6 @@ public final void hideHologram(Collection players) { players.forEach(this::hideHologram); } - /** - * Remove manual viewer. - * - * @param playerId target player id. - */ - public final void removeManualViewer(@NotNull UUID playerId) { - this.manualViewer.remove(playerId); - } - public final void updateHologram() { update(); } @@ -151,10 +126,6 @@ public final void refreshHologram(@NotNull final Collection pl return Collections.unmodifiableSet(this.shown); } - public final @NotNull @UnmodifiableView Set getManualViewer() { - return Collections.unmodifiableSet(this.manualViewer); - } - public final boolean isShown(@NotNull final UUID player) { return this.shown.contains(player); } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java index 14e314ed..5f990977 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/property/visibility/Visibility.java @@ -5,7 +5,6 @@ import java.util.Arrays; import java.util.Optional; -import java.util.UUID; public enum Visibility { /** @@ -13,12 +12,9 @@ public enum Visibility { */ ALL((player, hologram) -> true), /** - * Only players from the list {@link Hologram#manualViewer} can see a hologram. - * - * @see Hologram#addManualViewer(UUID) to add player - * @see Hologram#removeManualViewer(UUID) to remote player + * Manual control. */ - MANUAL_VIEWER((player, hologram) -> hologram.getManualViewer().contains(player.getUniqueId())), + MANUAL((player, hologram) -> hologram.isShown(player)), /** * The player needs permission to see a specific hologram. */ diff --git a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java index 400cfa7a..b7694042 100644 --- a/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java +++ b/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java @@ -5,10 +5,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.*; import org.jetbrains.annotations.NotNull; public final class PlayerListener implements Listener { @@ -39,7 +36,6 @@ public void onQuit(@NotNull final PlayerQuitEvent event) { FancyHolograms.get().getScheduler().runTaskAsynchronously(() -> { for (final var hologram : this.plugin.getHologramsManager().getHolograms()) { hologram.hideHologram(event.getPlayer()); - hologram.removeManualViewer(event.getPlayer().getUniqueId()); } }); }