diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java index 54f711d..a865cb8 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/IguanaPartRestriction.java @@ -15,6 +15,9 @@ public class IguanaPartRestriction { @Handler public void postInit(FMLPostInitializationEvent event) { + // init the helper + RestrictionHelper.initPatternParts(); + 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 50bfe3a..29243e0 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/PartRestrictionHandler.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/PartRestrictionHandler.java @@ -5,11 +5,19 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import iguanaman.iguanatweakstconstruct.reference.Config; import iguanaman.iguanatweakstconstruct.restriction.RestrictionHelper; 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.PatternBuilder; import tconstruct.library.event.PartBuilderEvent; +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 java.util.List; import java.util.Set; public class PartRestrictionHandler { @@ -24,17 +32,8 @@ public class PartRestrictionHandler { return; int matID = set.materialID; - // get the part that should be built - //ItemStack part = PatternBuilder.instance.getMatchingPattern(event.pattern, event.material, set); - // find matching entry for the toolpart - String name = RestrictionHelper.getPatternName(event.pattern); - if(name == null || name.isEmpty()) - return; - - // check if it's restricted - Set restrictedMats = RestrictionConfig.restrictedParts.get(name); // guaranteed to exist - if(restrictedMats.contains(matID)) + if(RestrictionHelper.isRestricted(event.pattern, matID)) event.setResult(Event.Result.DENY); } @@ -44,12 +43,30 @@ public class PartRestrictionHandler { return; // we're only interested if it's a tool part - if (!(event.itemStack.getItem() instanceof IToolPart)) + if (!(event.itemStack.getItem() instanceof IPattern)) return; - // is restricted? - ItemStack stack = event.itemStack; - IToolPart part = (IToolPart)stack.getItem(); + boolean foundMat = false; + // regular materials + List materialIDs = RestrictionHelper.getPatternMaterials(event.itemStack); + if(materialIDs != null) { + for (Integer id : materialIDs) { + ToolMaterial mat = TConstructRegistry.getMaterial(id); + event.toolTip.add(mat.style() + mat.name()); + } + foundMat = true; + } + // custom materials + List materials = RestrictionHelper.getPatternCustomMaterials(event.itemStack); + if(materials != null) { + for (CustomMaterial mat : materials) { + event.toolTip.add(mat.input.getDisplayName()); + } + foundMat = true; + } + + if(!foundMat) + event.toolTip.add(EnumChatFormatting.DARK_RED + "No known Materials"); } } diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java index a080597..2eda216 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionConfig.java @@ -13,7 +13,7 @@ import java.util.Set; public class RestrictionConfig { private Configuration configfile; - public static Map> restrictedParts = new HashMap>(); +// public static Map> restrictedParts = new HashMap>(); public void init(File file) { configfile = new Configuration(file); @@ -26,6 +26,7 @@ public class RestrictionConfig { // todo: move this to an extra config and load it in postInit so everything is registered public void loadRestrictedParts() { + Log.info("Applying Tool Part restrictions"); configfile.setCategoryComment("Restrictions", "Tweak Module: Allows to blacklist certain things from being created."); // construct the comment containing all the info needed :I StringBuilder comment = new StringBuilder(); @@ -43,12 +44,9 @@ public class RestrictionConfig { // part names comment.append("partnames are: "); // patterns - for(String name : RestrictionHelper.patternNames) { + for(String name : RestrictionHelper.configNameToPattern.keySet()) { comment.append(name); comment.append(", "); - - // prepare map - restrictedParts.put(name, new HashSet()); } comment.append("all\n"); @@ -97,17 +95,14 @@ public class RestrictionConfig { // check if we have to add all if("all".equals(restriction[1])) { - for(String name : RestrictionHelper.patternNames) - restrictedParts.get(name).add(matID); + for(RestrictionHelper.ItemMeta key : RestrictionHelper.configNameToPattern.values()) + RestrictionHelper.addRestriction(key, matID); + continue; } // check if valid part - valid = false; - for(String name : RestrictionHelper.patternNames) - if(name.equals(restriction[1])) { - valid = true; - } + valid = RestrictionHelper.configNameToPattern.keySet().contains(restriction[1]); if(!valid) { @@ -116,7 +111,7 @@ public class RestrictionConfig { } // add restriction :) - restrictedParts.get(restriction[1]).add(matID); + RestrictionHelper.addRestriction(RestrictionHelper.configNameToPattern.get(restriction[1]), matID); } } } diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java index 6f17d9a..7cb132c 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/restriction/RestrictionHelper.java @@ -1,19 +1,198 @@ package iguanaman.iguanatweakstconstruct.restriction; +import cpw.mods.fml.common.registry.GameRegistry; +import iguanaman.iguanatweakstconstruct.util.Log; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import tconstruct.library.TConstructRegistry; +import tconstruct.library.crafting.PatternBuilder; +import tconstruct.library.crafting.ToolBuilder; +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.tools.TinkerTools; +import tconstruct.tools.items.Pattern; + +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 String getPatternName(ItemStack pattern) - { - if(pattern.getItem() == TinkerTools.woodPattern) { - if (pattern.getItemDamage() > patternNames.length) - return ""; + 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> patternMaterialLookup; // item+metadata -> List of applicable materials + public static Map> patternCustomMaterialLookup; // item+metadata -> List of applicable custom materials - return patternNames[pattern.getItemDamage()]; + static { + configNameToPattern = new HashMap(); + patternMaterialLookup = new HashMap>(); + patternCustomMaterialLookup = new HashMap>(); + } + + + public static boolean isRestricted(ItemStack pattern, Integer matID) + { + boolean restricted = true; + List matIDs = patternMaterialLookup.get(new ItemMeta(pattern)); + if(matIDs != null) + { + for(Integer mat : matIDs) + if(mat.equals(matID)) { + restricted = false; + break; + } + } + + return restricted; + } + + public static int getPatternIndexFromName(String name) + { + for(int i = 0; i < patternNames.length; i++) + if(patternNames[i].equals(name)) + return i; + + return -1; + } + + public static List getPatternMaterials(ItemStack pattern) + { + return patternMaterialLookup.get(new ItemMeta(pattern)); + } + + public static List getPatternCustomMaterials(ItemStack pattern) + { + return patternCustomMaterialLookup.get(new ItemMeta(pattern)); + } + + public static void addRestriction(ItemMeta key, Integer materialID) + { + // fetch the material list + 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; + } + + // find the entry and remove it + ListIterator iter = materials.listIterator(); + while(iter.hasNext()) + { + Integer id = iter.next(); + if(id.equals(materialID)) // equals because Integer, not int + { + iter.remove(); + } + } + } + + public static void initPatternParts() + { + Log.info("Loading tool pattern combinations"); + // cycle through ALL combinations + for(Map.Entry entry : TConstructRegistry.patternPartMapping.entrySet()) + { + Item pattern = (Item)entry.getKey().get(0); // the pattern + Integer meta = (Integer)entry.getKey().get(1); // metadata of the pattern + Integer matID = (Integer)entry.getKey().get(2); // Material-ID of the material needed to craft + + String name; + if(pattern == TinkerTools.woodPattern && meta <= patternNames.length) + name = patternNames[meta]; + else + name = (new ItemStack(pattern, 1, meta)).getUnlocalizedName(); + + ItemMeta im = configNameToPattern.get(name); + // not registered in the mapping yet? + if(im == null) { + im = new ItemMeta(pattern, meta); + configNameToPattern.put(name, im); + } + + // add material + if(!patternMaterialLookup.containsKey(im)) + patternMaterialLookup.put(im, new LinkedList()); + + patternMaterialLookup.get(im).add(matID); + } + + // bowstring and fletchling are treated differently + for(Integer matID : TConstructRegistry.toolMaterials.keySet()) + { + // bowstring + CustomMaterial mat = TConstructRegistry.getCustomMaterial(matID, BowstringMaterial.class); + if(mat != null) + { + ItemMeta im = configNameToPattern.get("bowstring"); + // not registered in the mapping yet? + if(im == null) { + im = new ItemMeta(TinkerTools.woodPattern, 23); + configNameToPattern.put("bowstring", im); + } + + // add material + if(!patternCustomMaterialLookup.containsKey(im)) + patternCustomMaterialLookup.put(im, new LinkedList()); + + patternCustomMaterialLookup.get(im).add(mat); + } + + // fletchling + mat = TConstructRegistry.getCustomMaterial(matID, FletchingMaterial.class); + if(mat != null) + { + ItemMeta im = configNameToPattern.get("fletching"); + // not registered in the mapping yet? + if(im == null) { + im = new ItemMeta(TinkerTools.woodPattern, 24); + configNameToPattern.put("fletching", im); + } + + // add material + if(!patternCustomMaterialLookup.containsKey(im)) + patternCustomMaterialLookup.put(im, new LinkedList()); + + patternCustomMaterialLookup.get(im).add(mat); + } + } + } + + // item + metadata combination + public static class ItemMeta { + public Item item; + public Integer meta; + + public ItemMeta(Item item, Integer meta) { + this.item = item; + this.meta = meta; + } + + public ItemMeta(ItemStack stack) + { + this.item = stack.getItem(); + this.meta = stack.getItemDamage(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ItemMeta itemMeta = (ItemMeta) o; + + if (item != null ? !item.equals(itemMeta.item) : itemMeta.item != null) return false; + if (meta != null ? !meta.equals(itemMeta.meta) : itemMeta.meta != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = item != null ? item.hashCode() : 0; + result = 31 * result + (meta != null ? meta.hashCode() : 0); + return result; } - return null; } // the tool parts