Skip to content

Commit 124bdc6

Browse files
committed
Code clean
1 parent 752c4e4 commit 124bdc6

10 files changed

Lines changed: 126 additions & 166 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public class PlayerData implements Serializable {
7575
private final float fallDistance;
7676
private final Vector velocity;
7777
private final int heldItemSlot;
78-
private byte[] persistentData = null;
7978
private final Map<String, Map<String, Long>> advancementProgress = new HashMap<>();
8079
private final Table<Statistic, String, Integer> statistics = HashBasedTable.create();
8180
private final long lastSeen;
81+
private byte[] persistentData = null;
8282

8383
PlayerData(Player player, long lastSeen) {
8484
this.dataVersion = player.getServer().getUnsafe().getDataVersion();

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

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public final class SyncInv extends JavaPlugin {
108108
* Sync data with all servers in a group when a player logs out
109109
*/
110110
private boolean syncWithGroupOnLogout;
111-
111+
112112
/**
113113
* Store player data even if the player never joined the server
114114
*/
@@ -147,7 +147,7 @@ public final class SyncInv extends JavaPlugin {
147147
*/
148148
@Getter
149149
private int newestMap = 0;
150-
150+
151151
// Unknown player storing
152152
private Method methodGetOfflinePlayer = null;
153153
private Method methodGetHandle = null;
@@ -160,12 +160,6 @@ public final class SyncInv extends JavaPlugin {
160160
// Offline player health setting
161161
private Method methodSetHealth;
162162

163-
// Persistent data syncing
164-
private Method methodDeserializeCompound = null;
165-
private Method methodPdcSerialize = null;
166-
private Method methodGetRaw = null;
167-
private Method methodPutAll = null;
168-
169163
// Map syncing
170164
private Field fieldWorldMap;
171165
private Field fieldMapColor;
@@ -177,7 +171,7 @@ public final class SyncInv extends JavaPlugin {
177171
public void onEnable() {
178172
// Plugin startup logic
179173
loadConfig();
180-
174+
181175
playerDataFolder = new File(getServer().getWorlds().get(0).getWorldFolder(), "playerdata");
182176
try {
183177
methodGetOfflinePlayer = getServer().getClass().getMethod("getOfflinePlayer", GameProfile.class);
@@ -296,7 +290,7 @@ public void loadConfig() {
296290
syncWithGroupOnLogout = getConfig().getBoolean("sync-with-group-on-logout");
297291

298292
storeUnknownPlayers = getConfig().getBoolean("store-unknown-players");
299-
293+
300294
queryTimeout = getConfig().getInt("query-timeout");
301295
applyTimedOutQueries = getConfig().getBoolean("apply-timed-out-queries");
302296

@@ -317,19 +311,6 @@ && getConfig().contains("sync-" + syncType.getKey(), true)) {
317311
getLogger().log(Level.INFO, "Hooked into " + openInv.getName() + " " + openInv.getDescription().getVersion());
318312
}
319313

320-
if (shouldSync(SyncType.PERSISTENT_DATA)) {
321-
try {
322-
String basePackage = getServer().getClass().getPackage().getName();
323-
Class<?> c = Class.forName(basePackage + ".util.CraftNBTTagConfigSerializer");
324-
methodDeserializeCompound = c.getMethod("deserialize", Object.class);
325-
} catch (ClassNotFoundException | NoSuchMethodException e) {
326-
if (shouldSync(SyncType.PERSISTENT_DATA)) {
327-
getLogger().log(Level.WARNING, "Could not load static method required for persistent data syncing. Disabling it!", e);
328-
disableSync(SyncType.PERSISTENT_DATA);
329-
}
330-
}
331-
}
332-
333314
if (getServer().getMap((short) 0) == null) {
334315
getServer().createMap(getServer().getWorlds().get(0));
335316
}
@@ -401,14 +382,14 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
401382

402383
/**
403384
* Get a language message from the config and replace variables in it
404-
* @param key The key of the message (lang.<key>)
385+
* @param key The key of the message (lang.<key>)
405386
* @param replacements An array of variables to be replaced with certain strings in the format [var,repl,var,repl,...]
406387
* @return The message string with colorcodes and variables replaced
407388
*/
408389
public String getLang(String key, String... replacements) {
409390
String msg = ChatColor.translateAlternateColorCodes('&', getConfig().getString("lang." + key, getName() + ": &cMissing language key &6" + key));
410391
for (int i = 0; i + 1 < replacements.length; i += 2) {
411-
msg = msg.replace("%" + replacements[i] + "%", replacements[i+1]);
392+
msg = msg.replace("%" + replacements[i] + "%", replacements[i + 1]);
412393
}
413394
return msg;
414395
}
@@ -424,10 +405,10 @@ public boolean isLocked(UUID playerId) {
424405

425406
/**
426407
* Get the date when a player last logged out
427-
* @param playerId The UUID of the player
428-
* @param online Whether or not it should return the current time if the player is online
429-
* @return The timestamp of his last known data on the server in milliseconds;
430-
* 0 if the file doesn't exist or an error occurs. (Take a look at {File#lastModified})
408+
* @param playerId The UUID of the player
409+
* @param online Whether or not it should return the current time if the player is online
410+
* @return The timestamp of his last known data on the server in milliseconds;
411+
* 0 if the file doesn't exist or an error occurs. (Take a look at {File#lastModified})
431412
*/
432413
public long getLastSeen(UUID playerId, boolean online) {
433414
if (online) {
@@ -457,7 +438,7 @@ public long getLastSeen(UUID playerId, boolean online) {
457438
* @param playerId The UUID of the player
458439
* @param timeStamp The timestamp to set as the last modify time of the file in
459440
* milliseconds.
460-
* @return true if the time was successfully set
441+
* @return true if the time was successfully set
461442
*/
462443
public boolean setLastSeen(UUID playerId, long timeStamp) {
463444
File playerDat = getPlayerDataFile(playerId);
@@ -512,7 +493,7 @@ public boolean applyTimedOutQueries() {
512493
/**
513494
* Connect a player to a bungee server
514495
* @param playerId The UUID of the player
515-
* @param server The name of the server
496+
* @param server The name of the server
516497
*/
517498
public void connectToServer(UUID playerId, String server) {
518499
Player player = getServer().getPlayer(playerId);
@@ -526,7 +507,7 @@ public void connectToServer(UUID playerId, String server) {
526507

527508
/**
528509
* Apply a PlayerData object to its player
529-
* @param data The data to apply
510+
* @param data The data to apply
530511
*/
531512
public void applyData(PlayerData data, Runnable finished) {
532513
if (data == null)
@@ -731,7 +712,7 @@ public void applyData(PlayerData data, Runnable finished) {
731712
} catch (NullPointerException ignored) {
732713
// world is not known
733714
}
734-
for (Iterator<Advancement> it = getServer().advancementIterator(); it.hasNext();) {
715+
for (Iterator<Advancement> it = getServer().advancementIterator(); it.hasNext(); ) {
735716
Advancement advancement = it.next();
736717
Map<String, Long> awarded = data.getAdvancementProgress().get(advancement.getKey().toString());
737718
if (awarded != null) {
@@ -889,20 +870,13 @@ private void forceRender(MapView map) {
889870
map.addRenderer(new EmptyRenderer());
890871
}
891872

892-
private static class EmptyRenderer extends MapRenderer {
893-
@Override
894-
public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Player player) {
895-
896-
}
897-
}
898-
899873
private void cacheData(PlayerData data, Runnable finished) {
900874
playerDataCache.put(data.getPlayerId(), new AbstractMap.SimpleEntry<>(data, finished));
901875
}
902876

903877
/**
904878
* Get data that was cached which should be applied on a player's login
905-
* @param player The player to get the data for
879+
* @param player The player to get the data for
906880
* @return A cache entry containing the PlayerData and the notification Runnable when applied successfully
907881
*/
908882
public Map.Entry<PlayerData, Runnable> getCachedData(Player player) {
@@ -922,7 +896,7 @@ private boolean createNewEmptyData(UUID playerId) {
922896
if (playerDat.exists()) {
923897
return false;
924898
}
925-
899+
926900
try {
927901
playerDat.getParentFile().mkdirs();
928902
Files.copy(getResource("empty.dat"), playerDat.toPath());
@@ -932,7 +906,7 @@ private boolean createNewEmptyData(UUID playerId) {
932906
}
933907
return false;
934908
}
935-
909+
936910
public PlayerData getData(Player player) {
937911
PlayerData data = new PlayerData(player, getLastSeen(player.getUniqueId(), player.isOnline()));
938912

@@ -947,7 +921,7 @@ public PlayerData getData(Player player) {
947921
}
948922

949923
if (shouldSync(SyncType.ADVANCEMENTS)) {
950-
for (Iterator<Advancement> it = getServer().advancementIterator(); it.hasNext();) {
924+
for (Iterator<Advancement> it = getServer().advancementIterator(); it.hasNext(); ) {
951925
Advancement advancement = it.next();
952926
AdvancementProgress progress = player.getAdvancementProgress(advancement);
953927
Map<String, Long> awarded = new HashMap<>();
@@ -1059,7 +1033,7 @@ public PlayerData getData(Player player) {
10591033

10601034
/**
10611035
* The sound to play when a player gets unlocked, should match the vanilla levelup
1062-
* @param playerId The uuid of the Player to play the sound to
1036+
* @param playerId The uuid of the Player to play the sound to
10631037
*/
10641038
public void playLoadSound(UUID playerId) {
10651039
Player player = getServer().getPlayer(playerId);
@@ -1070,7 +1044,7 @@ public void playLoadSound(UUID playerId) {
10701044

10711045
/**
10721046
* The sound to play when a player gets unlocked, should match the vanilla levelup
1073-
* @param player The Player to play the sound to
1047+
* @param player The Player to play the sound to
10741048
*/
10751049
public void playLoadSound(Player player) {
10761050
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.7f, 1);
@@ -1097,7 +1071,7 @@ public void checkMap(int id) {
10971071
* Make sure that a task runs on the primary thread
10981072
*/
10991073
public void runSync(Runnable run) {
1100-
if(getServer().isPrimaryThread() || disabling) {
1074+
if (getServer().isPrimaryThread() || disabling) {
11011075
run.run();
11021076
} else {
11031077
getServer().getScheduler().runTask(this, run);
@@ -1108,7 +1082,7 @@ public void runSync(Runnable run) {
11081082
* Make sure that a task does not run on the primary thread
11091083
*/
11101084
public void runAsync(Runnable run) {
1111-
if(!getServer().isPrimaryThread() && !disabling) {
1085+
if (!getServer().isPrimaryThread() && !disabling) {
11121086
getServer().getScheduler().runTaskAsynchronously(this, run);
11131087
} else {
11141088
run.run();
@@ -1165,4 +1139,9 @@ public UUID getWorldId(MapView map) {
11651139
}
11661140
return null;
11671141
}
1142+
1143+
private static class EmptyRenderer extends MapRenderer {
1144+
@Override
1145+
public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Player player) {}
1146+
}
11681147
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public PlayerFreezeListener(SyncInv plugin) {
4646

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

5555
@EventHandler(ignoreCancelled = true)
5656
public void onPlayerMove(PlayerMoveEvent e) {
57-
if(!sameBlock(e.getFrom(), e.getTo()) && plugin.isLocked(e.getPlayer().getUniqueId())) {
57+
if (!sameBlock(e.getFrom(), e.getTo()) && plugin.isLocked(e.getPlayer().getUniqueId())) {
5858
e.setCancelled(true);
5959
if (e.getFrom().getBlockY() == e.getTo().getBlockY()) {
6060
e.getPlayer().sendMessage(plugin.getLang("cant-move"));
@@ -70,86 +70,86 @@ private boolean sameBlock(Location from, Location to) {
7070

7171
@EventHandler(ignoreCancelled = true)
7272
public void onPlayerPickupItem(PlayerPickupItemEvent e) {
73-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
73+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
7474
e.setCancelled(true);
7575
e.getPlayer().sendMessage(plugin.getLang("cant-pickup-items"));
7676
}
7777
}
7878

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

8787
@EventHandler(ignoreCancelled = true)
8888
public void onPlayerDamage(EntityDamageEvent e) {
89-
if(e.getEntity() instanceof Player && plugin.isLocked(e.getEntity().getUniqueId())) {
89+
if (e.getEntity() instanceof Player && plugin.isLocked(e.getEntity().getUniqueId())) {
9090
e.setCancelled(true);
9191
}
9292
}
9393

9494
@EventHandler(ignoreCancelled = true)
9595
public void onInventoryInteraction(InventoryClickEvent e) {
96-
if(plugin.isLocked(e.getWhoClicked().getUniqueId())) {
96+
if (plugin.isLocked(e.getWhoClicked().getUniqueId())) {
9797
e.setCancelled(true);
9898
e.getWhoClicked().sendMessage(plugin.getLang("wait-for-loading"));
9999
}
100100
}
101101

102102
@EventHandler(ignoreCancelled = true)
103103
public void onInventoryInteraction(InventoryDragEvent 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(InventoryOpenEvent e) {
112-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
112+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
113113
e.setCancelled(true);
114114
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
115115
}
116116
}
117117

118118
@EventHandler(ignoreCancelled = true)
119119
public void onInteraction(PlayerInteractEvent 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 onDamageEntity(EntityDamageByEntityEvent e) {
128-
if(e.getDamager() instanceof Player && plugin.isLocked(e.getDamager().getUniqueId())) {
128+
if (e.getDamager() instanceof Player && plugin.isLocked(e.getDamager().getUniqueId())) {
129129
e.setCancelled(true);
130130
e.getDamager().sendMessage(plugin.getLang("wait-for-loading"));
131131
}
132132
}
133133

134134
@EventHandler(ignoreCancelled = true)
135135
public void onBlockDamage(BlockDamageEvent e) {
136-
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
136+
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
137137
e.setCancelled(true);
138138
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
139139
}
140140
}
141141

142142
@EventHandler(ignoreCancelled = true)
143143
public void onChat(AsyncPlayerChatEvent 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(PlayerCommandPreprocessEvent 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
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.bukkit.event.player.PlayerQuitEvent;
1111

1212
import java.util.Set;
13-
import java.util.UUID;
1413

1514
/*
1615
* SyncInv
@@ -44,7 +43,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
4443
}
4544
PlayerDataQuery query = plugin.getMessenger().removeQuery(event.getPlayer().getUniqueId());
4645
if (query != null) {
47-
// The player is gone although he had a query...
46+
// The player is gone, although he had a query...
4847
// We have to make sure now that the time of the data file matches the old one
4948
// and not send our data to all the other servers as it might be outdated
5049
plugin.runLater(() -> {
@@ -68,5 +67,4 @@ public void onPlayerQuit(PlayerQuitEvent event) {
6867
// Update last seen
6968
plugin.runLater(() -> plugin.setLastSeen(event.getPlayer().getUniqueId(), System.currentTimeMillis()), 1);
7069
}
71-
7270
}

0 commit comments

Comments
 (0)