Skip to content

Commit 5492606

Browse files
committed
Add back support to save by name
Enable it in the config.yml.
1 parent cb8d6d1 commit 5492606

File tree

8 files changed

+152
-22
lines changed

8 files changed

+152
-22
lines changed

src/main/java/nl/rutgerkok/betterenderchest/BetterEnderChest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ public interface BetterEnderChest {
309309
*/
310310
void severe(String message, Throwable thrown);
311311

312+
/**
313+
* Gets whether UUIDs are used to save chests. If false, the chests will be
314+
* saved using player names.
315+
*
316+
* @return Whether UUIDs are used to save chests.
317+
*/
318+
boolean useUuidsForSaving();
319+
312320
/**
313321
* Logs a warning. Message will be prefixed with the plugin name between
314322
* square brackets.

src/main/java/nl/rutgerkok/betterenderchest/BetterEnderChestPlugin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public static class PublicChest {
8484
private Registry<ProtectionBridge> protectionBridges = new Registry<ProtectionBridge>();
8585
private int rankUpgrades;
8686
private SaveAndLoadError saveAndLoadError;
87+
private boolean useUuids;
8788
private BetterEnderUUIDConverter uuidConverter;
8889

8990
@Override
@@ -272,6 +273,10 @@ public void initConfig() {
272273
}
273274
config.set("BetterEnderChest.language", language);
274275

276+
// UUIDs
277+
useUuids = config.getBoolean("BetterEnderChest.useUUIDs", true);
278+
config.set("BetterEnderChest.useUUIDs", useUuids);
279+
275280
// Save location
276281
String defaultSaveLocation = SaveLocation.getDefaultSaveLocation().toString();
277282
String givenSaveLocation = config.getString("BetterEnderChest.saveFolderLocation", defaultSaveLocation);
@@ -613,6 +618,11 @@ private void unloadIOServices() {
613618
uuidConverter = null;
614619
}
615620

621+
@Override
622+
public boolean useUuidsForSaving() {
623+
return useUuids;
624+
}
625+
616626
@Override
617627
public void warning(String message) {
618628
getLogger().warning(message);

src/main/java/nl/rutgerkok/betterenderchest/chestowner/ChestOwners.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import nl.rutgerkok.betterenderchest.uuidconversion.UUIDFetcher;
1313

1414
import org.bukkit.Bukkit;
15-
import org.bukkit.OfflinePlayer;
1615
import org.bukkit.entity.Player;
1716

1817
import com.google.common.cache.Cache;
@@ -115,7 +114,13 @@ public void fromInput(final String name, final Consumer<ChestOwner> onSuccess, f
115114
return;
116115
}
117116

118-
// Check online players
117+
// Get by name if not using UUIDs
118+
if (!plugin.useUuidsForSaving()) {
119+
onSuccess.consume(new NamedChestOwner(name));
120+
return;
121+
}
122+
123+
// Get from cache or do a web lookup
119124
Bukkit.getScheduler().runTaskAsynchronously(plugin.getPlugin(), new Runnable() {
120125

121126
@Override
@@ -148,8 +153,12 @@ public void run() {
148153
* The player.
149154
* @return The <code>ChestOwner</code> belonging to the player.
150155
*/
151-
public ChestOwner playerChest(OfflinePlayer player) {
152-
return new PlayerChestOwner(player.getName(), player.getUniqueId());
156+
public ChestOwner playerChest(Player player) {
157+
if (plugin.useUuidsForSaving()) {
158+
return new UUIDChestOwner(player.getName(), player.getUniqueId());
159+
} else {
160+
return new NamedChestOwner(player.getName());
161+
}
153162
}
154163

155164
/**
@@ -162,7 +171,11 @@ public ChestOwner playerChest(OfflinePlayer player) {
162171
* @return The <code>ChestOwner</code> belonging to the player.
163172
*/
164173
public ChestOwner playerChest(String playerName, UUID uuid) {
165-
return new PlayerChestOwner(playerName, uuid);
174+
if (plugin.useUuidsForSaving()) {
175+
return new UUIDChestOwner(playerName, uuid);
176+
} else {
177+
return new NamedChestOwner(playerName);
178+
}
166179
}
167180

168181
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package nl.rutgerkok.betterenderchest.chestowner;
2+
3+
import nl.rutgerkok.betterenderchest.Translations;
4+
5+
import org.apache.commons.lang.Validate;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.OfflinePlayer;
8+
import org.bukkit.entity.Player;
9+
10+
/**
11+
* For chests referenced by name.
12+
*
13+
*/
14+
class NamedChestOwner implements ChestOwner {
15+
16+
private final String name;
17+
18+
NamedChestOwner(String name) {
19+
Validate.notNull(name, "Name may not be null");
20+
this.name = name;
21+
}
22+
23+
@Override
24+
public String getDisplayName() {
25+
return name;
26+
}
27+
28+
@Override
29+
public String getInventoryTitle() {
30+
return Translations.PRIVATE_CHEST_TITLE.toString(name);
31+
}
32+
33+
@SuppressWarnings("deprecation")
34+
// ^ The server owner chose to use names, not our problem if something
35+
// breaks
36+
@Override
37+
public Player getPlayer() {
38+
return Bukkit.getPlayerExact(name);
39+
}
40+
41+
@Override
42+
public String getSaveFileName() {
43+
return name.toLowerCase();
44+
}
45+
46+
@Override
47+
public boolean isDefaultChest() {
48+
return false;
49+
}
50+
51+
@Override
52+
public boolean isOwnerOnline() {
53+
return getPlayer() != null;
54+
}
55+
56+
@Override
57+
public boolean isPlayer(OfflinePlayer player) {
58+
return name.equalsIgnoreCase(player.getName());
59+
}
60+
61+
@Override
62+
public boolean isPublicChest() {
63+
return false;
64+
}
65+
66+
@Override
67+
public boolean isSpecialChest() {
68+
return false;
69+
}
70+
71+
}

src/main/java/nl/rutgerkok/betterenderchest/chestowner/PlayerChestOwner.java renamed to src/main/java/nl/rutgerkok/betterenderchest/chestowner/UUIDChestOwner.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
* Implementation of {@link ChestOwner} that represents a normal player.
1414
*
1515
*/
16-
final class PlayerChestOwner implements ChestOwner {
16+
final class UUIDChestOwner implements ChestOwner {
1717

1818
private final String displayName;
1919
private final UUID uuid;
2020

21-
PlayerChestOwner(String displayName, UUID uuid) {
21+
UUIDChestOwner(String displayName, UUID uuid) {
2222
Validate.notNull(displayName, "Name may not be null");
2323
Validate.notNull(uuid, "UUID may not be null");
2424
this.displayName = displayName;
@@ -27,10 +27,10 @@ final class PlayerChestOwner implements ChestOwner {
2727

2828
@Override
2929
public boolean equals(Object other) {
30-
if (!(other instanceof PlayerChestOwner)) {
30+
if (!(other instanceof UUIDChestOwner)) {
3131
return false;
3232
}
33-
return ((PlayerChestOwner) other).uuid.equals(this.uuid);
33+
return ((UUIDChestOwner) other).uuid.equals(this.uuid);
3434
}
3535

3636
@Override

src/main/java/nl/rutgerkok/betterenderchest/uuidconversion/BetterEnderUUIDConverter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ private boolean safeConvertWorldGroups(List<WorldGroup> groups) {
102102
plugin.severe("Failed to parse JSON", e);
103103
plugin.disableSaveAndLoad("Failed to parse JSON during UUID conversion", e);
104104
} catch (IOException e) {
105-
plugin.severe("Error during name->UUID conversion process", e);
106-
plugin.disableSaveAndLoad("Error during name->UUID conversion process", e);
105+
plugin.severe("Error during conversion process", e);
106+
plugin.disableSaveAndLoad("Error during conversion process", e);
107107
} catch (Throwable t) {
108-
plugin.severe("Unexpected error during name->UUID conversion process", t);
109-
plugin.disableSaveAndLoad("Unexpected error during name->UUID conversion process", t);
108+
plugin.severe("Unexpected error during conversion process", t);
109+
plugin.disableSaveAndLoad("Unexpected error during conversion process", t);
110110
}
111111
return false;
112112
}
@@ -123,12 +123,16 @@ public final void startConversion() {
123123
}
124124

125125
// Disable saving and loading for now
126-
plugin.log("Converting everything from name to UUID. This process may take a while.");
126+
if (plugin.useUuidsForSaving()) {
127+
plugin.log("Converting everything from name to UUID. This process may take a while.");
128+
} else {
129+
plugin.log("Changing save directory. This shouldn't take that long.");
130+
}
127131
plugin.log("The server will still be usable while the Ender Chests are converted, Ender Chests just won't open.");
128132
Exception convertingException = new Exception("Converting to UUID files, may take a while");
129133
// No stack trace needed
130134
convertingException.setStackTrace(new StackTraceElement[0]);
131-
plugin.disableSaveAndLoad("Converting to UUID files, may take a while", convertingException);
135+
plugin.disableSaveAndLoad("Converting files, may take a while", convertingException);
132136

133137
// Run conversion on another thread
134138
Bukkit.getScheduler().runTaskAsynchronously(plugin.getPlugin(), new Runnable() {

src/main/java/nl/rutgerkok/betterenderchest/uuidconversion/ConvertDirectoryTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import java.util.Map;
99
import java.util.Map.Entry;
1010

11-
import com.google.common.io.Files;
12-
1311
import nl.rutgerkok.betterenderchest.BetterEnderChest;
1412
import nl.rutgerkok.betterenderchest.WorldGroup;
1513
import nl.rutgerkok.betterenderchest.chestowner.ChestOwner;
1614
import nl.rutgerkok.betterenderchest.io.BetterEnderFileHandler;
1715

16+
import com.google.common.io.Files;
17+
1818
class ConvertDirectoryTask extends ConvertTask {
1919

2020
private final String extension;

src/main/java/nl/rutgerkok/betterenderchest/uuidconversion/UUIDFetcher.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* UUID fetcher by Nate Mortensen.
33
* https://gist.github.com/evilmidget38/df8dcd7855937e9d1e1f
44
*
5-
* Modified by BetterEnderChest to
6-
* - throw less generic exceptions
7-
* - return ChestOwner instead of UUID.
8-
* - use the new API Mojang provided to avoid getting rate-limited
5+
* Modified by BetterEnderChest to throw less generic exceptions, return
6+
* ChestOwner instead of UUID, use the new API Mojang provided to avoid getting
7+
* rate-limited and to add support to continue using names.
98
*/
109
package nl.rutgerkok.betterenderchest.uuidconversion;
1110

@@ -86,8 +85,33 @@ public UUIDFetcher(BetterEnderChest plugin, Collection<String> batch) {
8685

8786
@Override
8887
public Map<String, ChestOwner> call() throws IOException, ParseException {
88+
if (plugin.useUuidsForSaving()) {
89+
return callOnline();
90+
} else {
91+
return callOffline();
92+
}
93+
}
94+
95+
private Map<String, ChestOwner> callOffline() {
96+
// Converting is easy when not doing web lookups :)
97+
Map<String, ChestOwner> results = new HashMap<String, ChestOwner>();
98+
for (String name : names) {
99+
results.put(name, plugin.getChestOwners().playerChest(name, null));
100+
}
101+
results.putAll(specialChests);
102+
return results;
103+
}
104+
105+
private Map<String, ChestOwner> callOnline() throws IOException, ParseException {
106+
if (!plugin.useUuidsForSaving()) {
107+
// Should never happen, but it makes reviewing the code easier for
108+
// the BukkitDev staff, as they can easily see that all networking
109+
// can be blocked
110+
throw new IllegalStateException();
111+
}
112+
89113
Map<String, ChestOwner> uuidMap = new HashMap<String, ChestOwner>();
90-
for(int i = 0; i < names.size(); i+=MAX_SEARCH) {
114+
for (int i = 0; i < names.size(); i += MAX_SEARCH) {
91115
String body = buildBody(names, i);
92116
HttpURLConnection connection = createConnection();
93117
writeBody(connection, body);

0 commit comments

Comments
 (0)