Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
Where shall I even begin... Proper Part-Restricting?
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Aug 6, 2014
1 parent 0403990 commit f9fc628
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 33 deletions.
Expand Up @@ -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"));

Expand Down
Expand Up @@ -5,11 +5,19 @@
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 {
Expand All @@ -24,17 +32,8 @@ public void onPartBuilding(PartBuilderEvent.NormalPart event)
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 = RestrictionConfig.restrictedParts.get(name); // guaranteed to exist
if(restrictedMats.contains(matID))
if(RestrictionHelper.isRestricted(event.pattern, matID))
event.setResult(Event.Result.DENY);
}

Expand All @@ -44,12 +43,30 @@ public void onItemToolTip(ItemTooltipEvent event) {
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<Integer> 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<CustomMaterial> 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");
}
}
Expand Up @@ -13,7 +13,7 @@

public class RestrictionConfig {
private Configuration configfile;
public static Map<String, Set<Integer>> restrictedParts = new HashMap<String, Set<Integer>>();
// public static Map<String, Set<Integer>> restrictedParts = new HashMap<String, Set<Integer>>();

public void init(File file) {
configfile = new Configuration(file);
Expand All @@ -26,6 +26,7 @@ public void init(File file) {
// 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();
Expand All @@ -43,12 +44,9 @@ public void loadRestrictedParts()
// 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<Integer>());
}
comment.append("all\n");

Expand Down Expand Up @@ -97,17 +95,14 @@ public void loadRestrictedParts()
// 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)
{
Expand All @@ -116,7 +111,7 @@ public void loadRestrictedParts()
}

// add restriction :)
restrictedParts.get(restriction[1]).add(matID);
RestrictionHelper.addRestriction(RestrictionHelper.configNameToPattern.get(restriction[1]), matID);
}
}
}
@@ -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)
public static Map<String, ItemMeta> 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<ItemMeta, List<Integer>> patternMaterialLookup; // item+metadata -> List of applicable materials
public static Map<ItemMeta, List<CustomMaterial>> patternCustomMaterialLookup; // item+metadata -> List of applicable custom materials

static {
configNameToPattern = new HashMap<String, ItemMeta>();
patternMaterialLookup = new HashMap<ItemMeta, List<Integer>>();
patternCustomMaterialLookup = new HashMap<ItemMeta, List<CustomMaterial>>();
}


public static boolean isRestricted(ItemStack pattern, Integer matID)
{
if(pattern.getItem() == TinkerTools.woodPattern) {
if (pattern.getItemDamage() > patternNames.length)
return "";
boolean restricted = true;
List<Integer> 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<Integer> getPatternMaterials(ItemStack pattern)
{
return patternMaterialLookup.get(new ItemMeta(pattern));
}

public static List<CustomMaterial> getPatternCustomMaterials(ItemStack pattern)
{
return patternCustomMaterialLookup.get(new ItemMeta(pattern));
}

public static void addRestriction(ItemMeta key, Integer materialID)
{
// fetch the material list
List<Integer> 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<Integer> 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<List, ItemStack> 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<Integer>());

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<CustomMaterial>());

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<CustomMaterial>());

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;
}

return patternNames[pattern.getItemDamage()];
@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
Expand Down

0 comments on commit f9fc628

Please sign in to comment.