From a8f7ee3c703c04fec002854e067cf22744a71e41 Mon Sep 17 00:00:00 2001 From: Ben Woodworth Date: Fri, 25 Mar 2016 04:29:53 -0400 Subject: [PATCH] Added config file --- .../co/kepler/fastcraftplus/FastCraft.java | 5 +- .../kepler/fastcraftplus/config/Config.java | 30 +++++++ .../kepler/fastcraftplus/config/Language.java | 79 ++++++++++++++----- .../fastcraftplus/craftgui/GUIFastCraft.java | 4 +- .../craftgui/LayoutFastCraft.java | 6 +- ...ipesBasic.java => LayoutRecipesItems.java} | 6 +- src/main/resources/config.yml | 4 + src/main/resources/lang/EN.yml | 4 +- 8 files changed, 106 insertions(+), 32 deletions(-) create mode 100644 src/main/java/co/kepler/fastcraftplus/config/Config.java rename src/main/java/co/kepler/fastcraftplus/craftgui/{LayoutRecipesBasic.java => LayoutRecipesItems.java} (60%) create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/co/kepler/fastcraftplus/FastCraft.java b/src/main/java/co/kepler/fastcraftplus/FastCraft.java index 3bb67b3d..415ca8d7 100644 --- a/src/main/java/co/kepler/fastcraftplus/FastCraft.java +++ b/src/main/java/co/kepler/fastcraftplus/FastCraft.java @@ -1,6 +1,7 @@ package co.kepler.fastcraftplus; import co.kepler.fastcraftplus.api.gui.GUI; +import co.kepler.fastcraftplus.config.Config; import co.kepler.fastcraftplus.config.Language; import co.kepler.fastcraftplus.craftgui.GUIFastCraft; import org.bukkit.event.Listener; @@ -11,6 +12,7 @@ public class FastCraft extends JavaPlugin implements Listener { private static FastCraft instance; + private Config config; private Language lang; /** @@ -63,7 +65,8 @@ public static void logInfo(String msg) { public void onEnable() { instance = this; - lang = new Language("EN"); // TODO Language from config + config = new Config(); + lang = new Language(config.getLanguage()); GUIFastCraft.init(); } diff --git a/src/main/java/co/kepler/fastcraftplus/config/Config.java b/src/main/java/co/kepler/fastcraftplus/config/Config.java new file mode 100644 index 00000000..b7e73cab --- /dev/null +++ b/src/main/java/co/kepler/fastcraftplus/config/Config.java @@ -0,0 +1,30 @@ +package co.kepler.fastcraftplus.config; + +import co.kepler.fastcraftplus.FastCraft; +import org.bukkit.configuration.Configuration; + +/** + * A class for access to the FastCraft+ configuration. + */ +public class Config { + private Configuration config; + + public Config() { + loadConfig(); + } + + /** + * Loads config with default values, and saves to update the config. + */ + public void loadConfig() { + FastCraft fastCraft = FastCraft.getInstance(); + fastCraft.saveDefaultConfig(); + config = fastCraft.getConfig(); + config.options().copyDefaults(true); + fastCraft.saveConfig(); + } + + public String getLanguage() { + return config.getString("language"); + } +} diff --git a/src/main/java/co/kepler/fastcraftplus/config/Language.java b/src/main/java/co/kepler/fastcraftplus/config/Language.java index 7b89e805..93ec4e74 100644 --- a/src/main/java/co/kepler/fastcraftplus/config/Language.java +++ b/src/main/java/co/kepler/fastcraftplus/config/Language.java @@ -22,13 +22,11 @@ public class Language { private static final String NOT_FOUND = ChatColor.RED + "[Lang: %key%]"; private static final String NOT_FOUND_KEY = "key"; - - private YamlConfiguration lang; - private Map itemNames; - public final GUI gui = new GUI(); public final Commands commands = new Commands(); public final Items items = new Items(); + private YamlConfiguration lang; + private Map itemNames; /** * Create an instance of Language @@ -137,10 +135,15 @@ public class GUI { public final Ingredients ingredients = new Ingredients(); public final Toolbar toolbar = new Toolbar(); - public String title() { return get("gui.title"); } + public String title() { + return get("gui.title"); + } public class Ingredients { - public String label() { return get("gui.ingredients.label"); } + public String label() { + return get("gui.ingredients.label"); + } + public String item(int amount, String item) { return get("gui.ingredients.item", "amount", s(amount), "item", item); } @@ -151,7 +154,7 @@ public class Toolbar { public final PagePrev pagePrev = new PagePrev(); public final PageNext pageNext = new PageNext(); - public final CraftBasic craftBasic = new CraftBasic(); + public final CraftItems craftItems = new CraftItems(); public final CraftArmor craftArmor = new CraftArmor(); public final CraftFireworks craftFireworks = new CraftFireworks(); public final Refresh refresh = new Refresh(); @@ -159,49 +162,83 @@ public class Toolbar { public final Workbench workbench = new Workbench(); public class PagePrev { - public String title() { return get(tb + "page-prev.title"); } + public String title() { + return get(tb + "page-prev.title"); + } + public List description(int prev, int total, int cur) { return getList(tb + "page-prev.description", "prev", s(prev), "total", s(total), "cur", s(cur)); } } public class PageNext { - public String title() { return get(tb + "page-next.title"); } + public String title() { + return get(tb + "page-next.title"); + } + public List description(int prev, int total, int cur) { return getList(tb + "page-next.description", "next", s(prev), "total", s(total), "cur", s(cur)); } } - public class CraftBasic { - public String title() { return get(tb + "craft-basic.title"); } - public List description() { return getList(tb + "craft-basic.description"); } + public class CraftItems { + public String title() { + return get(tb + "craft-items.title"); + } + + public List description() { + return getList(tb + "craft-items.description"); + } } public class CraftArmor { - public String title() { return get(tb + "craft-armor.title"); } - public List description() { return getList(tb + "craft-armor.description"); } + public String title() { + return get(tb + "craft-armor.title"); + } + + public List description() { + return getList(tb + "craft-armor.description"); + } } public class CraftFireworks { - public String title() { return get(tb + "craft-fireworks.title"); } - public List description() { return getList(tb + "craft-fireworks.description"); } + public String title() { + return get(tb + "craft-fireworks.title"); + } + + public List description() { + return getList(tb + "craft-fireworks.description"); + } } public class Refresh { - public String title() { return get(tb + "refresh.title"); } - public List description() { return getList(tb + "refresh.description"); } + public String title() { + return get(tb + "refresh.title"); + } + + public List description() { + return getList(tb + "refresh.description"); + } } public class Multiplier { - public String title(int mult) { return get(tb + "multiplier.title", "mult", s(mult)); } + public String title(int mult) { + return get(tb + "multiplier.title", "mult", s(mult)); + } + public List description(int mult) { return getList(tb + "refresh.description", "mult", s(mult)); } } public class Workbench { - public String title() { return get(tb + "workbench.title"); } - public List description() { return getList(tb + "workbench.description"); } + public String title() { + return get(tb + "workbench.title"); + } + + public List description() { + return getList(tb + "workbench.description"); + } } } } diff --git a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java index d6b6577b..4925706b 100644 --- a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java +++ b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java @@ -123,8 +123,8 @@ public boolean onClick(GUIButton.Click info) { // Create Crafting button btnTabCrafting = new GUIButtonGlowing(new GUIItemBuilder(Material.STICK) - .setDisplayName(FastCraft.lang().gui.toolbar.craftBasic.title()) - .setLore(FastCraft.lang().gui.toolbar.craftBasic.description()) + .setDisplayName(FastCraft.lang().gui.toolbar.craftItems.title()) + .setLore(FastCraft.lang().gui.toolbar.craftItems.description()) .setHideInfo(true).build()); btnTabCrafting.setClickAction(new GUIButton.ClickAction() { public boolean onClick(GUIButton.Click info) { diff --git a/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutFastCraft.java b/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutFastCraft.java index 7cbe710d..9cbf0177 100644 --- a/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutFastCraft.java +++ b/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutFastCraft.java @@ -12,7 +12,7 @@ public class LayoutFastCraft extends LayoutMulti { private final GUIFastCraft gui; private final Layout layoutNavbar; - private final LayoutRecipesBasic layoutCraftingBasic; + private final LayoutRecipesItems layoutCraftingItems; private final LayoutRecipesArmor layoutCraftingArmor; private final LayoutRecipesFireworks layoutCraftingFireworks; @@ -22,7 +22,7 @@ public LayoutFastCraft(GUIFastCraft gui) { // Initialize layout pages layoutNavbar = getBottomLayout(); - layoutCraftingBasic = new LayoutRecipesBasic(gui); + layoutCraftingItems = new LayoutRecipesItems(gui); layoutCraftingArmor = new LayoutRecipesArmor(gui); layoutCraftingFireworks = new LayoutRecipesFireworks(gui); @@ -53,7 +53,7 @@ public Layout getLayoutNavbar() { public LayoutRecipes getLayoutTab(CraftingTab tab) { switch (tab) { case CRAFTING: - return layoutCraftingBasic; + return layoutCraftingItems; case ARMOR: return layoutCraftingArmor; case FIREWORKS: diff --git a/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutRecipesBasic.java b/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutRecipesItems.java similarity index 60% rename from src/main/java/co/kepler/fastcraftplus/craftgui/LayoutRecipesBasic.java rename to src/main/java/co/kepler/fastcraftplus/craftgui/LayoutRecipesItems.java index 204d4fd4..60fb1570 100644 --- a/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutRecipesBasic.java +++ b/src/main/java/co/kepler/fastcraftplus/craftgui/LayoutRecipesItems.java @@ -3,11 +3,11 @@ import co.kepler.fastcraftplus.crafting.RecipeUtil; /** - * A recipes layout that shows basic crafting recipes. + * A recipes layout that shows item crafting recipes. */ -public class LayoutRecipesBasic extends LayoutRecipes { +public class LayoutRecipesItems extends LayoutRecipes { - public LayoutRecipesBasic(GUIFastCraft gui) { + public LayoutRecipesItems(GUIFastCraft gui) { super(gui); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 00000000..68065483 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,4 @@ +# language: EN Custom languages can be added by creating another file +# in the lang directory of the FastCraft+ jar file. + +language: EN diff --git a/src/main/resources/lang/EN.yml b/src/main/resources/lang/EN.yml index c519dd6e..6a530b6f 100644 --- a/src/main/resources/lang/EN.yml +++ b/src/main/resources/lang/EN.yml @@ -22,10 +22,10 @@ gui: description: # Variables: prev, total, cur - '&bGo to page %next%/%total%' - craft-basic: + craft-items: title: '&aCraft Items' description: - - '&bCraft basic items' + - '&bCraft items' craft-armor: title: '&aCraft Dyed Armor'