Skip to content

Commit 2ef7eff

Browse files
committed
update use textComponents, since we rely on paper anyway
1 parent 2373033 commit 2ef7eff

7 files changed

Lines changed: 52 additions & 32 deletions

File tree

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
<version>6.5.1.RELEASE</version>
6060
<scope>compile</scope>
6161
</dependency>
62+
<dependency>
63+
<groupId>de.themoep</groupId>
64+
<artifactId>minedown-adventure</artifactId>
65+
<version>1.7.1-SNAPSHOT</version>
66+
<scope>compile</scope>
67+
</dependency>
6268
</dependencies>
6369

6470
<profiles>
@@ -130,6 +136,10 @@
130136
<pattern>org.reactivestreams</pattern>
131137
<shadedPattern>de.minebench.syncinv.lib.reactivestreams</shadedPattern>
132138
</relocation>
139+
<relocation>
140+
<pattern>de.themoep.minedown</pattern>
141+
<shadedPattern>de.minebench.syncinv.lib.minedown</shadedPattern>
142+
</relocation>
133143
</relocations>
134144
</configuration>
135145
</execution>

src/main/java/de/minebench/syncinv/SyncInv.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
import de.minebench.syncinv.messenger.PlayerDataQuery;
1919
import de.minebench.syncinv.messenger.RedisMessenger;
2020
import de.minebench.syncinv.messenger.ServerMessenger;
21+
import de.themoep.minedown.adventure.MineDown;
2122
import lombok.Getter;
22-
import org.bukkit.ChatColor;
23+
import net.kyori.adventure.text.Component;
2324
import org.bukkit.GameRule;
2425
import org.bukkit.Location;
2526
import org.bukkit.Material;
@@ -294,16 +295,15 @@ public void onEnable() {
294295
if (query.getYoungestServer() == null) {
295296
openInvCommand.onCommand(sender, command, label, args);
296297
} else {
297-
sender.sendMessage(ChatColor.RED + "Current server does not have newest player data! "
298-
+ ChatColor.GRAY + "Connecting to server " + query.getYoungestServer() + " which has the newest data...");
298+
sender.sendMessage(getLang("stale-player-data", "server", query.getYoungestServer()));
299299
connectToServer(((Player) sender).getUniqueId(), query.getYoungestServer());
300300
}
301301
});
302302
if (q == null) {
303-
sender.sendMessage(ChatColor.RED + "Could not query information from other servers! Take a look at the log for more details.");
303+
sender.sendMessage(getLang("query-error"));
304304
}
305305
} else {
306-
sender.sendMessage(ChatColor.RED + "Player not found!");
306+
sender.sendMessage(getLang("player-not-found"));
307307
}
308308
});
309309
return true;
@@ -355,8 +355,11 @@ public void onDisable() {
355355
}
356356

357357
public void loadConfig() {
358-
saveDefaultConfig();
359-
reloadConfig();
358+
reloadConfig(); // load working config
359+
360+
// save newly added config options to disk
361+
getConfig().options().copyDefaults(true);
362+
saveConfig();
360363

361364
debug = getConfig().getBoolean("debug");
362365

@@ -469,11 +472,11 @@ && getConfig().contains("sync-" + syncType.getKey(), true)) {
469472
}
470473
}
471474

472-
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
475+
public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args) {
473476
if (args.length > 0) {
474477
if ("reload".equalsIgnoreCase(args[0]) && sender.hasPermission("syncing.command.reload")) {
475478
loadConfig();
476-
sender.sendMessage(ChatColor.YELLOW + "Config reloaded!");
479+
sender.sendMessage(getLang("config-reloaded"));
477480
return true;
478481
}
479482
}
@@ -482,16 +485,19 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
482485

483486
/**
484487
* Get a language message from the config and replace variables in it
485-
* @param key The key of the message (lang.<key>)
486-
* @param replacements An array of variables to be replaced with certain strings in the format [var,repl,var,repl,...]
488+
* @param key The key of the message (lang.<key>)
489+
* @param replacements An array of String to replace placeholders (uses the % character as placeholder indicators)
490+
* in format of [placeholder, repl, placeholder,repl,...]
487491
* @return The message string with colorcodes and variables replaced
488492
*/
489-
public String getLang(String key, String... replacements) {
490-
String msg = ChatColor.translateAlternateColorCodes('&', getConfig().getString("lang." + key, getName() + ": &cMissing language key &6" + key));
491-
for (int i = 0; i + 1 < replacements.length; i += 2) {
492-
msg = msg.replace("%" + replacements[i] + "%", replacements[i+1]);
493+
public Component getLang(String key, String... replacements) {
494+
String rawMsg = getConfig().getString("lang." + key); // use default defined by config (values from the config in jar)
495+
496+
if (rawMsg == null) { // key is missing
497+
return MineDown.parse(getName() + ": &cMissing language key &6" + key);
498+
} else {
499+
return MineDown.parse(rawMsg, replacements);
493500
}
494-
return msg;
495501
}
496502

497503
/**
@@ -1244,7 +1250,7 @@ public void kick(UUID playerId, String key) {
12441250
runSync(() -> {
12451251
Player player = getServer().getPlayer(playerId);
12461252
if (player != null) {
1247-
player.kickPlayer(getLang(key));
1253+
player.kick(getLang(key));
12481254
}
12491255
});
12501256
}

src/main/java/de/minebench/syncinv/listeners/MapCreationListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import de.minebench.syncinv.SyncInv;
44
import de.minebench.syncinv.SyncType;
5-
import org.bukkit.ChatColor;
65
import org.bukkit.Material;
76
import org.bukkit.entity.HumanEntity;
87
import org.bukkit.event.EventHandler;
@@ -60,7 +59,7 @@ public void onMapScale(PrepareItemCraftEvent event) {
6059
plugin.logDebug(event.getView().getPlayer().getName() + " is not on the world that map " + map.getId() + " is from! (" + mapId + ")");
6160
event.getInventory().setResult(null);
6261
for (HumanEntity viewer : event.getViewers()) {
63-
viewer.sendMessage(ChatColor.RED + "Please switch to the world where this map was created to scale it!");
62+
viewer.sendMessage(plugin.getLang("map-scale-wrong-world"));
6463
}
6564
break;
6665
}

src/main/java/de/minebench/syncinv/listeners/PlayerConnectionValidateLoginListener.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import de.minebench.syncinv.SyncInv;
44
import io.papermc.paper.connection.PlayerLoginConnection;
55
import io.papermc.paper.event.connection.PlayerConnectionValidateLoginEvent;
6-
import net.kyori.adventure.text.Component;
7-
import net.kyori.adventure.text.format.NamedTextColor;
86
import org.bukkit.entity.Entity;
97
import org.bukkit.entity.Player;
108
import org.bukkit.event.EventHandler;
@@ -42,12 +40,12 @@ public void onPlayerLogin(PlayerConnectionValidateLoginEvent e) {
4240
if (e.isAllowed() && e.getConnection() instanceof PlayerLoginConnection connection && connection.getUnsafeProfile() != null) {
4341
// Sync login listener for sanity checking as we don't want to allow the player to exist twice
4442
Entity entity = plugin.getServer().getEntity(connection.getUnsafeProfile().getUniqueId());
45-
if (entity instanceof Player) {
46-
e.kickMessage(Component.text("A player with your UUID already exists!").color(NamedTextColor.RED));
43+
if (entity instanceof Player player) {
44+
e.kickMessage(plugin.getLang("login_denied_duplicated_uuid"));
4745
plugin.logDebug("A player object with the same UUID " + connection.getUnsafeProfile().getUniqueId() + " already exists on the server.");
4846
// Kick player. This should do nothing if it's not a real one (e.g. one loaded by OpenInv)
4947
// Removal of such players is up to OpenInv itself
50-
((Player) entity).kickPlayer("Login from different location.");
48+
player.kick(plugin.getLang("kick_duplicated_uuid"));
5149
} else if (entity != null) {
5250
// Well... this is weird. An entity with the same UUID as the player's exists?!? Removing it just to be sure...
5351
plugin.getLogger().info("A " + entity + " with the same UUID " + connection.getUnsafeProfile().getUniqueId()

src/main/java/de/minebench/syncinv/listeners/PlayerJoinListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import de.minebench.syncinv.PlayerData;
44
import de.minebench.syncinv.SyncInv;
5-
import org.bukkit.ChatColor;
65
import org.bukkit.event.EventHandler;
76
import org.bukkit.event.EventPriority;
87
import org.bukkit.event.Listener;
@@ -41,12 +40,12 @@ public void onPlayerPreLogin(AsyncPlayerPreLoginEvent e) {
4140
if (e.getLoginResult() == AsyncPlayerPreLoginEvent.Result.ALLOWED) {
4241
if (plugin.getMessenger() == null) {
4342
e.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
44-
e.setKickMessage(ChatColor.RED + plugin.getName() + " is not enabled! Please contact an administrator!");
43+
e.kickMessage(plugin.getLang("login_denied_plugin_invalid_state", "plugin", plugin.getName()));
4544
return;
4645
}
4746
if (plugin.getMessenger().queryData(e.getUniqueId()) == null && (!plugin.getMessenger().isAllowedToBeAlone() || !plugin.getMessenger().isAlone())) {
4847
e.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
49-
e.setKickMessage(ChatColor.RED + "Unable to query player data!");
48+
e.kickMessage(plugin.getLang("login_denied_missing_data"));
5049
return;
5150
}
5251
}

src/main/java/de/minebench/syncinv/listeners/PlayerLoginListener.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.minebench.syncinv.listeners;
22

33
import de.minebench.syncinv.SyncInv;
4-
import org.bukkit.ChatColor;
54
import org.bukkit.entity.Entity;
65
import org.bukkit.entity.Player;
76
import org.bukkit.event.EventHandler;
@@ -40,13 +39,13 @@ public void onPlayerLogin(PlayerLoginEvent e) {
4039
if (e.getResult() == PlayerLoginEvent.Result.ALLOWED) {
4140
// Sync login listener for sanity checking as we don't want to allow the player to exist twice
4241
Entity entity = plugin.getServer().getEntity(e.getPlayer().getUniqueId());
43-
if (entity instanceof Player) {
42+
if (entity instanceof Player player) {
4443
e.setResult(PlayerLoginEvent.Result.KICK_OTHER);
45-
e.setKickMessage(ChatColor.RED + "A player with your UUID already exists!");
44+
e.kickMessage(plugin.getLang("login_denied_duplicated_uuid"));
4645
plugin.logDebug("A player object with the same UUID " + e.getPlayer().getUniqueId() + " already exists on the server.");
4746
// Kick player. This should do nothing if it's not a real one (e.g. one loaded by OpenInv)
4847
// Removal of such players is up to OpenInv itself
49-
((Player) entity).kickPlayer("Login from different location.");
48+
player.kick(plugin.getLang("kick_duplicated_uuid"));
5049
} else if (entity != null) {
5150
// Well... this is weird. An entity with the same UUID as the player's exists?!? Removing it just to be sure...
5251
plugin.getLogger().info("A " + entity + " with the same UUID " + e.getPlayer().getUniqueId()

src/main/resources/config.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,13 @@ lang:
111111
cant-drop-items: "&cYou can only drop items once your player data is loaded!"
112112
cant-move: "&cYou can only move once your player data is loaded!"
113113
wait-for-loading: "&cPlease wait until your player data is loaded!"
114-
cant-load-data: "&cSorry but the server wasn't able to load your latest player data.\nPlease contact an administrator if this happens often!"
114+
cant-load-data: "&cSorry but the server wasn't able to load your latest player data.\nPlease contact an administrator if this happens often!"
115+
config-reloaded: "&eConfig reloaded!"
116+
player-not-found: "&cPlayer not found!"
117+
query-error : "Could not query information from other servers! Take a look at the log for more details."
118+
stale-player-data : "&cCurrent server does not have newest player data! &7Connecting to server %server% which has the newest data..."
119+
map-scale-wrong-world : "&cPlease switch to the world where this map was created to scale it!"
120+
login_denied_duplicated_uuid : "&cA player with your UUID already exists!"
121+
kick_duplicated_uuid : "Login from different location."
122+
login_denied_missing_data : "&cUnable to query player data!"
123+
login_denied_plugin_invalid_state : "&c %plugin% is not enabled! Please contact an administrator!"

0 commit comments

Comments
 (0)