From 7362286ead4c13700a4050f8ed22893bfced47fd Mon Sep 17 00:00:00 2001 From: mezz Date: Sun, 1 Mar 2015 01:04:07 -0800 Subject: [PATCH] Fix #531 Combine config item parsing for backpack and flowers --- .../java/forestry/core/utils/StackUtils.java | 68 ++++++++++++++++ .../forestry/plugins/PluginApiculture.java | 39 ++------- .../java/forestry/plugins/PluginStorage.java | 80 ++++--------------- 3 files changed, 90 insertions(+), 97 deletions(-) diff --git a/src/main/java/forestry/core/utils/StackUtils.java b/src/main/java/forestry/core/utils/StackUtils.java index 5cd7ff12d..c4f8ef25c 100755 --- a/src/main/java/forestry/core/utils/StackUtils.java +++ b/src/main/java/forestry/core/utils/StackUtils.java @@ -10,8 +10,11 @@ ******************************************************************************/ package forestry.core.utils; +import java.text.NumberFormat; +import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Random; import net.minecraft.block.Block; @@ -28,8 +31,11 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; import net.minecraftforge.oredict.OreDictionary; +import cpw.mods.fml.common.registry.GameData; + import forestry.core.config.Defaults; import forestry.core.inventory.InvTools; +import forestry.core.proxy.Proxies; public class StackUtils { @@ -460,4 +466,66 @@ public class StackUtils { public static boolean equals(Block block, int meta, ItemStack stack) { return block == getBlock(stack) && meta == stack.getItemDamage(); } + + public static List parseItemStackStrings(String itemStackStrings) { + String[] parts = itemStackStrings.split("(\\s*;\\s*)+"); + + List itemStacks = new ArrayList(); + + for (String itemStackString : parts) { + ItemStack itemStack = StackUtils.parseItemStackString(itemStackString); + if (itemStack != null) { + itemStacks.add(itemStack); + } + } + + return itemStacks; + } + + public static ItemStack parseItemStackString(String itemStackString) { + itemStackString = itemStackString.trim(); + if (itemStackString.isEmpty()) { + return null; + } + + String[] parts = itemStackString.split(":+"); + + if (parts.length != 2 && parts.length != 3) { + Proxies.log.warning("ItemStack string (" + itemStackString + ") isn't formatted properly. Suitable formats are :, :: or ::*, e.g. IC2:blockWall:*"); + return null; + } + + String name = parts[0] + ":" + parts[1]; + int meta; + + if (parts.length == 2) { + meta = 0; + } else { + try { + meta = parts[2].equals("*") ? OreDictionary.WILDCARD_VALUE : NumberFormat.getIntegerInstance().parse(parts[2]).intValue(); + } catch (ParseException e) { + Proxies.log.warning("ItemStack string (" + itemStackString + ") has improperly formatter metadata. Suitable metadata are integer values or *."); + return null; + } + } + + Item item = GameData.getItemRegistry().getRaw(name); + + if (item == null) { + Block block = GameData.getBlockRegistry().getRaw(name); + + if (block != null) { + item = Item.getItemFromBlock(block); + } + if (item == null) { + Proxies.log.warning("Failed to find (" + itemStackString + ") in the Forge item and block registries."); + return null; + } + } + ItemStack itemStack = new ItemStack(item, 1, meta); + + Proxies.log.finer("Parsed (" + itemStackString + ") into: " + itemStack); + + return itemStack; + } } diff --git a/src/main/java/forestry/plugins/PluginApiculture.java b/src/main/java/forestry/plugins/PluginApiculture.java index 18855d0cc..a49233198 100644 --- a/src/main/java/forestry/plugins/PluginApiculture.java +++ b/src/main/java/forestry/plugins/PluginApiculture.java @@ -11,6 +11,7 @@ package forestry.plugins; import java.util.ArrayList; +import java.util.List; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; @@ -19,7 +20,6 @@ import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.init.Blocks; import net.minecraft.init.Items; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.nbt.NBTTagCompound; @@ -155,6 +155,7 @@ import forestry.core.items.ItemScoop; import forestry.core.proxy.Proxies; import forestry.core.render.EntitySnowFX; import forestry.core.utils.ShapedRecipeCustom; +import forestry.core.utils.StackUtils; @Plugin(pluginID = "Apiculture", name = "Apiculture", author = "SirSengir", url = Defaults.URL, unlocalizedDescription = "for.plugin.apiculture.description") public class PluginApiculture extends ForestryPlugin { @@ -297,7 +298,7 @@ public class PluginApiculture extends ForestryPlugin { secondPrincessChance = Integer.parseInt(secondPrincess.value); property = apicultureConfig.get("beekeeping.flowers.custom", CONFIG_CATEGORY, ""); - property.comment = "add additional flower blocks for apiaries here in the format id:meta. separate blocks using ';'. will be treated like vanilla flowers. not recommended for flowers implemented as tile entities."; + property.comment = "add additional flower blocks for apiaries here in the format modid:name or modid:name:meta. separate blocks using ';'. wildcard for metadata: '*'. will be treated like vanilla flowers. not recommended for flowers implemented as tile entities."; parseAdditionalFlowers(property.value, FlowerManager.plainFlowers); property = apicultureConfig.get("species.blacklist", CONFIG_CATEGORY, ""); @@ -1215,38 +1216,8 @@ public class PluginApiculture extends ForestryPlugin { } private void parseAdditionalFlowers(String list, ArrayList target) { - String[] parts = list.split("[;]+"); - - for (String part : parts) { - if (part.isEmpty()) { - continue; - } - - String[] ident = part.split("[:]+"); - - if (ident.length != 1 && ident.length != 2) { - Proxies.log.warning("Failed to add flower of (" + part + ") to vanilla flower provider since it isn't formatted properly."); - continue; - } - - Item item = GameData.getItemRegistry().getRaw(ident[0]); - - if (item == null) { - Block block = GameData.getBlockRegistry().getRaw(ident[0]); - - if (block == null || block == Blocks.air || Item.getItemFromBlock(block) == null) { - Proxies.log.warning("Failed to add flower of (" + part + ") to vanilla flower provider since it couldn't be found."); - continue; - } - - item = Item.getItemFromBlock(block); - } - - int meta = ident.length > 1 ? Integer.parseInt(ident[1]) : 0; - - Proxies.log.finer("Adding flower of (" + part + ") to vanilla flower provider."); - target.add(new ItemStack(item, 1, meta)); - } + List flowers = StackUtils.parseItemStackStrings(list); + target.addAll(flowers); } private void parseBeeBlacklist(String list) { diff --git a/src/main/java/forestry/plugins/PluginStorage.java b/src/main/java/forestry/plugins/PluginStorage.java index 091a5bf4d..4d739a112 100755 --- a/src/main/java/forestry/plugins/PluginStorage.java +++ b/src/main/java/forestry/plugins/PluginStorage.java @@ -10,8 +10,6 @@ ******************************************************************************/ package forestry.plugins; -import java.text.NumberFormat; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -44,12 +42,10 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IShearable; -import net.minecraftforge.oredict.OreDictionary; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.network.IGuiHandler; -import cpw.mods.fml.common.registry.GameData; import forestry.api.core.Tabs; import forestry.api.recipes.RecipeManagers; @@ -70,6 +66,7 @@ import forestry.core.interfaces.ISaveEventHandler; import forestry.core.items.ItemCrated; import forestry.core.network.GuiId; import forestry.core.proxy.Proxies; +import forestry.core.utils.StackUtils; import forestry.storage.BackpackDefinition; import forestry.storage.BackpackHelper; import forestry.storage.CrateRegistry; @@ -121,23 +118,23 @@ public class PluginStorage extends ForestryPlugin implements IOreDictionaryHandl config = new Configuration(); Property backpackConf = config.get("backpacks.miner.items", CONFIG_CATEGORY, ""); - backpackConf.comment = "add additional blocks and items for the miner's backpack here in the format id:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; - parseBackpackItems("Miner's Backpack", backpackConf.value, BackpackManager.definitions.get("miner")); + backpackConf.comment = "add additional blocks and items for the miner's backpack here in the format modid:name:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; + parseBackpackItems(backpackConf.value, BackpackManager.definitions.get("miner")); backpackConf = config.get("backpacks.digger.items", CONFIG_CATEGORY, ""); - backpackConf.comment = "add additional blocks and items for the digger's backpack here in the format id:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; - parseBackpackItems("Digger's Backpack", backpackConf.value, BackpackManager.definitions.get("digger")); + backpackConf.comment = "add additional blocks and items for the digger's backpack here in the format modid:name:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; + parseBackpackItems(backpackConf.value, BackpackManager.definitions.get("digger")); backpackConf = config.get("backpacks.forester.items", CONFIG_CATEGORY, ""); - backpackConf.comment = "add additional blocks and items for the forester's backpack here in the format id:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; - parseBackpackItems("Forester's Backpack", backpackConf.value, BackpackManager.definitions.get("forester")); + backpackConf.comment = "add additional blocks and items for the forester's backpack here in the format modid:name:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; + parseBackpackItems(backpackConf.value, BackpackManager.definitions.get("forester")); backpackConf = config.get("backpacks.hunter.items", CONFIG_CATEGORY, ""); - backpackConf.comment = "add additional blocks and items for the hunter's backpack here in the format id:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; - parseBackpackItems("Hunter's Backpack", backpackConf.value, BackpackManager.definitions.get("hunter")); + backpackConf.comment = "add additional blocks and items for the hunter's backpack here in the format modid:name:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; + parseBackpackItems(backpackConf.value, BackpackManager.definitions.get("hunter")); backpackConf = config.get("backpacks.adventurer.items", CONFIG_CATEGORY, ""); - backpackConf.comment = "add blocks and items for the adventurer's backpack here in the format id:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; - parseBackpackItems("Adventurer's Backpack", backpackConf.value, BackpackManager.definitions.get("adventurer")); + backpackConf.comment = "add blocks and items for the adventurer's backpack here in the format modid:name:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; + parseBackpackItems(backpackConf.value, BackpackManager.definitions.get("adventurer")); backpackConf = config.get("backpacks.builder.items", CONFIG_CATEGORY, ""); - backpackConf.comment = "add blocks and items for the builder's backpack here in the format id:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; - parseBackpackItems("Builder's Backpack", backpackConf.value, BackpackManager.definitions.get("builder")); + backpackConf.comment = "add blocks and items for the builder's backpack here in the format modid:name:meta. separate blocks and items using ';'. wildcard for metadata: '*'"; + parseBackpackItems(backpackConf.value, BackpackManager.definitions.get("builder")); config.save(); @@ -249,7 +246,7 @@ public class PluginStorage extends ForestryPlugin implements IOreDictionaryHandl return true; } - parseBackpackItems(tokens[0] + "'s Backpack", tokens[1], BackpackManager.definitions.get(tokens[0])); + parseBackpackItems(tokens[1], BackpackManager.definitions.get(tokens[0])); return true; } @@ -568,52 +565,9 @@ public class PluginStorage extends ForestryPlugin implements IOreDictionaryHandl protected void registerCrates() { } - private static void parseBackpackItems(String backpackIdent, String list, IBackpackDefinition target) { - String[] parts = list.split("(\\s*;\\s*)+"); - - for (String part : parts) { - part = part.trim(); - if (part.isEmpty()) { - continue; - } - - String[] ident = part.split(":+"); - - if (ident.length != 2 && ident.length != 3) { - Proxies.log.warning("Failed to add block/item of (" + part + ") to " + backpackIdent + " since it isn't formatted properly. Suitable are , : or :*, e.g. IC2:blockWall:*."); - continue; - } - - String name = ident[0] + ":" + ident[1]; - int meta; - - if (ident.length == 2) { - meta = 0; - } else { - try { - meta = ident[2].equals("*") ? OreDictionary.WILDCARD_VALUE : NumberFormat.getIntegerInstance().parse(ident[2]).intValue(); - } catch (ParseException e) { - Proxies.log.warning("Failed to add block/item of (" + part + ") to " + backpackIdent + " since its metadata isn't formatted properly. Suitable are integer values or *."); - continue; - } - } - - Item item = GameData.getItemRegistry().getRaw(name); - - if (item == null) { - Block block = GameData.getBlockRegistry().getRaw(name); - - if (block == null || Item.getItemFromBlock(block) == null) { - Proxies.log.warning("Failed to add block/item of (" + part + ") to " + backpackIdent + " since it couldn't be found."); - continue; - } - - item = Item.getItemFromBlock(block); - } - - Proxies.log.finer("Adding block/item of (" + part + ") to " + backpackIdent + "."); - target.addValidItem(new ItemStack(item, 1, meta)); - } + private static void parseBackpackItems(String list, IBackpackDefinition target) { + List backpackItems = StackUtils.parseItemStackStrings(list); + target.addValidItems(backpackItems); } @Override