Skip to content

Commit

Permalink
Revamped displayname placeholder to custom placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
AerWyn81 committed May 2, 2024
1 parent effdca7 commit 289b7fe
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id("java")
}

version = "2.4.9"
version = "2.4.10"

allprojects {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
public final class PlayerProfileLight {
private final UUID uuid;
private final String name;
private final String displayName;
private final String customDisplay;

public PlayerProfileLight(UUID uuid, String name, String displayName) {
public PlayerProfileLight(UUID uuid, String name, String customDisplay) {
this.uuid = uuid;
this.name = name;
this.displayName = displayName;
this.customDisplay = customDisplay;
}

public UUID uuid() {
Expand All @@ -21,7 +21,7 @@ public String name() {
return name;
}

public String displayName() {
return displayName;
public String customDisplay() {
return customDisplay;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void updatePlayerInfo(PlayerProfileLight profile) throws InternalExceptio
try (PreparedStatement ps = connection.prepareStatement(Requests.UPDATE_PLAYER_MYSQL)) {
ps.setString(1, profile.uuid().toString());
ps.setString(2, profile.name());
ps.setString(3, profile.displayName());
ps.setString(3, profile.customDisplay());
ps.executeUpdate();
} catch (SQLException ex) {
throw new InternalException(ex);
Expand Down Expand Up @@ -358,7 +358,7 @@ public boolean hasPlayerRenamed(PlayerProfileLight profile) throws InternalExcep
ResultSet rs = ps.executeQuery();

if (rs.next()) {
return !profile.name().equals(rs.getString("pName")) || !profile.displayName().equals(rs.getString("pDisplayName"));
return !profile.name().equals(rs.getString("pName")) || !profile.customDisplay().equals(rs.getString("pDisplayName"));
}
} catch (Exception ex) {
throw new InternalException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void updatePlayerInfo(PlayerProfileLight profile) throws InternalExceptio
try (PreparedStatement ps = connection.prepareStatement(Requests.UPDATE_PLAYER_MYSQL)) {
ps.setString(1, profile.uuid().toString());
ps.setString(2, profile.name());
ps.setString(3, profile.displayName());
ps.setString(3, profile.customDisplay());
ps.executeUpdate();
} catch (SQLException ex) {
throw new InternalException(ex);
Expand Down Expand Up @@ -354,11 +354,11 @@ public boolean hasPlayerRenamed(PlayerProfileLight profile) throws InternalExcep

try (PreparedStatement ps = connection.prepareStatement(Requests.CHECK_PLAYER_NAME)) {
ps.setString(1, profile.uuid().toString());
ps.setString(2, profile.displayName());
ps.setString(2, profile.customDisplay());
ResultSet rs = ps.executeQuery();

if (rs.next()) {
return !profile.name().equals(rs.getString("pName")) || !profile.displayName().equals(rs.getString("pDisplayName"));
return !profile.name().equals(rs.getString("pName")) || !profile.customDisplay().equals(rs.getString("pDisplayName"));
}
} catch (Exception ex) {
throw new InternalException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void updatePlayerInfo(PlayerProfileLight profile) throws InternalExceptio
try (PreparedStatement ps = connection.prepareStatement(Requests.UPDATE_PLAYER)) {
ps.setString(1, profile.uuid().toString());
ps.setString(2, profile.name());
ps.setString(3, profile.displayName());
ps.setString(3, profile.customDisplay());

ps.executeUpdate();
} catch (Exception ex) {
Expand Down Expand Up @@ -309,7 +309,7 @@ public boolean hasPlayerRenamed(PlayerProfileLight profile) throws InternalExcep
ResultSet rs = ps.executeQuery();

if (rs.next()) {
return !profile.name().equals(rs.getString("pName")) || !profile.displayName().equals(rs.getString("pDisplayName"));
return !profile.name().equals(rs.getString("pName")) || !profile.customDisplay().equals(rs.getString("pDisplayName"));
}
} catch (Exception ex) {
throw new InternalException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.aerwyn81.headblocks.HeadBlocks;
import fr.aerwyn81.headblocks.data.HeadLocation;
import fr.aerwyn81.headblocks.services.HeadService;
import fr.aerwyn81.headblocks.services.LanguageService;
import fr.aerwyn81.headblocks.services.StorageService;
import fr.aerwyn81.headblocks.utils.internal.InternalException;
import fr.aerwyn81.headblocks.utils.message.MessageUtils;
Expand All @@ -13,7 +12,6 @@
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;

public class PlaceholderHook extends PlaceholderExpansion {

Expand Down Expand Up @@ -72,7 +70,7 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
}
}

// %headblocks_leaderboard_<position>_<name|displayname|value>%
// %headblocks_leaderboard_<position>_<name|custom|value>%
if (identifier.contains("leaderboard")) {
var str = identifier.split("_");
try {
Expand All @@ -94,19 +92,19 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
case "name" -> {
return p.getKey().name();
}
case "displayname" -> {
var displayName = p.getKey().displayName();
case "custom" -> {
var displayName = p.getKey().customDisplay();
return displayName.isEmpty() ? p.getKey().name() : displayName;
}
case "value" -> {
return String.valueOf(p.getValue());
}
default -> {
return p.getKey().displayName() + " (" + p.getKey().name() + ") " + ": " + p.getValue();
return p.getKey().customDisplay() + " (" + p.getKey().name() + ") " + ": " + p.getValue();
}
}
} catch (Exception ex) {
return "Cannot parse the leaderboard placeholder. Use %headblocks_leaderboard_<position>_<name|displayname|value>%.";
return "Cannot parse the leaderboard placeholder. Use %headblocks_leaderboard_<position>_<name|custom|value>%.";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,16 @@ public static int getHeadClickCommandsSlotsRequired() {
public static boolean isPreventPistonExtension() { return config.getBoolean("externalInteractions.piston", true); }

public static boolean isPreventLiquidFlow() { return config.getBoolean("externalInteractions.water", true); }

public static String getPlaceholdersLeaderboardPrefix() {
return config.getString("placeholders.leaderboard.prefix", "");
}

public static String getPlaceholdersLeaderboardSuffix() {
return config.getString("placeholders.leaderboard.suffix", "");
}

public static boolean isPlaceholdersLeaderboardUseNickname() {
return config.getBoolean("placeholders.leaderboard.nickname", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fr.aerwyn81.headblocks.storages.types.Redis;
import fr.aerwyn81.headblocks.utils.internal.InternalException;
import fr.aerwyn81.headblocks.utils.message.MessageUtils;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;

import java.io.*;
Expand Down Expand Up @@ -187,11 +188,12 @@ private static boolean backupDatabase() {
public static void loadPlayer(Player player) {
UUID pUuid = player.getUniqueId();
String playerName = player.getName();
String playerDisplayName = player.getDisplayName();

try {
boolean isExist = containsPlayer(pUuid);

String playerDisplayName = getCustomDisplay(player);

var playerProfile = new PlayerProfileLight(pUuid, playerName, playerDisplayName);

if (isExist) {
Expand All @@ -215,6 +217,26 @@ public static void loadPlayer(Player player) {
}
}

private static String getCustomDisplay(Player player) {
var customName = player.getName();

if (ConfigService.isPlaceholdersLeaderboardUseNickname()) {
customName = player.getDisplayName();
}

var prefix = ConfigService.getPlaceholdersLeaderboardPrefix();
if (!prefix.isEmpty()) {
customName = PlaceholderAPI.setPlaceholders(player, prefix) + customName;
}

var suffix = ConfigService.getPlaceholdersLeaderboardSuffix();
if (!prefix.isEmpty()) {
customName = customName + PlaceholderAPI.setPlaceholders(player, suffix);
}

return customName;
}

public static void unloadPlayer(Player player) {
UUID uuid = player.getUniqueId();
String playerName = player.getName();
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ externalInteractions:
piston: true
water: true

# Used to customize the %headblocks_leaderboard_<position>_custom% placeholder
# Support placeholders (PlaceholderAPI)
# IMPORTANT: Player had to reconnect or server restart to make the modification effective
placeholders:
leaderboard:
prefix: ""
suffix: ""
nickname: false

# Tiered rewards : provide a head count to trigger a reward command list
# Add the tier, then messages, commands or broadcast message which will be executed
# You can add "slotsRequired" to give the reward only if the player has enough space in inventory
Expand Down
2 changes: 1 addition & 1 deletion docs/config/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ See the list below:
- `%headblocks_order_previous%`: if order is specified, display the previous named head found (_raw integer or "-" if not calculable_)
- `%headblocks_order_current%`: if order is specified, display the current named head found (_raw integer or "-" if not calculable_)
- `%headblocks_order_next%`: if order is specified, display the next named head to found (_raw integer or "-" if not calculable_)
- `%headblocks_leaderboard_<position>_<name|displayname|value>%`: used to build a leaderboard
- `%headblocks_leaderboard_<position>_<name|custom|value>%`: used to build a leaderboard (custom check config file)

0 comments on commit 289b7fe

Please # to comment.