Skip to content

Commit f3765ba

Browse files
Merge pull request #2956 from Slimefun/fix/2942
Fixes #2942
2 parents 2adedd2 + bf22f40 commit f3765ba

File tree

4 files changed

+94
-34
lines changed

4 files changed

+94
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* Fixed Grappling Hook vanishing in creative mode
7272
* Fixed #2944
7373
* Fixed #2837
74+
* Fixed #2942
7475

7576
## Release Candidate 21 (14 Mar 2021)
7677
https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#21

src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Language getDefaultLanguage() {
150150

151151
@Override
152152
public Language getLanguage(@Nonnull Player p) {
153-
Validate.notNull("Player cannot be null!");
153+
Validate.notNull(p, "Player cannot be null!");
154154

155155
PersistentDataContainer container = p.getPersistentDataContainer();
156156
String language = container.get(languageKey, PersistentDataType.STRING);
@@ -253,7 +253,7 @@ private Set<String> getTotalKeys(@Nonnull Language lang) {
253253
}
254254

255255
@Nonnull
256-
private Set<String> getKeys(FileConfiguration... files) {
256+
private Set<String> getKeys(@Nonnull FileConfiguration... files) {
257257
Set<String> keys = new HashSet<>();
258258

259259
for (FileConfiguration cfg : files) {

src/main/java/io/github/thebusybiscuit/slimefun4/core/services/localization/Language.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ public ItemStack getItem() {
160160
*/
161161
@Nonnull
162162
public String getName(@Nonnull Player p) {
163-
String name = SlimefunPlugin.getLocalization().getMessage(p, "languages." + id);
164-
return name != null ? name : toString();
163+
return SlimefunPlugin.getLocalization().getMessage(p, "languages." + id);
165164
}
166165

167166
/**

src/main/java/io/github/thebusybiscuit/slimefun4/core/services/localization/SlimefunLocalization.java

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.github.thebusybiscuit.slimefun4.core.services.localization;
22

3-
import java.util.Arrays;
43
import java.util.Collection;
4+
import java.util.Collections;
55
import java.util.List;
66
import java.util.function.UnaryOperator;
77

88
import javax.annotation.Nonnull;
99
import javax.annotation.Nullable;
10+
import javax.annotation.ParametersAreNonnullByDefault;
1011

12+
import org.apache.commons.lang.Validate;
1113
import org.bukkit.ChatColor;
1214
import org.bukkit.Keyed;
1315
import org.bukkit.NamespacedKey;
@@ -33,9 +35,9 @@
3335
/**
3436
* This is an abstract parent class of {@link LocalizationService}.
3537
* There is not really much more I can say besides that...
36-
*
38+
*
3739
* @author TheBusyBiscuit
38-
*
40+
*
3941
* @see LocalizationService
4042
*
4143
*/
@@ -48,55 +50,57 @@ protected SlimefunLocalization(@Nonnull SlimefunPlugin plugin) {
4850
/**
4951
* This method attempts to return the {@link Language} with the given
5052
* language code.
51-
*
53+
*
5254
* @param id
5355
* The language code
54-
*
56+
*
5557
* @return A {@link Language} with the given id or null
5658
*/
5759
@Nullable
5860
public abstract Language getLanguage(@Nonnull String id);
5961

6062
/**
6163
* This method returns the currently selected {@link Language} of a {@link Player}.
62-
*
64+
*
6365
* @param p
6466
* The {@link Player} to query
65-
*
67+
*
6668
* @return The {@link Language} that was selected by the given {@link Player}
6769
*/
70+
@Nullable
6871
public abstract Language getLanguage(@Nonnull Player p);
6972

7073
/**
7174
* This method returns the default {@link Language} of this {@link Server}
72-
*
75+
*
7376
* @return The default {@link Language}
7477
*/
78+
@Nullable
7579
public abstract Language getDefaultLanguage();
7680

7781
/**
7882
* This returns whether a {@link Language} with the given id exists within
7983
* the project resources.
80-
*
84+
*
8185
* @param id
8286
* The {@link Language} id
83-
*
87+
*
8488
* @return Whether the project contains a {@link Language} with that id
8589
*/
8690
protected abstract boolean hasLanguage(@Nonnull String id);
8791

8892
/**
8993
* This method returns a full {@link Collection} of every {@link Language} that was
9094
* found.
91-
*
95+
*
9296
* @return A {@link Collection} that contains every installed {@link Language}
9397
*/
9498
@Nonnull
9599
public abstract Collection<Language> getLanguages();
96100

97101
/**
98102
* This method adds a new {@link Language} with the given id and texture.
99-
*
103+
*
100104
* @param id
101105
* The {@link Language} id
102106
* @param texture
@@ -117,7 +121,28 @@ protected void loadEmbeddedLanguages() {
117121
}
118122
}
119123

120-
public String getMessage(Player p, String key) {
124+
@Nonnull
125+
@Override
126+
public String getMessage(@Nonnull String key) {
127+
Validate.notNull(key, "Message key cannot be null!");
128+
129+
Language language = getDefaultLanguage();
130+
131+
String message = language == null ? null : language.getMessagesFile().getString(key);
132+
133+
if (message == null) {
134+
Language fallback = getLanguage(SupportedLanguage.ENGLISH.getLanguageId());
135+
return fallback.getMessagesFile().getString(key);
136+
}
137+
138+
return message;
139+
}
140+
141+
@Nonnull
142+
public String getMessage(@Nonnull Player p, @Nonnull String key) {
143+
Validate.notNull(p, "Player cannot be null!");
144+
Validate.notNull(key, "Message key cannot be null!");
145+
121146
Language language = getLanguage(p);
122147

123148
if (language == null) {
@@ -134,11 +159,15 @@ public String getMessage(Player p, String key) {
134159
return message;
135160
}
136161

137-
public List<String> getMessages(Player p, String key) {
162+
@Nonnull
163+
public List<String> getMessages(@Nonnull Player p, @Nonnull String key) {
164+
Validate.notNull(p, "Player cannot be null!");
165+
Validate.notNull(key, "Message key cannot be null!");
166+
138167
Language language = getLanguage(p);
139168

140169
if (language == null) {
141-
return Arrays.asList("NO LANGUAGE FOUND");
170+
return Collections.singletonList("NO LANGUAGE FOUND");
142171
}
143172

144173
List<String> messages = language.getMessagesFile().getStringList(key);
@@ -151,37 +180,55 @@ public List<String> getMessages(Player p, String key) {
151180
return messages;
152181
}
153182

183+
@Nonnull
184+
@ParametersAreNonnullByDefault
154185
public List<String> getMessages(Player p, String key, UnaryOperator<String> function) {
186+
Validate.notNull(p, "Player cannot be null!");
187+
Validate.notNull(key, "Message key cannot be null!");
188+
Validate.notNull(function, "Function cannot be null!");
189+
155190
List<String> messages = getMessages(p, key);
156191
messages.replaceAll(function);
157192

158193
return messages;
159194
}
160195

161-
public String getResearchName(Player p, NamespacedKey key) {
196+
@Nullable
197+
public String getResearchName(@Nonnull Player p, @Nonnull NamespacedKey key) {
198+
Validate.notNull(p, "Player cannot be null!");
199+
Validate.notNull(key, "NamespacedKey cannot be null!");
200+
162201
Language language = getLanguage(p);
163202

164-
if (language.getResearchesFile() == null) {
203+
if (language == null || language.getResearchesFile() == null) {
165204
return null;
166205
}
167206

168-
return language.getResearchesFile().getString(key.getNamespace() + "." + key.getKey());
207+
return language.getResearchesFile().getString(key.getNamespace() + '.' + key.getKey());
169208
}
170209

171-
public String getCategoryName(Player p, NamespacedKey key) {
210+
@Nullable
211+
public String getCategoryName(@Nonnull Player p, @Nonnull NamespacedKey key) {
212+
Validate.notNull(p, "Player cannot be null!");
213+
Validate.notNull(key, "NamespacedKey cannot be null!");
214+
172215
Language language = getLanguage(p);
173216

174-
if (language.getCategoriesFile() == null) {
217+
if (language == null || language.getCategoriesFile() == null) {
175218
return null;
176219
}
177220

178-
return language.getCategoriesFile().getString(key.getNamespace() + "." + key.getKey());
221+
return language.getCategoriesFile().getString(key.getNamespace() + '.' + key.getKey());
179222
}
180223

181-
public String getResourceString(Player p, String key) {
224+
@Nonnull
225+
public String getResourceString(@Nonnull Player p, @Nonnull String key) {
226+
Validate.notNull(p, "Player cannot be null!");
227+
Validate.notNull(key, "Message key cannot be null!");
228+
182229
Language language = getLanguage(p);
183230

184-
String value = language.getResourcesFile() != null ? language.getResourcesFile().getString(key) : null;
231+
String value = language != null && language.getResourcesFile() != null ? language.getResourcesFile().getString(key) : null;
185232

186233
if (value != null) {
187234
return value;
@@ -191,16 +238,20 @@ public String getResourceString(Player p, String key) {
191238
}
192239
}
193240

194-
public ItemStack getRecipeTypeItem(Player p, RecipeType recipeType) {
241+
@Nonnull
242+
public ItemStack getRecipeTypeItem(@Nonnull Player p, @Nonnull RecipeType recipeType) {
243+
Validate.notNull(p, "Player cannot be null!");
244+
Validate.notNull(recipeType, "Recipe type cannot be null!");
245+
195246
Language language = getLanguage(p);
196247
ItemStack item = recipeType.toItem();
197248
NamespacedKey key = recipeType.getKey();
198249

199-
if (language.getRecipeTypesFile() == null || !language.getRecipeTypesFile().contains(key.getNamespace() + "." + key.getKey())) {
250+
if (language == null || language.getRecipeTypesFile() == null || !language.getRecipeTypesFile().contains(key.getNamespace() + '.' + key.getKey())) {
200251
language = getLanguage("en");
201252
}
202253

203-
if (!language.getRecipeTypesFile().contains(key.getNamespace() + "." + key.getKey())) {
254+
if (!language.getRecipeTypesFile().contains(key.getNamespace() + '.' + key.getKey())) {
204255
return item;
205256
}
206257

@@ -218,7 +269,10 @@ public ItemStack getRecipeTypeItem(Player p, RecipeType recipeType) {
218269
}
219270

220271
@Override
221-
public void sendMessage(CommandSender recipient, String key, boolean addPrefix) {
272+
public void sendMessage(@Nonnull CommandSender recipient, @Nonnull String key, boolean addPrefix) {
273+
Validate.notNull(recipient, "Recipient cannot be null!");
274+
Validate.notNull(key, "Message key cannot be null!");
275+
222276
String prefix = addPrefix ? getPrefix() : "";
223277

224278
if (recipient instanceof Player) {
@@ -229,6 +283,9 @@ public void sendMessage(CommandSender recipient, String key, boolean addPrefix)
229283
}
230284

231285
public void sendActionbarMessage(@Nonnull Player player, @Nonnull String key, boolean addPrefix) {
286+
Validate.notNull(player, "Player cannot be null!");
287+
Validate.notNull(key, "Message key cannot be null!");
288+
232289
String prefix = addPrefix ? getPrefix() : "";
233290
String message = ChatColors.color(prefix + getMessage(player, key));
234291

@@ -237,15 +294,17 @@ public void sendActionbarMessage(@Nonnull Player player, @Nonnull String key, bo
237294
}
238295

239296
@Override
240-
public void sendMessage(CommandSender recipient, String key) {
297+
public void sendMessage(@Nonnull CommandSender recipient, @Nonnull String key) {
241298
sendMessage(recipient, key, true);
242299
}
243300

301+
@ParametersAreNonnullByDefault
244302
public void sendMessage(CommandSender recipient, String key, UnaryOperator<String> function) {
245303
sendMessage(recipient, key, true, function);
246304
}
247305

248306
@Override
307+
@ParametersAreNonnullByDefault
249308
public void sendMessage(CommandSender recipient, String key, boolean addPrefix, UnaryOperator<String> function) {
250309
if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
251310
return;
@@ -261,7 +320,7 @@ public void sendMessage(CommandSender recipient, String key, boolean addPrefix,
261320
}
262321

263322
@Override
264-
public void sendMessages(CommandSender recipient, String key) {
323+
public void sendMessages(@Nonnull CommandSender recipient, @Nonnull String key) {
265324
String prefix = getPrefix();
266325

267326
if (recipient instanceof Player) {
@@ -278,6 +337,7 @@ public void sendMessages(CommandSender recipient, String key) {
278337
}
279338

280339
@Override
340+
@ParametersAreNonnullByDefault
281341
public void sendMessages(CommandSender recipient, String key, boolean addPrefix, UnaryOperator<String> function) {
282342
String prefix = addPrefix ? getPrefix() : "";
283343

@@ -294,8 +354,8 @@ public void sendMessages(CommandSender recipient, String key, boolean addPrefix,
294354
}
295355
}
296356

357+
@ParametersAreNonnullByDefault
297358
public void sendMessages(CommandSender recipient, String key, UnaryOperator<String> function) {
298359
sendMessages(recipient, key, true, function);
299360
}
300-
301361
}

0 commit comments

Comments
 (0)