Skip to content

Commit 553fb1b

Browse files
committed
Merge pull request #33 from yoadsn/modify_stores_use_uuid
Modify stores use uuid
2 parents 013f814 + 8c8807f commit 553fb1b

16 files changed

+197
-143
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This is quite a stable version, any issues or feature requests are welcome!
1515
- Allows definition of player groups, and sending chat messages to these groups
1616
- Alloes listening to all chats taking place on the server
1717
- Logs chat messages to files and/or [LogEntries](logentries.com)
18+
- Configuration store files use the player UUID since version 1.3
1819

1920
##Installation
2021
- Drop the JAR in the plugins folder
@@ -84,24 +85,24 @@ So for example, a "French" configured player might still send some "English" tex
8485

8586
##Language Configuration
8687
Language settings per player are stored in the file **langStore.json** in the plugin's data folder.
87-
This file contains a single JSON object. The keys of this object are the player name and the values are the language.
88-
Configuration of the language is stored in the file when the plugin is disabled and read then the plugin is enabled.
88+
This file contains a single JSON object. The keys of this object are the player UUID and the values are the language.
89+
Configuration of the language is stored in the file when the plugin is disabled and read when the plugin is enabled.
8990
It is possible to edit the configuration with a normal text editor but please note:
9091
- The file must contain a single valid JSON object in the format described above.
9192
- A restart of the plugin is required to read configuration which was edited directly in the file.
9293

9394
Example of a valid language configuration JSON object:
9495
```JSON
9596
{
96-
"playerName1": "arabic",
97-
"playerName2": "hebrew"
97+
"player1UUID": "arabic",
98+
"player2UUID": "hebrew"
9899
}
99100
```
100101

101102
##Groups Configuration
102-
A Player can belong to a group (or more then one).
103+
A Player can belong to zero or more groups.
103104
To define the groups create a file called **groupsStore.json** in the plugin's data folder.
104-
The file contains a single JSON object. The keys of this object are the group name and the values are the group member players.
105+
The file contains a single JSON object. The keys of this object are the group name and the values are the group member players' UUID's.
105106
Each value is a JSON Array with string values for each player in the group.
106107

107108
- The file must contain a single valid JSON object in the format described above.
@@ -113,8 +114,8 @@ Example of a valid groups configuration JSON object:
113114
```JSON
114115
{
115116
"blueGroup" : [
116-
"playerName1",
117-
"playerName2"
117+
"player1UUID",
118+
"player2UUID"
118119
]
119120
}
120121
```
@@ -146,6 +147,9 @@ The config.yml file can configure the following for chat logging:
146147

147148
##Latest Changes
148149

150+
Since 1.3:
151+
- Groups and Languages store files use UUID. Old configuration file are not compatible and needs to be recreated. (No migration tool is available)
152+
149153
Since 1.2:
150154
- Added chat logging support using a file logger and/or logentries logger.
151155
- Added a configuration key for the whispering radius

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.gamesforpeace</groupId>
66
<artifactId>TonguesPlugin</artifactId>
7-
<version>1.2</version>
7+
<version>1.3</version>
88
<packaging>jar</packaging>
99

1010
<name>TonguesPlugin</name>
@@ -30,7 +30,7 @@
3030
<dependency>
3131
<groupId>org.bukkit</groupId>
3232
<artifactId>bukkit</artifactId>
33-
<version>1.7.2-R0.2</version>
33+
<version>1.7.9-R0.2</version>
3434
</dependency>
3535
<dependency>
3636
<groupId>com.memetix</groupId>

src/main/java/org/gamesforpeace/tongues/ChatDestinationResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.gamesforpeace.tongues;
22

33
import java.util.Set;
4+
import java.util.UUID;
45

56
import org.bukkit.entity.Player;
67

78
public interface ChatDestinationResolver {
89

9-
Player getOnlinePlayer(String string);
10+
Player getOnlinePlayer(UUID id);
11+
12+
Player getOnlinePlayer(String name);
1013

1114
Set<Player> getAllOnlinePlayers();
1215

src/main/java/org/gamesforpeace/tongues/ConcurrentPlayerLanguageStore.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@
1111
public class ConcurrentPlayerLanguageStore implements PlayerLanguageStore {
1212

1313
private final HashSet<String> supportedLanguages;
14-
private final ConcurrentHashMap<String, String> languageStore;
14+
private final ConcurrentHashMap<UUID, String> languageStore;
1515
private final String defaultLanguage;
1616

1717
public ConcurrentPlayerLanguageStore(Set<String> supportedLnaguages, String defaultLanguage) {
1818
this.supportedLanguages = new HashSet<String>(supportedLnaguages);
19-
languageStore = new ConcurrentHashMap<String, String>();
19+
languageStore = new ConcurrentHashMap<UUID, String>();
2020
this.defaultLanguage = defaultLanguage;
2121
}
2222

23-
public String getLanguageForPlayer(String playerName) {
23+
public String getLanguageForPlayer(UUID playerId) {
2424

25-
String playerStoredLang = languageStore.get(playerName);
25+
String playerStoredLang = languageStore.get(playerId);
2626
if (playerStoredLang == null) {
2727
playerStoredLang = defaultLanguage;
28-
languageStore.put(playerName, defaultLanguage);
28+
languageStore.put(playerId, defaultLanguage);
2929
}
3030

3131
return playerStoredLang;
3232
}
3333

34-
public void setLanguageForPlayer(String playerName, String language) {
35-
languageStore.put(playerName, language);
34+
public void setLanguageForPlayer(UUID playerId, String language) {
35+
languageStore.put(playerId, language);
3636
}
3737

38-
public void clearLanguageForPlayer(String playerName) {
39-
languageStore.remove(playerName);
38+
public void clearLanguageForPlayer(UUID playerId) {
39+
languageStore.remove(playerId);
4040
}
4141

4242
public Boolean isLanguageSupported(String language) {
@@ -47,12 +47,12 @@ public String getDefaultLanguage() {
4747
return defaultLanguage;
4848
}
4949

50-
public Map<String, String> getAllPlayerLanguages() {
50+
public Map<UUID, String> getAllPlayerLanguages() {
5151
return Collections.unmodifiableMap(languageStore);
5252
}
5353

54-
public void setPlayerLanguages(Map<String, String> playerLanguagesToSet) {
55-
for (Entry<String, String> languagePlayerSetting : playerLanguagesToSet.entrySet()) {
54+
public void setPlayerLanguages(Map<UUID, String> playerLanguagesToSet) {
55+
for (Entry<UUID, String> languagePlayerSetting : playerLanguagesToSet.entrySet()) {
5656
setLanguageForPlayer(languagePlayerSetting.getKey(), languagePlayerSetting.getValue());
5757
}
5858
}

src/main/java/org/gamesforpeace/tongues/PlayerLanguageStore.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55

66
public interface PlayerLanguageStore {
77

8-
String getLanguageForPlayer(String playerName);
8+
String getLanguageForPlayer(UUID playerId);
99

1010
/**
1111
* This method does not check to see if the language is supported or not.
1212
* It is the callers responsibility to consult "IsLanguageSupported" if required.
13-
* @param playerName
13+
* @param playerId
1414
* @param language - assumed to always be a lowercase langauge name
1515
* @return
1616
*/
17-
void setLanguageForPlayer(String playerName, String language);
17+
void setLanguageForPlayer(UUID playerId, String language);
1818

1919
/**
2020
* This method clears the setup language of a player
21-
* @param playerName
21+
* @param playerId
2222
* @return
2323
*/
24-
void clearLanguageForPlayer(String playerName);
24+
void clearLanguageForPlayer(UUID playerId);
2525

2626
/**
2727
* Checks if this store supports the language
@@ -40,11 +40,11 @@ public interface PlayerLanguageStore {
4040
* Gets all non-default player languages stored
4141
* @return All stored non-default player langauges
4242
*/
43-
Map<String, String> getAllPlayerLanguages();
43+
Map<UUID, String> getAllPlayerLanguages();
4444

4545
/**
4646
* Would use the set of values to override or add to the existing configuration
4747
* @param playerLanguagesToSet - Assumed to contain valid languages
4848
*/
49-
void setPlayerLanguages(Map<String, String> playerLanguagesToSet);
49+
void setPlayerLanguages(Map<UUID, String> playerLanguagesToSet);
5050
}

src/main/java/org/gamesforpeace/tongues/SetLangCommandExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
7676
// We have a player to operate on
7777
if (subjectOfCommand != null) {
7878

79-
String currLang = langStore.getLanguageForPlayer(subjectOfCommand.getName());
79+
String currLang = langStore.getLanguageForPlayer(subjectOfCommand.getUniqueId());
8080

8181
if (changeToLang.equalsIgnoreCase(QUERY_SETUP_LANG)) {
8282
if (currLang.equalsIgnoreCase(langStore.getDefaultLanguage())) {
@@ -93,7 +93,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
9393
responseMessage = String.format(MSG_LANG_IS_ALREADY_SETUP_FTM, changeToLang);
9494

9595
} else if (changeToLang.equalsIgnoreCase(CLEAR_SETUP_LANG)) {
96-
langStore.clearLanguageForPlayer(subjectOfCommand.getName());
96+
langStore.clearLanguageForPlayer(subjectOfCommand.getUniqueId());
9797
responseMessage = MSG_CLEARED_SETUP_LANG;
9898

9999
success = true;
@@ -103,7 +103,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
103103
responseMessage = String.format(ERR_LANG_NOT_SUPPORTED_FMT, changeToLang);
104104

105105
} else {
106-
langStore.setLanguageForPlayer(subjectOfCommand.getName(), changeToLang);
106+
langStore.setLanguageForPlayer(subjectOfCommand.getUniqueId(), changeToLang);
107107
responseMessage = String.format(MSG_PLAYER_LANG_CHANGED_FMT, subjectOfCommand.getName(),
108108
changeToLang);
109109

src/main/java/org/gamesforpeace/tongues/TonguesPlugin.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.HashSet;
77
import java.util.Set;
8+
import java.util.UUID;
89
import java.util.concurrent.CopyOnWriteArraySet;
910

1011
import org.bukkit.Bukkit;
@@ -32,7 +33,7 @@ public final class TonguesPlugin extends JavaPlugin implements ChatMessenger, Tr
3233
private ListenCommandExecutor listenCommandExecutor;
3334
private PlayerLanguageStore langStore;
3435
private ChatLogger chatLogger = null;
35-
private HashMap<String, HashSet<String>> groupsStore;
36+
private HashMap<String, HashSet<UUID>> groupsStore;
3637

3738
@Override
3839
public void onEnable(){
@@ -102,10 +103,10 @@ private void LoadPlayerLanguages() {
102103
try {
103104
PlayerLanguageStorePersister langStorePersister = new PlayerLanguageStorePersister(getDataFolder(), LANG_STORE_FILENAME, getLogger());
104105
if (!langStorePersister.load(langStore)) {
105-
getLogger().warning("Could not load player languages configuration file upon plugin enable.");
106+
getLogger().warning("Could not load player languages configuration file upon plugin enable. Did you migrate to UUIDs? Skipping.");
106107
}
107108
} catch (IOException e) {
108-
getLogger().warning("Unable to access player languages persistence store for loading. Skipping.");
109+
getLogger().warning("Unable to access player languages persistence store for loading. Did you migrate to UUIDs? Skipping.");
109110
}
110111
}
111112

@@ -117,8 +118,8 @@ private void LoadGroups() {
117118
}
118119

119120
if (groupsStore == null) {
120-
groupsStore = new HashMap<String, HashSet<String>>();
121-
getLogger().warning("Unable to access player groups persistence store for loading. Skipping.");
121+
groupsStore = new HashMap<String, HashSet<UUID>>();
122+
getLogger().warning("Unable to access player groups persistence store for loading. Did you migrate to UUIDs? Skipping.");
122123
}
123124
}
124125

@@ -142,7 +143,7 @@ public void sendTranslatedMessageAsync(String message, Player source, Player des
142143
final Player sendFrom = source;
143144
final Player sendTo = dest;
144145
final String sendMessage = message;
145-
final String destinationLanguage = langStore.getLanguageForPlayer(sendTo.getName());
146+
final String destinationLanguage = langStore.getLanguageForPlayer(sendTo.getUniqueId());
146147

147148
new BukkitRunnable() {
148149

@@ -209,23 +210,27 @@ public void run() {
209210
public void postCommand(CommandSender sender, String commandLine) {
210211
Bukkit.dispatchCommand(sender, commandLine);
211212
}
212-
213+
213214
public Player getOnlinePlayer(String name) {
214215
return getServer().getPlayer(name);
215216
}
216217

218+
public Player getOnlinePlayer(UUID id) {
219+
return getServer().getPlayer(id);
220+
}
221+
217222
public Set<Player> getAllOnlinePlayers() {
218223
return new CopyOnWriteArraySet<Player>(Arrays.asList(getServer().getOnlinePlayers()));
219224
}
220225

221226
public Set<Player> getGroupPlayers(String groupName) {
222227
if (groupsStore.containsKey(groupName)) {
223228

224-
HashSet<String> groupPlayerNames = groupsStore.get(groupName);
229+
HashSet<UUID> groupPlayerIds = groupsStore.get(groupName);
225230

226-
Set<Player> groupPlayers = new HashSet<Player>(groupPlayerNames.size());
227-
for (String name : groupPlayerNames) {
228-
Player player = getOnlinePlayer(name);
231+
Set<Player> groupPlayers = new HashSet<Player>(groupPlayerIds.size());
232+
for (UUID id : groupPlayerIds) {
233+
Player player = getOnlinePlayer(id);
229234
if (player != null) {
230235
groupPlayers.add(player);
231236
}

src/main/java/org/gamesforpeace/tongues/persistence/PlayerGroupsPersister.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import java.lang.reflect.Type;
1212
import java.util.HashMap;
1313
import java.util.HashSet;
14+
import java.util.UUID;
1415
import java.util.logging.Logger;
1516

1617
import org.apache.commons.lang.Validate;
1718

1819
import com.google.common.base.Charsets;
20+
import com.google.common.io.Files;
1921
import com.google.gson.Gson;
2022
import com.google.gson.GsonBuilder;
2123
import com.google.gson.reflect.TypeToken;
@@ -49,7 +51,7 @@ private File getDataFile() {
4951
return dataFile;
5052
}
5153

52-
public boolean persist(HashMap<String, HashSet<String>> playerGroups) {
54+
public boolean persist(HashMap<String, HashSet<UUID>> playerGroups) {
5355
File outputDataFile = getDataFile();
5456
if (outputDataFile != null) {
5557

@@ -59,7 +61,7 @@ public boolean persist(HashMap<String, HashSet<String>> playerGroups) {
5961
BufferedWriter writer = new BufferedWriter(osw);
6062

6163
try{
62-
Type storedType = new TypeToken<HashMap<String, HashSet<String>>>() { }.getType();
64+
Type storedType = new TypeToken<HashMap<String, HashSet<UUID>>>() { }.getType();
6365
writer.write(gson.toJson(playerGroups, storedType));
6466
} finally {
6567
writer.close();
@@ -79,16 +81,16 @@ public boolean persist(HashMap<String, HashSet<String>> playerGroups) {
7981
return false;
8082
}
8183

82-
public HashMap<String, HashSet<String>> load() {
84+
public HashMap<String, HashSet<UUID>> load() {
8385
File inputDataFile = getDataFile();
8486
if (inputDataFile != null) {
8587

8688
try {
8789
InputStreamReader isr = new InputStreamReader( new FileInputStream(inputDataFile), Charsets.UTF_8);
8890
JsonReader reader = new JsonReader(isr);
8991

90-
Type typeOfHashMap = new TypeToken<HashMap<String, HashSet<String>>>() { }.getType();
91-
HashMap<String, HashSet<String>> playerGroups = gson.fromJson(reader, typeOfHashMap);
92+
Type typeOfHashMap = new TypeToken<HashMap<String, HashSet<UUID>>>() { }.getType();
93+
HashMap<String, HashSet<UUID>> playerGroups = gson.fromJson(reader, typeOfHashMap);
9294

9395
reader.close();
9496

@@ -102,6 +104,16 @@ public HashMap<String, HashSet<String>> load() {
102104
}
103105
}
104106

107+
if (inputDataFile.exists()) {
108+
logger.info("Possible format error. Copying old groups store file aside before it is overridden with a new empty store file in the correct format.");
109+
File badFormatFile = new File(datafolder, "bad_format_" + storageFileName);
110+
try {
111+
Files.copy(inputDataFile, badFormatFile);
112+
} catch (IOException e) {
113+
logger.warning(e.toString());
114+
}
115+
}
116+
105117
return null;
106118
}
107119
}

0 commit comments

Comments
 (0)