Skip to content

Commit

Permalink
Cleanup code, and fix Sayan vanish name didn't work for some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Dec 16, 2024
1 parent 05f9f62 commit b928484
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package dev.mrshawn.deathmessages.hooks;

import dev.mrshawn.deathmessages.DeathMessages;
import dev.mrshawn.deathmessages.config.Settings;
import dev.mrshawn.deathmessages.files.Config;
import dev.mrshawn.deathmessages.utils.Util;
import net.kyori.adventure.text.Component;
import org.sayandev.sayanvanish.api.SayanVanishAPI;
import org.sayandev.sayanvanish.bukkit.api.BukkitUser;
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI;

import org.bukkit.entity.Player;

import java.util.UUID;

public class SayanVanishExtension {
Expand All @@ -15,4 +22,14 @@ public SayanVanishAPI<BukkitUser> getSayanVanishAPI() {
public boolean isVanished(UUID uuid) {
return getSayanVanishAPI().isVanished(uuid);
}

public static boolean isSayanVanished(Player p) {
return DeathMessages.getHooks().sayanVanishEnabled && DeathMessages.getHooks().sayanVanishExtension.isVanished(p.getUniqueId());
}

public static Component getVanishedName() {
final String name = Settings.getInstance().getConfig().getString(Config.HOOKS_SAYANVANISH_VANISHED_NAME.getPath());

return Util.convertFromLegacy(name != null ? name : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -843,12 +843,10 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
final boolean hasBiome = msg.contains(Component.text("%biome%"));
final boolean hasDistance = msg.contains(Component.text("%distance%"));

msg = msg.replaceText(Util.replace("%entity%", Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() == null ? Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity)) : entity.getCustomName()))
.replaceText(Util.replace("%killer%", player.getName()))
.replaceText(Util.replace("%killer_display%", player.getDisplayName()))
msg = msg.replaceText(Util.replace("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%killer%", Util.getPlayerName(player)))
.replaceText(Util.replace("%killer_display%", Util.getPlayerDisplayName(player)))
.replaceText(Util.replace("%world%", entity.getLocation().getWorld().getName()))
.replaceText(Util.replace("%world_environment%", getEnvironment(entity.getLocation().getWorld().getEnvironment())))
.replaceText(Util.replace("%x%", String.valueOf(entity.getLocation().getBlock().getX())))
Expand Down Expand Up @@ -891,16 +889,13 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
}

public static String entityDeathPlaceholders(String msg, Player player, Entity entity, boolean hasOwner) {
final boolean isSayanVanished = isSayanVanished(player);
final boolean hasBiome = msg.contains("%biome%");
final boolean hasDistance = msg.contains("%distance%");

msg = msg.replaceAll("%entity%", Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%entity_display%", entity.getCustomName() == null ? Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity)) : entity.getCustomName())
.replaceAll("%killer%", isSayanVanished ? getVanishedName() : player.getName())
.replaceAll("%killer_display%", isSayanVanished ? getVanishedName() : player.getDisplayName())
msg = msg.replaceAll("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%killer%", Util.getPlayerName(player))
.replaceAll("%killer_display%", Util.getPlayerDisplayName(player))
.replaceAll("%world%", entity.getLocation().getWorld().getName())
.replaceAll("%world_environment%", getEnvironment(entity.getLocation().getWorld().getEnvironment()))
.replaceAll("%x%", String.valueOf(entity.getLocation().getBlock().getX()))
Expand Down Expand Up @@ -938,12 +933,11 @@ public static String entityDeathPlaceholders(String msg, Player player, Entity e
}

public static Component playerDeathPlaceholders(Component msg, PlayerManager pm, Entity mob) {
final boolean isSayanVanished = isSayanVanished(pm.getPlayer());
final boolean hasBiome = msg.contains(Component.text("%biome%"));
final boolean hasDistance = msg.contains(Component.text("%distance%"));

msg = msg.replaceText(Util.replace("%player%", isSayanVanished ? getVanishedName() : pm.getName()))
.replaceText(Util.replace("%player_display%", isSayanVanished ? getVanishedName() : pm.getPlayer().getDisplayName()))
msg = msg.replaceText(Util.replace("%player%", Util.getPlayerName(pm)))
.replaceText(Util.replace("%player_display%", Util.getPlayerDisplayName(pm)))
.replaceText(Util.replace("%world%", pm.getLastLocation().getWorld().getName()))
.replaceText(Util.replace("%world_environment%", getEnvironment(pm.getLastLocation().getWorld().getEnvironment())))
.replaceText(Util.replace("%x%", String.valueOf(pm.getLastLocation().getBlock().getX())))
Expand Down Expand Up @@ -990,7 +984,7 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,

if (mob instanceof Player) {
Player p = (Player) mob;
msg = msg.replaceText(Util.replace("%killer_display%", p.getDisplayName()));
msg = msg.replaceText(Util.replace("%killer_display%", Util.getPlayerDisplayName(p)));
}
}

Expand All @@ -1007,12 +1001,11 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,
}

public static String playerDeathPlaceholders(String msg, PlayerManager pm, Entity mob) {
final boolean isSayanVanished = isSayanVanished(pm.getPlayer());
final boolean hasBiome = msg.contains("%biome%");
final boolean hasDistance = msg.contains("%distance%");

msg = msg.replaceAll("%player%", isSayanVanished ? getVanishedName() : pm.getName())
.replaceAll("%player_display%", isSayanVanished ? getVanishedName() : pm.getPlayer().getDisplayName())
msg = msg.replaceAll("%player%", Util.getPlayerName(pm))
.replaceAll("%player_display%", Util.getPlayerDisplayName(pm))
.replaceAll("%world%", pm.getLastLocation().getWorld().getName())
.replaceAll("%world_environment%", getEnvironment(pm.getLastLocation().getWorld().getEnvironment()))
.replaceAll("%x%", String.valueOf(pm.getLastLocation().getBlock().getX()))
Expand Down Expand Up @@ -1059,7 +1052,7 @@ public static String playerDeathPlaceholders(String msg, PlayerManager pm, Entit

if (mob instanceof Player) {
Player p = (Player) mob;
msg = msg.replaceAll("%killer_display%", isSayanVanished(p) ? getVanishedName() : p.getDisplayName());
msg = msg.replaceAll("%killer_display%", Util.getPlayerDisplayName(p));
}
}

Expand Down Expand Up @@ -1267,15 +1260,4 @@ public static FileConfiguration getPlayerDeathMessages() {
public static FileConfiguration getEntityDeathMessages() {
return EntityDeathMessages.getInstance().getConfig();
}

private static boolean isSayanVanished(Player p) {
if (DeathMessages.getHooks().sayanVanishEnabled) {
return DeathMessages.getHooks().sayanVanishExtension.isVanished(p.getUniqueId());
}
return false;
}

private static String getVanishedName() {
return Settings.getInstance().getConfig().getString(Config.HOOKS_SAYANVANISH_VANISHED_NAME.getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import dev.mrshawn.deathmessages.enums.MobType;
import dev.mrshawn.deathmessages.files.Config;
import dev.mrshawn.deathmessages.files.FileStore;
import dev.mrshawn.deathmessages.hooks.SayanVanishExtension;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -45,6 +47,13 @@ public static TextReplacementConfig replace(String matchLiteral, String replace)
.build();
}

public static TextReplacementConfig replace(String matchLiteral, Component replace) {
return TextReplacementConfig.builder()
.matchLiteral(matchLiteral)
.replacement((replace != null) ? replace : Component.text(matchLiteral)) // Prevent null replacement
.build();
}

public static Component formatMessage(String path) {
return convertFromLegacy(Messages.getInstance().getConfig().getString(path)).replaceText(PREFIX);
}
Expand Down Expand Up @@ -179,6 +188,40 @@ public static String randomNumeric(int length) {
return sb.toString();
}

public static String getPlayerName(PlayerManager pm) {
if (SayanVanishExtension.isSayanVanished(pm.getPlayer())) {
return PlainTextComponentSerializer.plainText().serialize(SayanVanishExtension.getVanishedName());
}

return pm.getName();
}

public static String getPlayerName(Player player) {
if (SayanVanishExtension.isSayanVanished(player)) {
return PlainTextComponentSerializer.plainText().serialize(SayanVanishExtension.getVanishedName());
}

return player.getName();
}

public static String getPlayerDisplayName(PlayerManager pm) {
final Player player = pm.getPlayer();

if (SayanVanishExtension.isSayanVanished(player)) {
return PlainTextComponentSerializer.plainText().serialize(SayanVanishExtension.getVanishedName());
}

return player.getDisplayName();
}

public static String getPlayerDisplayName(Player player) {
if (SayanVanishExtension.isSayanVanished(player)) {
return PlainTextComponentSerializer.plainText().serialize(SayanVanishExtension.getVanishedName());
}

return player.getDisplayName();
}

/*
Sakamoto Util
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package dev.mrshawn.deathmessages.hooks;

import dev.mrshawn.deathmessages.DeathMessages;
import dev.mrshawn.deathmessages.config.Settings;
import dev.mrshawn.deathmessages.files.Config;
import dev.mrshawn.deathmessages.utils.Util;
import net.kyori.adventure.text.Component;
import org.sayandev.sayanvanish.api.SayanVanishAPI;
import org.sayandev.sayanvanish.bukkit.api.BukkitUser;
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI;

import org.bukkit.entity.Player;

import java.util.UUID;

public class SayanVanishExtension {
Expand All @@ -15,4 +22,14 @@ public SayanVanishAPI<BukkitUser> getSayanVanishAPI() {
public boolean isVanished(UUID uuid) {
return getSayanVanishAPI().isVanished(uuid);
}

public static boolean isSayanVanished(Player p) {
return DeathMessages.getHooks().sayanVanishEnabled && DeathMessages.getHooks().sayanVanishExtension.isVanished(p.getUniqueId());
}

public static Component getVanishedName() {
final String name = Settings.getInstance().getConfig().getString(Config.HOOKS_SAYANVANISH_VANISHED_NAME.getPath());

return Util.convertFromLegacy(name != null ? name : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,10 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
final boolean hasBiome = msg.contains(Component.text("%biome%"));
final boolean hasDistance = msg.contains(Component.text("%distance%"));

msg = msg.replaceText(Util.replace("%entity%", Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() == null ? Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity)) : entity.getCustomName()))
.replaceText(Util.replace("%killer%", player.getName()))
.replaceText(Util.replace("%killer_display%", player.getDisplayName()))
msg = msg.replaceText(Util.replace("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity))))
.replaceText(Util.replace("%killer%", Util.getPlayerName(player)))
.replaceText(Util.replace("%killer_display%", Util.getPlayerDisplayNameComponent(player)))
.replaceText(Util.replace("%world%", entity.getLocation().getWorld().getName()))
.replaceText(Util.replace("%world_environment%", getEnvironment(entity.getLocation().getWorld().getEnvironment())))
.replaceText(Util.replace("%x%", String.valueOf(entity.getLocation().getBlock().getX())))
Expand Down Expand Up @@ -885,16 +883,13 @@ public static Component entityDeathPlaceholders(Component msg, Player player, En
}

public static String entityDeathPlaceholders(String msg, Player player, Entity entity, boolean hasOwner) {
final boolean isSayanVanished = isSayanVanished(player);
final boolean hasBiome = msg.contains("%biome%");
final boolean hasDistance = msg.contains("%distance%");

msg = msg.replaceAll("%entity%", Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%entity_display%", entity.getCustomName() == null ? Messages.getInstance().getConfig().getString("Mobs."
+ EntityUtil.getConfigNodeByEntity(entity)) : entity.getCustomName())
.replaceAll("%killer%", isSayanVanished ? getVanishedName() : player.getName())
.replaceAll("%killer_display%", isSayanVanished ? getVanishedName() : player.getDisplayName())
msg = msg.replaceAll("%entity%", Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%entity_display%", entity.getCustomName() != null ? entity.getCustomName() : Messages.getInstance().getConfig().getString("Mobs." + EntityUtil.getConfigNodeByEntity(entity)))
.replaceAll("%killer%", Util.getPlayerName(player))
.replaceAll("%killer_display%", Util.getPlayerDisplayName(player))
.replaceAll("%world%", entity.getLocation().getWorld().getName())
.replaceAll("%world_environment%", getEnvironment(entity.getLocation().getWorld().getEnvironment()))
.replaceAll("%x%", String.valueOf(entity.getLocation().getBlock().getX()))
Expand Down Expand Up @@ -932,12 +927,11 @@ public static String entityDeathPlaceholders(String msg, Player player, Entity e
}

public static Component playerDeathPlaceholders(Component msg, PlayerManager pm, Entity mob) {
final boolean isSayanVanished = isSayanVanished(pm.getPlayer());
final boolean hasBiome = msg.contains(Component.text("%biome%"));
final boolean hasDistance = msg.contains(Component.text("%distance%"));

msg = msg.replaceText(Util.replace("%player%", isSayanVanished ? getVanishedName() : pm.getName()))
.replaceText(Util.replace("%player_display%", isSayanVanished ? getVanishedName() : pm.getPlayer().getDisplayName()))
msg = msg.replaceText(Util.replace("%player%", Util.getPlayerName(pm)))
.replaceText(Util.replace("%player_display%", Util.getPlayerDisplayNameComponent(pm)))
.replaceText(Util.replace("%world%", pm.getLastLocation().getWorld().getName()))
.replaceText(Util.replace("%world_environment%", getEnvironment(pm.getLastLocation().getWorld().getEnvironment())))
.replaceText(Util.replace("%x%", String.valueOf(pm.getLastLocation().getBlock().getX())))
Expand Down Expand Up @@ -984,7 +978,7 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,

if (mob instanceof Player) {
Player p = (Player) mob;
msg = msg.replaceText(Util.replace("%killer_display%", p.getDisplayName()));
msg = msg.replaceText(Util.replace("%killer_display%", Util.getPlayerDisplayNameComponent(p)));
}
}

Expand All @@ -1001,12 +995,11 @@ public static Component playerDeathPlaceholders(Component msg, PlayerManager pm,
}

public static String playerDeathPlaceholders(String msg, PlayerManager pm, Entity mob) {
final boolean isSayanVanished = isSayanVanished(pm.getPlayer());
final boolean hasBiome = msg.contains("%biome%");
final boolean hasDistance = msg.contains("%distance%");

msg = msg.replaceAll("%player%", isSayanVanished ? getVanishedName() : pm.getName())
.replaceAll("%player_display%", isSayanVanished ? getVanishedName() : pm.getPlayer().getDisplayName())
msg = msg.replaceAll("%player%", Util.getPlayerName(pm))
.replaceAll("%player_display%", Util.getPlayerDisplayName(pm))
.replaceAll("%world%", pm.getLastLocation().getWorld().getName())
.replaceAll("%world_environment%", getEnvironment(pm.getLastLocation().getWorld().getEnvironment()))
.replaceAll("%x%", String.valueOf(pm.getLastLocation().getBlock().getX()))
Expand Down Expand Up @@ -1053,7 +1046,7 @@ public static String playerDeathPlaceholders(String msg, PlayerManager pm, Entit

if (mob instanceof Player) {
Player p = (Player) mob;
msg = msg.replaceAll("%killer_display%", isSayanVanished(p) ? getVanishedName() : p.getDisplayName());
msg = msg.replaceAll("%killer_display%", Util.getPlayerDisplayName(p));
}
}

Expand Down Expand Up @@ -1257,15 +1250,4 @@ public static FileConfiguration getPlayerDeathMessages() {
public static FileConfiguration getEntityDeathMessages() {
return EntityDeathMessages.getInstance().getConfig();
}

private static boolean isSayanVanished(Player p) {
if (DeathMessages.getHooks().sayanVanishEnabled) {
return DeathMessages.getHooks().sayanVanishExtension.isVanished(p.getUniqueId());
}
return false;
}

private static String getVanishedName() {
return Settings.getInstance().getConfig().getString(Config.HOOKS_SAYANVANISH_VANISHED_NAME.getPath());
}
}
Loading

0 comments on commit b928484

Please sign in to comment.