Implement Part Restrictions! \o/

This commit is contained in:
Bernhard Bonigl committed 2014-08-06 19:22:13 +02:00
1 parent 0e0aeb88bc
commit dc33ccd940
4 files changed
+87 -52

No files matched your search

@@ -4,6 +4,7 @@ import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import iguanaman.iguanatweakstconstruct.util.Log;
import iguanaman.iguanatweakstconstruct.util.RestrictionHelper;
import net.minecraft.item.Item;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
@@ -67,7 +68,7 @@ public class Config {
public static boolean removeStoneTorchRecipe;
public static boolean castsBurnMaterial;
public static boolean easyToolRepair;
public static Map<String, Set<ToolMaterial>> restrictedParts = new HashMap<String, Set<ToolMaterial>>();
public static Map<String, Set<Integer>> restrictedParts = new HashMap<String, Set<Integer>>();
// debug
public static boolean showDebugXP;
@@ -309,7 +310,7 @@ public class Config {
configfile.save();
}
private void loadRestrictedParts()
public void loadRestrictedParts()
{
configfile.setCategoryComment("Restrictions", "Tweak Module: Allows to blacklist certain things from being created.");
StringBuilder comment = new StringBuilder();
@@ -326,29 +327,25 @@ public class Config {
comment.append('\n');
// part names
comment.append("partnames are: ");
// wooden patterns
String[] patternNames = getPatternNames();
for(int i = 0; i < patternNames.length; i++) {
// don't add duplicates, since we have both wooden and metal patterns in here
if(restrictedParts.containsKey(patternNames[i]))
continue;
comment.append(patternNames[i]);
if(i < patternNames.length-1)
comment.append(", ");
// patterns
for(String name : RestrictionHelper.patternNames) {
comment.append(name);
comment.append(", ");
// prepare map
restrictedParts.put(patternNames[i], new HashSet<ToolMaterial>());
restrictedParts.put(name, new HashSet<Integer>());
}
comment.append('\n');
comment.append("all\n");
// load the actual config after creating this long information comment ._.
String[] input = configfile.getStringList("toolParts", "Restrictions", null, comment.toString());
String[] input = configfile.getStringList("toolParts", "Restrictions", new String[0], comment.toString());
// work through the entries
for(String str : input)
{
if(str.isEmpty())
continue;
String[] restriction = str.split(":");
String part = null;
ToolMaterial material = null;
if(restriction.length != 2)
@@ -371,12 +368,30 @@ public class Config {
continue;
}
// find material id
Integer matID = -1;
for(Map.Entry<Integer, ToolMaterial> entry : TConstructRegistry.toolMaterials.entrySet())
if(entry.getValue() == material)
matID = entry.getKey();
if(matID == -1)
{
Log.error(String.format("Couldn't find Material ID for %s", material.materialName));
}
// check if we have to add all
if("all".equals(restriction[1]))
{
for(String name : RestrictionHelper.patternNames)
restrictedParts.get(name).add(matID);
continue;
}
// check if valid part
valid = false;
for(String pattern : patternNames)
if(pattern.equals(restriction[1])) {
for(String name : RestrictionHelper.patternNames)
if(name.equals(restriction[1])) {
valid = true;
part = pattern;
}
if(!valid)
@@ -386,38 +401,10 @@ public class Config {
}
// add restriction :)
restrictedParts.get(part).add(material);
restrictedParts.get(restriction[1]).add(matID);
}
}
private String[] getPatternNames()
{
TConstructRegistry.patternPartMapping.keySet();
try {
Field names = Pattern.class.getDeclaredField("patternName");
names.setAccessible(true);
String[] p1 = (String[])names.get(null);
names = MetalPattern.class.getDeclaredField("patternName");
names.setAccessible(true);
String[] p2 = (String[])names.get(null);
String[] result = new String[p1.length + p2.length];
int i = 0;
for(String s : p1)
result[i++] = s;
for(String s : p2)
result[i++] = s;
return result;
} catch (NoSuchFieldException e) {
Log.error("Couldn't find Pattern names");
} catch (IllegalAccessException e) {
Log.error("Couldn't retrieve Pattern names");
}
return new String[0];
}
@SubscribeEvent
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event)
{
@@ -17,6 +17,7 @@ public class Reference {
public static final String PULSE_MOBHEADS = "MobHeads";
public static final String PULSE_TWEAKS = "IguanaTweaks";
public static final String PULSE_ITEMS = "IguanaItems";
public static final String PULSE_RESTRICTIONS = "IguanaRestrictions";
public static final String PROXY_CLIENT_CLASS = "iguanaman.iguanatweakstconstruct.proxy.ClientProxy";
public static final String PROXY_SERVER_CLASS = "iguanaman.iguanatweakstconstruct.proxy.ServerProxy";
@@ -1,17 +1,38 @@
package iguanaman.iguanatweakstconstruct.tweaks.handlers;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import iguanaman.iguanatweakstconstruct.reference.Config;
import iguanaman.iguanatweakstconstruct.util.Log;
import iguanaman.iguanatweakstconstruct.util.RestrictionHelper;
import tconstruct.library.crafting.PatternBuilder;
import tconstruct.library.event.PartBuilderEvent;
import java.util.Set;
public class PartRestrictionHandler {
@SubscribeEvent
public void onPartBuilding(PartBuilderEvent event)
public void onPartBuilding(PartBuilderEvent.NormalPart event)
{
if(event.pattern != null)
Log.info(event.pattern.getDisplayName());
if(event.otherPattern != null)
Log.info(event.otherPattern.getDisplayName());
PatternBuilder.ItemKey key = PatternBuilder.instance.getItemKey(event.material);
if(key == null)
return;
PatternBuilder.MaterialSet set = ((PatternBuilder.MaterialSet)PatternBuilder.instance.materialSets.get(key.key));
if(set == null)
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<Integer> restrictedMats = Config.restrictedParts.get(name); // guaranteed to exist
if(restrictedMats.contains(matID))
event.setResult(Event.Result.DENY);
}
}
@@ -0,0 +1,26 @@
package iguanaman.iguanatweakstconstruct.util;
import net.minecraft.item.ItemStack;
import tconstruct.library.util.IToolPart;
import tconstruct.tools.TinkerTools;
import java.util.HashMap;
import java.util.Map;
// todo: refactor this properly with a Map or something when i need to restrict more than vanilla
public abstract class RestrictionHelper {
// the tool parts
public static final String[] patternNames = new String[] { "ingot", "rod", "pickaxe", "shovel", "axe", "swordblade", "largeguard", "mediumguard", "crossbar", "binding", "frypan", "sign",
"knifeblade", "chisel", "largerod", "toughbinding", "largeplate", "broadaxe", "scythe", "excavator", "largeblade", "hammerhead", "fullguard", "bowstring", "fletching", "arrowhead" };
public static String getPatternName(ItemStack pattern)
{
if(pattern.getItem() == TinkerTools.woodPattern) {
if (pattern.getItemDamage() > patternNames.length)
return "";
return patternNames[pattern.getItemDamage()];
}
return null;
}
}