Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Player: add hiding with invisibility effect
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Dec 21, 2023
1 parent eb52845 commit 288beb5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/main/java/net/flectone/chat/component/FComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

public class FComponent {

private final BaseComponent component;
private BaseComponent component;
protected final FConfiguration locale;
protected final FConfiguration config;
@Getter
private HoverEvent hoverEvent;
private ClickEvent clickEvent;

public FComponent(@Nullable BaseComponent baseComponent) {
this.component = baseComponent;
Expand All @@ -32,6 +33,12 @@ public FComponent(@NotNull String text) {
this(new TextComponent(fromLegacyText(text)));
}

public void set(@NotNull String text) {
component = new TextComponent(fromLegacyText(text));
component.setClickEvent(clickEvent);
component.setHoverEvent(hoverEvent);
}

public FComponent() {
this(new TextComponent());
}
Expand Down Expand Up @@ -63,7 +70,7 @@ public FComponent addHoverText(@NotNull BaseComponent[] baseComponents) {

@NotNull
public FComponent addOpenURL(@NotNull String url) {
component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
return this;
}

Expand All @@ -75,18 +82,23 @@ public FComponent addHoverItem(@NotNull String itemJson) {

@NotNull
public FComponent addRunCommand(@NotNull String command) {
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command));
setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command));
return this;
}

@NotNull
public FComponent addSuggestCommand(@NotNull String command) {
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command));
setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command));
return this;
}

public void setHoverEvent(@NotNull HoverEvent hoverEvent) {
component.setHoverEvent(hoverEvent);
this.hoverEvent = hoverEvent;
}

public void setClickEvent(@NotNull ClickEvent clickEvent) {
component.setClickEvent(clickEvent);
this.clickEvent = clickEvent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.flectone.chat.util.Pair;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -17,6 +18,14 @@ public FPlayerComponent(@Nullable CommandSender sender, @Nullable CommandSender

if (!(sender instanceof Player player) || !(recipient instanceof Player recip)) return;

if (config.getVaultBoolean(player, "player.name.hide-invisible")
&& player.hasPotionEffect(PotionEffectType.INVISIBILITY)
&& player.hasPermission("flectonechat.player.name.invisible")) {
text = text.replace(player.getName(), locale.getVaultString(player, "player.name.invisible"));
set(text);
return;
}

Pair<String, Pair<String, HoverModule.CommandType>> hoverInfo = null;

FModule fModule = FlectoneChat.getPlugin().getModuleManager().get(HoverModule.class);
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/net/flectone/chat/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.flectone.chat.module.player.name.NameModule;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -90,11 +91,11 @@ public static String formatPlayerString(@Nullable CommandSender commandSender, @

FModule fModule = FlectoneChat.getPlugin().getModuleManager().get(NameModule.class);
if (fModule instanceof NameModule nameModule) {
if (string.contains("<player_name_tab")) {
if (string.contains("<player_name_tab>")) {
string = string.replace("<player_name_tab>", nameModule.getTab(player));
}

if (string.contains("<player_name_real")) {
if (string.contains("<player_name_real>")) {
string = string.replace("<player_name_real>", nameModule.getReal(player));
}

Expand All @@ -111,6 +112,20 @@ public static String formatPlayerString(@Nullable CommandSender commandSender, @
}
}

if (isInvisible(player)) {
String invisibleName = FlectoneChat.getPlugin().getFileManager().getLocale().getVaultString(player, "player.name.invisible");

return string
.replace("<player_prefix>", "")
.replace("<player_suffix>", "")
.replace("<vault_prefix>", "")
.replace("<world_prefix>", "")
.replace("<stream_prefix>", "")
.replace("<player>", invisibleName)
.replace("<vault_suffix>", "")
.replace("<afk_suffix>", "");
}

FPlayer fPlayer = FlectoneChat.getPlugin().getPlayerManager().get(player);

return string
Expand All @@ -127,4 +142,9 @@ public static String joinArray(@NotNull String[] strings, int start, @NotNull St
.collect(Collectors.joining(delimiter));
}

public static boolean isInvisible(@NotNull Player player) {
return FlectoneChat.getPlugin().getFileManager().getConfig().getVaultBoolean(player, "player.name.hide-invisible")
&& player.hasPotionEffect(PotionEffectType.INVISIBILITY)
&& player.hasPermission("flectonechat.player.name.invisible");
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ default:
real: "<GRADIENT:11b8f0><player></GRADIENT:87cee6>"
suffix: "<afk_suffix><vault_suffix>"

hide-invisible: true

name-tag:
enable: true

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ permissions:
default: true
flectonechat.player.hover:
default: true
flectonechat.player.name.invisible:
default: true
flectonechat.player.name.prefix:
default: true
flectonechat.player.name.suffix:
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/settings/languages/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ default:
message: "#ff4e4e← &&2<player> &&1left the server for the first time"

player:
name:
invisible: "? Invisible"
hover:
message: "&&1Click to write to &&2<player>"
below-name:
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/settings/languages/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ default:
message: "#ff4e4e← &&2<player> &&1впервые вышел с сервера"

player:
name:
invisible: "? Невидимка"
hover:
message: "&&1Написать игроку &&2<player>"
below-name:
Expand Down

0 comments on commit 288beb5

Please sign in to comment.