Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new property Viewer for hologram. #99 #100

Merged
merged 9 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +12,7 @@
import org.joml.Vector3f;

import java.util.Locale;
import java.util.Optional;

public class DisplayHologramData implements Data {

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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()),
BigTows marked this conversation as resolved.
Show resolved Hide resolved
;


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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed in favour of Enum#valueOf(String#toUpperCase)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valueOf is throwable, for safe parsing null needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no instance where it returns null anyway, if it is null wouldn't there be issues in other places? If so the error would be more helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Optional ? :)

}
BigTows marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion build.gradle.kts
BigTows marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new command object for every tabcomplete is probably not a good idea, I would just copy the contents of the method over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is should be refactored at all.

All CMDs impltement Subcommand with execute and tabcompletion methods.


default -> null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be a good idea to add backwards compatibility and support the old boolean option for a while.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to check booleans and convert into Predicate?


final var billboard = switch (billboardName.toLowerCase(Locale.ROOT)) {
case "fixed" -> Display.Billboard.FIXED;
Expand Down