From a42d4d1d50b1bc0ffc3f1c543b81854435fa7048 Mon Sep 17 00:00:00 2001 From: Trent Hensler Date: Fri, 2 Mar 2018 19:07:34 -0800 Subject: [PATCH] Handle null config section to be more friendly with people upgrading --- .../massivecraft/factions/cmd/CmdPerm.java | 10 +++- .../fperms/gui/PermissableActionGUI.java | 54 ++++++++++++++----- .../fperms/gui/PermissableRelationGUI.java | 46 +++++++++++----- .../zcore/fperms/gui/PermissionGUI.java | 2 + 4 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdPerm.java b/src/main/java/com/massivecraft/factions/cmd/CmdPerm.java index 92f50c68..4934b242 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdPerm.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdPerm.java @@ -40,10 +40,16 @@ public class CmdPerm extends FCommand { @Override public void perform() { if (args.size() == 0) { - me.openInventory(new PermissableRelationGUI(fme).getInventory()); + PermissableRelationGUI gui = new PermissableRelationGUI(fme); + gui.build(); + + me.openInventory(gui.getInventory()); return; } else if (args.size() == 1 && getPermissable(argAsString(0)) != null) { - me.openInventory(new PermissableActionGUI(fme, getPermissable(argAsString(0))).getInventory()); + PermissableActionGUI gui = new PermissableActionGUI(fme, getPermissable(argAsString(0))); + gui.build(); + + me.openInventory(gui.getInventory()); return; } diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableActionGUI.java b/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableActionGUI.java index 10584e33..a43af63c 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableActionGUI.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableActionGUI.java @@ -33,23 +33,33 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { private HashMap specialSlots = new HashMap<>(); private ArrayList usedDummySlots = new ArrayList<>(); - private final ConfigurationSection ACTION_CONFIG = P.p.getConfig().getConfigurationSection("fperm-gui.action"); + private final ConfigurationSection section; public PermissableActionGUI(FPlayer fme, Permissable permissable) { this.fme = fme; this.permissable = permissable; + this.section = P.p.getConfig().getConfigurationSection("fperm-gui.action"); + } - guiSize = ACTION_CONFIG.getInt("rows", 3); + public void build() { + if (section == null) { + P.p.log(Level.WARNING, "Attempted to build f perm GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return; + } + + guiSize = section.getInt("rows", 3); if (guiSize > 5) { guiSize = 5; P.p.log(Level.INFO, "Action GUI size out of bounds, defaulting to 5"); } + guiSize *= 9; - String guiName = ChatColor.translateAlternateColorCodes('&', ACTION_CONFIG.getString("name", "FactionPerms")); + String guiName = ChatColor.translateAlternateColorCodes('&', section.getString("name", "FactionPerms")); actionGUI = Bukkit.createInventory(this, guiSize, guiName); - for (String key : ACTION_CONFIG.getConfigurationSection("slots").getKeys(false)) { - int slot = ACTION_CONFIG.getInt("slots." + key); + for (String key : section.getConfigurationSection("slots").getKeys(false)) { + int slot = section.getInt("slots." + key); if (slot + 1 > guiSize || slot < 0) { P.p.log(Level.WARNING, "Invalid slot for: " + key.toUpperCase()); continue; @@ -66,7 +76,7 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { continue; } - actionSlots.put(ACTION_CONFIG.getInt("slots." + key), permissableAction); + actionSlots.put(section.getInt("slots." + key), permissableAction); } buildDummyItems(); @@ -154,6 +164,12 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { } private ItemStack getSpecialItem(SpecialItem specialItem) { + if (section == null) { + P.p.log(Level.WARNING, "Attempted to build f perm GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return new ItemStack(Material.AIR); + } + switch (specialItem) { case RELATION: return permissable.buildItem(); @@ -179,7 +195,13 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { } private void buildDummyItems() { - for (String key : ACTION_CONFIG.getConfigurationSection("dummy-items").getKeys(false)) { + if (section == null) { + P.p.log(Level.WARNING, "Attempted to build f perm GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return; + } + + for (String key : section.getConfigurationSection("dummy-items").getKeys(false)) { int dummyId; try { dummyId = Integer.parseInt(key); @@ -193,7 +215,7 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { continue; } - List dummySlots = ACTION_CONFIG.getIntegerList("dummy-items." + key); + List dummySlots = section.getIntegerList("dummy-items." + key); for (Integer slot : dummySlots) { if (slot + 1 > guiSize || slot < 0) { P.p.log(Level.WARNING, "Invalid slot: " + slot + " for dummy item: " + key); @@ -206,9 +228,15 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { } private ItemStack buildDummyItem(int id) { - final ConfigurationSection DUMMY_CONFIG = P.p.getConfig().getConfigurationSection("fperm-gui.dummy-items." + id); + final ConfigurationSection dummySection = P.p.getConfig().getConfigurationSection("fperm-gui.dummy-items." + id); - Material material = Material.matchMaterial(DUMMY_CONFIG.getString("material", "")); + if (dummySection == null) { + P.p.log(Level.WARNING, "Attempted to build dummy items for F PERM GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return new ItemStack(Material.AIR); + } + + Material material = Material.matchMaterial(dummySection.getString("material", "")); if (material == null) { P.p.log(Level.WARNING, "Invalid material for dummy item: " + id); return null; @@ -218,7 +246,7 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { DyeColor color; try { - color = DyeColor.valueOf(DUMMY_CONFIG.getString("color", "")); + color = DyeColor.valueOf(dummySection.getString("color", "")); } catch (Exception exception) { color = null; } @@ -228,10 +256,10 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI { ItemMeta itemMeta = itemStack.getItemMeta(); - itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', DUMMY_CONFIG.getString("name", " "))); + itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', dummySection.getString("name", " "))); List lore = new ArrayList<>(); - for (String loreLine : DUMMY_CONFIG.getStringList("lore")) { + for (String loreLine : dummySection.getStringList("lore")) { lore.add(ChatColor.translateAlternateColorCodes('&', loreLine)); } itemMeta.setLore(lore); diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableRelationGUI.java b/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableRelationGUI.java index 0c7cf8d2..af71af04 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableRelationGUI.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissableRelationGUI.java @@ -31,24 +31,34 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI { private HashMap relationSlots = new HashMap<>(); - private final ConfigurationSection RELATION_CONFIG = P.p.getConfig().getConfigurationSection("fperm-gui.relation"); + private final ConfigurationSection section; public PermissableRelationGUI(FPlayer fme) { this.fme = fme; + this.section = P.p.getConfig().getConfigurationSection("fperm-gui.relation"); + } + + public void build() { + if (section == null) { + P.p.log(Level.WARNING, "Attempted to build f perm GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return; + } // Build basic Inventory info - guiSize = RELATION_CONFIG.getInt("rows", 3); + guiSize = section.getInt("rows", 3); if (guiSize > 5) { guiSize = 5; P.p.log(Level.INFO, "Relation GUI size out of bounds, defaulting to 5"); } + guiSize *= 9; - String guiName = ChatColor.translateAlternateColorCodes('&', RELATION_CONFIG.getString("name", "FactionPermissions")); + String guiName = ChatColor.translateAlternateColorCodes('&', section.getString("name", "FactionPermissions")); relationGUI = Bukkit.createInventory(this, guiSize, guiName); - for (String key : RELATION_CONFIG.getConfigurationSection("slots").getKeys(false)) { - int slot = RELATION_CONFIG.getInt("slots." + key); + for (String key : section.getConfigurationSection("slots").getKeys(false)) { + int slot = section.getInt("slots." + key); if (slot + 1 > guiSize && slot > 0) { P.p.log(Level.WARNING, "Invalid slot of " + key.toUpperCase() + " in relation GUI skipping it"); continue; @@ -109,7 +119,13 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI { } private void buildDummyItems() { - for (String key : RELATION_CONFIG.getConfigurationSection("dummy-items").getKeys(false)) { + if (section == null) { + P.p.log(Level.WARNING, "Attempted to build f perm GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return; + } + + for (String key : section.getConfigurationSection("dummy-items").getKeys(false)) { int dummyId; try { dummyId = Integer.parseInt(key); @@ -123,7 +139,7 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI { continue; } - List dummySlots = RELATION_CONFIG.getIntegerList("dummy-items." + key); + List dummySlots = section.getIntegerList("dummy-items." + key); for (Integer slot : dummySlots) { if (slot + 1 > guiSize || slot < 0) { P.p.log(Level.WARNING, "Invalid slot: " + slot + " for dummy item: " + key); @@ -135,9 +151,15 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI { } private ItemStack buildDummyItem(int id) { - final ConfigurationSection DUMMY_CONFIG = P.p.getConfig().getConfigurationSection("fperm-gui.dummy-items." + id); + final ConfigurationSection dummySection = P.p.getConfig().getConfigurationSection("fperm-gui.dummy-items." + id); - Material material = Material.matchMaterial(DUMMY_CONFIG.getString("material", "")); + if (dummySection == null) { + P.p.log(Level.WARNING, "Attempted to build f perm GUI but config section not present."); + P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config."); + return new ItemStack(Material.AIR); + } + + Material material = Material.matchMaterial(dummySection.getString("material", "")); if (material == null) { P.p.log(Level.WARNING, "Invalid material for dummy item: " + id); return null; @@ -147,7 +169,7 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI { DyeColor color; try { - color = DyeColor.valueOf(DUMMY_CONFIG.getString("color", "")); + color = DyeColor.valueOf(dummySection.getString("color", "")); } catch (Exception exception) { color = null; } @@ -157,10 +179,10 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI { ItemMeta itemMeta = itemStack.getItemMeta(); - itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', DUMMY_CONFIG.getString("name", " "))); + itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', dummySection.getString("name", " "))); List lore = new ArrayList<>(); - for (String loreLine : DUMMY_CONFIG.getStringList("lore")) { + for (String loreLine : dummySection.getStringList("lore")) { lore.add(ChatColor.translateAlternateColorCodes('&', loreLine)); } itemMeta.setLore(lore); diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissionGUI.java b/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissionGUI.java index 0cfaa4fe..e01e3fb4 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissionGUI.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/gui/PermissionGUI.java @@ -6,6 +6,8 @@ public interface PermissionGUI { public void onClick(int slot, ClickType action); + public void build(); + public enum SpecialItem { BACK, RELATION;