Fix #531 Combine config item parsing for backpack and flowers

This commit is contained in:
mezz committed 2015-03-01 01:04:07 -08:00
1 parent 90b13eac1d
commit 7362286ead
3 files changed
+90 -97

No files matched your search

@@ -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<ItemStack> 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<ItemStack> flowers = StackUtils.parseItemStackStrings(list);
target.addAll(flowers);
}
private void parseBeeBlacklist(String list) {
@@ -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 <name>, <name>:<meta> or <name>:*, 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<ItemStack> backpackItems = StackUtils.parseItemStackStrings(list);
target.addValidItems(backpackItems);
}
@Override