Skip to content

Commit 40ccb70

Browse files
committed
Fixing some NPEs in the menu builder, altering some length title messages because of Bukkits 32 char limit.
Adding some logging for exceptional failure states brought about by bad config
1 parent 8c69355 commit 40ccb70

2 files changed

Lines changed: 54 additions & 36 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target/
2+
.classpath
3+
.settings/**
4+
.project

src/com/github/igotyou/FactoryMod/utility/MenuBuilder.java

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void openFactoryBrowser(Player p, String startingFac) {
9696
}
9797
}
9898
if (egg == null) {
99+
FactoryMod.getPlugin().warning("There is no factory with name " + comp);
99100
p.sendMessage(ChatColor.RED
100101
+ "There is no factory with the name you entered");
101102
return;
@@ -109,8 +110,8 @@ public void openFactoryBrowser(Player p, String startingFac) {
109110
// creation option
110111
ItemStack creationStack = new ItemStack(Material.CHEST);
111112
ISUtils.setName(creationStack, "Setup");
112-
ISUtils.addLore(creationStack, ChatColor.LIGHT_PURPLE
113-
+ "Click to display more information",
113+
ISUtils.addLore(creationStack,
114+
ChatColor.LIGHT_PURPLE + "Click to display more information",
114115
ChatColor.LIGHT_PURPLE + "on how to setup this factory");
115116
Clickable creationClickable = new Clickable(creationStack) {
116117
@Override
@@ -139,10 +140,9 @@ public void clicked(Player arg0) {
139140
// upgrade option
140141
ItemStack upgradeStack = new ItemStack(Material.FURNACE);
141142
ISUtils.setName(upgradeStack, "Upgrades");
142-
ISUtils.addLore(upgradeStack, ChatColor.LIGHT_PURPLE
143-
+ "Click to display more information about",
144-
ChatColor.LIGHT_PURPLE
145-
+ "the possible upgrades to this factory");
143+
ISUtils.addLore(upgradeStack,
144+
ChatColor.LIGHT_PURPLE + "Click to display more information about",
145+
ChatColor.LIGHT_PURPLE + "the possible upgrades to this factory");
146146
Clickable upgradeClickable = new Clickable(upgradeStack) {
147147
@Override
148148
public void clicked(Player arg0) {
@@ -159,12 +159,16 @@ public void clicked(Player arg0) {
159159
private void openRecipeBrowser(Player p, String facName) {
160160
ClickableInventory.forceCloseInventory(p);
161161
ClickableInventory recipeInventory = new ClickableInventory(36,
162-
"All recipes for " + facName);
162+
"Recipes for " + facName); // Bukkit has 32 char limit on inventory
163163
FurnCraftChestEgg egg = (FurnCraftChestEgg) manager.getEgg(facName);
164164
List<IRecipe> recipes = egg.getRecipes();
165165

166166
// put recipes
167+
int j = 0;
167168
for (int i = 0; i < recipes.size(); i++) {
169+
if (recipes.get(i) == null) {
170+
continue;
171+
}
168172
Clickable c = new Clickable(
169173
((InputRecipe) recipes.get(i)).getRecipeRepresentation()) {
170174
@Override
@@ -173,14 +177,14 @@ public void clicked(Player arg0) {
173177
ISUtils.getName(this.getItemStack()));
174178
}
175179
};
176-
recipeInventory.setSlot(c, i);
180+
recipeInventory.setSlot(c, j++);
177181
}
178182

179183
// back option
180184
ItemStack backStack = new ItemStack(Material.ARROW);
181185
ISUtils.setName(backStack, "Back to factory overview");
182-
ISUtils.addLore(backStack, ChatColor.LIGHT_PURPLE
183-
+ "Click to go back");
186+
ISUtils.addLore(backStack,
187+
ChatColor.LIGHT_PURPLE + "Click to go back");
184188
Clickable backClickable = new Clickable(backStack) {
185189
@Override
186190
public void clicked(Player arg0) {
@@ -197,21 +201,19 @@ private void openSetupBrowser(Player p, String facName) {
197201
FurnCraftChestEgg egg = (FurnCraftChestEgg) manager.getEgg(facName);
198202
FurnCraftChestEgg parEgg = (FurnCraftChestEgg) manager
199203
.getEgg(parentFactories.get(facName));
200-
ClickableInventory ci = new ClickableInventory(54, "How to get a "
201-
+ egg.getName());
204+
ClickableInventory ci = new ClickableInventory(54, "Create a "
205+
+ egg.getName()); // Bukkit has 32 char limit on inventory
202206
ItemStack cr = new ItemStack(Material.WORKBENCH);
203207
ItemStack fur = new ItemStack(Material.FURNACE);
204208
ItemStack che = new ItemStack(Material.CHEST);
205209
if (parEgg == null) {// creation factory
206-
ISUtils.setLore(cr, ChatColor.LIGHT_PURPLE
207-
+ "This factory can be created with",
208-
ChatColor.LIGHT_PURPLE
209-
+ "a normal crafting table, furnace and chest");
210-
ISUtils.setLore(che, ChatColor.LIGHT_PURPLE
211-
+ "Arrange the 3 blocks like this,", ChatColor.LIGHT_PURPLE
212-
+ "put the materials below in the chest",
213-
ChatColor.LIGHT_PURPLE
214-
+ "and hit the craftingtable with a stick");
210+
ISUtils.setLore(cr,
211+
ChatColor.LIGHT_PURPLE + "This factory can be created with",
212+
ChatColor.LIGHT_PURPLE + "a normal crafting table, furnace and chest");
213+
ISUtils.setLore(che,
214+
ChatColor.LIGHT_PURPLE + "Arrange the 3 blocks like this,",
215+
ChatColor.LIGHT_PURPLE + "put the materials below in the chest",
216+
ChatColor.LIGHT_PURPLE + "and hit the crafting table with a stick");
215217
DecorationStack furnDec = new DecorationStack(fur);
216218
DecorationStack chestDec = new DecorationStack(che);
217219
DecorationStack craStack = new DecorationStack(cr);
@@ -256,8 +258,9 @@ public void clicked(Player arg0) {
256258
}
257259
};
258260
ci.setSlot(craCli, 4);
259-
ISUtils.setLore(fur, ChatColor.LIGHT_PURPLE
260-
+ "Click to display information", ChatColor.LIGHT_PURPLE + "on this factory");
261+
ISUtils.setLore(fur,
262+
ChatColor.LIGHT_PURPLE + "Click to display information",
263+
ChatColor.LIGHT_PURPLE + "on this factory");
261264
Clickable furCli = new Clickable(fur) {
262265
@Override
263266
public void clicked(Player arg0) {
@@ -296,8 +299,7 @@ public void clicked(Player arg0) {
296299
ci.setSlot(input, 22);
297300
ItemStack backStack = new ItemStack(Material.ARROW);
298301
ISUtils.setName(backStack, "Back to factory overview");
299-
ISUtils.addLore(backStack, ChatColor.LIGHT_PURPLE
300-
+ "Click to go back");
302+
ISUtils.addLore(backStack, ChatColor.LIGHT_PURPLE + "Click to go back");
301303
Clickable backClickable = new Clickable(backStack) {
302304
@Override
303305
public void clicked(Player arg0) {
@@ -312,18 +314,18 @@ private void openUpgradeBrowser(Player p, String facName) {
312314
ClickableInventory.forceCloseInventory(p);
313315
FurnCraftChestEgg egg = (FurnCraftChestEgg) manager
314316
.getEgg(factoryViewed.get(p.getUniqueId()));
315-
ClickableInventory ci = new ClickableInventory(18, "Possible upgrades");
316317
List<IRecipe> upgrades = new LinkedList<IRecipe>();
317318
for (IRecipe recipe : egg.getRecipes()) {
318319
if (recipe instanceof Upgraderecipe) {
319320
upgrades.add(recipe);
320321
}
321322
}
323+
ClickableInventory ci = new ClickableInventory(
324+
Math.max(18, (upgrades.size() / 9) * 9), "Possible upgrades");
322325
if (upgrades.size() == 0) {
323326
ItemStack bar = new ItemStack(Material.BARRIER);
324327
ISUtils.setName(bar, "No upgrades available");
325-
ISUtils.addLore(bar, ChatColor.LIGHT_PURPLE
326-
+ "Click to go back");
328+
ISUtils.addLore(bar, ChatColor.LIGHT_PURPLE + "Click to go back");
327329
Clickable noUpgrades = new Clickable(bar) {
328330
@Override
329331
public void clicked(Player p) {
@@ -335,8 +337,8 @@ public void clicked(Player p) {
335337
for (IRecipe recipe : upgrades) {
336338
ItemStack recStack = ((InputRecipe) recipe)
337339
.getRecipeRepresentation();
338-
ISUtils.setLore(recStack, ChatColor.LIGHT_PURPLE
339-
+ "Click to display more information");
340+
ISUtils.setLore(recStack,
341+
ChatColor.LIGHT_PURPLE + "Click to display more information");
340342
Clickable c = new Clickable(
341343
((InputRecipe) recipe).getRecipeRepresentation()) {
342344
@Override
@@ -350,8 +352,7 @@ public void clicked(Player p) {
350352
}
351353
ItemStack backStack = new ItemStack(Material.ARROW);
352354
ISUtils.setName(backStack, "Back to factory overview");
353-
ISUtils.addLore(backStack, ChatColor.LIGHT_PURPLE
354-
+ "Click to go back");
355+
ISUtils.addLore(backStack, ChatColor.LIGHT_PURPLE + "Click to go back");
355356
Clickable backClickable = new Clickable(backStack) {
356357
@Override
357358
public void clicked(Player arg0) {
@@ -364,15 +365,30 @@ public void clicked(Player arg0) {
364365

365366
private void openDetailedRecipeBrowser(Player p, String recipeName) {
366367
ClickableInventory.forceCloseInventory(p);
368+
if (recipeName == null){
369+
FactoryMod.getPlugin().warning("Recipe name cannot be null in openDetailedRecipeBrowser calls");
370+
return;
371+
}
367372
FurnCraftChestEgg egg = (FurnCraftChestEgg) manager
368373
.getEgg(factoryViewed.get(p.getUniqueId()));
369374
InputRecipe rec = null;
370375
for (IRecipe recipe : egg.getRecipes()) {
371-
if (recipe.getRecipeName().equals(recipeName)) {
376+
if (recipe == null || recipe.getRecipeName() == null) {
377+
FactoryMod.getPlugin().warning("Null recipe or recipe name registered with "+
378+
egg.getName());
379+
continue;
380+
}
381+
if (recipeName.equals(recipe.getRecipeName())) {
372382
rec = (InputRecipe) recipe;
373383
break;
374384
}
375385
}
386+
if (rec == null) {
387+
FactoryMod.getPlugin().warning("There is no recipe with name " + recipeName);
388+
p.sendMessage(ChatColor.RED
389+
+ "There is no recipe that matches " + recipeName);
390+
return;
391+
}
376392
ClickableInventory ci = new ClickableInventory(54, recipeName);
377393
ItemStack inputStack = new ItemStack(Material.PAPER);
378394
ISUtils.setName(inputStack, "Input materials");
@@ -517,8 +533,7 @@ public void showSorterFace(Player p, Sorter s, BlockFace face) {
517533
ClickableInventory ci = new ClickableInventory(54,
518534
"Items for this side");
519535
viewed.put(p.getUniqueId(), s);
520-
for (ItemStack is : s.getItemsForSide(face)
521-
.getItemStackRepresentation()) {
536+
for (ItemStack is : s.getItemsForSide(face).getItemStackRepresentation()) {
522537
is.setAmount(1);
523538
ci.addSlot(new Clickable(is) {
524539
@Override
@@ -531,7 +546,6 @@ public void clicked(Player arg0) {
531546
});
532547
}
533548
ci.showInventory(p);
534-
535549
}
536550

537551
}

0 commit comments

Comments
 (0)