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
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;
}

View File

@ -33,23 +33,33 @@ public class PermissableActionGUI implements InventoryHolder, PermissionGUI {
private HashMap<Integer, SpecialItem> specialSlots = new HashMap<>();
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) {
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<Integer> dummySlots = ACTION_CONFIG.getIntegerList("dummy-items." + key);
List<Integer> 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<String> lore = new ArrayList<>();
for (String loreLine : DUMMY_CONFIG.getStringList("lore")) {
for (String loreLine : dummySection.getStringList("lore")) {
lore.add(ChatColor.translateAlternateColorCodes('&', loreLine));
}
itemMeta.setLore(lore);

View File

@ -31,24 +31,34 @@ public class PermissableRelationGUI implements InventoryHolder, PermissionGUI {
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) {
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<Integer> dummySlots = RELATION_CONFIG.getIntegerList("dummy-items." + key);
List<Integer> 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<String> lore = new ArrayList<>();
for (String loreLine : DUMMY_CONFIG.getStringList("lore")) {
for (String loreLine : dummySection.getStringList("lore")) {
lore.add(ChatColor.translateAlternateColorCodes('&', loreLine));
}
itemMeta.setLore(lore);

View File

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