diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java index 0ec2d2a..deb39f2 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java @@ -22,7 +22,7 @@ public class IguanaPartRestriction { public void postInit(FMLPostInitializationEvent event) { // init the helper - RestrictionHelper.initPatternParts(); + RestrictionHelper.init(); RestrictionConfig config = new RestrictionConfig(); config.init(new File(IguanaTweaksTConstruct.configPath, "restrictions.cfg")); diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/PartRestrictionHandler.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/PartRestrictionHandler.java index fc9c251..827d33a 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/PartRestrictionHandler.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/PartRestrictionHandler.java @@ -9,14 +9,17 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.event.entity.player.ItemTooltipEvent; import tconstruct.library.TConstructRegistry; +import tconstruct.library.crafting.CastingRecipe; import tconstruct.library.crafting.PatternBuilder; import tconstruct.library.event.PartBuilderEvent; +import tconstruct.library.event.SmelteryCastEvent; import tconstruct.library.tools.BowstringMaterial; import tconstruct.library.tools.CustomMaterial; import tconstruct.library.tools.FletchingMaterial; import tconstruct.library.tools.ToolMaterial; import tconstruct.library.util.IPattern; import tconstruct.library.util.IToolPart; +import tconstruct.tools.items.ToolPart; import java.util.List; import java.util.Set; @@ -38,6 +41,24 @@ public class PartRestrictionHandler { event.setResult(Event.Result.DENY); } + @SubscribeEvent + public void onPartCasting(SmelteryCastEvent.CastingTable event) + { + // null checks + if(event.recipe == null || event.recipe.output == null) + return; + CastingRecipe recipe = event.recipe; + ItemStack output = recipe.output; + + if(output.getItem() == null || !(output.getItem() instanceof IToolPart)) + return; + + IToolPart part = (IToolPart)output.getItem(); + ToolMaterial mat = TConstructRegistry.getMaterial(part.getMaterialID(output)); + if(RestrictionHelper.isRestricted(event.recipe.cast, mat)) + event.setResult(Event.Result.DENY); + } + @SubscribeEvent public void onItemToolTip(ItemTooltipEvent event) { if (event.entityPlayer == null) diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java index ea44631..851012f 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java @@ -83,21 +83,27 @@ public class RestrictionConfig { { for(RestrictionHelper.ItemMeta key : RestrictionHelper.configNameToPattern.values()) RestrictionHelper.addRestriction(key, material); + for(RestrictionHelper.ItemMeta key : RestrictionHelper.configNameToCast.values()) + RestrictionHelper.addRestriction(key, material); continue; } // check if valid part - boolean valid = RestrictionHelper.configNameToPattern.keySet().contains(restriction[1]); + boolean validPattern = RestrictionHelper.configNameToPattern.keySet().contains(restriction[1]); + boolean validCast = RestrictionHelper.configNameToCast.keySet().contains(restriction[1]); - if(!valid) + if(!validPattern && !validCast) { Log.error(String.format("Found invalid part %s in restriction entry: %s", restriction[1], str)); continue; } // add restriction :) - RestrictionHelper.addRestriction(RestrictionHelper.configNameToPattern.get(restriction[1]), material); + if(validPattern) + RestrictionHelper.addRestriction(RestrictionHelper.configNameToPattern.get(restriction[1]), material); + if(validCast) + RestrictionHelper.addRestriction(RestrictionHelper.configNameToCast.get(restriction[1]), material); } } @@ -138,11 +144,12 @@ public class RestrictionConfig { } // check if valid part - boolean valid = RestrictionHelper.configNameToPattern.keySet().contains(restriction[1]); + boolean validPattern = RestrictionHelper.configNameToPattern.keySet().contains(restriction[1]); + boolean validCast = RestrictionHelper.configNameToCast.keySet().contains(restriction[1]); - if(!valid) + if(!validPattern && !validCast) { - Log.error(String.format("Found invalid part %s in restriction entry: %s", restriction[1], str)); + Log.error(String.format("Found invalid part %s in allow-entry: %s", restriction[1], str)); continue; } @@ -152,8 +159,16 @@ public class RestrictionConfig { RestrictionHelper.clearRecipes(material); memory.add(material); } + + boolean success = false; // then allow it - RestrictionHelper.addAllowed(RestrictionHelper.configNameToPattern.get(restriction[1]), material); + if(validPattern) + success |= RestrictionHelper.addAllowed(RestrictionHelper.configNameToPattern.get(restriction[1]), material); + if(validCast) + success |= RestrictionHelper.addAllowed(RestrictionHelper.configNameToCast.get(restriction[1]), material); + + if(!success) + Log.error(String.format("You're trying to allow an invalid recipe: %s", str)); } } } diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java index 03b65ed..dc433bf 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java @@ -6,6 +6,7 @@ import net.minecraft.block.material.Material; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import tconstruct.library.TConstructRegistry; +import tconstruct.library.crafting.CastingRecipe; import tconstruct.library.crafting.PatternBuilder; import tconstruct.library.crafting.ToolBuilder; import tconstruct.library.tools.BowstringMaterial; @@ -13,6 +14,9 @@ import tconstruct.library.tools.CustomMaterial; import tconstruct.library.tools.FletchingMaterial; import tconstruct.library.tools.ToolMaterial; import tconstruct.library.util.IPattern; +import tconstruct.library.util.IToolPart; +import tconstruct.smeltery.TinkerSmeltery; +import tconstruct.smeltery.items.MetalPattern; import tconstruct.tools.TinkerTools; import tconstruct.tools.items.Pattern; @@ -21,12 +25,14 @@ import java.util.*; // todo: refactor this properly with a Map or something when i need to restrict more than vanilla public abstract class RestrictionHelper { public static Map configNameToPattern; // holds the names that can be used in the config and maps them to item-meta combinations to retrieve the materials + public static Map configNameToCast; // same as above but for metal casts // this list contains all ALLOWED pattern - material combinations public static Map> patternMaterialLookup; // item+metadata -> List of applicable materials public static Map> patternCustomMaterialLookup; // item+metadata -> List of applicable custom materials static { configNameToPattern = new HashMap(); + configNameToCast = new HashMap(); patternMaterialLookup = new HashMap>(); patternCustomMaterialLookup = new HashMap>(); } @@ -105,14 +111,55 @@ public abstract class RestrictionHelper { } } - public static void addAllowed(ItemMeta key, ToolMaterial material) + // Don't judge me. This function is an absolute terror because I realized that it'd allow invalid combinations if I + // don't check everything again too late. Should probbaly simply create a set of originally allowed parts, and than + // build the overlapping set. + public static boolean addAllowed(ItemMeta key, ToolMaterial material) { - // fetch the material list + // check if material was allowed for casting + if(key.item instanceof MetalPattern) { + boolean allowed = false; + for (CastingRecipe recipe : TConstructRegistry.getTableCasting().getCastingRecipes()) { + // only recipes with cast + if (recipe.cast == null || recipe.output == null) + continue; + // we're only interested in toolparts, not making the casts themselves etc. or buckets. + if (!(recipe.output.getItem() instanceof IToolPart)) + continue; + + int matID = ((IToolPart)recipe.output.getItem()).getMaterialID(recipe.output); + + if (key.item == recipe.cast.getItem() && key.meta == recipe.cast.getItemDamage() && TConstructRegistry.getMaterial(matID) == material) { + allowed = true; + break; + } + } + // nope. we don't want wood as valid casting material! + if(!allowed) + return false; + } + // wooden pattern? + else if(key.item instanceof Pattern) + { + boolean allowed = false; + for(List entry : TConstructRegistry.patternPartMapping.keySet()) { + Item pattern = (Item) entry.get(0); // the pattern + int meta = (Integer) entry.get(1); // metadata of the pattern + int matID = (Integer)entry.get(2); // Material-ID of the material needed to craft + + if (key.item == pattern && key.meta == meta && TConstructRegistry.getMaterial(matID) == material) { + allowed = true; + break; + } + } + if(!allowed) + return false; + } List materials = patternMaterialLookup.get(key); if(materials == null) { Log.debug(String.format("Couldn't find lookup entry for %s:%d", key.item.getUnlocalizedName(), key.meta)); - return; + return false; } // find the entry so we don't have a double entry @@ -122,15 +169,24 @@ public abstract class RestrictionHelper { ToolMaterial mat = iter.next(); if(mat == material) { - return; + // duplicate + return true; } } // item is not in list. add it materials.add(material); + + return true; } - public static void initPatternParts() + public static void init() + { + initPatternParts(); + initCastParts(); + } + + private static void initPatternParts() { Log.info("Loading tool pattern combinations"); // cycle through ALL combinations @@ -201,6 +257,44 @@ public abstract class RestrictionHelper { } } + private static void initCastParts() + { + Log.info("Loading tool casting combinations"); + // cycle through ALL combinations + for(CastingRecipe recipe : TConstructRegistry.getTableCasting().getCastingRecipes()) + { + // only recipes with cast + if(recipe.cast == null || recipe.output == null) + continue; + // we're only interested in toolparts, not making the casts themselves etc. or buckets. + if(!(recipe.output.getItem() instanceof IToolPart)) + continue; + + Item pattern = recipe.cast.getItem(); + int meta = recipe.cast.getItemDamage(); + int matID = ((IToolPart)recipe.output.getItem()).getMaterialID(recipe.output); + + String name; + if(pattern == TinkerSmeltery.metalPattern && meta <= patternNames.length) + name = patternNames[meta]; + else + name = (new ItemStack(pattern, 1, meta)).getUnlocalizedName(); + + ItemMeta im = configNameToCast.get(name); + // not registered in the mapping yet? + if(im == null) { + im = new ItemMeta(pattern, meta); + configNameToCast.put(name, im); + } + + // add material + if(!patternMaterialLookup.containsKey(im)) + patternMaterialLookup.put(im, new LinkedList()); + + patternMaterialLookup.get(im).add(TConstructRegistry.getMaterial(matID)); + } + } + // the whole purpose of this is so that each tooltip has the same order >_< public static void sortEntries() { @@ -251,7 +345,7 @@ public abstract class RestrictionHelper { ItemMeta itemMeta = (ItemMeta) o; - if (item != null ? !item.equals(itemMeta.item) : itemMeta.item != null) return false; + if (item != null ? !(item == itemMeta.item) : itemMeta.item != null) return false; if (meta != null ? !meta.equals(itemMeta.meta) : itemMeta.meta != null) return false; return true;