From da16b662b4c652c2fbeb03d009184922db269181 Mon Sep 17 00:00:00 2001 From: gravitylow Date: Wed, 16 Apr 2014 21:10:12 -0400 Subject: [PATCH] Update faction storage to use UUID --- .../com/massivecraft/factions/FPlayer.java | 4 +- .../com/massivecraft/factions/Faction.java | 36 ++-- .../massivecraft/factions/cmd/CmdOwner.java | 6 +- .../zcore/persist/EntityCollection.java | 163 ++++++++++++++---- 4 files changed, 153 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/FPlayer.java b/src/main/java/com/massivecraft/factions/FPlayer.java index e862bd51..6c773d2e 100644 --- a/src/main/java/com/massivecraft/factions/FPlayer.java +++ b/src/main/java/com/massivecraft/factions/FPlayer.java @@ -222,7 +222,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator { Faction currentFaction = this.getFaction(); currentFaction.removeFPlayer(this); if (currentFaction.isNormal()) { - currentFaction.clearClaimOwnership(this.getId()); + currentFaction.clearClaimOwnership(this); } } @@ -302,7 +302,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator { } public String getName() { - if(getPlayer() != null) { + if(isOnline()) { return getPlayer().getName(); } /*OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(getId())); diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index a3c681c1..793c8eab 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -31,19 +31,22 @@ public class Faction extends Entity implements EconomyParticipator { private transient Set fplayers = new HashSet(); // FIELD: invites - // Where string is a lowercase player name private Set invites; + public Set getInvites() { + return invites; + } + public void invite(FPlayer fplayer) { - this.invites.add(fplayer.getName().toLowerCase()); + this.invites.add(fplayer.getId()); } public void deinvite(FPlayer fplayer) { - this.invites.remove(fplayer.getName().toLowerCase()); + this.invites.remove(fplayer.getId()); } public boolean isInvited(FPlayer fplayer) { - return this.invites.contains(fplayer.getName().toLowerCase()); + return this.invites.contains(fplayer.getId()); } // FIELD: open @@ -539,6 +542,10 @@ public class Faction extends Entity implements EconomyParticipator { // Ownership of specific claims //----------------------------------------------// + public Map> getClaimOwnership() { + return claimOwnership; + } + public void clearAllClaimOwnership() { claimOwnership.clear(); } @@ -547,13 +554,12 @@ public class Faction extends Entity implements EconomyParticipator { claimOwnership.remove(loc); } - public void clearClaimOwnership(String playerName) { - if (playerName == null || playerName.isEmpty()) { + public void clearClaimOwnership(FPlayer player) { + if (id == null || id.isEmpty()) { return; } Set ownerData; - String player = playerName.toLowerCase(); for (Entry> entry : claimOwnership.entrySet()) { ownerData = entry.getValue(); @@ -562,7 +568,7 @@ public class Faction extends Entity implements EconomyParticipator { Iterator iter = ownerData.iterator(); while (iter.hasNext()) { - if (iter.next().equals(player)) { + if (iter.next().equals(player.getId())) { iter.remove(); } } @@ -586,7 +592,7 @@ public class Faction extends Entity implements EconomyParticipator { return ownerData != null && !ownerData.isEmpty(); } - public boolean isPlayerInOwnerList(String playerName, FLocation loc) { + public boolean isPlayerInOwnerList(FPlayer player, FLocation loc) { if (claimOwnership.isEmpty()) { return false; } @@ -594,28 +600,28 @@ public class Faction extends Entity implements EconomyParticipator { if (ownerData == null) { return false; } - if (ownerData.contains(playerName.toLowerCase())) { + if (ownerData.contains(player.getId())) { return true; } return false; } - public void setPlayerAsOwner(String playerName, FLocation loc) { + public void setPlayerAsOwner(FPlayer player, FLocation loc) { Set ownerData = claimOwnership.get(loc); if (ownerData == null) { ownerData = new HashSet(); } - ownerData.add(playerName.toLowerCase()); + ownerData.add(player.getId()); claimOwnership.put(loc, ownerData); } - public void removePlayerAsOwner(String playerName, FLocation loc) { + public void removePlayerAsOwner(FPlayer player, FLocation loc) { Set ownerData = claimOwnership.get(loc); if (ownerData == null) { return; } - ownerData.remove(playerName.toLowerCase()); + ownerData.remove(player.getId()); claimOwnership.put(loc, ownerData); } @@ -664,7 +670,7 @@ public class Faction extends Entity implements EconomyParticipator { Set ownerData = claimOwnership.get(loc); // if no owner list, owner list is empty, or player is in owner list, they're allowed - if (ownerData == null || ownerData.isEmpty() || ownerData.contains(fplayer.getName().toLowerCase())) + if (ownerData == null || ownerData.isEmpty() || ownerData.contains(fplayer.getId())) return true; return false; diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdOwner.java b/src/main/java/com/massivecraft/factions/cmd/CmdOwner.java index 3e17c2c0..44133ccd 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdOwner.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdOwner.java @@ -79,8 +79,8 @@ public class CmdOwner extends FCommand { return; } - if (myFaction.isPlayerInOwnerList(playerName, flocation)) { - myFaction.removePlayerAsOwner(playerName, flocation); + if (myFaction.isPlayerInOwnerList(target, flocation)) { + myFaction.removePlayerAsOwner(target, flocation); fme.msg("You have removed ownership of this claimed land from %s.", playerName); return; } @@ -89,7 +89,7 @@ public class CmdOwner extends FCommand { if (!payForCommand(Conf.econCostOwner, "to set ownership of claimed land", "for setting ownership of claimed land")) return; - myFaction.setPlayerAsOwner(playerName, flocation); + myFaction.setPlayerAsOwner(target, flocation); fme.msg("You have added %s to the owner list for this claimed land.", playerName); } diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java index 77acc8e5..58e9b9bf 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -1,6 +1,8 @@ package com.massivecraft.factions.zcore.persist; +import com.massivecraft.factions.FLocation; import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Faction; import com.massivecraft.factions.zcore.util.DiscUtil; import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.factions.zcore.util.UUIDFetcher; @@ -225,24 +227,13 @@ public abstract class EntityCollection { Type type = this.getMapType(); if (type.toString().contains("FPlayer")) { Map data = this.gson.fromJson(content, type); - List invalidNames = new ArrayList(); - // Convert any leftover player names in this file - ArrayList list = new ArrayList(); - for (String value : data.keySet()) { - if (!value.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { - // Not a valid UUID.. - if (!value.matches("[a-zA-Z0-9_]{2,16}")) { - // Not even a valid player name.. go ahead and mark it for removal - invalidNames.add(value); - } else { - // We'll mark this as one for conversion to UUID - list.add(value); - } - } - } + Set list = whichKeysNeedMigration(data.keySet()); + Set invalidList = whichKeysAreInvalid(list); + list.removeAll(invalidList); + if (list.size() > 0) { // We've got some converting to do! - Bukkit.getLogger().log(Level.INFO, "Please wait while Factions converts " + list.size() + " old player names to UUID. This may take a while."); + Bukkit.getLogger().log(Level.INFO, "Factions is now updating players.json"); // First we'll make a backup, because god forbid anybody heed a warning File file = new File(this.file.getParentFile(), "players.json.old"); @@ -255,14 +246,15 @@ public abstract class EntityCollection { Bukkit.getLogger().log(Level.INFO, "Backed up your old data at " + file.getAbsolutePath()); // Start fetching those UUIDs - UUIDFetcher fetcher = new UUIDFetcher(list); + Bukkit.getLogger().log(Level.INFO, "Please wait while Factions converts " + list.size() + " old player names to UUID. This may take a while."); + UUIDFetcher fetcher = new UUIDFetcher(new ArrayList(list)); try { Map response = fetcher.call(); for (String s : list) { // Are we missing any responses? if (!response.containsKey(s)) { // They don't have a UUID so they should just be removed - invalidNames.add(s); + invalidList.add(s); } } for (String value : response.keySet()) { @@ -273,43 +265,142 @@ public abstract class EntityCollection { if (player == null) { // The player never existed here, and shouldn't persist - invalidNames.add(value); + invalidList.add(value); continue; } + player.setId(id); // Update the object so it knows + data.remove(value); // Out with the old... data.put(id, player); // And in with the new - player.setId(id); // Update the object so it knows } } catch (Exception e) { e.printStackTrace(); } - if (invalidNames.size() > 0) { - for (String name : invalidNames) { + if (invalidList.size() > 0) { + for (String name : invalidList) { // Remove all the invalid names we collected data.remove(name); } Bukkit.getLogger().log(Level.INFO, "While converting we found names that either don't have a UUID or aren't players and removed them from storage."); - Bukkit.getLogger().log(Level.INFO, "The following names were detected as being invalid: " + StringUtils.join(invalidNames, ", ")); + Bukkit.getLogger().log(Level.INFO, "The following names were detected as being invalid: " + StringUtils.join(invalidList, ", ")); } - saveToDisc(); // Update the flatfile - Bukkit.getLogger().log(Level.INFO, "Done converting to UUID."); + saveCore(this.file, (Map) data); // Update the flatfile + Bukkit.getLogger().log(Level.INFO, "Done converting players.json to UUID."); + } + return (Map) data; + } else { + Map data = this.gson.fromJson(content, type); + + // Do we have any names that need updating in claims or invites? + + int needsUpdate = 0; + for (String string : data.keySet()) { + Faction f = data.get(string); + needsUpdate += whichKeysNeedMigration(f.getInvites()).size(); + Map> claims = f.getClaimOwnership(); + for (FLocation key : f.getClaimOwnership().keySet()) { + needsUpdate += whichKeysNeedMigration(claims.get(key)).size(); + } + } + + if (needsUpdate > 0) { + // We've got some converting to do! + Bukkit.getLogger().log(Level.INFO, "Factions is now updating factions.json"); + + // First we'll make a backup, because god forbid anybody heed a warning + File file = new File(this.file.getParentFile(), "factions.json.old"); + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + saveCore(file, (Map) data); + Bukkit.getLogger().log(Level.INFO, "Backed up your old data at " + file.getAbsolutePath()); + + Bukkit.getLogger().log(Level.INFO, "Please wait while Factions converts " + needsUpdate + " old player names to UUID. This may take a while."); + + // Update claim ownership + + for (String string : data.keySet()) { + Faction f = data.get(string); + Map> claims = f.getClaimOwnership(); + for (FLocation key : claims.keySet()) { + Set set = claims.get(key); + + Set list = whichKeysNeedMigration(set); + + if (list.size() > 0) { + UUIDFetcher fetcher = new UUIDFetcher(new ArrayList(list)); + try { + Map response = fetcher.call(); + for (String value : response.keySet()) { + // Let's replace their old named entry with a UUID key + String id = response.get(value).toString(); + set.remove(value.toLowerCase()); // Out with the old... + set.add(id); // And in with the new + } + } catch (Exception e) { + e.printStackTrace(); + } + claims.put(key, set); // Update + } + } + } + + // Update invites + + for (String string : data.keySet()) { + Faction f = data.get(string); + Set invites = f.getInvites(); + Set list = whichKeysNeedMigration(invites); + + if (list.size() > 0) { + UUIDFetcher fetcher = new UUIDFetcher(new ArrayList(list)); + try { + Map response = fetcher.call(); + for (String value : response.keySet()) { + // Let's replace their old named entry with a UUID key + String id = response.get(value).toString(); + invites.remove(value.toLowerCase()); // Out with the old... + invites.add(id); // And in with the new + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + saveCore(this.file, (Map) data); // Update the flatfile + Bukkit.getLogger().log(Level.INFO, "Done converting factions.json to UUID."); } return (Map) data; } - try { - return this.gson.fromJson(content, type); - } catch (Exception ex) { - Bukkit.getLogger().log(Level.WARNING, "JSON error encountered loading \"" + file + "\": " + ex.getLocalizedMessage()); + } - // backup bad file, so user can attempt to recover something from it - File backup = new File(file.getPath() + "_bad"); - if (backup.exists()) backup.delete(); - Bukkit.getLogger().log(Level.WARNING, "Backing up copy of bad file to: " + backup); - file.renameTo(backup); - - return null; + private Set whichKeysNeedMigration(Set keys) { + HashSet list = new HashSet(); + for (String value : keys) { + if (!value.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { + // Not a valid UUID.. + if (value.matches("[a-zA-Z0-9_]{2,16}")) { + // Valid playername, we'll mark this as one for conversion to UUID + list.add(value); + } + } } + return list; + } + + private Set whichKeysAreInvalid(Set keys) { + Set list = new HashSet(); + for (String value : keys) { + if (!value.matches("[a-zA-Z0-9_]{2,16}")) { + // Not a valid player name.. go ahead and mark it for removal + list.add(value); + } + } + return list; } // -------------------------------------------- //