Skip to content

Commit ea9344d

Browse files
committed
Code clean
update to getFirst get uuid via non deprecated method consistent line continuation indentation add some javaDoc
1 parent abdb36b commit ea9344d

11 files changed

Lines changed: 157 additions & 119 deletions

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

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import de.minebench.syncinv.listeners.PlayerConnectionValidateLoginListener;
1212
import de.minebench.syncinv.listeners.PlayerFreezeListener;
1313
import de.minebench.syncinv.listeners.PlayerJoinListener;
14-
import de.minebench.syncinv.listeners.PlayerLoginListener;
1514
import de.minebench.syncinv.listeners.PlayerQuitListener;
15+
import de.minebench.syncinv.listeners.PlayerLoginListener;
1616
import de.minebench.syncinv.messenger.Message;
1717
import de.minebench.syncinv.messenger.MessageType;
1818
import de.minebench.syncinv.messenger.PlayerDataQuery;
@@ -50,7 +50,6 @@
5050
import java.lang.reflect.Field;
5151
import java.lang.reflect.InvocationTargetException;
5252
import java.lang.reflect.Method;
53-
import java.nio.charset.StandardCharsets;
5453
import java.nio.file.Files;
5554
import java.util.AbstractMap;
5655
import java.util.Date;
@@ -177,8 +176,8 @@ public void onEnable() {
177176
loadConfig();
178177

179178
playerDataFolder = getServer().getMinecraftVersion().startsWith("1.")
180-
? new File(getServer().getWorlds().get(0).getWorldFolder(), "playerdata")
181-
: new File(new File(getServer().getWorlds().get(0).getWorldFolder(), "players"), "data");
179+
? new File(getServer().getWorlds().getFirst().getWorldFolder(), "playerdata")
180+
: new File(new File(getServer().getWorlds().getFirst().getWorldFolder(), "players"), "data");
182181

183182
MethodHandle tempUUIDGetterHandle = null;
184183
try {
@@ -410,7 +409,7 @@ && getConfig().contains("sync-" + syncType.getKey(), true)) {
410409
}
411410

412411
if (getServer().getMap((short) 0) == null) {
413-
getServer().createMap(getServer().getWorlds().get(0));
412+
getServer().createMap(getServer().getWorlds().getFirst());
414413
}
415414
try {
416415
MapView map = null;
@@ -483,10 +482,10 @@ public boolean isLocked(UUID playerId) {
483482

484483
/**
485484
* Get the date when a player last logged out
486-
* @param playerId The UUID of the player
487-
* @param online Whether or not it should return the current time if the player is online
488-
* @return The timestamp of his last known data on the server in milliseconds;
489-
* 0 if the file doesn't exist or an error occurs. (Take a look at {File#lastModified})
485+
* @param playerId The UUID of the player
486+
* @param online Whether or not it should return the current time if the player is online
487+
* @return The timestamp of his last known data on the server in milliseconds;
488+
* 0 if the file doesn't exist or an error occurs. (Take a look at {File#lastModified})
490489
*/
491490
public long getLastSeen(UUID playerId, boolean online) {
492491
if (online) {
@@ -536,7 +535,7 @@ public boolean setLastSeen(UUID playerId, long timeStamp) {
536535
}
537536
// Workaround for systems that don't allow modifying the dat directly
538537
try {
539-
Files.write(lastSeen.toPath(), String.valueOf(timeStamp).getBytes(StandardCharsets.UTF_8));
538+
Files.writeString(lastSeen.toPath(), String.valueOf(timeStamp));
540539
return true;
541540
} catch (IOException e) {
542541
getLogger().log(Level.SEVERE, "Unable to store lastseen file for " + playerId, e);
@@ -585,7 +584,7 @@ public void connectToServer(UUID playerId, String server) {
585584

586585
/**
587586
* Apply a PlayerData object to its player
588-
* @param data The data to apply
587+
* @param data The data to apply
589588
*/
590589
public void applyData(PlayerData data, Runnable finished) {
591590
if (data == null)
@@ -929,27 +928,32 @@ private void forceRender(MapView map) {
929928
map.addRenderer(new EmptyRenderer());
930929
}
931930

931+
/**
932+
* @return Reference to the OpenInv plugin to load data for the query option
933+
*/
932934
public OpenInv getOpenInv() {
933-
return this.openInv;
935+
return openInv;
934936
}
935937

938+
/**
939+
* @return The messenger for communications between the servers
940+
*/
936941
public ServerMessenger getMessenger() {
937-
return this.messenger;
942+
return messenger;
938943
}
939944

945+
/**
946+
* @return The amount of seconds we should wait for a query to stopTimeout
947+
*/
940948
public int getQueryTimeout() {
941-
return this.queryTimeout;
949+
return queryTimeout;
942950
}
943951

952+
/**
953+
* @return The id of the newest map that was seen on this server
954+
*/
944955
public int getNewestMap() {
945-
return this.newestMap;
946-
}
947-
948-
private static class EmptyRenderer extends MapRenderer {
949-
@Override
950-
public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Player player) {
951-
952-
}
956+
return newestMap;
953957
}
954958

955959
private void cacheData(PlayerData data, Runnable finished) {
@@ -958,7 +962,7 @@ private void cacheData(PlayerData data, Runnable finished) {
958962

959963
/**
960964
* Get data that was cached which should be applied on a player's login
961-
* @param player The player to get the data for
965+
* @param player The player to get the data for
962966
* @return A cache entry containing the PlayerData and the notification Runnable when applied successfully
963967
*/
964968
public Map.Entry<PlayerData, Runnable> getCachedData(Player player) {
@@ -967,7 +971,7 @@ public Map.Entry<PlayerData, Runnable> getCachedData(Player player) {
967971

968972
/**
969973
* Remove the cached data of a player
970-
* @param player The player to remove the data for
974+
* @param player The player to remove the data for
971975
*/
972976
public void removeCachedData(Player player) {
973977
playerDataCache.invalidate(player.getUniqueId());
@@ -1124,7 +1128,7 @@ public PlayerData getData(Player player) {
11241128

11251129
/**
11261130
* The sound to play when a player gets unlocked, should match the vanilla levelup
1127-
* @param playerId The uuid of the Player to play the sound to
1131+
* @param playerId The uuid of the Player to play the sound to
11281132
*/
11291133
public void playLoadSound(UUID playerId) {
11301134
Player player = getServer().getPlayer(playerId);
@@ -1135,7 +1139,7 @@ public void playLoadSound(UUID playerId) {
11351139

11361140
/**
11371141
* The sound to play when a player gets unlocked, should match the vanilla levelup
1138-
* @param player The Player to play the sound to
1142+
* @param player The Player to play the sound to
11391143
*/
11401144
public void playLoadSound(Player player) {
11411145
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.7f, 1);
@@ -1150,7 +1154,7 @@ public void checkMap(int id) {
11501154
logDebug("Checking map " + id);
11511155
try {
11521156
while (getServer().getMap(id) == null) {
1153-
MapView map = getServer().createMap(getServer().getWorlds().get(0));
1157+
MapView map = getServer().createMap(getServer().getWorlds().getFirst());
11541158
logDebug("Created map " + map.getId());
11551159
}
11561160
} catch (Exception e) {
@@ -1235,4 +1239,10 @@ private enum FilterMode {
12351239
DENY,
12361240
ALLOW
12371241
}
1242+
1243+
private static class EmptyRenderer extends MapRenderer {
1244+
@Override
1245+
public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Player player) {
1246+
}
1247+
}
12381248
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ public void onPlayerLogin(PlayerConnectionValidateLoginEvent e) {
4141
// Event will have pre-cancelled result set for whitelist/banlist
4242
if (e.isAllowed() && e.getConnection() instanceof PlayerLoginConnection connection && connection.getUnsafeProfile() != null) {
4343
// Sync login listener for sanity checking as we don't want to allow the player to exist twice
44-
Entity entity = plugin.getServer().getEntity(connection.getUnsafeProfile().getUniqueId());
44+
Entity entity = plugin.getServer().getEntity(connection.getUnsafeProfile().getId());
4545
if (entity instanceof Player) {
4646
e.kickMessage(Component.text("A player with your UUID already exists!").color(NamedTextColor.RED));
47-
plugin.logDebug("A player object with the same UUID " + connection.getUnsafeProfile().getUniqueId() + " already exists on the server.");
47+
plugin.logDebug("A player object with the same UUID " + connection.getUnsafeProfile().getId() + " already exists on the server.");
4848
// Kick player. This should do nothing if it's not a real one (e.g. one loaded by OpenInv)
4949
// Removal of such players is up to OpenInv itself
5050
((Player) entity).kickPlayer("Login from different location.");
5151
} else if (entity != null) {
5252
// Well... this is weird. An entity with the same UUID as the player's exists?!? Removing it just to be sure...
53-
plugin.getLogger().info("A " + entity + " with the same UUID " + connection.getUnsafeProfile().getUniqueId()
53+
plugin.getLogger().info("A " + entity + " with the same UUID " + connection.getUnsafeProfile().getId()
5454
+ " as the player login in existed on the server at " + entity.getLocation() + "... removing it!");
5555
entity.remove();
5656
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public PlayerFreezeListener(SyncInv plugin) {
4747

4848
@EventHandler(ignoreCancelled = true)
4949
public void onDropItem(PlayerDropItemEvent e) {
50-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
50+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
5151
e.setCancelled(true);
5252
e.getPlayer().sendMessage(plugin.getLang("cant-drop-items"));
5353
}
5454
}
5555

5656
@EventHandler(ignoreCancelled = true)
5757
public void onPlayerMove(PlayerMoveEvent e) {
58-
if(!sameBlock(e.getFrom(), e.getTo()) && plugin.isLocked(e.getPlayer().getUniqueId())) {
58+
if (!sameBlock(e.getFrom(), e.getTo()) && plugin.isLocked(e.getPlayer().getUniqueId())) {
5959
e.setCancelled(true);
6060
if (e.getFrom().getBlockY() == e.getTo().getBlockY()) {
6161
e.getPlayer().sendMessage(plugin.getLang("cant-move"));
@@ -79,15 +79,15 @@ public void onPlayerPickupItem(EntityPickupItemEvent e) {
7979

8080
@EventHandler(ignoreCancelled = true)
8181
public void onPlayerPickupExp(PlayerExpChangeEvent e) {
82-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
82+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
8383
e.setAmount(0);
8484
e.getPlayer().sendMessage(plugin.getLang("cant-pickup-exp"));
8585
}
8686
}
8787

8888
@EventHandler(ignoreCancelled = true)
8989
public void onPlayerDamage(EntityDamageEvent e) {
90-
if(e.getEntity() instanceof Player && plugin.isLocked(e.getEntity().getUniqueId())) {
90+
if (e.getEntity() instanceof Player && plugin.isLocked(e.getEntity().getUniqueId())) {
9191
e.setCancelled(true);
9292
}
9393
}
@@ -101,63 +101,63 @@ public void onPlayerDeath(PlayerDeathEvent e) {
101101

102102
@EventHandler(ignoreCancelled = true)
103103
public void onInventoryInteraction(InventoryClickEvent e) {
104-
if(plugin.isLocked(e.getWhoClicked().getUniqueId())) {
104+
if (plugin.isLocked(e.getWhoClicked().getUniqueId())) {
105105
e.setCancelled(true);
106106
e.getWhoClicked().sendMessage(plugin.getLang("wait-for-loading"));
107107
}
108108
}
109109

110110
@EventHandler(ignoreCancelled = true)
111111
public void onInventoryInteraction(InventoryDragEvent e) {
112-
if(plugin.isLocked(e.getWhoClicked().getUniqueId())) {
112+
if (plugin.isLocked(e.getWhoClicked().getUniqueId())) {
113113
e.setCancelled(true);
114114
e.getWhoClicked().sendMessage(plugin.getLang("wait-for-loading"));
115115
}
116116
}
117117

118118
@EventHandler(ignoreCancelled = true)
119119
public void onInventoryInteraction(InventoryOpenEvent e) {
120-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
120+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
121121
e.setCancelled(true);
122122
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
123123
}
124124
}
125125

126126
@EventHandler(ignoreCancelled = true)
127127
public void onInteraction(PlayerInteractEvent e) {
128-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
128+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
129129
e.setCancelled(true);
130130
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
131131
}
132132
}
133133

134134
@EventHandler(ignoreCancelled = true)
135135
public void onDamageEntity(EntityDamageByEntityEvent e) {
136-
if(e.getDamager() instanceof Player && plugin.isLocked(e.getDamager().getUniqueId())) {
136+
if (e.getDamager() instanceof Player && plugin.isLocked(e.getDamager().getUniqueId())) {
137137
e.setCancelled(true);
138138
e.getDamager().sendMessage(plugin.getLang("wait-for-loading"));
139139
}
140140
}
141141

142142
@EventHandler(ignoreCancelled = true)
143143
public void onBlockDamage(BlockDamageEvent e) {
144-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
144+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
145145
e.setCancelled(true);
146146
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
147147
}
148148
}
149149

150150
@EventHandler(ignoreCancelled = true)
151151
public void onChat(AsyncPlayerChatEvent e) {
152-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
152+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
153153
e.setCancelled(true);
154154
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
155155
}
156156
}
157157

158158
@EventHandler(ignoreCancelled = true)
159159
public void onChat(PlayerCommandPreprocessEvent e) {
160-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
160+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
161161
e.setCancelled(true);
162162
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
163163
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void onPlayerPreLogin(AsyncPlayerPreLoginEvent e) {
4747
if (plugin.getMessenger().queryData(e.getUniqueId()) == null && (!plugin.getMessenger().isAllowedToBeAlone() || !plugin.getMessenger().isAlone())) {
4848
e.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
4949
e.setKickMessage(ChatColor.RED + "Unable to query player data!");
50-
return;
5150
}
5251
}
5352
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
4343
}
4444
PlayerDataQuery query = plugin.getMessenger().removeQuery(event.getPlayer().getUniqueId());
4545
if (query != null) {
46-
// The player is gone although he had a query...
46+
// The player is gone, although he had a query...
4747
// We have to make sure now that the time of the data file matches the old one
4848
// and not send our data to all the other servers as it might be outdated
4949
plugin.runLater(() -> {
@@ -67,5 +67,4 @@ public void onPlayerQuit(PlayerQuitEvent event) {
6767
// Update last seen
6868
plugin.runLater(() -> plugin.setLastSeen(event.getPlayer().getUniqueId(), System.currentTimeMillis()), 1);
6969
}
70-
7170
}

src/main/java/de/minebench/syncinv/messenger/Message.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ public class Message {
4040

4141
/**
4242
* A Message of a certain type. Optionally with some data
43-
* @param sender The sender which sent the message
44-
* @param id The transaction ID this Message is associated with
45-
* @param type The type of the message
46-
* @param objects The data, in the order that it should be sent
43+
*
44+
* @param sender The sender which sent the message
45+
* @param id The transaction ID this Message is associated with
46+
* @param type The type of the message
47+
* @param objects The data, in the order that it should be sent
4748
* @throws IllegalArgumentException when the amount of Objects given didn't match the MessageType requirements
4849
*/
4950
public Message(String sender, long id, MessageType type, Object... objects) {
@@ -100,10 +101,10 @@ public Object read() {
100101

101102
/**
102103
* Generate a byte array out of the data of this message
103-
* @return The generated byte array (starts with the sender
104-
* then the type ordinal, then the amount
105-
* of data being sent and each data object);
106-
* an empty one if an error occurred
104+
* @return The generated byte array (starts with the sender
105+
* then the type ordinal, then the amount
106+
* of data being sent and each data object);
107+
* an empty one if an error occurred
107108
*/
108109
public byte[] toByteArray() {
109110
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -127,12 +128,12 @@ public byte[] toByteArray() {
127128
/**
128129
* Get the message from a bytearray generated as described in the toByteArray method
129130
* @param bytes The bytes
130-
* @return The Message object
131+
* @return The Message object
131132
* @throws IOException
132-
* @throws IllegalArgumentException When the message type is not supported
133+
* @throws IllegalArgumentException When the message type is not supported
133134
* @throws ClassNotFoundException
134-
* @throws InvalidConfigurationException If the data is invalid
135-
* @throws VersionMismatchException If the received message is of a different version than it can accept
135+
* @throws InvalidConfigurationException If the data is invalid
136+
* @throws VersionMismatchException If the received message is of a different version than it can accept
136137
*/
137138
public static Message fromByteArray(byte[] bytes) throws IOException, IllegalArgumentException, ClassNotFoundException, InvalidConfigurationException, VersionMismatchException {
138139
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
@@ -152,6 +153,9 @@ public static Message fromByteArray(byte[] bytes) throws IOException, IllegalArg
152153
}
153154
}
154155

156+
/**
157+
* @return a Sting representation of this message
158+
*/
155159
public String toString() {
156160
return "Message(sender=" + this.getSender() + ", id=" + this.getId() + ", type=" + this.getType() + ", data=" + this.getData() + ")";
157161
}

0 commit comments

Comments
 (0)