Skip to content

Commit 603bf89

Browse files
committed
[Code Cleanup/Refactor] Remove declaration code./Move some Variables over to the up and coming Player UUID.
1 parent 9689d78 commit 603bf89

10 files changed

Lines changed: 58 additions & 55 deletions

File tree

src/main/java/ca/q0r/madvanced/MAdvanced.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323

2424
import java.util.Date;
2525
import java.util.HashMap;
26+
import java.util.UUID;
2627

2728
public class MAdvanced extends JavaPlugin {
2829
// Default Plugin Data
2930
private PluginManager pm;
3031
private PluginDescriptionFile pdfFile;
3132

3233
// Maps
33-
public HashMap<String, Boolean> isChatting = new HashMap<String, Boolean>();
34-
public HashMap<String, Boolean> isAFK = new HashMap<String, Boolean>();
34+
public HashMap<UUID, Boolean> isChatting = new HashMap<>();
35+
public HashMap<UUID, Boolean> isAFK = new HashMap<>();
3536

36-
public HashMap<String, Location> AFKLoc = new HashMap<String, Location>();
37+
public HashMap<UUID, Location> AFKLoc = new HashMap<>();
3738

38-
public HashMap<String, Long> lastMove = new HashMap<String, Long>();
39+
public HashMap<UUID, Long> lastMove = new HashMap<>();
3940

4041

4142
public void onEnable() {
@@ -72,9 +73,9 @@ public void run() {
7273
setupCommands();
7374

7475
for (Player players : getServer().getOnlinePlayers()) {
75-
isChatting.put(players.getName(), false);
76-
isAFK.put(players.getName(), false);
77-
lastMove.put(players.getName(), new Date().getTime());
76+
isChatting.put(players.getUniqueId(), false);
77+
isAFK.put(players.getUniqueId(), false);
78+
lastMove.put(players.getUniqueId(), new Date().getTime());
7879
}
7980

8081
// Stop the Timer
@@ -127,19 +128,19 @@ public void run() {
127128
}
128129

129130
for (Player player : getServer().getOnlinePlayers()) {
130-
if (isAFK.get(player.getName()) == null) {
131-
isAFK.put(player.getName(), false);
131+
if (isAFK.get(player.getUniqueId()) == null) {
132+
isAFK.put(player.getUniqueId(), false);
132133
}
133134

134-
if (isAFK.get(player.getName()) || lastMove.get(player.getName()) == null
135+
if (isAFK.get(player.getUniqueId()) || lastMove.get(player.getUniqueId()) == null
135136
|| API.checkPermissions(player.getName(), player.getWorld().getName(), "mchat.bypass.afk")) {
136137
continue;
137138
}
138139

139-
if (new Date().getTime() - (ConfigType.OPTION_AFK_TIMER.getInteger() * 1000) > lastMove.get(player.getName())) {
140+
if (new Date().getTime() - (ConfigType.OPTION_AFK_TIMER.getInteger() * 1000) > lastMove.get(player.getUniqueId())) {
140141
getServer().dispatchCommand(getServer().getConsoleSender(), "mchatafkother " + player.getName() + " " + LocaleType.MESSAGE_AFK_DEFAULT.getVal());
141142
} else {
142-
isAFK.put(player.getName(), false);
143+
isAFK.put(player.getUniqueId(), false);
143144
}
144145
}
145146
}
@@ -156,11 +157,11 @@ public void run() {
156157
continue;
157158
}
158159

159-
if (!isAFK.get(player.getName())) {
160+
if (!isAFK.get(player.getUniqueId())) {
160161
continue;
161162
}
162163

163-
if (new Date().getTime() - (ConfigType.OPTION_AFK_KICK_TIMER.getInteger() * 1000) > lastMove.get(player.getName())) {
164+
if (new Date().getTime() - (ConfigType.OPTION_AFK_KICK_TIMER.getInteger() * 1000) > lastMove.get(player.getUniqueId())) {
164165
player.kickPlayer(LocaleType.MESSAGE_AFK_DEFAULT.getVal());
165166
}
166167
}

src/main/java/ca/q0r/madvanced/commands/AFKCommand.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.bukkit.command.CommandSender;
1010
import org.bukkit.entity.Player;
1111

12+
import java.util.UUID;
13+
1214
public class AFKCommand implements CommandExecutor {
1315
private MAdvanced plugin;
1416

@@ -41,7 +43,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
4143

4244
Player player = (Player) sender;
4345

44-
if (isAfk(player.getName())) {
46+
if (isAfk(player.getUniqueId())) {
4547
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "mchatafkother " + player.getName());
4648
return true;
4749
}
@@ -50,7 +52,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
5052
return true;
5153
}
5254

53-
Boolean isAfk(String player) {
54-
return plugin.isAFK.get(player) != null && plugin.isAFK.get(player);
55+
Boolean isAfk(UUID uuid) {
56+
return plugin.isAFK.get(uuid) != null && plugin.isAFK.get(uuid);
5557
}
5658
}

src/main/java/ca/q0r/madvanced/commands/AFKOtherCommand.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
3434
return true;
3535
}
3636

37-
Boolean isAfk = plugin.isAFK.get(afkTarget.getName()) != null &&
38-
plugin.isAFK.get(afkTarget.getName());
37+
Boolean isAfk = plugin.isAFK.get(afkTarget.getUniqueId()) != null &&
38+
plugin.isAFK.get(afkTarget.getUniqueId());
3939

4040
String notification = LocaleType.MESSAGE_PLAYER_NOT_AFK.getVal();
4141

4242
String message = "";
4343

44-
String title = Parser.parsePlayerName(afkTarget.getName(), afkTarget.getWorld().getName());
45-
46-
4744
if (!isAfk) {
4845
if (args.length > 1) {
4946
for (int i = 1; i < args.length; ++i) {
@@ -58,7 +55,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
5855
notification = LocaleType.MESSAGE_PLAYER_AFK.getVal();
5956
}
6057

61-
TreeMap<String, String> rMap = new TreeMap<String, String>();
58+
TreeMap<String, String> rMap = new TreeMap<>();
6259

6360
rMap.put("player", Parser.parsePlayerName(afkTarget.getName(), afkTarget.getWorld().getName()));
6461
rMap.put("reason", message);
@@ -67,12 +64,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
6764
plugin.getServer().broadcastMessage(API.replace(notification, rMap, IndicatorType.LOCALE_VAR));
6865

6966
afkTarget.setSleepingIgnored(!isAfk);
70-
plugin.isAFK.put(afkTarget.getName(), !isAfk);
67+
plugin.isAFK.put(afkTarget.getUniqueId(), !isAfk);
7168

7269
String pLName = Parser.parseTabbedList(afkTarget.getName(), afkTarget.getWorld().getName());
7370

7471
if (!isAfk) {
75-
plugin.AFKLoc.put(afkTarget.getName(), afkTarget.getLocation());
72+
plugin.AFKLoc.put(afkTarget.getUniqueId(), afkTarget.getLocation());
7673

7774
pLName = MessageUtil.addColour("<gold>[" + LocaleType.MESSAGE_AFK_AFK.getVal() + "] ") + pLName;
7875
}

src/main/java/ca/q0r/madvanced/commands/ListCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
3232
return true;
3333
}
3434

35-
TreeMap<String, String> rMap = new TreeMap<String, String>();
35+
TreeMap<String, String> rMap = new TreeMap<>();
3636

3737
rMap.put("max", String.valueOf(plugin.getServer().getMaxPlayers()));
3838
rMap.put("players", String.valueOf(plugin.getServer().getOnlinePlayers().length));
3939

4040
sender.sendMessage(MessageUtil.addColour(API.replace(LocaleType.MESSAGE_LIST_HEADER.getVal(), rMap, IndicatorType.LOCALE_VAR)));
4141

42-
HashMap<String, Integer> cLMap = new HashMap<String, Integer>();
42+
HashMap<String, Integer> cLMap = new HashMap<>();
4343

4444
String msg = "";
4545
String line = "";
@@ -83,7 +83,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
8383
continue;
8484
}
8585

86-
if (plugin.isAFK.get(players.getName()) != null && plugin.isAFK.get(players.getName())) {
86+
if (plugin.isAFK.get(players.getUniqueId()) != null && plugin.isAFK.get(players.getUniqueId())) {
8787
if (msg.contains(iVar + ": &f")) {
8888
msg = msg.replace(iVar + ": &f", iVar + ": &f&4[" + LocaleType.MESSAGE_AFK_AFK.getVal() + "]" + mName + "&f, &f");
8989
} else {

src/main/java/ca/q0r/madvanced/commands/WhoCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void formatWho(CommandSender sender, Player recipient) {
4747
Integer exp = recipient.getLevel();
4848

4949
MessageUtil.sendColouredMessage(sender, "Player Name: " + recipient.getName());
50+
MessageUtil.sendColouredMessage(sender, "Player UUID: " + recipient.getUniqueId());
5051
MessageUtil.sendColouredMessage(sender, "Display Name: " + recipient.getDisplayName());
5152
MessageUtil.sendColouredMessage(sender, "Formatted Name: " + recipientName);
5253
MessageUtil.sendColouredMessage(sender, "Player's Location: [ " + loc + " ]");

src/main/java/ca/q0r/madvanced/events/ChatListener.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.event.player.AsyncPlayerChatEvent;
99

1010
import java.util.Date;
11+
import java.util.UUID;
1112

1213
public class ChatListener implements Listener {
1314
private MAdvanced plugin;
@@ -23,20 +24,20 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
2324
}
2425

2526
Player player = event.getPlayer();
26-
String pName = player.getName();
27+
UUID uuid = player.getUniqueId();
2728

2829
String msg = event.getMessage();
2930

3031
if (msg == null) {
3132
return;
3233
}
3334

34-
if (plugin.isAFK.get(pName) != null) {
35-
if (plugin.isAFK.get(pName)) {
35+
if (plugin.isAFK.get(uuid) != null) {
36+
if (plugin.isAFK.get(uuid)) {
3637
player.performCommand("mchatafk");
3738
}
3839
}
3940

40-
plugin.lastMove.put(pName, new Date().getTime());
41+
plugin.lastMove.put(uuid, new Date().getTime());
4142
}
4243
}

src/main/java/ca/q0r/madvanced/events/EntityListener.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public void onEntityDamage(EntityDamageEvent event) {
3030
if (attacker instanceof Player) {
3131
Player player = (Player) attacker;
3232

33-
if (plugin.isAFK.get(player.getName()) == null) {
33+
if (plugin.isAFK.get(player.getUniqueId()) == null) {
3434
return;
3535
}
3636

37-
if (plugin.isAFK.get(player.getName()) && ConfigType.OPTION_HC_AFK.getBoolean()) {
37+
if (plugin.isAFK.get(player.getUniqueId()) && ConfigType.OPTION_HC_AFK.getBoolean()) {
3838
damaged.setLastDamageCause(null);
3939
event.setCancelled(true);
4040
}
@@ -44,8 +44,8 @@ public void onEntityDamage(EntityDamageEvent event) {
4444
if (event.getEntity() instanceof Player) {
4545
Player player = (Player) event.getEntity();
4646

47-
if (plugin.isAFK.get(player.getName()) != null) {
48-
if (plugin.isAFK.get(player.getName())) {
47+
if (plugin.isAFK.get(player.getUniqueId()) != null) {
48+
if (plugin.isAFK.get(player.getUniqueId())) {
4949
event.setCancelled(true);
5050
}
5151
}

src/main/java/ca/q0r/madvanced/events/PlayerListener.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.bukkit.event.player.PlayerMoveEvent;
1515

1616
import java.util.Date;
17+
import java.util.UUID;
1718

1819
public class PlayerListener implements Listener {
1920
MAdvanced plugin;
@@ -25,9 +26,9 @@ public PlayerListener(MAdvanced instance) {
2526
@EventHandler
2627
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
2728
Player player = event.getPlayer();
28-
String pName = player.getName();
29+
UUID uuid = player.getUniqueId();
2930

30-
plugin.lastMove.put(pName, new Date().getTime());
31+
plugin.lastMove.put(uuid, new Date().getTime());
3132

3233
for (String aliases : plugin.getCommand("mchatafk").getAliases()) {
3334
if (event.getMessage().contains("/" + aliases) ||
@@ -36,22 +37,22 @@ public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
3637
}
3738
}
3839

39-
if (plugin.isAFK.get(pName) == null) {
40+
if (plugin.isAFK.get(uuid) == null) {
4041
return;
4142
}
4243

43-
if (plugin.isAFK.get(pName)) {
44-
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "mchatafkother " + pName);
44+
if (plugin.isAFK.get(uuid)) {
45+
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "mchatafkother " + uuid);
4546
}
4647
}
4748

4849
@EventHandler(priority = EventPriority.LOWEST)
4950
public void onPlayerJoin(PlayerJoinEvent event) {
5051
Player player = event.getPlayer();
5152

52-
plugin.isChatting.put(player.getName(), false);
53-
plugin.isAFK.put(player.getName(), false);
54-
plugin.lastMove.put(player.getName(), new Date().getTime());
53+
plugin.isChatting.put(player.getUniqueId(), false);
54+
plugin.isAFK.put(player.getUniqueId(), false);
55+
plugin.lastMove.put(player.getUniqueId(), new Date().getTime());
5556
}
5657

5758
@EventHandler
@@ -81,16 +82,16 @@ public void onPlayerMove(PlayerMoveEvent event) {
8182

8283
Player player = event.getPlayer();
8384

84-
plugin.lastMove.put(player.getName(), new Date().getTime());
85+
plugin.lastMove.put(player.getUniqueId(), new Date().getTime());
8586

86-
if (plugin.isAFK.get(player.getName()) == null) {
87+
if (plugin.isAFK.get(player.getUniqueId()) == null) {
8788
return;
8889
}
8990

90-
if (plugin.isAFK.get(player.getName())) {
91+
if (plugin.isAFK.get(player.getUniqueId())) {
9192
if (ConfigType.OPTION_HC_AFK.getBoolean()) {
92-
if (plugin.AFKLoc.get(player.getName()) != null) {
93-
player.teleport(plugin.AFKLoc.get(player.getName()));
93+
if (plugin.AFKLoc.get(player.getUniqueId()) != null) {
94+
player.teleport(plugin.AFKLoc.get(player.getUniqueId()));
9495
}
9596

9697
MessageUtil.sendMessage(player, LocaleType.MESSAGE_PLAYER_STILL_AFK.getVal());

src/main/java/ca/q0r/madvanced/yml/config/ConfigType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Double getDouble() {
4040

4141
public List<String> getList() {
4242
List<String> list = YmlManager.getYml(YmlType.CONFIG_YML).getConfig().getStringList(option);
43-
ArrayList<String> l = new ArrayList<String>();
43+
ArrayList<String> l = new ArrayList<>();
4444

4545
for (String s : list) {
4646
l.add(MessageUtil.addColour(s));

src/main/java/ca/q0r/madvanced/yml/config/ConfigYml.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public class ConfigYml extends Yml {
1919
public ConfigYml() {
2020
super(new File("plugins/MAdvanced/config.yml"), "MAdvanced Config");
2121

22-
whoAliases = new ArrayList<String>();
23-
listAliases = new ArrayList<String>();
24-
afkAliases = new ArrayList<String>();
25-
afkOtherAliases = new ArrayList<String>();
22+
whoAliases = new ArrayList<>();
23+
listAliases = new ArrayList<>();
24+
afkAliases = new ArrayList<>();
25+
afkOtherAliases = new ArrayList<>();
2626

27-
aliasMap = new HashMap<String, List<String>>();
27+
aliasMap = new HashMap<>();
2828

2929
loadDefaults();
3030
}

0 commit comments

Comments
 (0)