Added BentoBox reload command

Only reloads locales for now.
This commit is contained in:
tastybento 2018-08-12 19:52:13 -07:00
parent 94c5ac8e79
commit 4681a0bd7d
8 changed files with 82 additions and 45 deletions

View File

@ -1,31 +0,0 @@
package world.bentobox.bentobox.api.commands.admin;
import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
/**
* @author tastybento
*
*/
public class AdminReloadCommand extends CompositeCommand {
/**
* @param parent - parent command
*/
public AdminReloadCommand(CompositeCommand parent) {
super(parent, "reload", "rl");
}
@Override
public void setup() {
setDescription("commands.admin.reload.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
return true;
}
}

View File

@ -16,8 +16,10 @@ public class BentoBoxCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
setPermission("bentobox.admin");
new BentoBoxVersionCommand(this); new BentoBoxVersionCommand(this);
new BentoBoxAboutCommand(this); new BentoBoxAboutCommand(this);
new BentoBoxReloadCommand(this);
} }
@Override @Override

View File

@ -0,0 +1,44 @@
package world.bentobox.bentobox.commands;
import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
/**
* Displays information about Gamemodes, Addons and versioning.
*
* @author tastybento
*/
public class BentoBoxReloadCommand extends CompositeCommand {
/**
* Reloads locales command
* @param parent - command parent
*/
public BentoBoxReloadCommand(CompositeCommand parent) {
super(parent, "reload");
}
@Override
public void setup() {
setPermission("admin.reload");
setDescription("commands.bentobox.reload.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
this.askConfirmation(user, () -> reloadLocales(user));
return false;
}
/**
* Reloads the languages
* @param user
*/
public void reloadLocales(User user) {
getPlugin().getLocalesManager().reloadLanguages();
user.sendMessage("commands.bentobox.reload.locales-reloaded");
}
}

View File

@ -23,7 +23,7 @@ public class BentoBoxVersionCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
// Useless : this command uses the default values. setPermission("bentobox.admin");
} }
@Override @Override

View File

@ -115,7 +115,7 @@ public class AddonsManager {
for (String localeFile : listJarYamlFiles(jar, LOCALE_FOLDER)) { for (String localeFile : listJarYamlFiles(jar, LOCALE_FOLDER)) {
addon.saveResource(localeFile, localeDir, false, true); addon.saveResource(localeFile, localeDir, false, true);
} }
plugin.getLocalesManager().loadLocales(addon.getDescription().getName()); plugin.getLocalesManager().loadLocalesFromFile(addon.getDescription().getName());
// Fire the load event // Fire the load event
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
// Add it to the list of addons // Add it to the list of addons

View File

@ -30,8 +30,8 @@ public class LocalesManager {
public LocalesManager(BentoBox plugin) { public LocalesManager(BentoBox plugin) {
this.plugin = plugin; this.plugin = plugin;
loadLocalesFromJar("BentoBox"); copyLocalesFromJar("BentoBox");
loadLocales("BentoBox"); // Default loadLocalesFromFile("BentoBox"); // Default
} }
/** /**
@ -56,9 +56,14 @@ public class LocalesManager {
return null; return null;
} }
public void loadLocalesFromJar(String parent) { /**
* Copies all the locale files from the plugin jar to the filesystem.
* Only done if the locale folder does not already exist.
* @param folderName - the name of the destination folder
*/
private void copyLocalesFromJar(String folderName) {
// Run through the files and store the locales // Run through the files and store the locales
File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + folderName);
// If the folder does not exist, then make it and fill with the locale files from the jar // If the folder does not exist, then make it and fill with the locale files from the jar
// If it does exist, then new files will NOT be written! // If it does exist, then new files will NOT be written!
if (!localeDir.exists()) { if (!localeDir.exists()) {
@ -79,20 +84,21 @@ public class LocalesManager {
} }
/** /**
* Loads all the locales available. If the locale folder does not exist, one will be created and * Loads all the locales available in the locale folder given. Used for loading all locales from plugin and addons
* filled with locale files from the jar. *
* @param localeFolder - locale folder location relative to the plugin's data folder
*/ */
public void loadLocales(String parent) { public void loadLocalesFromFile(String localeFolder) {
// Describe the filter - we only want files that are correctly named // Filter for files of length 9 and ending with .yml
// Files must be 9 chars long
FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".yml") && name.length() == 9; FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".yml") && name.length() == 9;
// Run through the files and store the locales // Get the folder
File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + localeFolder);
if (!localeDir.exists()) { if (!localeDir.exists()) {
// If there is no locale folder, then return
return; return;
} }
// Store all the locales available // Run through the files and store the locales
for (File language : Objects.requireNonNull(localeDir.listFiles(ymlFilter))) { for (File language : Objects.requireNonNull(localeDir.listFiles(ymlFilter))) {
Locale localeObject = Locale.forLanguageTag(language.getName().substring(0, language.getName().length() - 4)); Locale localeObject = Locale.forLanguageTag(language.getName().substring(0, language.getName().length() - 4));
@ -145,4 +151,13 @@ public class LocalesManager {
public Map<Locale, BentoBoxLocale> getLanguages() { public Map<Locale, BentoBoxLocale> getLanguages() {
return this.languages; return this.languages;
} }
/**
* Reloads all the language files from the filesystem
*/
public void reloadLanguages() {
languages.clear();
loadLocalesFromFile("BentoBox");
plugin.getAddonsManager().getAddons().forEach(addon -> loadLocalesFromFile(addon.getDescription().getName()));
}
} }

View File

@ -175,6 +175,8 @@ commands:
description: "BentoBox admin command" description: "BentoBox admin command"
about: about:
description: "display copyright and license info" description: "display copyright and license info"
reload:
description: "reloads all locale files"
version: version:
description: "display info" description: "display info"
loaded-addons: "Loaded Add-Ons" loaded-addons: "Loaded Add-Ons"

View File

@ -8,3 +8,8 @@ authors: [tastybento, Poslovitch]
softdepend: [PlaceholderAPI, MVdWPlaceholderAPI] softdepend: [PlaceholderAPI, MVdWPlaceholderAPI]
loadbefore: [Multiverse-Core] loadbefore: [Multiverse-Core]
permissions:
bentobox.admin:
description: Allow bentobox command usage
default: op