Switch to thread-safe DTOs

This commit is contained in:
Andrzej Pomirski 2015-07-05 21:56:42 +02:00
parent 118f72e805
commit 7bc452e203
3 changed files with 57 additions and 3 deletions

View File

@ -2,8 +2,10 @@ package com.Acrobot.ChestShop.Listeners.Player;
import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.UUIDs.PlayerDTO;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
@ -11,12 +13,15 @@ import org.bukkit.event.player.PlayerJoinEvent;
* @author Acrobot * @author Acrobot
*/ */
public class PlayerConnect implements Listener { public class PlayerConnect implements Listener {
@EventHandler
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onPlayerConnect(final PlayerJoinEvent event) { public static void onPlayerConnect(final PlayerJoinEvent event) {
final PlayerDTO playerDTO = new PlayerDTO(event.getPlayer());
Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {
@Override @Override
public void run() { public void run() {
NameManager.storeUsername(event.getPlayer()); NameManager.storeUsername(playerDTO);
} }
}); });
} }

View File

@ -160,7 +160,7 @@ public class NameManager {
return name; return name;
} }
public static void storeUsername(final Player player) { public static void storeUsername(final PlayerDTO player) {
final UUID uuid = player.getUniqueId(); final UUID uuid = player.getUniqueId();
Account account = null; Account account = null;
@ -173,6 +173,13 @@ public class NameManager {
} }
if (account != null) { if (account != null) {
if (account.getName() != null && account.getShortName() == null) {
String shortenedName = NameUtil.stripUsername(account.getName());
account.setShortName(shortenedName);
}
account.setUuid(uuid); //HOW IS IT EVEN POSSIBLE THAT UUID IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?!
account.setLastSeenName(player.getName()); account.setLastSeenName(player.getName());
try { try {

View File

@ -0,0 +1,42 @@
package com.Acrobot.ChestShop.UUIDs;
import org.bukkit.entity.Player;
import java.util.UUID;
/**
* Data Transfer Object for Player objects
*
* Since Bukkit API is not thread-safe, this should work
* @author Andrzej Pomirski
*/
public class PlayerDTO {
private UUID uniqueId;
private String name;
public PlayerDTO(UUID uuid, String name) {
this.uniqueId = uuid;
this.name = name;
}
public PlayerDTO(Player player) {
this.uniqueId = player.getUniqueId();
this.name = player.getName();
}
public UUID getUniqueId() {
return uniqueId;
}
public void setUniqueId(UUID uniqueId) {
this.uniqueId = uniqueId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}