Reinstated Compression sigil. (#1374)

*  Reinstated Compression sigil.
- does not compress wooden planks into crafting tables
- searches for reversible 3x3/2x2 recipes with a single material type
- probably has a lot of redundant stuff and looks silly
- uses try/catch (might want to find a different approach, some people scoff at this)
- should probably have spent the night sleeping, I'm taking exams tomorrow and will probably sleep the whole day through.

* Learned how to properly handle the "NullPointerException"-situation.

* Update BaseCompressionHandler.java

* Update CompressionRegistry.java

* Removed (almost) all code comments (only a one-liner remains that serves as pointer to a completly commented-out class (StorageBlockCraftingRecipeAssimilator)).
Made methods and variables for long function calls to be more efficient.
Fixed an oversight that resulted in a NullPointerException after removing redundant checks that were made to prevent exactly that.

Rearranged and reformatted code.

* corrected something that could be considered a typo but was an oversight

* This should be it. An Array should be more efficient but you can correct me if I'm wrong. In either case it does what it is supposed to do.

* Fixed. Needed a seperate inventory for the reversible check (or clear the previously used one, but then I'd had to repopulate again and that would just have been messy)

* Forgot one of my lines.

* Fix and cleanup.
Could definitely clean more but that should suffice for now.
This commit is contained in:
AEon - Tobias 2018-08-26 22:05:30 +02:00 committed by Nick Ignoffo
parent 9440d3c0b9
commit 1f392721fa
7 changed files with 153 additions and 180 deletions

View file

@ -4,31 +4,98 @@ 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.world.World;
public class AdvancedCompressionHandler extends CompressionHandler {
@Override
public ItemStack compressInventory(ItemStack[] inv, World world) {
return test(inv, true, world);
private static InventoryCrafting[] inventoryCrafting = {
new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
},
3, 3),
new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
},
2, 2),
new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
}
},
1, 1)
};
private static ItemStack reversibleCheck;
public static boolean isResultStackReversible(ItemStack stack, World world) {
if (stack.isEmpty()) {
return false;
}
inventoryCrafting[2].setInventorySlotContents(0, stack);
ItemStack returnStack = getNNRecipeOutput(inventoryCrafting[2], world);
return !returnStack.isEmpty() && CompressionRegistry.areItemStacksEqual(reversibleCheck, returnStack);
}
public ItemStack test(ItemStack[] inv, boolean doDrain, World world) {
public static ItemStack getRecipe(ItemStack stack, World world, int gridSize) {
StorageBlockCraftingManager craftingManagerSB = StorageBlockCraftingManager.getInstance();
InventoryCrafting inventory = inventoryCrafting[3 - gridSize];
for (int i = 0; i < inventory.getSizeInventory(); i++) {
inventory.setInventorySlotContents(i, stack);
}
ItemStack notEmptyRecipe = craftingManagerSB.findMatchingRecipe(inventory, world);
if (!notEmptyRecipe.isEmpty()) {
return notEmptyRecipe;
}
ItemStack result = getNNRecipeOutput(inventory, world);
if (isResultStackReversible(result, world)) {
craftingManagerSB.addRecipe(CraftingManager.findMatchingRecipe(inventory, world));
return result;
}
return ItemStack.EMPTY;
}
public static ItemStack getNNRecipeOutput(InventoryCrafting inventory, World world) {
IRecipe checkForNull = CraftingManager.findMatchingRecipe(inventory, world);
if (checkForNull != null) {
return checkForNull.getRecipeOutput();
}
return ItemStack.EMPTY;
}
public static ItemStack get22Recipe(ItemStack stack, World world) {
return getRecipe(stack, world, 2);
}
public static ItemStack get33Recipe(ItemStack stack, World world) {
return getRecipe(stack, world, 3);
}
public ItemStack compressInventory(ItemStack[] inv, World world) {
for (ItemStack invStack : inv) {
if (invStack.isEmpty()) {
continue;
}
for (int i = 2; i <= 3; i++) {
for (int i = 3; i >= 2; i--) {
reversibleCheck = invStack;
ItemStack stack = getRecipe(invStack, world, i);
if (!stack.isEmpty()) {
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);
int needed = (i == 2 ? 4 : 9);
int remaining = iterateThroughInventory(invStack, invStack.getMaxStackSize() - needed, inv, needed, true); // if more than needed gets consumed at any point, the simulate test was needed after all
if (remaining <= 0)
return stack;
}
}
}
}
@ -36,103 +103,5 @@ public class AdvancedCompressionHandler extends CompressionHandler {
return ItemStack.EMPTY;
}
public int iterateThroughInventory(ItemStack required, int kept, ItemStack[] inv, int needed, boolean doDrain) {
int i = -1;
for (ItemStack invStack : inv) {
i++;
if (invStack.isEmpty()) {
continue;
}
if (invStack.isItemEqual(required) && (invStack.getTagCompound() == null ? required.getTagCompound() == null : invStack.getTagCompound().equals(required.getTagCompound()))) {
int stackSize = invStack.getCount();
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.setCount(remainingFromStack + used);
if (invStack.isEmpty()) {
inv[i] = ItemStack.EMPTY;
}
}
needed -= (stackSize - used - remainingFromStack);
}
if (needed <= 0) {
return 0;
}
}
}
return needed;
}
public static boolean isResultStackReversible(ItemStack stack, int gridSize, World world) {
if (stack.isEmpty()) {
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.isEmpty()) {
return false;
}
ItemStack compressedStack = ItemStack.EMPTY;
switch (gridSize) {
case 2:
compressedStack = get22Recipe(returnStack, world);
break;
case 3:
compressedStack = get33Recipe(returnStack, world);
break;
}
return !compressedStack.isEmpty() && 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).isEmpty();
}
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).isEmpty();
}
public static ItemStack get33Recipe(ItemStack stack, World world) {
return getRecipe(stack, world, 3);
}
}

View file

@ -8,11 +8,11 @@ public class BaseCompressionHandler extends CompressionHandler {
private final ItemStack result;
private final int leftover;
public BaseCompressionHandler(ItemStack requested, ItemStack result, int leftover) {
public BaseCompressionHandler(ItemStack input, ItemStack output, int remainder) {
super();
this.required = requested;
this.result = result;
this.leftover = leftover;
this.required = input;
this.result = output;
this.leftover = remainder;
}
public ItemStack getResultStack() {
@ -35,57 +35,18 @@ public class BaseCompressionHandler extends CompressionHandler {
}
public int getRemainingNeeded(ItemStack[] inv) {
return iterateThroughInventory(inv, false);
int needed = this.required.getCount();
int kept = this.getLeftover();
return iterateThroughInventory(this.required, kept, inv, needed, true);
}
public int drainInventory(ItemStack[] inv) {
return iterateThroughInventory(inv, true);
}
public int iterateThroughInventory(ItemStack[] inv, boolean doDrain) {
int needed = this.required.getCount();
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.getCount();
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.setCount(remainingFromStack + used);
if (invStack.isEmpty()) {
inv[i] = ItemStack.EMPTY;
}
}
needed -= (stackSize - used - remainingFromStack);
}
if (needed <= 0) {
return 0;
}
}
}
return needed;
return iterateThroughInventory(this.required, kept, inv, needed, true);
}
public int getLeftover() {
return this.leftover;
}
}
}

View file

@ -12,4 +12,48 @@ public abstract class CompressionHandler {
* @return The result of the compression
*/
public abstract ItemStack compressInventory(ItemStack[] inv, World world);
}
public int iterateThroughInventory(ItemStack required, int kept, ItemStack[] inv, int needed, boolean doDrain) {
int i = -1;
for (ItemStack invStack : inv) {
i++;
if (invStack.isEmpty()) {
continue;
}
if (invStack.isItemEqual(required) && (invStack.getTagCompound() == null ? required.getTagCompound() == null : invStack.getTagCompound().equals(required.getTagCompound()))) {
int stackSize = invStack.getCount();
int used = 0;
if (kept > 0) {
int remainingFromStack = Math.max(stackSize - kept, 0);
used += stackSize - remainingFromStack;
}
kept -= used; // 0
if (kept <= 0 && needed > 0) {
int remainingFromStack = Math.max(stackSize - used - needed, 0);
if (doDrain) {
invStack.setCount(remainingFromStack + used);
if (invStack.isEmpty()) {
inv[i] = ItemStack.EMPTY;
}
}
needed -= (stackSize - used - remainingFromStack);
}
if (needed <= 0) {
return 0;
}
}
}
return needed;
}
}

View file

@ -38,7 +38,7 @@ public class CompressionRegistry {
public static ItemStack compressInventory(ItemStack[] inv, World world) {
for (CompressionHandler handler : compressionRegistry) {
ItemStack stack = handler.compressInventory(inv, world);
if (stack != null) {
if (!stack.isEmpty()) {
return stack;
}
}
@ -78,16 +78,10 @@ public class CompressionRegistry {
}
public static int getItemThreshold(ItemStack stack) {
for (Map.Entry<ItemStack, Integer> entry : thresholdMap.entrySet()) {
if (areItemStacksEqual(entry.getKey(), stack)) {
return entry.getValue();
}
}
return 0;
return stack.getItem().getItemStackLimit(stack); //this should work according to the guide, leaving behind a full stack of the source item (unless otherwise specified with a BaseCompressionHandler recipe)
}
public static boolean areItemStacksEqual(ItemStack stack, ItemStack compressedStack) {
return stack.isItemEqual(compressedStack) && (stack.getTagCompound() == null ? !compressedStack.hasTagCompound() : stack.getTagCompound().equals(compressedStack.getTagCompound()));
}
}
}

View file

@ -14,11 +14,17 @@ import java.util.List;
public class StorageBlockCraftingManager {
private static final StorageBlockCraftingManager instance = new StorageBlockCraftingManager();
private List recipes = new LinkedList();
private List<IRecipe> recipes = new LinkedList<>();
public void addRecipe(IRecipe recipe){
this.recipes.add(recipe);
}
//recipes are currently added during runtime, this will only return recipes specifically added in init
//through BaseCompressionHandler
public void addStorageBlockRecipes() {
// this.recipes = new StorageBlockCraftingRecipeAssimilator().getPackingRecipes();
//this.recipes = new StorageBlockCraftingRecipeAssimilator().getPackingRecipes();
BMLog.DEBUG.info("Total number of compression recipes: " + this.recipes.size());
}
@ -26,7 +32,7 @@ public class StorageBlockCraftingManager {
return this.findMatchingRecipe(craftingInventory, world, this.recipes);
}
private ItemStack findMatchingRecipe(InventoryCrafting craftingInventory, World world, List list) {
private ItemStack findMatchingRecipe(InventoryCrafting craftingInventory, World world, List<IRecipe> list) {
int i = 0;
ItemStack itemstack = ItemStack.EMPTY;
ItemStack itemstack1 = ItemStack.EMPTY;
@ -62,7 +68,7 @@ public class StorageBlockCraftingManager {
return new ItemStack(itemstack.getItem(), 1, i1);
} else {
for (j = 0; j < list.size(); ++j) {
IRecipe irecipe = (IRecipe) list.get(j);
IRecipe irecipe = list.get(j);
if (irecipe.matches(craftingInventory, world)) {
return irecipe.getCraftingResult(craftingInventory);
@ -77,7 +83,7 @@ public class StorageBlockCraftingManager {
return instance;
}
private static boolean isResultStackReversible(ItemStack stack, int gridSize, World world, List list) {
private static boolean isResultStackReversible(ItemStack stack, int gridSize, World world, List<IRecipe> list) {
if (stack.isEmpty()) {
return false;
}
@ -107,7 +113,7 @@ public class StorageBlockCraftingManager {
return !compressedStack.isEmpty() && CompressionRegistry.areItemStacksEqual(stack, compressedStack);
}
private static ItemStack getRecipe(ItemStack stack, World world, int gridSize, List list) {
private static ItemStack getRecipe(ItemStack stack, World world, int gridSize, List<IRecipe> list) {
InventoryCrafting inventory = new InventoryCrafting(new Container() {
public boolean canInteractWith(EntityPlayer player) {
return false;
@ -120,19 +126,19 @@ public class StorageBlockCraftingManager {
return StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world, list);
}
private static boolean has22Recipe(ItemStack stack, World world, List list) {
private static boolean has22Recipe(ItemStack stack, World world, List<IRecipe> list) {
return !get22Recipe(stack, world, list).isEmpty();
}
private static ItemStack get22Recipe(ItemStack stack, World world, List list) {
private static ItemStack get22Recipe(ItemStack stack, World world, List<IRecipe> list) {
return getRecipe(stack, world, 2, list);
}
private static boolean has33Recipe(ItemStack stack, World world, List list) {
private static boolean has33Recipe(ItemStack stack, World world, List<IRecipe> list) {
return !get33Recipe(stack, world, list).isEmpty();
}
private static ItemStack get33Recipe(ItemStack stack, World world, List list) {
private static ItemStack get33Recipe(ItemStack stack, World world, List<IRecipe> list) {
return getRecipe(stack, world, 3, list);
}
}

View file

@ -17,8 +17,6 @@ public class ItemSigilCompression extends ItemSigilToggleableBase {
@Override
public void onSigilUpdate(ItemStack stack, World world, EntityPlayer player, int itemSlot, boolean isSelected) {
if (true)
return; // TODO - Rewrite compression system
if (PlayerHelper.isFakePlayer(player))
return;

View file

@ -85,6 +85,7 @@ public class ModRecipes
addAlchemyTableRecipes();
addPotionRecipes();
addLivingArmourDowngradeRecipes();
addCompressionHandlers();
}
public static void initOreDict()
@ -140,11 +141,11 @@ public class ModRecipes
{
Stopwatch stopwatch = Stopwatch.createStarted();
StorageBlockCraftingManager.getInstance().addStorageBlockRecipes();
CompressionRegistry.registerHandler(new BaseCompressionHandler(new ItemStack(Items.GLOWSTONE_DUST, 4, 0), new ItemStack(Blocks.GLOWSTONE), 64));
CompressionRegistry.registerHandler(new BaseCompressionHandler(new ItemStack(Items.SNOWBALL, 4, 0), new ItemStack(Blocks.SNOW), 8));
CompressionRegistry.registerHandler(new AdvancedCompressionHandler());
CompressionRegistry.registerItemThreshold(new ItemStack(Blocks.COBBLESTONE), 64);
stopwatch.stop();
BMLog.DEBUG.info("Added compression recipes in {}", stopwatch);