Handle null config section to be more friendly with people upgrading

This commit is contained in:
Trent Hensler 2018-03-02 19:07:34 -08:00
parent 81a33de33a
commit a42d4d1d50
4 changed files with 85 additions and 27 deletions

View File

@ -40,10 +40,16 @@ public class CmdPerm extends FCommand {
@Override @Override
public void perform() { public void perform() {
if (args.size() == 0) { if (args.size() == 0) {
me.openInventory(new PermissableRelationGUI(fme).getInventory()); PermissableRelationGUI gui = new PermissableRelationGUI(fme);
gui.build();
me.openInventory(gui.getInventory());
return; return;
} else if (args.size() == 1 && getPermissable(argAsString(0)) != null) { } 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; return;
} }

View File

@ -33,23 +33,33 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
private HashMap<Integer, SpecialItem> specialSlots = new HashMap<>(); private HashMap<Integer, SpecialItem> specialSlots = new HashMap<>();
private ArrayList<Integer> usedDummySlots = new ArrayList<>(); private ArrayList<Integer> 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) { public PermissableActionGUI(FPlayer fme, Permissable permissable) {
this.fme = fme; this.fme = fme;
this.permissable = permissable; 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) { if (guiSize > 5) {
guiSize = 5; guiSize = 5;
P.p.log(Level.INFO, "Action GUI size out of bounds, defaulting to 5"); P.p.log(Level.INFO, "Action GUI size out of bounds, defaulting to 5");
} }
guiSize *= 9; 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); actionGUI = Bukkit.createInventory(this, guiSize, guiName);
for (String key : ACTION_CONFIG.getConfigurationSection("slots").getKeys(false)) { for (String key : section.getConfigurationSection("slots").getKeys(false)) {
int slot = ACTION_CONFIG.getInt("slots." + key); int slot = section.getInt("slots." + key);
if (slot + 1 > guiSize || slot < 0) { if (slot + 1 > guiSize || slot < 0) {
P.p.log(Level.WARNING, "Invalid slot for: " + key.toUpperCase()); P.p.log(Level.WARNING, "Invalid slot for: " + key.toUpperCase());
continue; continue;
@ -66,7 +76,7 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
continue; continue;
} }
actionSlots.put(ACTION_CONFIG.getInt("slots." + key), permissableAction); actionSlots.put(section.getInt("slots." + key), permissableAction);
} }
buildDummyItems(); buildDummyItems();
@ -154,6 +164,12 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
} }
private ItemStack getSpecialItem(SpecialItem specialItem) { 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) { switch (specialItem) {
case RELATION: case RELATION:
return permissable.buildItem(); return permissable.buildItem();
@ -179,7 +195,13 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
} }
private void buildDummyItems() { 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; int dummyId;
try { try {
dummyId = Integer.parseInt(key); dummyId = Integer.parseInt(key);
@ -193,7 +215,7 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
continue; continue;
} }
List<Integer> dummySlots = ACTION_CONFIG.getIntegerList("dummy-items." + key); List<Integer> dummySlots = section.getIntegerList("dummy-items." + key);
for (Integer slot : dummySlots) { for (Integer slot : dummySlots) {
if (slot + 1 > guiSize || slot < 0) { if (slot + 1 > guiSize || slot < 0) {
P.p.log(Level.WARNING, "Invalid slot: " + slot + " for dummy item: " + key); 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) { 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) { if (material == null) {
P.p.log(Level.WARNING, "Invalid material for dummy item: " + id); P.p.log(Level.WARNING, "Invalid material for dummy item: " + id);
return null; return null;
@ -218,7 +246,7 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
DyeColor color; DyeColor color;
try { try {
color = DyeColor.valueOf(DUMMY_CONFIG.getString("color", "")); color = DyeColor.valueOf(dummySection.getString("color", ""));
} catch (Exception exception) { } catch (Exception exception) {
color = null; color = null;
} }
@ -228,10 +256,10 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
ItemMeta itemMeta = itemStack.getItemMeta(); ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', DUMMY_CONFIG.getString("name", " "))); itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', dummySection.getString("name", " ")));
List<String> lore = new ArrayList<>(); List<String> lore = new ArrayList<>();
for (String loreLine : DUMMY_CONFIG.getStringList("lore")) { for (String loreLine : dummySection.getStringList("lore")) {
lore.add(ChatColor.translateAlternateColorCodes('&', loreLine)); lore.add(ChatColor.translateAlternateColorCodes('&', loreLine));
} }
itemMeta.setLore(lore); itemMeta.setLore(lore);

View File

@ -31,24 +31,34 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI {
private HashMap<Integer, Permissable> relationSlots = new HashMap<>(); private HashMap<Integer, Permissable> relationSlots = new HashMap<>();
private final ConfigurationSection RELATION_CONFIG = P.p.getConfig().getConfigurationSection("fperm-gui.relation"); private final ConfigurationSection section;
public PermissableRelationGUI(FPlayer fme) { public PermissableRelationGUI(FPlayer fme) {
this.fme = 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 // Build basic Inventory info
guiSize = RELATION_CONFIG.getInt("rows", 3); guiSize = section.getInt("rows", 3);
if (guiSize > 5) { if (guiSize > 5) {
guiSize = 5; guiSize = 5;
P.p.log(Level.INFO, "Relation GUI size out of bounds, defaulting to 5"); P.p.log(Level.INFO, "Relation GUI size out of bounds, defaulting to 5");
} }
guiSize *= 9; 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); relationGUI = Bukkit.createInventory(this, guiSize, guiName);
for (String key : RELATION_CONFIG.getConfigurationSection("slots").getKeys(false)) { for (String key : section.getConfigurationSection("slots").getKeys(false)) {
int slot = RELATION_CONFIG.getInt("slots." + key); int slot = section.getInt("slots." + key);
if (slot + 1 > guiSize && slot > 0) { if (slot + 1 > guiSize && slot > 0) {
P.p.log(Level.WARNING, "Invalid slot of " + key.toUpperCase() + " in relation GUI skipping it"); P.p.log(Level.WARNING, "Invalid slot of " + key.toUpperCase() + " in relation GUI skipping it");
continue; continue;
@ -109,7 +119,13 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI {
} }
private void buildDummyItems() { 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; int dummyId;
try { try {
dummyId = Integer.parseInt(key); dummyId = Integer.parseInt(key);
@ -123,7 +139,7 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI {
continue; continue;
} }
List<Integer> dummySlots = RELATION_CONFIG.getIntegerList("dummy-items." + key); List<Integer> dummySlots = section.getIntegerList("dummy-items." + key);
for (Integer slot : dummySlots) { for (Integer slot : dummySlots) {
if (slot + 1 > guiSize || slot < 0) { if (slot + 1 > guiSize || slot < 0) {
P.p.log(Level.WARNING, "Invalid slot: " + slot + " for dummy item: " + key); 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) { 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) { if (material == null) {
P.p.log(Level.WARNING, "Invalid material for dummy item: " + id); P.p.log(Level.WARNING, "Invalid material for dummy item: " + id);
return null; return null;
@ -147,7 +169,7 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI {
DyeColor color; DyeColor color;
try { try {
color = DyeColor.valueOf(DUMMY_CONFIG.getString("color", "")); color = DyeColor.valueOf(dummySection.getString("color", ""));
} catch (Exception exception) { } catch (Exception exception) {
color = null; color = null;
} }
@ -157,10 +179,10 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI {
ItemMeta itemMeta = itemStack.getItemMeta(); ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', DUMMY_CONFIG.getString("name", " "))); itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', dummySection.getString("name", " ")));
List<String> lore = new ArrayList<>(); List<String> lore = new ArrayList<>();
for (String loreLine : DUMMY_CONFIG.getStringList("lore")) { for (String loreLine : dummySection.getStringList("lore")) {
lore.add(ChatColor.translateAlternateColorCodes('&', loreLine)); lore.add(ChatColor.translateAlternateColorCodes('&', loreLine));
} }
itemMeta.setLore(lore); itemMeta.setLore(lore);

View File

@ -6,6 +6,8 @@ public interface PermissionGUI {
public void onClick(int slot, ClickType action); public void onClick(int slot, ClickType action);
public void build();
public enum SpecialItem { public enum SpecialItem {
BACK, BACK,
RELATION; RELATION;