Skip to content

Commit

Permalink
Make every message besides root configureable
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Oct 26, 2024
1 parent 7b5949d commit 98cbf4a
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
package me.caseload.knockbacksync.command.subcommand;

import com.mojang.realmsclient.client.Ping;
import me.caseload.knockbacksync.Base;
import me.caseload.knockbacksync.ConfigWrapper;
import me.caseload.knockbacksync.command.generic.BuilderCommand;
import me.caseload.knockbacksync.command.generic.PlayerSelector;
import me.caseload.knockbacksync.event.KBSyncEventHandler;
import me.caseload.knockbacksync.event.events.ConfigReloadEvent;
import me.caseload.knockbacksync.manager.PlayerDataManager;
import me.caseload.knockbacksync.player.PlayerData;
import me.caseload.knockbacksync.sender.Sender;
import me.caseload.knockbacksync.util.ChatUtil;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.permission.PredicatePermission;
import sun.security.krb5.Config;

import java.util.UUID;
import java.util.function.Predicate;

public class PingCommand implements BuilderCommand {

private String pingSelfAvailableMessage;
private String pingSelfUnavailableMessage;
private String pingOtherAvailableMessage;
private String pingOtherUnavailableMessage;

private String mustSpecifyPlayerFromConsoleMessage;
private String knockbacksyncDisabledForYouMessage;
private String knockbacksyncDisabledForTargetMessage;

public PingCommand() {
loadConfig();
}

public void register(CommandManager<Sender> manager) {
manager.command(
manager.commandBuilder("knockbacksync", "kbsync", "kbs")
Expand All @@ -29,16 +51,76 @@ public void register(CommandManager<Sender> manager) {

if (targetSelector == null) {
if (context.sender().isConsole()) {
context.sender().sendMessage("You must specify a player to use this command from the console.");
context.sender().sendMessage(ChatUtil.translateAlternateColorCodes('&', mustSpecifyPlayerFromConsoleMessage));
} else {
context.sender().sendMessage(ChatUtil.getPingMessage(context.sender().getUniqueId(), null));
context.sender().sendMessage(getPingMessage(context.sender().getUniqueId(), null));
}
} else {
context.sender().sendMessage(ChatUtil.getPingMessage(context.sender().getUniqueId(), targetSelector.getSinglePlayer().getUUID()));
context.sender().sendMessage(getPingMessage(context.sender().getUniqueId(), targetSelector.getSinglePlayer().getUUID()));
}
})
);
Base.INSTANCE.getEventBus().registerListeners(this);
}
private void loadConfig() {
ConfigWrapper configWrapper = Base.INSTANCE.getConfigManager().getConfigWrapper();
pingSelfAvailableMessage = configWrapper.getString("messages.ping.self.available",
"Your real ping is &b%ping%&rms. Jitter: &b%jitter%&rms. Spike: &b%spike%&r. Compensated ping: &b%compensated%&rms.");
pingSelfUnavailableMessage = configWrapper.getString("messages.ping.self.unavailable",
"Accurate ping unavailable. Your estimated ping is &b%ping%&rms.");
pingOtherAvailableMessage = configWrapper.getString("messages.ping.other.available",
"%player%'s real ping is &b%ping%&rms. Jitter: &b%jitter%&rms. Spike: &b%spike%&r. Compensated ping: &b%compensated%&rms.");
pingOtherUnavailableMessage = configWrapper.getString("messages.ping.other.unavailable",
"Accurate ping unavailable. %player%'s estimated ping is &b%ping%&rms.");
mustSpecifyPlayerFromConsoleMessage = configWrapper.getString("messages.console.must_specify_player",
"&cYou must specify a player to use the knockbacksync ping command from the console.");
knockbacksyncDisabledForYouMessage = configWrapper.getString("messages.disabled.self",
"&cKnockback synchronization is currently disabled for you!");
knockbacksyncDisabledForTargetMessage = configWrapper.getString("messages.disabled.target",
"&cKnockback synchronization is currently disabled for the target player.");
}

private String getPingMessage(UUID senderUUID, UUID targetUUID) {
PlayerData playerData = null;
boolean isSelf = senderUUID.equals(targetUUID) || targetUUID == null;

if (senderUUID == null) {
senderUUID = targetUUID;
}

if (isSelf) {
playerData = PlayerDataManager.getPlayerData(senderUUID);
if (playerData == null) {
return ChatUtil.translateAlternateColorCodes('&', knockbacksyncDisabledForYouMessage);
}
} else {
playerData = PlayerDataManager.getPlayerData(targetUUID);
if (playerData == null) {
return ChatUtil.translateAlternateColorCodes('&', knockbacksyncDisabledForTargetMessage);
}
}

String rawReturnString;
if (playerData.getPing() == null) {
rawReturnString = isSelf ? pingSelfUnavailableMessage : pingOtherUnavailableMessage;
} else {
rawReturnString = isSelf ? pingSelfAvailableMessage : pingOtherAvailableMessage;
}

rawReturnString = rawReturnString
.replace("%player%", playerData.getPlatformPlayer().getName())
.replace("%ping%", playerData.getPing() == null ?
String.valueOf(playerData.getPlatformPlayer().getPing()) :
String.format("%.3f", playerData.getPing()))
.replace("%jitter%", String.format("%.3f", playerData.getJitter()))
.replace("%spike%", String.valueOf(playerData.isSpike()))
.replace("%compensated%", String.format("%.3f", playerData.getCompensatedPing()));

return ChatUtil.translateAlternateColorCodes('&', rawReturnString);
}

@KBSyncEventHandler
public void onConfigReload(ConfigReloadEvent event) {
loadConfig();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class ReloadCommand implements BuilderCommand {

private final ConfigManager configManager = Base.INSTANCE.getConfigManager();
private String rawReloadMessage = configManager.getConfigWrapper().getString("reload_message", "&aSuccessfully reloaded KnockbackSync.");
private String rawReloadMessage = configManager.getConfigWrapper().getString("messages.reload.success", "&aSuccessfully reloaded KnockbackSync.");

public void register(CommandManager<Sender> manager) {
manager.command(
Expand Down Expand Up @@ -45,6 +45,6 @@ public void register(CommandManager<Sender> manager) {

@KBSyncEventHandler
public void onConfigReload(ConfigReloadEvent event) {
rawReloadMessage = event.getConfigManager().getConfigWrapper().getString("reload_message", "&aSuccessfully reloaded KnockbackSync.");
rawReloadMessage = event.getConfigManager().getConfigWrapper().getString("messages.reload.success", "&aSuccessfully reloaded KnockbackSync.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public class StatusCommand implements BuilderCommand {
private static final String STATUS_SELF_PERMISSION = "knockbacksync.status.self";
private static final String STATUS_OTHER_PERMISSION = "knockbacksync.status.other";

private String globalStatusMessage;
private String playerStatusMessage;
private String playerDisabledGlobalMessage;
private String globalStatusEnabledMessage;
private String globalStatusDisabledMessage;
private String playerStatusEnabledMessage;
private String playerStatusDisabledMessage;
private String playerStatusGlobalDisabledMessage;
private String noSelfPermissionMessage;
private String noOtherPermissionMessage;

public StatusCommand() {
updateConfigValues();
Expand All @@ -49,25 +53,24 @@ public void register(CommandManager<Sender> manager) {
if (targetSelector == null) {
// Show global status
boolean globalStatus = configManager.isToggled();
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', globalStatusMessage) +
(globalStatus
? ChatUtil.translateAlternateColorCodes('&', "&aEnabled")
: ChatUtil.translateAlternateColorCodes('&', "&cDisabled")));
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&',
(globalStatus ? globalStatusEnabledMessage : globalStatusDisabledMessage))
);

if (sender.hasPermission(STATUS_SELF_PERMISSION, true)) {
// Show player status for the sender (no target specified)
if (!sender.isConsole()) {
showPlayerStatus(sender, Base.INSTANCE.getPlatformServer().getPlayer(sender.getUniqueId()));
}
} else {
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', "&cYou do not have permisssion to check your knockbacksync status."));
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', noSelfPermissionMessage));
}
} else {
if (sender.hasPermission(STATUS_OTHER_PERMISSION, true)) {
PlatformPlayer target = targetSelector.getSinglePlayer();
showPlayerStatus(sender, target);
} else {
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', "&cYou do not have permission to check the status of other players!"));
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', noOtherPermissionMessage));
}
}
})
Expand All @@ -80,16 +83,13 @@ private void showPlayerStatus(Sender sender, PlatformPlayer target) {
UUID uuid = target.getUUID();
boolean playerStatus = PlayerDataManager.containsPlayerData(uuid);

String statusMessage = ChatUtil.translateAlternateColorCodes('&', playerStatusMessage.replace("%player%", target.getName()));

String statusMessage;
if (!globalStatus) {
sender.sendMessage(statusMessage + ChatUtil.translateAlternateColorCodes('&', playerDisabledGlobalMessage));
statusMessage = playerStatusGlobalDisabledMessage.replace("%player%", target.getName());
} else {
sender.sendMessage(statusMessage +
(playerStatus
? ChatUtil.translateAlternateColorCodes('&', "&aEnabled")
: ChatUtil.translateAlternateColorCodes('&', "&cDisabled")));
statusMessage = (playerStatus ? this.playerStatusEnabledMessage : this.playerStatusDisabledMessage).replace("%player%", target.getName());
}
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', statusMessage));
}

@KBSyncEventHandler
Expand All @@ -99,8 +99,19 @@ public void onConfigReload(ConfigReloadEvent event) {

private void updateConfigValues() {
ConfigWrapper config = configManager.getConfigWrapper();
this.globalStatusMessage = config.getString("global_status_message", "&eGlobal KnockbackSync status: ");
this.playerStatusMessage = config.getString("player_status_message", "&e%player%'s KnockbackSync status: ");
this.playerDisabledGlobalMessage = config.getString("player_disabled_global_message", "&cDisabled (Global toggle is off)");
this.globalStatusEnabledMessage = config.getString("messages.status.global.enabled",
"&eGlobal KnockbackSync status: &aEnabled");
this.globalStatusDisabledMessage = config.getString("messages.status.global.disabled",
"&eGlobal KnockbackSync status: &cDisabled");
this.playerStatusEnabledMessage = config.getString("messages.status.player.enabled",
"&e%player%'s KnockbackSync status: &aEnabled");
this.playerStatusDisabledMessage = config.getString("messages.status.player.disabled",
"&e%player%'s KnockbackSync status: &cDisabled");
this.playerStatusGlobalDisabledMessage = config.getString("messages.status.player.global_disabled",
"&e%player%'s KnockbackSync status: &cDisabled (Global toggle is off)");
this.noSelfPermissionMessage = config.getString("messages.status.permission.no_self",
"&cYou do not have permission to check your knockbacksync status.");
this.noOtherPermissionMessage = config.getString("messages.status.permission.no_other",
"&cYou do not have permission to check the status of other players!");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.caseload.knockbacksync.command.subcommand;
import me.caseload.knockbacksync.Base;
import me.caseload.knockbacksync.ConfigWrapper;
import me.caseload.knockbacksync.command.generic.BuilderCommand;
import me.caseload.knockbacksync.command.generic.PlayerSelector;
import me.caseload.knockbacksync.event.events.ToggleOnOffEvent;
Expand All @@ -24,6 +25,26 @@ public class ToggleCommand implements BuilderCommand {
private static final String TOGGLE_SELF_PERMISSION = "knockbacksync.toggle.self";
private static final String TOGGLE_OTHER_PERMISSION = "knockbacksync.toggle.other";

private String noGlobalPermissionMessage;
private String noSelfPermissionMessage;
private String noOtherPermissionMessage;
private String serverDisabledMessage;

public ToggleCommand() {
loadConfig();
}

private void loadConfig() {
ConfigWrapper configWrapper = configManager.getConfigWrapper();
noGlobalPermissionMessage = configWrapper.getString("messages.toggle.permission.no_global",
"&cYou don't have permission to toggle the global setting.");
noSelfPermissionMessage = configWrapper.getString("messages.toggle.permission.no_self",
"&cYou do not have permission to toggle your knockback.");
noOtherPermissionMessage = configWrapper.getString("messages.toggle.permission.no_other",
"&cYou do not have permission to toggle the knockback of other player's.");
serverDisabledMessage = configWrapper.getString("messages.toggle.server_disabled",
"&cKnockbacksync is currently disabled on this server. Contact your server administrator for more information.");
}

@Override
public void register(CommandManager<Sender> manager) {
Expand All @@ -48,21 +69,21 @@ public void register(CommandManager<Sender> manager) {
if (permissionChecker.hasPermission(sender, TOGGLE_GLOBAL_PERMISSION, false)) {
toggleGlobalKnockback(configManager, sender);
} else {
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', "&cYou don't have permission to toggle the global setting."));
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', noGlobalPermissionMessage));
}
} else {
PlatformPlayer target = targetSelector.getSinglePlayer();
boolean senderIsTarget = sender.getUniqueId() == target.getUUID();
if (!senderIsTarget && !permissionChecker.hasPermission(sender, TOGGLE_OTHER_PERMISSION, false)) {
sender.sendMessage("You do not have permission to toggle the knockback of other player's.");
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', noOtherPermissionMessage));
return;
} else if (senderIsTarget && !permissionChecker.hasPermission(sender, TOGGLE_SELF_PERMISSION, true)) {
sender.sendMessage("You do not have permission to toggle your knockback.");
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', noSelfPermissionMessage));
return;
}

if (!configManager.isToggled()) {
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', "&cKnockbacksync is currently disabled on this server. Contact your server administrator for more information."));
sender.sendMessage(ChatUtil.translateAlternateColorCodes('&', serverDisabledMessage));
} else {
togglePlayerKnockback(target, configManager, sender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

public class ToggleOffGroundSubcommand implements BuilderCommand {

private String enableOffGroundSyncMessage;
private String disableOffGroundSyncMessage;
private boolean offGroundSyncEnabled;
private String offGroundSyncEnableMessage;
private String offGroundSyncDisableMessage;

public ToggleOffGroundSubcommand() {
loadConfigSettings();
Expand All @@ -40,7 +40,7 @@ public void register(CommandManager<Sender> manager) {
Base.INSTANCE.getConfigManager().getConfigWrapper().set("enable_offground_synchronization", toggledState);
Base.INSTANCE.getConfigManager().saveConfig();
String message = ChatUtil.translateAlternateColorCodes('&',
toggledState ? enableOffGroundSyncMessage : disableOffGroundSyncMessage);
toggledState ? offGroundSyncEnableMessage : offGroundSyncDisableMessage);
commandContext.sender().sendMessage(message);
}));
}
Expand All @@ -52,8 +52,10 @@ public void onConfigReloadEvent(ConfigReloadEvent event) {

private void loadConfigSettings() {
ConfigWrapper configWrapper = Base.INSTANCE.getConfigManager().getConfigWrapper();
offGroundSyncEnabled = configWrapper.getBoolean("enable_offground_synchronization", false);
enableOffGroundSyncMessage = configWrapper.getString("enable_offground_synchronization_message", "&aSuccessfully enabled offground experiment.");
disableOffGroundSyncMessage = configWrapper.getString("disable_offground_synchronization_message", "&cSuccessfully disabled offground experiment.");
this.offGroundSyncEnabled = configWrapper.getBoolean("settings.offground.enabled", false);
this.offGroundSyncEnableMessage = configWrapper.getString("messages.offground.enable",
"&aSuccessfully enabled offground synchronization.");
this.offGroundSyncDisableMessage = configWrapper.getString("messages.offground.disable",
"&cSuccessfully disabled offground synchronization.");
}
}
Loading

0 comments on commit 98cbf4a

Please sign in to comment.