Skip to content

Commit

Permalink
Fix #2276 No categories displaying if Apiculture is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedelosk committed Nov 3, 2018
1 parent d9ec3eb commit 6b16fbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BookCategoryDeserializer implements JsonDeserializer<BookCategory>
public BookCategory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
JsonObject object = json.getAsJsonObject();
String name = JsonUtils.getString(object, "name");
ItemStack stack = JsonUtil.deserializeItemStack(JsonUtils.getJsonObject(object, "icon"));
ItemStack stack = JsonUtil.deserializeItemStack(JsonUtils.getJsonObject(object, "icon"), ItemStack.EMPTY);
BookCategory category = new BookCategory(name);
category.setStack(stack);
return category;
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/forestry/core/utils/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import javax.annotation.Nullable;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

Expand All @@ -14,22 +12,28 @@ public class JsonUtil {
private JsonUtil() {
}

public static ItemStack deserializeItemStack(JsonObject object){
return deserializeItemStack(object, null);
public static ItemStack deserializeItemStack(JsonObject object, ItemStack fallback) {
return deserializeItemStack(object, fallback, false);
}

public static ItemStack deserializeItemStack(JsonObject object, @Nullable ItemStack fallback) {
public static ItemStack deserializeItemStack(JsonObject object, ItemStack fallback, boolean logError) {
if (!object.has("item")) {
if(fallback == null) {
throw new JsonSyntaxException("Unsupported icon type, currently only items are supported (add 'item' key)");
}else{
return fallback;
if(logError) {
Log.error("Unsupported icon type, currently only items are supported (add 'item' key)");
}
return fallback;
}
try {
Item item = net.minecraft.util.JsonUtils.getItem(object, "item");
int meta = net.minecraft.util.JsonUtils.getInt(object, "data", 0);
ItemStack stack = new ItemStack(item, 1, meta);
stack.setTagCompound(JsonUtils.readNBT(object, "nbt"));
return stack;
}catch (JsonSyntaxException e){
if(logError) {
Log.trace("Filed to parse item.", e);
}
return fallback;
}
Item item = net.minecraft.util.JsonUtils.getItem(object, "item");
int meta = net.minecraft.util.JsonUtils.getInt(object, "data", 0);
ItemStack stack = new ItemStack(item, 1, meta);
stack.setTagCompound(JsonUtils.readNBT(object, "nbt"));
return stack;
}
}

0 comments on commit 6b16fbb

Please sign in to comment.