Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.mcupdater.autopackager;
- import cofh.api.energy.EnergyStorage;
- import cofh.api.energy.TileEnergyHandler;
- import cofh.lib.util.helpers.InventoryHelper;
- import cpw.mods.fml.common.Optional.Interface;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.SortedSet;
- import java.util.TreeSet;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.inventory.InventoryCrafting;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.crafting.CraftingManager;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.ChatComponentText;
- import net.minecraft.util.ChatComponentTranslation;
- import net.minecraft.world.World;
- import net.minecraftforge.common.util.ForgeDirection;
- import org.mcupdater.shared.Position;
- @Optional.Interface(iface="com.dynious.refinedrelocation.api.tileentity.ISortingMember", modid="RefinedRelocation")
- public class TilePackager extends TileEnergyHandler
- {
- private Object sortingHandler;
- private ForgeDirection orientation;
- private int tickCounter = 0;
- private int tickDelay = AutoPackager.delayCycleNormal;
- private TilePackager.Mode mode;
- public TilePackager()
- {
- this.mode = TilePackager.Mode.HYBRID;
- }
- public void func_145845_h()
- {
- super.func_145845_h();
- if (++this.tickCounter >= this.tickDelay) {
- this.tickCounter = 0;
- if (this.storage.getEnergyStored() > AutoPackager.energyPerCycle)
- if (tryCraft()) {
- this.storage.extractEnergy(AutoPackager.energyPerCycle, false);
- this.tickDelay = AutoPackager.delayCycleNormal;
- } else {
- this.tickDelay = AutoPackager.delayCycleIdle;
- }
- }
- }
- private boolean tryCraft()
- {
- if (this.orientation == null) {
- return false;
- }
- Position inputPos = new Position(this.field_145851_c, this.field_145848_d, this.field_145849_e, this.orientation);
- Position outputPos = new Position(this.field_145851_c, this.field_145848_d, this.field_145849_e, this.orientation);
- inputPos.moveLeft(1.0D);
- outputPos.moveRight(1.0D);
- TileEntity tileInput = this.field_145850_b.func_147438_o((int)inputPos.x, (int)inputPos.y, (int)inputPos.z);
- TileEntity tileOutput = this.field_145850_b.func_147438_o((int)outputPos.x, (int)outputPos.y, (int)outputPos.z);
- Map slotMap = new HashMap();
- IInventory invInput;
- if (((tileInput instanceof IInventory)) && ((tileOutput instanceof IInventory))) {
- invInput = (IInventory)tileInput;
- IInventory invOutput = (IInventory)tileOutput;
- for (int slot = 0; slot < invInput.func_70302_i_(); slot++) {
- if ((invInput.func_70301_a(slot) != null) && (
- (!(invInput instanceof ISidedInventory)) || (((ISidedInventory)invInput).func_102008_b(slot, null, ForgeDirection.DOWN.ordinal()))))
- {
- if (slotMap.containsKey(invInput.func_70301_a(slot).func_77977_a() + ":" + invInput.func_70301_a(slot).func_77960_j())) {
- ((SortedSet)slotMap.get(invInput.func_70301_a(slot).func_77977_a() + ":" + invInput.func_70301_a(slot).func_77960_j())).add(Integer.valueOf(slot));
- } else {
- SortedSet slotList = new TreeSet();
- slotList.add(Integer.valueOf(slot));
- slotMap.put(invInput.func_70301_a(slot).func_77977_a() + ":" + invInput.func_70301_a(slot).func_77960_j(), slotList);
- }
- if (((this.mode == TilePackager.Mode.HYBRID) || (this.mode == TilePackager.Mode.SMALL)) && (invInput.func_70301_a(slot).field_77994_a >= 4)) {
- ItemStack testStack = invInput.func_70301_a(slot).func_77946_l();
- testStack.field_77994_a = 1;
- InventoryCrafting smallCraft = new InventoryCrafting(new Container()
- {
- public boolean func_75145_c(EntityPlayer entityPlayer) {
- return false;
- }
- }
- , 2, 2);
- for (int craftSlot = 0; craftSlot < 4; craftSlot++) {
- smallCraft.func_70299_a(craftSlot, testStack);
- }
- ItemStack result = CraftingManager.func_77594_a().func_82787_a(smallCraft, this.field_145850_b);
- if (result != null) {
- testStack = InventoryHelper.simulateInsertItemStackIntoInventory(invOutput, result, 1);
- if (testStack == null) {
- invInput.func_70298_a(slot, 4);
- InventoryHelper.insertItemStackIntoInventory(invOutput, result, 1);
- return true;
- }
- }
- }
- if (((this.mode == TilePackager.Mode.HYBRID) || (this.mode == TilePackager.Mode.LARGE)) && (invInput.func_70301_a(slot).field_77994_a >= 9)) {
- ItemStack testStack = invInput.func_70301_a(slot).func_77946_l();
- testStack.field_77994_a = 1;
- InventoryCrafting largeCraft = new InventoryCrafting(new Container()
- {
- public boolean func_75145_c(EntityPlayer entityPlayer) {
- return false;
- }
- }
- , 3, 3);
- for (int craftSlot = 0; craftSlot < 9; craftSlot++) {
- largeCraft.func_70299_a(craftSlot, testStack);
- }
- ItemStack result = CraftingManager.func_77594_a().func_82787_a(largeCraft, this.field_145850_b);
- if (result != null) {
- testStack = InventoryHelper.simulateInsertItemStackIntoInventory(invOutput, result, 1);
- if (testStack == null) {
- invInput.func_70298_a(slot, 9);
- InventoryHelper.insertItemStackIntoInventory(invOutput, result, 1);
- return true;
- }
- }
- }
- }
- }
- for (Map.Entry entry : slotMap.entrySet()) {
- if (((SortedSet)entry.getValue()).size() > 1) {
- SortedSet slots = (SortedSet)entry.getValue();
- while (slots.size() > 1) {
- if ((invInput.func_70301_a(((Integer)slots.first()).intValue()) == null) || (!(invInput.func_70301_a(((Integer)slots.first()).intValue()).func_77977_a() + ":" + invInput.func_70301_a(((Integer)slots.first()).intValue()).func_77960_j()).equals(entry.getKey())) || (invInput.func_70301_a(((Integer)slots.first()).intValue()).field_77994_a >= invInput.func_70301_a(((Integer)slots.first()).intValue()).func_77976_d())) {
- slots.remove(slots.first());
- }
- else if ((invInput.func_70301_a(((Integer)slots.last()).intValue()) == null) || (!invInput.func_70301_a(((Integer)slots.last()).intValue()).func_77969_a(invInput.func_70301_a(((Integer)slots.first()).intValue()))) || (!ItemStack.func_77970_a(invInput.func_70301_a(((Integer)slots.first()).intValue()), invInput.func_70301_a(((Integer)slots.last()).intValue())))) {
- slots.remove(slots.last());
- }
- else if (invInput.func_70301_a(((Integer)slots.first()).intValue()).field_77994_a + invInput.func_70301_a(((Integer)slots.last()).intValue()).field_77994_a <= invInput.func_70301_a(((Integer)slots.first()).intValue()).func_77976_d()) {
- invInput.func_70301_a(((Integer)slots.first()).intValue()).field_77994_a += invInput.func_70301_a(((Integer)slots.last()).intValue()).field_77994_a;
- invInput.func_70299_a(((Integer)slots.last()).intValue(), null);
- } else {
- int spaceRemain = invInput.func_70301_a(((Integer)slots.first()).intValue()).func_77976_d() - invInput.func_70301_a(((Integer)slots.first()).intValue()).field_77994_a;
- invInput.func_70301_a(((Integer)slots.first()).intValue()).field_77994_a += spaceRemain;
- invInput.func_70298_a(((Integer)slots.last()).intValue(), spaceRemain);
- }
- }
- }
- }
- }
- return false;
- }
- public void func_145841_b(NBTTagCompound tagCompound)
- {
- super.func_145841_b(tagCompound);
- tagCompound.func_74768_a("orientation", this.orientation.ordinal());
- tagCompound.func_74768_a("mode", this.mode.ordinal());
- }
- public void func_145839_a(NBTTagCompound tagCompound)
- {
- super.func_145839_a(tagCompound);
- this.orientation = ForgeDirection.getOrientation(tagCompound.func_74762_e("orientation"));
- this.mode = TilePackager.Mode.values()[tagCompound.func_74762_e("mode")];
- }
- public void setOrientation(ForgeDirection orientation) {
- this.orientation = orientation;
- }
- public void cycleMode(EntityPlayer player) {
- this.mode = TilePackager.Mode.values()[((this.mode.ordinal() + 1) % TilePackager.Mode.values().length)];
- if (!this.field_145850_b.field_72995_K)
- player.func_145747_a(new ChatComponentText("Current mode: " + this.mode.getMessage()));
- }
- public void checkMode(EntityPlayer player)
- {
- if (!this.field_145850_b.field_72995_K)
- player.func_145747_a(new ChatComponentTranslation("Current mode: " + this.mode.getMessage(), new Object[0]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement