More sigils

Deleted weird file
This commit is contained in:
Arcaratus 2015-12-28 19:09:51 -05:00
parent 0ec0570d1a
commit b96725fe98
31 changed files with 1011 additions and 53 deletions

View file

@ -0,0 +1,141 @@
package WayofTime.bloodmagic.compress;
import WayofTime.bloodmagic.api.compress.CompressionHandler;
import WayofTime.bloodmagic.api.compress.CompressionRegistry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public class AdvancedCompressionHandler extends CompressionHandler {
@Override
public ItemStack compressInventory(ItemStack[] inv, World world) {
return test(inv, true, world);
}
public ItemStack test(ItemStack[] inv, boolean doDrain, World world) {
for (ItemStack invStack : inv) {
if (invStack == null) {
continue;
}
for (int i = 2; i <= 3; i++) {
ItemStack stacky = getRecipe(invStack, world, i);
if (stacky != null) {
int threshold = CompressionRegistry.getItemThreshold(invStack);
int needed = i * i;
int neededLeft = iterateThroughInventory(invStack, threshold + invStack.getMaxStackSize() - needed, inv, needed, false);
if (neededLeft <= 0) {
iterateThroughInventory(invStack, 0, inv, needed, true);
return stacky;
}
}
}
}
return null;
}
public int iterateThroughInventory(ItemStack required, int kept, ItemStack[] inv, int needed, boolean doDrain) {
int i = -1;
for (ItemStack invStack : inv) {
i++;
if (invStack == null) {
continue;
}
if (invStack.isItemEqual(required) && (invStack.getTagCompound() == null ? required.getTagCompound() == null : invStack.getTagCompound().equals(required.getTagCompound()))) {
int stackSize = invStack.stackSize;
int used = 0;
if (kept > 0) {
int remainingFromStack = Math.max(stackSize - kept, 0);
used += stackSize - remainingFromStack;
}
kept -= used;
if (kept <= 0 && needed > 0) {
int remainingFromStack = Math.max(stackSize - used - needed, 0);
if (doDrain) {
invStack.stackSize = remainingFromStack + used;
if (invStack.stackSize <= 0) {
inv[i] = null;
}
}
needed -= (stackSize - used - remainingFromStack);
}
if (needed <= 0) {
return 0;
}
}
}
return needed;
}
public static boolean isResultStackReversible(ItemStack stack, int gridSize, World world) {
if (stack == null) {
return false;
}
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
}, 2, 2);
inventory.setInventorySlotContents(0, stack);
ItemStack returnStack = StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world);
if (returnStack == null) {
return false;
}
ItemStack compressedStack = null;
switch (gridSize) {
case 2:
compressedStack = get22Recipe(returnStack, world);
break;
case 3:
compressedStack = get33Recipe(returnStack, world);
break;
}
return compressedStack != null && CompressionRegistry.areItemStacksEqual(stack, compressedStack);
}
public static ItemStack getRecipe(ItemStack stack, World world, int gridSize) {
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
}, gridSize, gridSize);
for (int i = 0; i < inventory.getSizeInventory(); i++) {
inventory.setInventorySlotContents(i, stack);
}
return StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world);
}
public static boolean has22Recipe(ItemStack stack, World world) {
return get22Recipe(stack, world) != null;
}
public static ItemStack get22Recipe(ItemStack stack, World world) {
return getRecipe(stack, world, 2);
}
public static boolean has33Recipe(ItemStack stack, World world) {
return get33Recipe(stack, world) != null;
}
public static ItemStack get33Recipe(ItemStack stack, World world) {
return getRecipe(stack, world, 3);
}
}

View file

@ -0,0 +1,93 @@
package WayofTime.bloodmagic.compress;
import WayofTime.bloodmagic.api.compress.CompressionHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public class BaseCompressionHandler extends CompressionHandler {
private final ItemStack required;
private final ItemStack result;
private final int leftover;
public BaseCompressionHandler(ItemStack requested, ItemStack result, int leftover) {
super();
this.required = requested;
this.result = result;
this.leftover = leftover;
}
public ItemStack getResultStack() {
return this.result.copy();
}
public ItemStack getRequiredStack() {
return this.required.copy();
}
@Override
public ItemStack compressInventory(ItemStack[] inv, World world) {
int remaining = this.getRemainingNeeded(inv);
if (remaining <= 0) {
this.drainInventory(inv);
return this.getResultStack();
}
return null;
}
public int getRemainingNeeded(ItemStack[] inv) {
return iterateThroughInventory(inv, false);
}
public int drainInventory(ItemStack[] inv) {
return iterateThroughInventory(inv, true);
}
public int iterateThroughInventory(ItemStack[] inv, boolean doDrain) {
int needed = this.required.stackSize;
int kept = this.getLeftover();
int i = -1;
for (ItemStack invStack : inv) {
i++;
if (invStack == null) {
continue;
}
if (invStack.isItemEqual(this.required) && (invStack.getTagCompound() == null ? this.required.getTagCompound() == null : invStack.getTagCompound().equals(this.required.getTagCompound()))) {
int stackSize = invStack.stackSize;
int used = 0;
if (kept > 0) {
int remainingFromStack = Math.max(stackSize - kept, 0);
used += stackSize - remainingFromStack;
}
kept -= used;
if (kept <= 0 && needed > 0) {
int remainingFromStack = Math.max(stackSize - used - needed, 0);
if (doDrain) {
invStack.stackSize = remainingFromStack + used;
if (invStack.stackSize <= 0) {
inv[i] = null;
}
}
needed -= (stackSize - used - remainingFromStack);
}
if (needed <= 0) {
return 0;
}
}
}
return needed;
}
public int getLeftover() {
return this.leftover;
}
}

View file

@ -0,0 +1,140 @@
package WayofTime.bloodmagic.compress;
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.api.compress.CompressionRegistry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import java.util.LinkedList;
import java.util.List;
public class StorageBlockCraftingManager {
private static final StorageBlockCraftingManager instance = new StorageBlockCraftingManager();
private List recipes = new LinkedList();
public static StorageBlockCraftingManager getInstance() {
return instance;
}
public void addStorageBlockRecipes() {
this.recipes = new StorageBlockCraftingRecipeAssimilator().getPackingRecipes();
BloodMagic.instance.getLogger().info("Total number of compression recipes: " + this.recipes.size());
}
private static boolean isResultStackReversible(ItemStack stack, int gridSize, World world, List list) {
if (stack == null) {
return false;
}
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
}, 2, 2);
inventory.setInventorySlotContents(0, stack);
ItemStack returnStack = StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world, list);
if (returnStack == null || returnStack.getItem() == null) {
return false;
}
ItemStack compressedStack = null;
switch (gridSize) {
case 2:
compressedStack = get22Recipe(returnStack, world, list);
break;
case 3:
compressedStack = get33Recipe(returnStack, world, list);
break;
}
return compressedStack != null && CompressionRegistry.areItemStacksEqual(stack, compressedStack);
}
private static ItemStack getRecipe(ItemStack stack, World world, int gridSize, List list) {
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
}, gridSize, gridSize);
for (int i = 0; i < inventory.getSizeInventory(); i++) {
inventory.setInventorySlotContents(i, stack);
}
return StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world, list);
}
private static boolean has22Recipe(ItemStack stack, World world, List list) {
return get22Recipe(stack, world, list) != null;
}
private static ItemStack get22Recipe(ItemStack stack, World world, List list) {
return getRecipe(stack, world, 2, list);
}
private static boolean has33Recipe(ItemStack stack, World world, List list) {
return get33Recipe(stack, world, list) != null;
}
private static ItemStack get33Recipe(ItemStack stack, World world, List list) {
return getRecipe(stack, world, 3, list);
}
public ItemStack findMatchingRecipe(InventoryCrafting craftingInventory, World world) {
return this.findMatchingRecipe(craftingInventory, world, this.recipes);
}
private ItemStack findMatchingRecipe(InventoryCrafting craftingInventory, World world, List list) {
int i = 0;
ItemStack itemstack = null;
ItemStack itemstack1 = null;
int j;
for (j = 0; j < craftingInventory.getSizeInventory(); ++j) {
ItemStack itemstack2 = craftingInventory.getStackInSlot(j);
if (itemstack2 != null) {
if (i == 0) {
itemstack = itemstack2;
}
if (i == 1) {
itemstack1 = itemstack2;
}
++i;
}
}
if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable()) {
Item item = itemstack.getItem();
int j1 = item.getMaxDamage() - itemstack.getItemDamage();
int k = item.getMaxDamage() - itemstack1.getItemDamage();
int l = j1 + k + item.getMaxDamage() * 5 / 100;
int i1 = item.getMaxDamage() - l;
if (i1 < 0) {
i1 = 0;
}
return new ItemStack(itemstack.getItem(), 1, i1);
} else {
for (j = 0; j < list.size(); ++j) {
IRecipe irecipe = (IRecipe) list.get(j);
if (irecipe.matches(craftingInventory, world)) {
return irecipe.getCraftingResult(craftingInventory);
}
}
return null;
}
}
}

View file

@ -0,0 +1,253 @@
package WayofTime.bloodmagic.compress;
import WayofTime.bloodmagic.BloodMagic;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import java.util.*;
public class StorageBlockCraftingRecipeAssimilator {
public List<IRecipe> getPackingRecipes() {
// grab all recipes potentially suitable for packing or unpacking
List<PackingRecipe> packingRecipes = new LinkedList<PackingRecipe>();
List<IRecipe> unpackingRecipes = new ArrayList<IRecipe>();
for (IRecipe recipe : getCraftingRecipes()) {
ItemStack output = recipe.getRecipeOutput();
if (output == null || output.getItem() == null) continue;
if (output.stackSize == 1) {
PackingRecipe packingRecipe = getPackingRecipe(recipe);
if (packingRecipe != null) {
packingRecipes.add(packingRecipe);
}
} else if ((output.stackSize == 4 || output.stackSize == 9) && recipe.getRecipeSize() == 1) {
unpackingRecipes.add(recipe);
}
}
// grab all packing recipes which accept the output of any of the unpacking recipes
Container container = makeDummyContainer();
InventoryCrafting inventoryUnpack = new InventoryCrafting(container, 2, 2);
InventoryCrafting inventory2x2 = new InventoryCrafting(container, 2, 2);
InventoryCrafting inventory3x3 = new InventoryCrafting(container, 3, 3);
World world = null; // TODO: use a proper dummy world?
List<IRecipe> ret = new ArrayList<IRecipe>();
for (IRecipe recipeUnpack : unpackingRecipes) {
ItemStack unpacked = recipeUnpack.getRecipeOutput();
InventoryCrafting inventory = null;
for (Iterator<PackingRecipe> it = packingRecipes.iterator(); it.hasNext(); ) {
PackingRecipe recipePack = it.next();
// check if the packing recipe accepts the unpacking recipe's output
boolean matched = false;
if (recipePack.possibleInputs != null) { // the recipe could be parsed, use its inputs directly since that's faster
// verify recipe size
if (recipePack.inputCount != unpacked.stackSize) continue;
// check if any of the input options matches the unpacked item stack
for (ItemStack stack : recipePack.possibleInputs) {
if (areInputsIdentical(unpacked, stack)) {
matched = true;
break;
}
}
} else { // unknown IRecipe, check through the recipe conventionally
// verify recipe size for 3x3 to skip anything smaller quickly
if (unpacked.stackSize == 9 && recipePack.recipe.getRecipeSize() < 9) continue;
// initialize inventory late, but only once per unpack recipe
if (inventory == null) {
if (unpacked.stackSize == 4) {
inventory = inventory2x2;
} else {
inventory = inventory3x3;
}
for (int i = 0; i < unpacked.stackSize; i++) {
inventory.setInventorySlotContents(i, unpacked.copy());
}
}
// check if the packing recipe accepts the unpacked item stack
matched = recipePack.recipe.matches(inventory, world);
}
if (matched) {
// check if the unpacking recipe accepts the packing recipe's output
ItemStack packOutput = recipePack.recipe.getRecipeOutput();
inventoryUnpack.setInventorySlotContents(0, packOutput.copy());
if (recipeUnpack.matches(inventoryUnpack, world)) {
ret.add(recipePack.recipe);
BloodMagic.instance.getLogger().info("Adding the following recipe to the Compression Handler: " + packOutput);
it.remove();
}
}
}
}
return ret;
}
@SuppressWarnings("unchecked")
private List<IRecipe> getCraftingRecipes() {
return CraftingManager.getInstance().getRecipeList();
}
private Container makeDummyContainer() {
return new Container() {
@Override
public boolean canInteractWith(EntityPlayer player) {
return true;
}
};
}
private PackingRecipe getPackingRecipe(IRecipe recipe) {
if (recipe.getRecipeSize() < 4) return null;
List<?> inputs;
if (recipe instanceof ShapedRecipes) {
inputs = Arrays.asList(((ShapedRecipes) recipe).recipeItems);
} else if (recipe instanceof ShapelessRecipes) {
inputs = ((ShapelessRecipes) recipe).recipeItems;
} else if (recipe instanceof ShapedOreRecipe) {
inputs = Arrays.asList(((ShapedOreRecipe) recipe).getInput());
} else if (recipe instanceof ShapelessOreRecipe) {
inputs = ((ShapelessOreRecipe) recipe).getInput();
} else {
return new PackingRecipe(recipe, null, -1);
}
// check if the recipe inputs are size 4 or 9
int count = 0;
for (Object o : inputs) {
if (o != null) count++;
}
if (count != 4 && count != 9) return null;
// grab identical inputs
List<ItemStack> identicalInputs = getIdenticalInputs(inputs);
if (identicalInputs == null) return null;
return new PackingRecipe(recipe, identicalInputs, count);
}
/**
* Determine the item stacks from the provided inputs which are suitable for every input element.
*
* @param inputs List of all inputs, null elements are being ignored.
* @return List List of all options.
*/
@SuppressWarnings("unchecked")
private List<ItemStack> getIdenticalInputs(List<?> inputs) {
List<ItemStack> options = null;
for (Object input : inputs) {
if (input == null) continue;
List<ItemStack> offers;
if (input instanceof ItemStack) {
offers = Collections.singletonList((ItemStack) input);
} else if (input instanceof List) {
offers = (List<ItemStack>) input;
if (offers.isEmpty()) return null;
} else {
throw new RuntimeException("invalid input: "+input.getClass());
}
if (options == null) {
options = new ArrayList<ItemStack>(offers);
continue;
}
for (Iterator<ItemStack> it = options.iterator(); it.hasNext(); ) {
ItemStack stackReq = it.next();
boolean found = false;
for (ItemStack stackCmp : offers) {
if (areInputsIdentical(stackReq, stackCmp)) {
found = true;
break;
}
}
if (!found) {
it.remove();
if (options.isEmpty()) return null;
}
}
}
return options;
}
private boolean areInputsIdentical(ItemStack a, ItemStack b) {
try {
if (a.getItem() != b.getItem())
return false;
int dmgA = a.getItemDamage();
int dmgB = b.getItemDamage();
return dmgA == dmgB || dmgA == OreDictionary.WILDCARD_VALUE || dmgB == OreDictionary.WILDCARD_VALUE;
} catch (NullPointerException e) {
BloodMagic.instance.getLogger().error("A mod in this instance has registered an item with a null input. Known problem mods are:");
// String err = "";
// for (String problem : problemMods)
// err += (err.length() > 0 ? ", " : "") + problem;
// BloodMagic.instance.getLogger().error(err);
return false;
}
}
private static class PackingRecipe {
PackingRecipe(IRecipe recipe, List<ItemStack> possibleInputs, int inputCount) {
this.recipe = recipe;
this.possibleInputs = possibleInputs;
this.inputCount = inputCount;
}
final IRecipe recipe;
final List<ItemStack> possibleInputs;
final int inputCount;
}
}