mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-12-04 16:33:23 +01:00
Fixed some nasty bugs
This commit is contained in:
parent
303bbc317b
commit
22a7c5ae94
@ -35,4 +35,8 @@ public class LocalizationLoadingException extends IOException {
|
||||
return locale;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return super.getMessage() + " (While trying to load localization for locale " + getLocale() + ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@ -30,7 +29,7 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
|
||||
try {
|
||||
loadLocale(locale);
|
||||
} catch (NoSuchLocalizationException e) {
|
||||
//let's take the defaults from the enum!
|
||||
// let's take the defaults from the enum!
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,11 +38,12 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
|
||||
try {
|
||||
loadLocale(locale);
|
||||
} catch (NoSuchLocalizationException e) {
|
||||
core.log(Level.WARNING, "An error occured while trying to get the localization for: "
|
||||
+ e.getLocale().getDisplayCountry(DEFAULT_LOCALE));
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
if (!isLocaleLoaded(locale))
|
||||
throw new LocalizationLoadingException("Couldn't load the localization: "
|
||||
+ locale.toString(), locale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,26 +51,38 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
|
||||
*/
|
||||
@Override
|
||||
public void loadLocale(Locale l) throws NoSuchLocalizationException {
|
||||
InputStream stream = null;
|
||||
messages.remove(l);
|
||||
|
||||
InputStream resstream = null;
|
||||
InputStream filestream = null;
|
||||
|
||||
try {
|
||||
stream = new FileInputStream(new File(core.getDataFolder(), l.getLanguage() + ".yml"));
|
||||
filestream = new FileInputStream(new File(core.getDataFolder(), l.getLanguage() + ".yml"));
|
||||
} catch (FileNotFoundException e) {
|
||||
}
|
||||
|
||||
// only if that file didn't exist, we try to get the localization from the JAR.
|
||||
if (stream == null) // this way, users can easily overwrite localizations from the JAR.
|
||||
stream = core.getResource(new StringBuilder(LOCALIZATION_FOLDER_NAME).append("/")
|
||||
try {
|
||||
resstream = core.getResource(new StringBuilder(LOCALIZATION_FOLDER_NAME).append("/")
|
||||
.append(l.getLanguage()).append(".yml").toString());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
if (stream == null)
|
||||
if ((resstream == null) && (filestream == null))
|
||||
throw new NoSuchLocalizationException(l);
|
||||
|
||||
messages.put(l, new HashMap<MultiverseMessage, String>(MultiverseMessage.values().length));
|
||||
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(stream);
|
||||
FileConfiguration resconfig = (resstream == null) ? null : YamlConfiguration.loadConfiguration(resstream);
|
||||
FileConfiguration fileconfig = (filestream == null) ? null : YamlConfiguration.loadConfiguration(filestream);
|
||||
for (MultiverseMessage m : MultiverseMessage.values()) {
|
||||
messages.get(l).put(m, config.getString(m.toString(), m.getDefault()));
|
||||
String value = m.getDefault();
|
||||
|
||||
if (resconfig != null)
|
||||
value = resconfig.getString(m.toString(), value);
|
||||
if (fileconfig != null)
|
||||
value = fileconfig.getString(m.toString(), value);
|
||||
|
||||
messages.get(l).put(m, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,8 +107,9 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
|
||||
*/
|
||||
@Override
|
||||
public String getMessage(MultiverseMessage key) {
|
||||
if (!isLocaleLoaded(DEFAULT_LOCALE))
|
||||
if (!isLocaleLoaded(locale)) {
|
||||
return key.getDefault();
|
||||
}
|
||||
else
|
||||
return messages.get(locale).get(key);
|
||||
}
|
||||
@ -108,11 +121,11 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
|
||||
public String getMessage(MultiverseMessage key, Locale locale) {
|
||||
try {
|
||||
maybeLoadLocale(locale);
|
||||
return messages.get(locale).get(key);
|
||||
} catch (LocalizationLoadingException e) {
|
||||
e.printStackTrace();
|
||||
return getMessage(key);
|
||||
}
|
||||
return messages.get(locale).get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,8 +66,6 @@ public class TestLocalization {
|
||||
// This should be the same core as creator.getCore()
|
||||
assertEquals(core, creator.getCore());
|
||||
|
||||
new File(TestInstanceCreator.pluginDirectory, "en.yml").delete();
|
||||
|
||||
// Make sure there is neither the file nor the resource
|
||||
assertNull(core.getResource("localization/en.yml"));
|
||||
assertFalse(new File(TestInstanceCreator.pluginDirectory, "en.yml").exists());
|
||||
@ -98,8 +96,6 @@ public class TestLocalization {
|
||||
// This should be the same core as creator.getCore()
|
||||
assertEquals(core, creator.getCore());
|
||||
|
||||
new File(TestInstanceCreator.pluginDirectory, "en.yml").delete();
|
||||
|
||||
// Make sure there is no file, only the resource
|
||||
assertFalse(new File(TestInstanceCreator.pluginDirectory, "en.yml").exists());
|
||||
assertTrue(new File("src/main/resources/localization/en.yml").exists());
|
||||
@ -150,13 +146,14 @@ public class TestLocalization {
|
||||
assertEquals(core, creator.getCore());
|
||||
|
||||
// Create the file
|
||||
BufferedWriter bwriter = new BufferedWriter(new FileWriter(new File(TestInstanceCreator.pluginDirectory, "en.yml")));
|
||||
File file = new File(TestInstanceCreator.pluginDirectory, "en.yml");
|
||||
BufferedWriter bwriter = new BufferedWriter(new FileWriter(file));
|
||||
String expected = "a test-string from the user-file";
|
||||
bwriter.write("TEST_STRING: " + expected);
|
||||
bwriter.close();
|
||||
|
||||
// Make sure there is the file and the resource
|
||||
assertTrue(new File(core.getDataFolder(), "en.yml").exists());
|
||||
assertTrue(file.exists());
|
||||
assertTrue(new File("src/main/resources/localization/en.yml").exists());
|
||||
doAnswer(new Answer<InputStream>() {
|
||||
public InputStream answer(InvocationOnMock invocation) throws Throwable {
|
||||
@ -182,5 +179,8 @@ public class TestLocalization {
|
||||
String actual = core.getMessageProvider().getMessage(MultiverseMessage.TEST_STRING);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
|
||||
// Clean up afterwards:
|
||||
assertTrue(file.delete());
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,8 @@ import org.powermock.api.mockito.PowerMockito;
|
||||
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
|
||||
import com.onarandombox.MultiverseCore.localization.MessageProvider;
|
||||
import com.onarandombox.MultiverseCore.localization.SimpleMessageProvider;
|
||||
import com.onarandombox.MultiverseCore.utils.FileUtils;
|
||||
import com.onarandombox.MultiverseCore.utils.WorldManager;
|
||||
|
||||
@ -136,6 +138,10 @@ public class TestInstanceCreator {
|
||||
worldmanagerfield.setAccessible(true);
|
||||
worldmanagerfield.set(core, wm);
|
||||
|
||||
// Set messageProvider
|
||||
MessageProvider messageProvider = PowerMockito.spy(new SimpleMessageProvider(core));
|
||||
core.setMessageProvider(messageProvider);
|
||||
|
||||
// Init our command sender
|
||||
commandSender = spy(new TestCommandSender(mockServer));
|
||||
Bukkit.setServer(mockServer);
|
||||
|
Loading…
Reference in New Issue
Block a user