From 33d9cb9fdb3df53c7f1b08c26d5904df3b820b48 Mon Sep 17 00:00:00 2001 From: Shadows_of_Fire Date: Thu, 9 Aug 2018 23:04:07 -0400 Subject: [PATCH] properly fix #1157 --- .../mod/inventory/ContainerBag.java | 184 ++++++++---------- .../tile/TileEntityAtomicReconstructor.java | 11 +- .../mod/tile/TileEntityBatteryBox.java | 10 +- .../mod/tile/TileEntityBioReactor.java | 82 ++++---- .../mod/tile/TileEntityBreaker.java | 150 +++++++------- .../mod/tile/TileEntityCanolaPress.java | 65 ++++--- .../mod/tile/TileEntityCoalGenerator.java | 64 +++--- .../mod/tile/TileEntityCoffeeMachine.java | 101 +++++----- .../mod/tile/TileEntityCompost.java | 12 +- .../tile/TileEntityDirectionalBreaker.java | 63 +++--- .../mod/tile/TileEntityEmpowerer.java | 15 +- .../mod/tile/TileEntityEnergizer.java | 43 ++-- .../mod/tile/TileEntityEnervator.java | 57 +++--- .../mod/tile/TileEntityFarmer.java | 10 +- .../mod/tile/TileEntityFeeder.java | 77 ++++---- .../mod/tile/TileEntityFurnaceDouble.java | 109 +++++------ .../mod/tile/TileEntityGrinder.java | 135 +++++++------ .../mod/tile/TileEntityInputter.java | 149 +++++++------- .../mod/tile/TileEntityInventoryBase.java | 18 +- .../mod/tile/TileEntityItemRepairer.java | 79 ++++---- .../mod/tile/TileEntityMiner.java | 10 +- .../mod/tile/TileEntityPhantomBreaker.java | 18 +- .../mod/tile/TileEntityPhantomItemface.java | 29 ++- .../mod/tile/TileEntityPhantomPlacer.java | 123 ++++++------ .../mod/tile/TileEntityPlacer.java | 12 +- .../mod/tile/TileEntityRangedCollector.java | 10 +- .../mod/tile/TileEntityXPSolidifier.java | 14 +- .../actuallyadditions/mod/util/AwfulUtil.java | 1 + .../mod/util/ItemStackHandlerAA.java | 56 +++++- .../actuallyadditions/mod/util/StackUtil.java | 87 ++------- 30 files changed, 874 insertions(+), 920 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java index 310a41f03..bb640e1c3 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerBag.java @@ -41,7 +41,7 @@ import net.minecraftforge.fml.common.registry.ForgeRegistries; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class ContainerBag extends Container implements IButtonReactor{ +public class ContainerBag extends Container implements IButtonReactor { public final FilterSettings filter = new FilterSettings(4, false, true, false, false, 0, -1000); private final ItemStackHandlerAA bagInventory; @@ -51,36 +51,29 @@ public class ContainerBag extends Container implements IButtonReactor{ private boolean oldAutoInsert; private final ItemStack sack; - public ContainerBag(ItemStack sack, InventoryPlayer inventory, boolean isVoid){ + public ContainerBag(ItemStack sack, InventoryPlayer inventory, boolean isVoid) { this.inventory = inventory; - this.bagInventory = new ItemStackHandlerAA(getSlotAmount(isVoid)) { - @Override - public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { - if(isBlacklisted(stack)) return stack; - return super.insertItem(slot, stack, simulate); - }; - }; + this.bagInventory = new ItemStackHandlerAA(getSlotAmount(isVoid), (slot, stack, automation) -> !isBlacklisted(stack), ItemStackHandlerAA.REMOVE_TRUE); this.isVoid = isVoid; this.sack = sack; - for(int i = 0; i < 4; i++){ - this.addSlotToContainer(new SlotFilter(this.filter, i, 155, 10+i*18)); + for (int i = 0; i < 4; i++) { + this.addSlotToContainer(new SlotFilter(this.filter, i, 155, 10 + i * 18)); } - if(this.isVoid){ - this.addSlotToContainer(new SlotDeletion(this.bagInventory, 0, 64, 65){ + if (this.isVoid) { + this.addSlotToContainer(new SlotDeletion(this.bagInventory, 0, 64, 65) { @Override - public boolean isItemValid(ItemStack stack){ + public boolean isItemValid(ItemStack stack) { return ContainerBag.this.filter.check(stack); } }); - } - else{ - for(int i = 0; i < 4; i++){ - for(int j = 0; j < 7; j++){ - this.addSlotToContainer(new SlotItemHandlerUnconditioned(this.bagInventory, j+i*7, 10+j*18, 10+i*18){ + } else { + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 7; j++) { + this.addSlotToContainer(new SlotItemHandlerUnconditioned(this.bagInventory, j + i * 7, 10 + j * 18, 10 + i * 18) { @Override - public boolean isItemValid(ItemStack stack){ + public boolean isItemValid(ItemStack stack) { return !isBlacklisted(stack) && ContainerBag.this.filter.check(stack); } }); @@ -88,24 +81,23 @@ public class ContainerBag extends Container implements IButtonReactor{ } } - for(int i = 0; i < 3; i++){ - for(int j = 0; j < 9; j++){ - this.addSlotToContainer(new Slot(inventory, j+i*9+9, 8+j*18, 94+i*18)); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 9; j++) { + this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 94 + i * 18)); } } - for(int i = 0; i < 9; i++){ - if(i == inventory.currentItem){ - this.addSlotToContainer(new SlotImmovable(inventory, i, 8+i*18, 152)); - } - else{ - this.addSlotToContainer(new Slot(inventory, i, 8+i*18, 152)); + for (int i = 0; i < 9; i++) { + if (i == inventory.currentItem) { + this.addSlotToContainer(new SlotImmovable(inventory, i, 8 + i * 18, 152)); + } else { + this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 152)); } } ItemStack stack = inventory.getCurrentItem(); - if(StackUtil.isValid(stack) && stack.getItem() instanceof ItemBag){ + if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemBag) { ItemDrill.loadSlotsFromNBT(this.bagInventory, inventory.getCurrentItem()); - if(stack.hasTagCompound()){ + if (stack.hasTagCompound()) { NBTTagCompound compound = stack.getTagCompound(); this.filter.readFromNBT(compound, "Filter"); this.autoInsert = compound.getBoolean("AutoInsert"); @@ -113,16 +105,16 @@ public class ContainerBag extends Container implements IButtonReactor{ } } - public static int getSlotAmount(boolean isVoid){ + public static int getSlotAmount(boolean isVoid) { return isVoid ? 1 : 28; } @Override - public void detectAndSendChanges(){ + public void detectAndSendChanges() { super.detectAndSendChanges(); - if(this.filter.needsUpdateSend() || this.autoInsert != this.oldAutoInsert){ - for(IContainerListener listener : this.listeners){ + if (this.filter.needsUpdateSend() || this.autoInsert != this.oldAutoInsert) { + for (IContainerListener listener : this.listeners) { listener.sendWindowProperty(this, 0, this.filter.isWhitelist ? 1 : 0); listener.sendWindowProperty(this, 1, this.filter.respectMeta ? 1 : 0); listener.sendWindowProperty(this, 2, this.filter.respectNBT ? 1 : 0); @@ -137,70 +129,54 @@ public class ContainerBag extends Container implements IButtonReactor{ @Override @SideOnly(Side.CLIENT) - public void updateProgressBar(int id, int data){ - if(id == 0){ + public void updateProgressBar(int id, int data) { + if (id == 0) { this.filter.isWhitelist = data == 1; - } - else if(id == 1){ + } else if (id == 1) { this.filter.respectMeta = data == 1; - } - else if(id == 2){ + } else if (id == 2) { this.filter.respectNBT = data == 1; - } - else if(id == 3){ + } else if (id == 3) { this.filter.respectOredict = data; - } - else if(id == 4){ + } else if (id == 4) { this.autoInsert = data == 1; - } - else if(id == 5){ + } else if (id == 5) { this.filter.respectMod = data == 1; } } @Override - public ItemStack transferStackInSlot(EntityPlayer player, int slot){ - int inventoryStart = this.bagInventory.getSlots()+4; - int inventoryEnd = inventoryStart+26; - int hotbarStart = inventoryEnd+1; - int hotbarEnd = hotbarStart+8; + public ItemStack transferStackInSlot(EntityPlayer player, int slot) { + int inventoryStart = this.bagInventory.getSlots() + 4; + int inventoryEnd = inventoryStart + 26; + int hotbarStart = inventoryEnd + 1; + int hotbarEnd = hotbarStart + 8; Slot theSlot = this.inventorySlots.get(slot); - if(theSlot != null && theSlot.getHasStack()){ + if (theSlot != null && theSlot.getHasStack()) { ItemStack newStack = theSlot.getStack(); ItemStack currentStack = newStack.copy(); //Other Slots in Inventory excluded - if(slot >= inventoryStart){ + if (slot >= inventoryStart) { //Shift from Inventory - if(this.isVoid || !this.filter.check(newStack) || !this.mergeItemStack(newStack, 4, 32, false)){ - if(slot >= inventoryStart && slot <= inventoryEnd){ - if(!this.mergeItemStack(newStack, hotbarStart, hotbarEnd+1, false)){ - return StackUtil.getEmpty(); - } - } - else if(slot >= inventoryEnd+1 && slot < hotbarEnd+1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd+1, false)){ - return StackUtil.getEmpty(); - } + if (this.isVoid || !this.filter.check(newStack) || !this.mergeItemStack(newStack, 4, 32, false)) { + if (slot >= inventoryStart && slot <= inventoryEnd) { + if (!this.mergeItemStack(newStack, hotbarStart, hotbarEnd + 1, false)) { return StackUtil.getEmpty(); } + } else if (slot >= inventoryEnd + 1 && slot < hotbarEnd + 1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd + 1, false)) { return StackUtil.getEmpty(); } } // - } - else if(!this.mergeItemStack(newStack, inventoryStart, hotbarEnd+1, false)){ - return StackUtil.getEmpty(); - } + } else if (!this.mergeItemStack(newStack, inventoryStart, hotbarEnd + 1, false)) { return StackUtil.getEmpty(); } - if(!StackUtil.isValid(newStack)){ + if (!StackUtil.isValid(newStack)) { theSlot.putStack(StackUtil.getEmpty()); - } - else{ + } else { theSlot.onSlotChanged(); } - if(newStack.getCount() == currentStack.getCount()){ - return StackUtil.getEmpty(); - } + if (newStack.getCount() == currentStack.getCount()) { return StackUtil.getEmpty(); } theSlot.onTake(player, newStack); return currentStack; @@ -209,22 +185,20 @@ public class ContainerBag extends Container implements IButtonReactor{ } @Override - public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player){ - if(SlotFilter.checkFilter(this, slotId, player)){ + public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player) { + if (SlotFilter.checkFilter(this, slotId, player)) { return StackUtil.getEmpty(); - } - else if(clickTypeIn == ClickType.SWAP && dragType == this.inventory.currentItem){ + } else if (clickTypeIn == ClickType.SWAP && dragType == this.inventory.currentItem) { return ItemStack.EMPTY; - } - else{ + } else { return super.slotClick(slotId, dragType, clickTypeIn, player); } } @Override - public void onContainerClosed(EntityPlayer player){ + public void onContainerClosed(EntityPlayer player) { ItemStack stack = this.inventory.getCurrentItem(); - if(StackUtil.isValid(stack) && stack.getItem() instanceof ItemBag){ + if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemBag) { ItemDrill.writeSlotsToNBT(this.bagInventory, this.inventory.getCurrentItem()); NBTTagCompound compound = stack.getTagCompound(); this.filter.writeToNBT(compound, "Filter"); @@ -234,41 +208,39 @@ public class ContainerBag extends Container implements IButtonReactor{ } @Override - public boolean canInteractWith(EntityPlayer player){ + public boolean canInteractWith(EntityPlayer player) { return !sack.isEmpty() && player.getHeldItemMainhand() == sack; } @Override - public void onButtonPressed(int buttonID, EntityPlayer player){ - if(buttonID == 0){ + public void onButtonPressed(int buttonID, EntityPlayer player) { + if (buttonID == 0) { this.autoInsert = !this.autoInsert; - } - else{ + } else { this.filter.onButtonPressed(buttonID); } } - + private static final List> BLACKLIST = new ArrayList<>(); - + private static boolean runOnce = false; - + public static boolean isBlacklisted(ItemStack stack) { - if(!runOnce) { - runOnce = true; - for(String s : ConfigStringListValues.SACK_BLACKLIST.getValue()) { - String[] split = s.split("@"); - Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(split[0])); - if(item == null) { - ActuallyAdditions.LOGGER.error("Invalid item in sack blacklist: " + s); - continue; - } - if(split.length == 1) - BLACKLIST.add(Pair.of(item, 0)); - else if(split.length == 2) { - BLACKLIST.add(Pair.of(item, Integer.parseInt(split[1]))); - } - } - } - return BLACKLIST.contains(Pair.of(stack.getItem(), stack.getMetadata())); + if (!runOnce) { + runOnce = true; + for (String s : ConfigStringListValues.SACK_BLACKLIST.getValue()) { + String[] split = s.split("@"); + Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(split[0])); + if (item == null) { + ActuallyAdditions.LOGGER.error("Invalid item in sack blacklist: " + s); + continue; + } + if (split.length == 1) BLACKLIST.add(Pair.of(item, 0)); + else if (split.length == 2) { + BLACKLIST.add(Pair.of(item, Integer.parseInt(split[1]))); + } + } + } + return BLACKLIST.contains(Pair.of(stack.getItem(), stack.getMetadata())); } } \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityAtomicReconstructor.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityAtomicReconstructor.java index 75202f261..7eb6e947f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityAtomicReconstructor.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityAtomicReconstructor.java @@ -18,11 +18,11 @@ import de.ellpeck.actuallyadditions.mod.blocks.InitBlocks; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.misc.SoundHandler; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.state.IBlockState; import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraft.util.SoundCategory; @@ -143,13 +143,8 @@ public class TileEntityAtomicReconstructor extends TileEntityInventoryBase imple } @Override - public boolean canInsert(int i, ItemStack stack, boolean fromAutomation) { - return StackUtil.isValid(stack) && stack.getItem() instanceof ILensItem; - } - - @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation) { - return true; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> StackUtil.isValid(stack) && stack.getItem() instanceof ILensItem; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBatteryBox.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBatteryBox.java index c3b4349ca..ee025aeb9 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBatteryBox.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBatteryBox.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.List; import de.ellpeck.actuallyadditions.mod.items.ItemBattery; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.item.ItemStack; @@ -144,13 +145,8 @@ public class TileEntityBatteryBox extends TileEntityInventoryBase implements ISh } @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation){ - return true; - } - - @Override - public boolean canInsert(int slot, ItemStack stack, boolean fromAutomation){ - return stack.getItem() instanceof ItemBattery; + public IAcceptor getAcceptor(){ + return (slot, stack, automation) -> stack.getItem() instanceof ItemBattery; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBioReactor.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBioReactor.java index 97e6ec0ef..6337603c4 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBioReactor.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBioReactor.java @@ -10,6 +10,12 @@ package de.ellpeck.actuallyadditions.mod.tile; +import java.util.ArrayList; +import java.util.List; + +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.block.Block; import net.minecraft.block.IGrowable; @@ -23,10 +29,7 @@ import net.minecraft.util.EnumFacing; import net.minecraftforge.common.IPlantable; import net.minecraftforge.energy.IEnergyStorage; -import java.util.ArrayList; -import java.util.List; - -public class TileEntityBioReactor extends TileEntityInventoryBase implements ISharingEnergyProvider{ +public class TileEntityBioReactor extends TileEntityInventoryBase implements ISharingEnergyProvider { public final CustomEnergyStorage storage = new CustomEnergyStorage(200000, 0, 800); @@ -37,41 +40,38 @@ public class TileEntityBioReactor extends TileEntityInventoryBase implements ISh private int lastBurnTime; private int lastProducePerTick; - public TileEntityBioReactor(){ + public TileEntityBioReactor() { super(8, "bioReactor"); } - public static boolean isValidItem(ItemStack stack){ - if(StackUtil.isValid(stack)){ + public static boolean isValidItem(ItemStack stack) { + if (StackUtil.isValid(stack)) { Item item = stack.getItem(); - if(isValid(item)){ + if (isValid(item)) { return true; - } - else if(item instanceof ItemBlock){ - return isValid(Block.getBlockFromItem(item)); - } + } else if (item instanceof ItemBlock) { return isValid(Block.getBlockFromItem(item)); } } return false; } - private static boolean isValid(Object o){ + private static boolean isValid(Object o) { return o instanceof IPlantable || o instanceof IGrowable || o instanceof ItemFood; } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(this.burnTime <= 0){ + if (this.burnTime <= 0) { List types = null; - if(!this.isRedstonePowered && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()){ - for(int i = 0; i < this.inv.getSlots(); i++){ + if (!this.isRedstonePowered && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) { + for (int i = 0; i < this.inv.getSlots(); i++) { ItemStack stack = this.inv.getStackInSlot(i); - if(StackUtil.isValid(stack)){ + if (StackUtil.isValid(stack)) { Item item = stack.getItem(); - if(isValidItem(stack) && (types == null || !types.contains(item))){ - if(types == null){ + if (isValidItem(stack) && (types == null || !types.contains(item))) { + if (types == null) { types = new ArrayList(); } types.add(item); @@ -84,32 +84,30 @@ public class TileEntityBioReactor extends TileEntityInventoryBase implements ISh this.markDirty(); } - if(types != null && !types.isEmpty()){ + if (types != null && !types.isEmpty()) { int amount = types.size(); - this.producePerTick = (int)Math.pow(amount*2, 2); + this.producePerTick = (int) Math.pow(amount * 2, 2); - this.maxBurnTime = 200-(int)Math.pow(1.8, amount); + this.maxBurnTime = 200 - (int) Math.pow(1.8, amount); this.burnTime = this.maxBurnTime; - } - else{ + } else { this.burnTime = 0; this.maxBurnTime = 0; this.producePerTick = 0; } - } - else{ + } else { this.burnTime--; this.storage.receiveEnergyInternal(this.producePerTick, false); } - if((this.lastBurnTime != this.burnTime || this.lastProducePerTick != this.producePerTick) && this.sendUpdateWithInterval()){ + if ((this.lastBurnTime != this.burnTime || this.lastProducePerTick != this.producePerTick) && this.sendUpdateWithInterval()) { this.lastBurnTime = this.burnTime; this.lastProducePerTick = this.producePerTick; } } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); this.storage.writeToNBT(compound); @@ -119,7 +117,7 @@ public class TileEntityBioReactor extends TileEntityInventoryBase implements ISh } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { super.readSyncableNBT(compound, type); this.storage.readFromNBT(compound); @@ -129,43 +127,43 @@ public class TileEntityBioReactor extends TileEntityInventoryBase implements ISh } @Override - public boolean canExtract(int index, ItemStack stack, boolean byAutomation){ - return false; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> isValidItem(stack); } @Override - public boolean canInsert(int index, ItemStack stack, boolean fromAutomation){ - return isValidItem(stack); + public IRemover getRemover() { + return ItemStackHandlerAA.REMOVE_FALSE; } @Override - public int getEnergyToSplitShare(){ + public int getEnergyToSplitShare() { return this.storage.getEnergyStored(); } @Override - public boolean doesShareEnergy(){ + public boolean doesShareEnergy() { return true; } @Override - public EnumFacing[] getEnergyShareSides(){ + public EnumFacing[] getEnergyShareSides() { return EnumFacing.values(); } @Override - public boolean canShareTo(TileEntity tile){ + public boolean canShareTo(TileEntity tile) { return true; } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } @Override - public int getComparatorStrength(){ - float calc = ((float)this.storage.getEnergyStored()/(float)this.storage.getMaxEnergyStored())*15F; - return (int)calc; + public int getComparatorStrength() { + float calc = ((float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored()) * 15F; + return (int) calc; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java index fa35ba264..39ea3d00a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java @@ -10,6 +10,7 @@ package de.ellpeck.actuallyadditions.mod.tile; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.Block; @@ -25,90 +26,89 @@ import net.minecraftforge.fluids.IFluidBlock; public class TileEntityBreaker extends TileEntityInventoryBase { - public boolean isPlacer; - private int currentTime; + public boolean isPlacer; + private int currentTime; - public TileEntityBreaker(int slots, String name) { - super(slots, name); - } + public TileEntityBreaker(int slots, String name) { + super(slots, name); + } - public TileEntityBreaker() { - super(9, "breaker"); - this.isPlacer = false; - } + public TileEntityBreaker() { + super(9, "breaker"); + this.isPlacer = false; + } - @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { - super.writeSyncableNBT(compound, type); - if (type != NBTType.SAVE_BLOCK) { - compound.setInteger("CurrentTime", this.currentTime); - } - } + @Override + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { + super.writeSyncableNBT(compound, type); + if (type != NBTType.SAVE_BLOCK) { + compound.setInteger("CurrentTime", this.currentTime); + } + } - @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type) { - super.readSyncableNBT(compound, type); - if (type != NBTType.SAVE_BLOCK) { - this.currentTime = compound.getInteger("CurrentTime"); - } - } + @Override + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + super.readSyncableNBT(compound, type); + if (type != NBTType.SAVE_BLOCK) { + this.currentTime = compound.getInteger("CurrentTime"); + } + } - @Override - public void updateEntity() { - super.updateEntity(); - if (!this.world.isRemote) { - if (!this.isRedstonePowered && !this.isPulseMode) { - if (this.currentTime > 0) { - this.currentTime--; - if (this.currentTime <= 0) { - this.doWork(); - } - } else { - this.currentTime = 15; - } - } - } - } - - @Override - public boolean canInsert(int slot, ItemStack stack, boolean automation) { - if(isPlacer) return true; - else return !automation; - } + @Override + public void updateEntity() { + super.updateEntity(); + if (!this.world.isRemote) { + if (!this.isRedstonePowered && !this.isPulseMode) { + if (this.currentTime > 0) { + this.currentTime--; + if (this.currentTime <= 0) { + this.doWork(); + } + } else { + this.currentTime = 15; + } + } + } + } - private void doWork() { - EnumFacing side = WorldUtil.getDirectionByPistonRotation(world.getBlockState(pos)); - BlockPos breakCoords = pos.offset(side); - IBlockState stateToBreak = world.getBlockState(breakCoords); - Block blockToBreak = stateToBreak.getBlock(); + @Override + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation; + } - if (!this.isPlacer && blockToBreak != Blocks.AIR && !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && stateToBreak.getBlockHardness(this.world, breakCoords) >= 0.0F) { - NonNullList drops = NonNullList.create(); - blockToBreak.getDrops(drops, world, breakCoords, stateToBreak, 0); - float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, world, breakCoords); + private void doWork() { + EnumFacing side = WorldUtil.getDirectionByPistonRotation(world.getBlockState(pos)); + BlockPos breakCoords = pos.offset(side); + IBlockState stateToBreak = world.getBlockState(breakCoords); + Block blockToBreak = stateToBreak.getBlock(); - if (chance > 0 && world.rand.nextFloat() <= chance) { - if (StackUtil.canAddAll(inv, drops, false)) { - world.destroyBlock(breakCoords, false); - StackUtil.addAll(inv, drops, false); - this.markDirty(); - } - } - } else if (this.isPlacer) { - int slot = StackUtil.findFirstFilled(inv); - if(slot == -1) return; - this.inv.setStackInSlot(slot, WorldUtil.useItemAtSide(side, world, pos, inv.getStackInSlot(slot))); - } - } + if (!this.isPlacer && blockToBreak != Blocks.AIR && !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && stateToBreak.getBlockHardness(this.world, breakCoords) >= 0.0F) { + NonNullList drops = NonNullList.create(); + blockToBreak.getDrops(drops, world, breakCoords, stateToBreak, 0); + float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, world, breakCoords); - @Override - public boolean isRedstoneToggle() { - return true; - } + if (chance > 0 && world.rand.nextFloat() <= chance) { + if (StackUtil.canAddAll(inv, drops, false)) { + world.destroyBlock(breakCoords, false); + StackUtil.addAll(inv, drops, false); + this.markDirty(); + } + } + } else if (this.isPlacer) { + int slot = StackUtil.findFirstFilled(inv); + if (slot == -1) return; + this.inv.setStackInSlot(slot, WorldUtil.useItemAtSide(side, world, pos, inv.getStackInSlot(slot))); + } + } - @Override - public void activateOnPulse() { - this.doWork(); - } + @Override + public boolean isRedstoneToggle() { + return true; + } + + @Override + public void activateOnPulse() { + this.doWork(); + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java index 317a09ef1..12adb463e 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java @@ -13,6 +13,8 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.fluids.InitFluids; import de.ellpeck.actuallyadditions.mod.items.InitItems; import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.Util; import net.minecraft.item.ItemStack; @@ -24,15 +26,15 @@ import net.minecraftforge.fluids.FluidTank; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class TileEntityCanolaPress extends TileEntityInventoryBase implements ISharingFluidHandler{ +public class TileEntityCanolaPress extends TileEntityInventoryBase implements ISharingFluidHandler { public static final int PRODUCE = 80; public static final int ENERGY_USE = 35; private static final int TIME = 30; public final CustomEnergyStorage storage = new CustomEnergyStorage(40000, 100, 0); - public final FluidTank tank = new FluidTank(2*Util.BUCKET){ + public final FluidTank tank = new FluidTank(2 * Util.BUCKET) { @Override - public boolean canFill(){ + public boolean canFill() { return false; } }; @@ -41,28 +43,28 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IS private int lastTankAmount; private int lastProcessTime; - public TileEntityCanolaPress(){ + public TileEntityCanolaPress() { super(1, "canolaPress"); } @SideOnly(Side.CLIENT) - public int getTankScaled(int i){ - return this.tank.getFluidAmount()*i/this.tank.getCapacity(); + public int getTankScaled(int i) { + return this.tank.getFluidAmount() * i / this.tank.getCapacity(); } @SideOnly(Side.CLIENT) - public int getProcessScaled(int i){ - return this.currentProcessTime*i/TIME; + public int getProcessScaled(int i) { + return this.currentProcessTime * i / TIME; } @SideOnly(Side.CLIENT) - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("ProcessTime", this.currentProcessTime); } this.storage.writeToNBT(compound); @@ -71,8 +73,8 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IS } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { this.currentProcessTime = compound.getInteger("ProcessTime"); } this.storage.readFromNBT(compound); @@ -81,14 +83,14 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IS } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ - if(isCanola(inv.getStackInSlot(0)) && PRODUCE <= this.tank.getCapacity()-this.tank.getFluidAmount()){ - if(this.storage.getEnergyStored() >= ENERGY_USE){ + if (!this.world.isRemote) { + if (isCanola(inv.getStackInSlot(0)) && PRODUCE <= this.tank.getCapacity() - this.tank.getFluidAmount()) { + if (this.storage.getEnergyStored() >= ENERGY_USE) { this.currentProcessTime++; this.storage.extractEnergyInternal(ENERGY_USE, false); - if(this.currentProcessTime >= TIME){ + if (this.currentProcessTime >= TIME) { this.currentProcessTime = 0; this.inv.setStackInSlot(0, StackUtil.shrink(this.inv.getStackInSlot(0), 1)); @@ -97,12 +99,11 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IS this.markDirty(); } } - } - else{ + } else { this.currentProcessTime = 0; } - if((this.storage.getEnergyStored() != this.lastEnergyStored || this.tank.getFluidAmount() != this.lastTankAmount | this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()){ + if ((this.storage.getEnergyStored() != this.lastEnergyStored || this.tank.getFluidAmount() != this.lastTankAmount | this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()) { this.lastEnergyStored = this.storage.getEnergyStored(); this.lastProcessTime = this.currentProcessTime; this.lastTankAmount = this.tank.getFluidAmount(); @@ -111,41 +112,41 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IS } @Override - public boolean canInsert(int slot, ItemStack stack, boolean fromAutomation){ - return (slot == 0 && isCanola(stack)); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> slot == 0 && isCanola(stack); } - public static boolean isCanola(ItemStack stack){ + public static boolean isCanola(ItemStack stack) { return stack.getItem() == InitItems.itemMisc && stack.getMetadata() == TheMiscItems.CANOLA.ordinal(); } @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation){ - return !byAutomation; + public IRemover getRemover() { + return (slot, automation) -> !automation; } @Override - public FluidTank getFluidHandler(EnumFacing facing){ + public FluidTank getFluidHandler(EnumFacing facing) { return this.tank; } @Override - public int getMaxFluidAmountToSplitShare(){ + public int getMaxFluidAmountToSplitShare() { return this.tank.getFluidAmount(); } @Override - public boolean doesShareFluid(){ + public boolean doesShareFluid() { return true; } @Override - public EnumFacing[] getFluidShareSides(){ + public EnumFacing[] getFluidShareSides() { return EnumFacing.values(); } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java index 2c7b86178..380358e09 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java @@ -10,6 +10,8 @@ package de.ellpeck.actuallyadditions.mod.tile; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -19,7 +21,7 @@ import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class TileEntityCoalGenerator extends TileEntityInventoryBase implements ISharingEnergyProvider{ +public class TileEntityCoalGenerator extends TileEntityInventoryBase implements ISharingEnergyProvider { public static final int PRODUCE = 30; public final CustomEnergyStorage storage = new CustomEnergyStorage(60000, 0, 80); @@ -30,23 +32,23 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements private int lastCurrentBurnTime; private int lastCompare; - public TileEntityCoalGenerator(){ + public TileEntityCoalGenerator() { super(1, "coalGenerator"); } @SideOnly(Side.CLIENT) - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } @SideOnly(Side.CLIENT) - public int getBurningScaled(int i){ - return this.currentBurnTime*i/this.maxBurnTime; + public int getBurningScaled(int i) { + return this.currentBurnTime * i / this.maxBurnTime; } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("BurnTime", this.currentBurnTime); compound.setInteger("MaxBurnTime", this.maxBurnTime); } @@ -55,8 +57,8 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { this.currentBurnTime = compound.getInteger("BurnTime"); this.maxBurnTime = compound.getInteger("MaxBurnTime"); } @@ -65,32 +67,32 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ + if (!this.world.isRemote) { boolean flag = this.currentBurnTime > 0; - if(this.currentBurnTime > 0){ + if (this.currentBurnTime > 0) { this.currentBurnTime--; this.storage.receiveEnergyInternal(PRODUCE, false); } ItemStack stack = inv.getStackInSlot(0); int burn = TileEntityFurnace.getItemBurnTime(stack); - if(!this.isRedstonePowered && this.currentBurnTime <= 0 && burn > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()){ + if (!this.isRedstonePowered && this.currentBurnTime <= 0 && burn > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) { this.maxBurnTime = burn; this.currentBurnTime = burn; ItemStack copy = stack.copy(); stack.shrink(1); - if(stack.isEmpty()) inv.setStackInSlot(0, copy.getItem().getContainerItem(copy)); + if (stack.isEmpty()) inv.setStackInSlot(0, copy.getItem().getContainerItem(copy)); } - if(flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()){ + if (flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()) { this.lastCompare = this.getComparatorStrength(); this.markDirty(); } - if((this.storage.getEnergyStored() != this.lastEnergy || this.currentBurnTime != this.lastCurrentBurnTime || this.lastBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()){ + if ((this.storage.getEnergyStored() != this.lastEnergy || this.currentBurnTime != this.lastCurrentBurnTime || this.lastBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); this.lastCurrentBurnTime = this.currentBurnTime; this.lastBurnTime = this.currentBurnTime; @@ -99,44 +101,46 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements } @Override - public int getComparatorStrength(){ - float calc = ((float)this.storage.getEnergyStored()/(float)this.storage.getMaxEnergyStored())*15F; - return (int)calc; + public int getComparatorStrength() { + float calc = ((float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored()) * 15F; + return (int) calc; } @Override - public boolean canInsert(int i, ItemStack stack, boolean fromAutomation){ - return TileEntityFurnace.getItemBurnTime(stack) > 0; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> TileEntityFurnace.getItemBurnTime(stack) > 0; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation){ - if(!byAutomation) return true; - return TileEntityFurnace.getItemBurnTime(this.inv.getStackInSlot(0)) <= 0; + public IRemover getRemover() { + return (slot, automation) -> { + if (!automation) return true; + return TileEntityFurnace.getItemBurnTime(this.inv.getStackInSlot(0)) <= 0; + }; } @Override - public int getEnergyToSplitShare(){ + public int getEnergyToSplitShare() { return this.storage.getEnergyStored(); } @Override - public boolean doesShareEnergy(){ + public boolean doesShareEnergy() { return true; } @Override - public EnumFacing[] getEnergyShareSides(){ + public EnumFacing[] getEnergyShareSides() { return EnumFacing.values(); } @Override - public boolean canShareTo(TileEntity tile){ + public boolean canShareTo(TileEntity tile) { return true; } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java index 209fd37a0..9b8b5b276 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java @@ -16,6 +16,8 @@ import de.ellpeck.actuallyadditions.mod.items.ItemCoffee; import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems; import de.ellpeck.actuallyadditions.mod.misc.SoundHandler; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.Util; import net.minecraft.entity.player.EntityPlayer; @@ -30,7 +32,7 @@ import net.minecraftforge.fluids.FluidTank; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, ISharingFluidHandler{ +public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, ISharingFluidHandler { public static final int SLOT_COFFEE_BEANS = 0; public static final int SLOT_INPUT = 1; @@ -41,14 +43,14 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements public static final int COFFEE_CACHE_MAX_AMOUNT = 300; private static final int TIME_USED = 500; public final CustomEnergyStorage storage = new CustomEnergyStorage(300000, 250, 0); - public final FluidTank tank = new FluidTank(4*Util.BUCKET){ + public final FluidTank tank = new FluidTank(4 * Util.BUCKET) { @Override - public boolean canDrain(){ + public boolean canDrain() { return false; } @Override - public boolean canFillFluidType(FluidStack fluid){ + public boolean canFillFluidType(FluidStack fluid) { return fluid.getFluid() == FluidRegistry.WATER; } }; @@ -59,63 +61,63 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements private int lastCoffeeAmount; private int lastBrewTime; - public TileEntityCoffeeMachine(){ + public TileEntityCoffeeMachine() { super(11, "coffeeMachine"); } @SideOnly(Side.CLIENT) - public int getCoffeeScaled(int i){ - return this.coffeeCacheAmount*i/COFFEE_CACHE_MAX_AMOUNT; + public int getCoffeeScaled(int i) { + return this.coffeeCacheAmount * i / COFFEE_CACHE_MAX_AMOUNT; } @SideOnly(Side.CLIENT) - public int getWaterScaled(int i){ - return this.tank.getFluidAmount()*i/this.tank.getCapacity(); + public int getWaterScaled(int i) { + return this.tank.getFluidAmount() * i / this.tank.getCapacity(); } @SideOnly(Side.CLIENT) - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } @SideOnly(Side.CLIENT) - public int getBrewScaled(int i){ - return this.brewTime*i/TIME_USED; + public int getBrewScaled(int i) { + return this.brewTime * i / TIME_USED; } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); this.storage.writeToNBT(compound); this.tank.writeToNBT(compound); compound.setInteger("Cache", this.coffeeCacheAmount); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("Time", this.brewTime); } } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { super.readSyncableNBT(compound, type); this.storage.readFromNBT(compound); this.tank.readFromNBT(compound); this.coffeeCacheAmount = compound.getInteger("Cache"); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { this.brewTime = compound.getInteger("Time"); } } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ + if (!this.world.isRemote) { this.storeCoffee(); - if(this.brewTime > 0 || this.isRedstonePowered){ + if (this.brewTime > 0 || this.isRedstonePowered) { this.brew(); } - if((this.coffeeCacheAmount != this.lastCoffeeAmount || this.storage.getEnergyStored() != this.lastEnergy || this.tank.getFluidAmount() != this.lastTank || this.brewTime != this.lastBrewTime) && this.sendUpdateWithInterval()){ + if ((this.coffeeCacheAmount != this.lastCoffeeAmount || this.storage.getEnergyStored() != this.lastEnergy || this.tank.getFluidAmount() != this.lastTank || this.brewTime != this.lastBrewTime) && this.sendUpdateWithInterval()) { this.lastCoffeeAmount = this.coffeeCacheAmount; this.lastEnergy = this.storage.getEnergyStored(); this.lastTank = this.tank.getFluidAmount(); @@ -125,39 +127,44 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return (i >= 3 && ItemCoffee.getIngredientFromStack(stack) != null) || (i == SLOT_COFFEE_BEANS && stack.getItem() == InitItems.itemCoffeeBean) || (i == SLOT_INPUT && stack.getItem() == InitItems.itemMisc && stack.getItemDamage() == TheMiscItems.CUP.ordinal()); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> (slot >= 3 && ItemCoffee.getIngredientFromStack(stack) != null) || (slot == SLOT_COFFEE_BEANS && stack.getItem() == InitItems.itemCoffeeBean) || (slot == SLOT_INPUT && stack.getItem() == InitItems.itemMisc && stack.getItemDamage() == TheMiscItems.CUP.ordinal()); } - public void storeCoffee(){ - if(StackUtil.isValid(this.inv.getStackInSlot(SLOT_COFFEE_BEANS)) && this.inv.getStackInSlot(SLOT_COFFEE_BEANS).getItem() == InitItems.itemCoffeeBean){ + @Override + public IRemover getRemover() { + return (slot, automation) -> slot == SLOT_OUTPUT || (slot >= 3 && slot < this.inv.getSlots() && ItemCoffee.getIngredientFromStack(inv.getStackInSlot(slot)) == null); + } + + public void storeCoffee() { + if (StackUtil.isValid(this.inv.getStackInSlot(SLOT_COFFEE_BEANS)) && this.inv.getStackInSlot(SLOT_COFFEE_BEANS).getItem() == InitItems.itemCoffeeBean) { int toAdd = 2; - if(toAdd <= COFFEE_CACHE_MAX_AMOUNT-this.coffeeCacheAmount){ + if (toAdd <= COFFEE_CACHE_MAX_AMOUNT - this.coffeeCacheAmount) { this.inv.setStackInSlot(SLOT_COFFEE_BEANS, StackUtil.shrink(this.inv.getStackInSlot(SLOT_COFFEE_BEANS), 1)); this.coffeeCacheAmount += toAdd; } } } - public void brew(){ - if(!this.world.isRemote){ + public void brew() { + if (!this.world.isRemote) { ItemStack input = this.inv.getStackInSlot(SLOT_INPUT); - if (StackUtil.isValid(input) && input.getItem() == InitItems.itemMisc && input.getItemDamage() == TheMiscItems.CUP.ordinal() && !StackUtil.isValid(this.inv.getStackInSlot(SLOT_OUTPUT)) && this.coffeeCacheAmount >= CACHE_USE && this.tank.getFluid() != null && this.tank.getFluid().getFluid() == FluidRegistry.WATER && this.tank.getFluidAmount() >= WATER_USE){ - if(this.storage.getEnergyStored() >= ENERGY_USED){ - if(this.brewTime%30 == 0){ + if (StackUtil.isValid(input) && input.getItem() == InitItems.itemMisc && input.getItemDamage() == TheMiscItems.CUP.ordinal() && !StackUtil.isValid(this.inv.getStackInSlot(SLOT_OUTPUT)) && this.coffeeCacheAmount >= CACHE_USE && this.tank.getFluid() != null && this.tank.getFluid().getFluid() == FluidRegistry.WATER && this.tank.getFluidAmount() >= WATER_USE) { + if (this.storage.getEnergyStored() >= ENERGY_USED) { + if (this.brewTime % 30 == 0) { this.world.playSound(null, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), SoundHandler.coffeeMachine, SoundCategory.BLOCKS, 0.35F, 1.0F); } this.brewTime++; this.storage.extractEnergyInternal(ENERGY_USED, false); - if(this.brewTime >= TIME_USED){ + if (this.brewTime >= TIME_USED) { this.brewTime = 0; ItemStack output = new ItemStack(InitItems.itemCoffee); - for(int i = 3; i < this.inv.getSlots(); i++){ - if(StackUtil.isValid(this.inv.getStackInSlot(i))){ + for (int i = 3; i < this.inv.getSlots(); i++) { + if (StackUtil.isValid(this.inv.getStackInSlot(i))) { CoffeeIngredient ingredient = ItemCoffee.getIngredientFromStack(this.inv.getStackInSlot(i)); - if(ingredient != null){ - if(ingredient.effect(output)){ + if (ingredient != null) { + if (ingredient.effect(output)) { this.inv.setStackInSlot(i, StackUtil.shrinkForContainer(this.inv.getStackInSlot(i), 1)); } } @@ -169,47 +176,41 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements this.tank.drainInternal(WATER_USE, true); } } - } - else{ + } else { this.brewTime = 0; } } } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return slot == SLOT_OUTPUT || (slot >= 3 && slot < this.inv.getSlots() && ItemCoffee.getIngredientFromStack(stack) == null); - } - - @Override - public void onButtonPressed(int buttonID, EntityPlayer player){ - if(buttonID == 0 && this.brewTime <= 0){ + public void onButtonPressed(int buttonID, EntityPlayer player) { + if (buttonID == 0 && this.brewTime <= 0) { this.brew(); } } @Override - public FluidTank getFluidHandler(EnumFacing facing){ + public FluidTank getFluidHandler(EnumFacing facing) { return this.tank; } @Override - public int getMaxFluidAmountToSplitShare(){ + public int getMaxFluidAmountToSplitShare() { return 0; } @Override - public boolean doesShareFluid(){ + public boolean doesShareFluid() { return false; } @Override - public EnumFacing[] getFluidShareSides(){ + public EnumFacing[] getFluidShareSides() { return null; } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCompost.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCompost.java index f4d55b054..f87b5d002 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCompost.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCompost.java @@ -12,6 +12,8 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.recipe.CompostRecipe; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.block.state.IBlockState; @@ -90,13 +92,13 @@ public class TileEntityCompost extends TileEntityInventoryBase { } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation) { - return getRecipeForInput(stack) != null; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> getRecipeForInput(stack) != null; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation) { - return getRecipeForInput(stack) == null; + public IRemover getRemover() { + return (slot, automation) -> getRecipeForInput(inv.getStackInSlot(slot)) == null; } public IBlockState getCurrentDisplay() { @@ -109,7 +111,7 @@ public class TileEntityCompost extends TileEntityInventoryBase { else if (r.getInput().apply(input)) return r.getInputDisplay(); } - if(displayRecipe != null) return displayRecipe.getInputDisplay(); + if (displayRecipe != null) return displayRecipe.getInputDisplay(); return Blocks.AIR.getDefaultState(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java index a6e217404..bc9b5a8db 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java @@ -10,6 +10,7 @@ package de.ellpeck.actuallyadditions.mod.tile; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.Block; @@ -21,7 +22,7 @@ import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraftforge.energy.IEnergyStorage; -public class TileEntityDirectionalBreaker extends TileEntityInventoryBase{ +public class TileEntityDirectionalBreaker extends TileEntityInventoryBase { public static final int RANGE = 8; public static final int ENERGY_USE = 5; @@ -29,66 +30,65 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase{ private int lastEnergy; private int currentTime; - public TileEntityDirectionalBreaker(){ + public TileEntityDirectionalBreaker() { super(9, "directionalBreaker"); } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); this.storage.writeToNBT(compound); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("CurrentTime", this.currentTime); } } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { super.readSyncableNBT(compound, type); this.storage.readFromNBT(compound); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { this.currentTime = compound.getInteger("CurrentTime"); } } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ - if(!this.isRedstonePowered && !this.isPulseMode){ - if(this.currentTime > 0){ + if (!this.world.isRemote) { + if (!this.isRedstonePowered && !this.isPulseMode) { + if (this.currentTime > 0) { this.currentTime--; - if(this.currentTime <= 0){ + if (this.currentTime <= 0) { this.doWork(); } - } - else{ + } else { this.currentTime = 15; } } - if(this.storage.getEnergyStored() != this.lastEnergy && this.sendUpdateWithInterval()){ + if (this.storage.getEnergyStored() != this.lastEnergy && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); } } } - private void doWork(){ - if(this.storage.getEnergyStored() >= ENERGY_USE*RANGE){ + private void doWork() { + if (this.storage.getEnergyStored() >= ENERGY_USE * RANGE) { IBlockState state = this.world.getBlockState(this.pos); EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(state); - for(int i = 0; i < RANGE; i++){ - BlockPos coordsBlock = this.pos.offset(sideToManipulate, i+1); + for (int i = 0; i < RANGE; i++) { + BlockPos coordsBlock = this.pos.offset(sideToManipulate, i + 1); IBlockState breakState = world.getBlockState(coordsBlock); Block blockToBreak = breakState.getBlock(); - if(blockToBreak != null && !this.world.isAirBlock(coordsBlock) && this.world.getBlockState(coordsBlock).getBlockHardness(this.world, coordsBlock) > -1.0F){ - NonNullList drops = NonNullList.create(); + if (blockToBreak != null && !this.world.isAirBlock(coordsBlock) && this.world.getBlockState(coordsBlock).getBlockHardness(this.world, coordsBlock) > -1.0F) { + NonNullList drops = NonNullList.create(); blockToBreak.getDrops(drops, world, coordsBlock, breakState, 0); float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.world, coordsBlock); - if(chance > 0 && this.world.rand.nextFloat() <= chance){ - if(StackUtil.canAddAll(this.inv, drops, false)){ + if (chance > 0 && this.world.rand.nextFloat() <= chance) { + if (StackUtil.canAddAll(this.inv, drops, false)) { this.world.playEvent(2001, coordsBlock, Block.getStateId(this.world.getBlockState(coordsBlock))); this.world.setBlockToAir(coordsBlock); StackUtil.addAll(this.inv, drops, false); @@ -102,31 +102,26 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase{ } @Override - public boolean canInsert(int slot, ItemStack stack, boolean automation){ - return !automation; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation; } - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ + public boolean isRedstoneToggle() { return true; } @Override - public boolean isRedstoneToggle(){ - return true; - } - - @Override - public void activateOnPulse(){ + public void activateOnPulse() { this.doWork(); } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEmpowerer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEmpowerer.java index 257800579..d067463d6 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEmpowerer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEmpowerer.java @@ -17,6 +17,8 @@ import javax.annotation.Nullable; import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.recipe.EmpowererRecipe; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -48,9 +50,10 @@ public class TileEntityEmpowerer extends TileEntityInventoryBase { } return recipesThatWork; } - + public static boolean isPossibleInput(ItemStack stack) { - for(EmpowererRecipe r : ActuallyAdditionsAPI.EMPOWERER_RECIPES) if(r.getInput().apply(stack)) return true; + for (EmpowererRecipe r : ActuallyAdditionsAPI.EMPOWERER_RECIPES) + if (r.getInput().apply(stack)) return true; return false; } @@ -153,13 +156,13 @@ public class TileEntityEmpowerer extends TileEntityInventoryBase { } @Override - public boolean canInsert(int index, ItemStack stack, boolean automation) { - return !automation || !getRecipesForInput(stack).isEmpty(); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || isPossibleInput(stack); } @Override - public boolean canExtract(int index, ItemStack stack, boolean automation) { - return !automation || getRecipesForInput(stack).isEmpty(); + public IRemover getRemover() { + return (slot, automation) -> !automation || !isPossibleInput(inv.getStackInSlot(0)); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnergizer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnergizer.java index a12be9d72..31e8e1f76 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnergizer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnergizer.java @@ -10,84 +10,85 @@ package de.ellpeck.actuallyadditions.mod.tile; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.enchantment.EnchantmentHelper; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; -public class TileEntityEnergizer extends TileEntityInventoryBase{ +public class TileEntityEnergizer extends TileEntityInventoryBase { public final CustomEnergyStorage storage = new CustomEnergyStorage(50000, 1000, 0); private int lastEnergy; - public TileEntityEnergizer(){ + public TileEntityEnergizer() { super(2, "energizer"); } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { this.storage.writeToNBT(compound); super.writeSyncableNBT(compound, type); } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { this.storage.readFromNBT(compound); super.readSyncableNBT(compound, type); } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ - if(StackUtil.isValid(this.inv.getStackInSlot(0)) && !StackUtil.isValid(this.inv.getStackInSlot(1))){ - if(this.storage.getEnergyStored() > 0){ + if (!this.world.isRemote) { + if (StackUtil.isValid(this.inv.getStackInSlot(0)) && !StackUtil.isValid(this.inv.getStackInSlot(1))) { + if (this.storage.getEnergyStored() > 0) { int received = 0; boolean canTakeUp = false; - if(this.inv.getStackInSlot(0).hasCapability(CapabilityEnergy.ENERGY, null)){ + if (this.inv.getStackInSlot(0).hasCapability(CapabilityEnergy.ENERGY, null)) { IEnergyStorage cap = this.inv.getStackInSlot(0).getCapability(CapabilityEnergy.ENERGY, null); - if(cap != null){ + if (cap != null) { received = cap.receiveEnergy(this.storage.getEnergyStored(), false); canTakeUp = cap.getEnergyStored() >= cap.getMaxEnergyStored(); } } - if(received > 0){ + if (received > 0) { this.storage.extractEnergyInternal(received, false); } - if(canTakeUp){ + if (canTakeUp) { this.inv.setStackInSlot(1, this.inv.getStackInSlot(0).copy()); this.inv.setStackInSlot(0, StackUtil.shrink(this.inv.getStackInSlot(0), 1)); } } } - if(this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ + if (this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); } } } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || (i == 0 && (stack.hasCapability(CapabilityEnergy.ENERGY, null))); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || (slot == 0 && (stack.hasCapability(CapabilityEnergy.ENERGY, null))); } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !EnchantmentHelper.hasBindingCurse(stack) && !automation || (slot == 1); + public IRemover getRemover() { + return (slot, automation) -> !EnchantmentHelper.hasBindingCurse(inv.getStackInSlot(slot)) && !automation || (slot == 1); } - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java index 1428f5017..4f3eb061f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java @@ -10,105 +10,106 @@ package de.ellpeck.actuallyadditions.mod.tile; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; -public class TileEntityEnervator extends TileEntityInventoryBase implements ISharingEnergyProvider{ +public class TileEntityEnervator extends TileEntityInventoryBase implements ISharingEnergyProvider { public final CustomEnergyStorage storage = new CustomEnergyStorage(50000, 0, 1000); private int lastEnergy; - public TileEntityEnervator(){ + public TileEntityEnervator() { super(2, "enervator"); } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { this.storage.writeToNBT(compound); super.writeSyncableNBT(compound, type); } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { this.storage.readFromNBT(compound); super.readSyncableNBT(compound, type); } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ - if(StackUtil.isValid(this.inv.getStackInSlot(0)) && !StackUtil.isValid(this.inv.getStackInSlot(1))){ - if(this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()){ + if (!this.world.isRemote) { + if (StackUtil.isValid(this.inv.getStackInSlot(0)) && !StackUtil.isValid(this.inv.getStackInSlot(1))) { + if (this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) { int extracted = 0; boolean canTakeUp = false; - int maxExtract = this.storage.getMaxEnergyStored()-this.storage.getEnergyStored(); - if(this.inv.getStackInSlot(0).hasCapability(CapabilityEnergy.ENERGY, null)){ + int maxExtract = this.storage.getMaxEnergyStored() - this.storage.getEnergyStored(); + if (this.inv.getStackInSlot(0).hasCapability(CapabilityEnergy.ENERGY, null)) { IEnergyStorage cap = this.inv.getStackInSlot(0).getCapability(CapabilityEnergy.ENERGY, null); - if(cap != null){ + if (cap != null) { extracted = cap.extractEnergy(maxExtract, false); canTakeUp = cap.getEnergyStored() <= 0; } } - if(extracted > 0){ + if (extracted > 0) { this.storage.receiveEnergyInternal(extracted, false); } - if(canTakeUp){ + if (canTakeUp) { this.inv.setStackInSlot(1, this.inv.getStackInSlot(0).copy()); this.inv.getStackInSlot(0).shrink(1); } } } - if(this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ + if (this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); } } } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || (i == 0 && (stack.hasCapability(CapabilityEnergy.ENERGY, null))); - } - - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || (slot == 0 && (stack.hasCapability(CapabilityEnergy.ENERGY, null))); } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || slot == 1; + public IRemover getRemover() { + return (slot, automation) -> !automation || slot == 1; + } + + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } @Override - public int getEnergyToSplitShare(){ + public int getEnergyToSplitShare() { return this.storage.getEnergyStored(); } @Override - public boolean doesShareEnergy(){ + public boolean doesShareEnergy() { return true; } @Override - public EnumFacing[] getEnergyShareSides(){ + public EnumFacing[] getEnergyShareSides() { return EnumFacing.values(); } @Override - public boolean canShareTo(TileEntity tile){ + public boolean canShareTo(TileEntity tile) { return true; } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFarmer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFarmer.java index 983283a2d..a1c48f424 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFarmer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFarmer.java @@ -18,6 +18,8 @@ import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.farmer.FarmerResult; import de.ellpeck.actuallyadditions.api.farmer.IFarmerBehavior; import de.ellpeck.actuallyadditions.api.internal.IFarmer; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.state.IBlockState; @@ -138,13 +140,13 @@ public class TileEntityFarmer extends TileEntityInventoryBase implements IFarmer } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation) { - return !automation || i < 6; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || slot < 6; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation) { - return !automation || slot >= 6; + public IRemover getRemover() { + return (slot, automation) -> !automation || slot >= 6; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFeeder.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFeeder.java index a0bb98b82..47e80ccb7 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFeeder.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFeeder.java @@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.tile; import java.util.List; import java.util.Optional; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import net.minecraft.entity.passive.EntityAnimal; import net.minecraft.entity.passive.EntityHorse; import net.minecraft.init.Items; @@ -23,7 +24,7 @@ import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.MathHelper; -public class TileEntityFeeder extends TileEntityInventoryBase{ +public class TileEntityFeeder extends TileEntityInventoryBase { public static final int THRESHOLD = 30; private static final int TIME = 100; @@ -32,77 +33,77 @@ public class TileEntityFeeder extends TileEntityInventoryBase{ private int lastAnimalAmount; private int lastTimer; - public TileEntityFeeder(){ + public TileEntityFeeder() { super(1, "feeder"); } - public int getCurrentTimerToScale(int i){ - return this.currentTimer*i/TIME; + public int getCurrentTimerToScale(int i) { + return this.currentTimer * i / TIME; } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); compound.setInteger("Timer", this.currentTimer); - if(type == NBTType.SYNC){ + if (type == NBTType.SYNC) { compound.setInteger("Animals", this.currentAnimalAmount); } } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { super.readSyncableNBT(compound, type); this.currentTimer = compound.getInteger("Timer"); - if(type == NBTType.SYNC){ + if (type == NBTType.SYNC) { this.currentAnimalAmount = compound.getInteger("Animals"); } } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); currentTimer = MathHelper.clamp(++currentTimer, 0, 100); - if(world.isRemote) return; + if (world.isRemote) return; int range = 5; ItemStack stack = this.inv.getStackInSlot(0); - if(!stack.isEmpty() && this.currentTimer >= TIME) { - List animals = this.world.getEntitiesWithinAABB(EntityAnimal.class, new AxisAlignedBB(this.pos.getX()-range, this.pos.getY()-range, this.pos.getZ()-range, this.pos.getX()+range, this.pos.getY()+range, this.pos.getZ()+range)); - this.currentAnimalAmount = animals.size(); - if(currentAnimalAmount >= 2 && currentAnimalAmount < THRESHOLD){ - Optional opt = animals.stream().filter((e) -> canBeFed(stack, e)).findAny(); - if(opt.isPresent()) { - feedAnimal(opt.get()); - stack.shrink(1); - this.currentTimer = 0; - markDirty(); - } + if (!stack.isEmpty() && this.currentTimer >= TIME) { + List animals = this.world.getEntitiesWithinAABB(EntityAnimal.class, new AxisAlignedBB(this.pos.getX() - range, this.pos.getY() - range, this.pos.getZ() - range, this.pos.getX() + range, this.pos.getY() + range, this.pos.getZ() + range)); + this.currentAnimalAmount = animals.size(); + if (currentAnimalAmount >= 2 && currentAnimalAmount < THRESHOLD) { + Optional opt = animals.stream().filter((e) -> canBeFed(stack, e)).findAny(); + if (opt.isPresent()) { + feedAnimal(opt.get()); + stack.shrink(1); + this.currentTimer = 0; + markDirty(); + } } } - if((this.lastAnimalAmount != this.currentAnimalAmount || this.lastTimer != this.currentTimer) && this.sendUpdateWithInterval()){ - this.lastAnimalAmount = this.currentAnimalAmount; - this.lastTimer = this.currentTimer; + if ((this.lastAnimalAmount != this.currentAnimalAmount || this.lastTimer != this.currentTimer) && this.sendUpdateWithInterval()) { + this.lastAnimalAmount = this.currentAnimalAmount; + this.lastTimer = this.currentTimer; } } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation; + public IRemover getRemover() { + return (slot, automation) -> !automation; } - - private static void feedAnimal(EntityAnimal animal){ + + private static void feedAnimal(EntityAnimal animal) { animal.setInLove(null); - for(int i = 0; i < 7; i++){ - double d = animal.world.rand.nextGaussian()*0.02D; - double d1 = animal.world.rand.nextGaussian()*0.02D; - double d2 = animal.world.rand.nextGaussian()*0.02D; - animal.world.spawnParticle(EnumParticleTypes.HEART, (animal.posX+(double)(animal.world.rand.nextFloat()*animal.width*2.0F))-animal.width, animal.posY+0.5D+(double)(animal.world.rand.nextFloat()*animal.height), (animal.posZ+(double)(animal.world.rand.nextFloat()*animal.width*2.0F))-animal.width, d, d1, d2); + for (int i = 0; i < 7; i++) { + double d = animal.world.rand.nextGaussian() * 0.02D; + double d1 = animal.world.rand.nextGaussian() * 0.02D; + double d2 = animal.world.rand.nextGaussian() * 0.02D; + animal.world.spawnParticle(EnumParticleTypes.HEART, (animal.posX + (double) (animal.world.rand.nextFloat() * animal.width * 2.0F)) - animal.width, animal.posY + 0.5D + (double) (animal.world.rand.nextFloat() * animal.height), (animal.posZ + (double) (animal.world.rand.nextFloat() * animal.width * 2.0F)) - animal.width, d, d1, d2); } } - - private static boolean canBeFed(ItemStack stack, EntityAnimal animal){ - if(animal instanceof EntityHorse && ((EntityHorse) animal).isTame()){ - Item item = stack.getItem(); - return (animal.getGrowingAge() == 0 && !animal.isInLove()) && (item == Items.GOLDEN_APPLE || item == Items.GOLDEN_CARROT); + + private static boolean canBeFed(ItemStack stack, EntityAnimal animal) { + if (animal instanceof EntityHorse && ((EntityHorse) animal).isTame()) { + Item item = stack.getItem(); + return (animal.getGrowingAge() == 0 && !animal.isInLove()) && (item == Items.GOLDEN_APPLE || item == Items.GOLDEN_CARROT); } return animal.getGrowingAge() == 0 && !animal.isInLove() && animal.isBreedingItem(stack); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java index 15a7c7353..4a270b7dc 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceDouble.java @@ -13,6 +13,8 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.blocks.BlockFurnaceDouble; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.block.state.IBlockState; @@ -23,7 +25,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraftforge.energy.IEnergyStorage; -public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements IButtonReactor{ +public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements IButtonReactor { public static final int SLOT_INPUT_1 = 0; public static final int SLOT_OUTPUT_1 = 1; @@ -41,34 +43,32 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements private boolean lastAutoSplit; private boolean lastSmelted; - public TileEntityFurnaceDouble(){ + public TileEntityFurnaceDouble() { super(4, "furnaceDouble"); } - public static void autoSplit(ItemStackHandlerAA inv, int slot1, int slot2){ + public static void autoSplit(ItemStackHandlerAA inv, int slot1, int slot2) { ItemStack first = inv.getStackInSlot(slot1); ItemStack second = inv.getStackInSlot(slot2); - if(StackUtil.isValid(first) || StackUtil.isValid(second)){ + if (StackUtil.isValid(first) || StackUtil.isValid(second)) { ItemStack toSplit = StackUtil.getEmpty(); - if(!StackUtil.isValid(first) && StackUtil.isValid(second) && second.getCount() > 1){ + if (!StackUtil.isValid(first) && StackUtil.isValid(second) && second.getCount() > 1) { toSplit = second; - } - else if(!StackUtil.isValid(second) && StackUtil.isValid(first) && first.getCount() > 1){ + } else if (!StackUtil.isValid(second) && StackUtil.isValid(first) && first.getCount() > 1) { toSplit = first; - } - else if(ItemUtil.canBeStacked(first, second)){ - if(first.getCount() < first.getMaxStackSize() || second.getCount() < second.getMaxStackSize()){ - if(!((first.getCount() <= second.getCount()+1 && first.getCount() >= second.getCount()-1) || (second.getCount() <= first.getCount()+1 && second.getCount() >= first.getCount()-1))){ + } else if (ItemUtil.canBeStacked(first, second)) { + if (first.getCount() < first.getMaxStackSize() || second.getCount() < second.getMaxStackSize()) { + if (!((first.getCount() <= second.getCount() + 1 && first.getCount() >= second.getCount() - 1) || (second.getCount() <= first.getCount() + 1 && second.getCount() >= first.getCount() - 1))) { toSplit = first; toSplit.grow(second.getCount()); } } } - if(StackUtil.isValid(toSplit)){ + if (StackUtil.isValid(toSplit)) { ItemStack splitFirst = toSplit.copy(); - ItemStack secondSplit = splitFirst.splitStack(splitFirst.getCount()/2); + ItemStack secondSplit = splitFirst.splitStack(splitFirst.getCount() / 2); inv.setStackInSlot(slot1, splitFirst); inv.setStackInSlot(slot2, secondSplit); } @@ -76,9 +76,9 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("FirstSmeltTime", this.firstSmeltTime); compound.setInteger("SecondSmeltTime", this.secondSmeltTime); compound.setBoolean("IsAutoSplit", this.isAutoSplit); @@ -87,9 +87,9 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { super.readSyncableNBT(compound, type); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { this.firstSmeltTime = compound.getInteger("FirstSmeltTime"); this.secondSmeltTime = compound.getInteger("SecondSmeltTime"); this.isAutoSplit = compound.getBoolean("IsAutoSplit"); @@ -98,10 +98,10 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ - if(this.isAutoSplit){ + if (!this.world.isRemote) { + if (this.isAutoSplit) { autoSplit(this.inv, SLOT_INPUT_1, SLOT_INPUT_2); } @@ -110,46 +110,44 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements boolean canSmeltOnFirst = this.canSmeltOn(SLOT_INPUT_1, SLOT_OUTPUT_1); boolean canSmeltOnSecond = this.canSmeltOn(SLOT_INPUT_2, SLOT_OUTPUT_2); - if(canSmeltOnFirst){ - if(this.storage.getEnergyStored() >= ENERGY_USE){ + if (canSmeltOnFirst) { + if (this.storage.getEnergyStored() >= ENERGY_USE) { this.firstSmeltTime++; - if(this.firstSmeltTime >= SMELT_TIME){ + if (this.firstSmeltTime >= SMELT_TIME) { this.finishBurning(SLOT_INPUT_1, SLOT_OUTPUT_1); this.firstSmeltTime = 0; } this.storage.extractEnergyInternal(ENERGY_USE, false); } smelted = true; - } - else{ + } else { this.firstSmeltTime = 0; } - if(canSmeltOnSecond){ - if(this.storage.getEnergyStored() >= ENERGY_USE){ + if (canSmeltOnSecond) { + if (this.storage.getEnergyStored() >= ENERGY_USE) { this.secondSmeltTime++; - if(this.secondSmeltTime >= SMELT_TIME){ + if (this.secondSmeltTime >= SMELT_TIME) { this.finishBurning(SLOT_INPUT_2, SLOT_OUTPUT_2); this.secondSmeltTime = 0; } this.storage.extractEnergyInternal(ENERGY_USE, false); } smelted = true; - } - else{ + } else { this.secondSmeltTime = 0; } - if(smelted != this.lastSmelted){ + if (smelted != this.lastSmelted) { IBlockState currState = this.world.getBlockState(this.pos); - if(currState.getValue(BlockFurnaceDouble.IS_ON) != smelted){ + if (currState.getValue(BlockFurnaceDouble.IS_ON) != smelted) { this.world.setBlockState(this.pos, currState.withProperty(BlockFurnaceDouble.IS_ON, smelted)); } this.lastSmelted = smelted; } - if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstSmelt != this.firstSmeltTime || this.lastSecondSmelt != this.secondSmeltTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()){ + if ((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstSmelt != this.firstSmeltTime || this.lastSecondSmelt != this.secondSmeltTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); this.lastFirstSmelt = this.firstSmeltTime; this.lastAutoSplit = this.isAutoSplit; @@ -159,58 +157,55 @@ public class TileEntityFurnaceDouble extends TileEntityInventoryBase implements } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || ((i == SLOT_INPUT_1 || i == SLOT_INPUT_2) && StackUtil.isValid(FurnaceRecipes.instance().getSmeltingResult(stack))); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || ((slot == SLOT_INPUT_1 || slot == SLOT_INPUT_2) && StackUtil.isValid(FurnaceRecipes.instance().getSmeltingResult(stack))); } - public boolean canSmeltOn(int theInput, int theOutput){ - if(StackUtil.isValid(this.inv.getStackInSlot(theInput))){ + @Override + public IRemover getRemover() { + return (slot, automation) -> !automation || (slot == SLOT_OUTPUT_1 || slot == SLOT_OUTPUT_2); + } + + public boolean canSmeltOn(int theInput, int theOutput) { + if (StackUtil.isValid(this.inv.getStackInSlot(theInput))) { ItemStack output = FurnaceRecipes.instance().getSmeltingResult(this.inv.getStackInSlot(theInput)); - if(StackUtil.isValid(output)){ - if(!StackUtil.isValid(this.inv.getStackInSlot(theOutput)) || (this.inv.getStackInSlot(theOutput).isItemEqual(output) && this.inv.getStackInSlot(theOutput).getCount() <= this.inv.getStackInSlot(theOutput).getMaxStackSize()-output.getCount())){ - return true; - } + if (StackUtil.isValid(output)) { + if (!StackUtil.isValid(this.inv.getStackInSlot(theOutput)) || (this.inv.getStackInSlot(theOutput).isItemEqual(output) && this.inv.getStackInSlot(theOutput).getCount() <= this.inv.getStackInSlot(theOutput).getMaxStackSize() - output.getCount())) { return true; } } } return false; } - public void finishBurning(int theInput, int theOutput){ + public void finishBurning(int theInput, int theOutput) { ItemStack output = FurnaceRecipes.instance().getSmeltingResult(this.inv.getStackInSlot(theInput)); - if(!StackUtil.isValid(this.inv.getStackInSlot(theOutput))){ + if (!StackUtil.isValid(this.inv.getStackInSlot(theOutput))) { this.inv.setStackInSlot(theOutput, output.copy()); - } - else if(this.inv.getStackInSlot(theOutput).getItem() == output.getItem()){ + } else if (this.inv.getStackInSlot(theOutput).getItem() == output.getItem()) { this.inv.getStackInSlot(theOutput).grow(output.getCount()); } this.inv.getStackInSlot(theInput).shrink(1); } - public int getFirstTimeToScale(int i){ - return this.firstSmeltTime*i/SMELT_TIME; + public int getFirstTimeToScale(int i) { + return this.firstSmeltTime * i / SMELT_TIME; } - public int getSecondTimeToScale(int i){ - return this.secondSmeltTime*i/SMELT_TIME; + public int getSecondTimeToScale(int i) { + return this.secondSmeltTime * i / SMELT_TIME; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || (slot == SLOT_OUTPUT_1 || slot == SLOT_OUTPUT_2); - } - - @Override - public void onButtonPressed(int buttonID, EntityPlayer player){ - if(buttonID == 0){ + public void onButtonPressed(int buttonID, EntityPlayer player) { + if (buttonID == 0) { this.isAutoSplit = !this.isAutoSplit; this.markDirty(); } } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java index d6b6e40dd..8a6a0b2e1 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityGrinder.java @@ -10,12 +10,13 @@ package de.ellpeck.actuallyadditions.mod.tile; - import de.ellpeck.actuallyadditions.api.recipe.CrusherRecipe; import de.ellpeck.actuallyadditions.mod.blocks.BlockFurnaceDouble; import de.ellpeck.actuallyadditions.mod.misc.SoundHandler; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.recipe.CrusherRecipeRegistry; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.Util; import net.minecraft.block.state.IBlockState; @@ -26,7 +27,7 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.SoundCategory; import net.minecraftforge.energy.IEnergyStorage; -public class TileEntityGrinder extends TileEntityInventoryBase implements IButtonReactor{ +public class TileEntityGrinder extends TileEntityInventoryBase implements IButtonReactor { public static final int SLOT_INPUT_1 = 0; public static final int SLOT_OUTPUT_1_1 = 1; @@ -46,18 +47,18 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IButto private boolean lastAutoSplit; private boolean lastCrushed; - public TileEntityGrinder(int slots, String name){ + public TileEntityGrinder(int slots, String name) { super(slots, name); } - public TileEntityGrinder(){ + public TileEntityGrinder() { super(3, "grinder"); this.isDouble = false; } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("FirstCrushTime", this.firstCrushTime); compound.setInteger("SecondCrushTime", this.secondCrushTime); compound.setBoolean("IsAutoSplit", this.isAutoSplit); @@ -67,8 +68,8 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IButto } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { this.firstCrushTime = compound.getInteger("FirstCrushTime"); this.secondCrushTime = compound.getInteger("SecondCrushTime"); this.isAutoSplit = compound.getBoolean("IsAutoSplit"); @@ -78,10 +79,10 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IButto } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ - if(this.isDouble && this.isAutoSplit){ + if (!this.world.isRemote) { + if (this.isDouble && this.isAutoSplit) { TileEntityFurnaceDouble.autoSplit(this.inv, SLOT_INPUT_1, SLOT_INPUT_2); } @@ -89,164 +90,158 @@ public class TileEntityGrinder extends TileEntityInventoryBase implements IButto boolean canCrushOnFirst = this.canCrushOn(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2); boolean canCrushOnSecond = false; - if(this.isDouble){ + if (this.isDouble) { canCrushOnSecond = this.canCrushOn(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2); } boolean shouldPlaySound = false; - if(canCrushOnFirst){ - if(this.storage.getEnergyStored() >= ENERGY_USE){ - if(this.firstCrushTime%20 == 0){ + if (canCrushOnFirst) { + if (this.storage.getEnergyStored() >= ENERGY_USE) { + if (this.firstCrushTime % 20 == 0) { shouldPlaySound = true; } this.firstCrushTime++; - if(this.firstCrushTime >= this.getMaxCrushTime()){ + if (this.firstCrushTime >= this.getMaxCrushTime()) { this.finishCrushing(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2); this.firstCrushTime = 0; } this.storage.extractEnergyInternal(ENERGY_USE, false); } crushed = storage.getEnergyStored() >= ENERGY_USE; - } - else{ + } else { this.firstCrushTime = 0; } - if(this.isDouble){ - if(canCrushOnSecond){ - if(this.storage.getEnergyStored() >= ENERGY_USE){ - if(this.secondCrushTime%20 == 0){ + if (this.isDouble) { + if (canCrushOnSecond) { + if (this.storage.getEnergyStored() >= ENERGY_USE) { + if (this.secondCrushTime % 20 == 0) { shouldPlaySound = true; } this.secondCrushTime++; - if(this.secondCrushTime >= this.getMaxCrushTime()){ + if (this.secondCrushTime >= this.getMaxCrushTime()) { this.finishCrushing(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2); this.secondCrushTime = 0; } this.storage.extractEnergyInternal(ENERGY_USE, false); } crushed = storage.getEnergyStored() >= ENERGY_USE; - } - else{ + } else { this.secondCrushTime = 0; } } - if(crushed != this.lastCrushed){ + if (crushed != this.lastCrushed) { IBlockState currState = this.world.getBlockState(this.pos); - if(currState.getValue(BlockFurnaceDouble.IS_ON) != crushed){ + if (currState.getValue(BlockFurnaceDouble.IS_ON) != crushed) { this.world.setBlockState(this.pos, currState.withProperty(BlockFurnaceDouble.IS_ON, crushed)); } this.lastCrushed = crushed; } - if((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstCrush != this.firstCrushTime || this.lastSecondCrush != this.secondCrushTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()){ + if ((this.lastEnergy != this.storage.getEnergyStored() || this.lastFirstCrush != this.firstCrushTime || this.lastSecondCrush != this.secondCrushTime || this.isAutoSplit != this.lastAutoSplit) && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); this.lastFirstCrush = this.firstCrushTime; this.lastSecondCrush = this.secondCrushTime; this.lastAutoSplit = this.isAutoSplit; } - if(shouldPlaySound){ + if (shouldPlaySound) { this.world.playSound(null, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), SoundHandler.crusher, SoundCategory.BLOCKS, 0.025F, 1.0F); } } } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || ((i == SLOT_INPUT_1 || i == SLOT_INPUT_2) && CrusherRecipeRegistry.getRecipeFromInput(stack) != null); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || ((slot == SLOT_INPUT_1 || slot == SLOT_INPUT_2) && CrusherRecipeRegistry.getRecipeFromInput(stack) != null); } - public boolean canCrushOn(int theInput, int theFirstOutput, int theSecondOutput){ - if(StackUtil.isValid(this.inv.getStackInSlot(theInput))){ + @Override + public IRemover getRemover() { + return (slot, automation) -> !automation || (slot == SLOT_OUTPUT_1_1 || slot == SLOT_OUTPUT_1_2 || slot == SLOT_OUTPUT_2_1 || slot == SLOT_OUTPUT_2_2); + } + + public boolean canCrushOn(int theInput, int theFirstOutput, int theSecondOutput) { + if (StackUtil.isValid(this.inv.getStackInSlot(theInput))) { CrusherRecipe recipe = CrusherRecipeRegistry.getRecipeFromInput(inv.getStackInSlot(theInput)); - if(recipe == null) return false; + if (recipe == null) return false; ItemStack outputOne = recipe.getOutputOne(); ItemStack outputTwo = recipe.getOutputTwo(); - if(StackUtil.isValid(outputOne)){ - if(outputOne.getItemDamage() == Util.WILDCARD){ + if (StackUtil.isValid(outputOne)) { + if (outputOne.getItemDamage() == Util.WILDCARD) { outputOne.setItemDamage(0); } - if(StackUtil.isValid(outputTwo) && outputTwo.getItemDamage() == Util.WILDCARD){ + if (StackUtil.isValid(outputTwo) && outputTwo.getItemDamage() == Util.WILDCARD) { outputTwo.setItemDamage(0); } - if((!StackUtil.isValid(this.inv.getStackInSlot(theFirstOutput)) || (this.inv.getStackInSlot(theFirstOutput).isItemEqual(outputOne) && this.inv.getStackInSlot(theFirstOutput).getCount() <= this.inv.getStackInSlot(theFirstOutput).getMaxStackSize()-outputOne.getCount())) && (!StackUtil.isValid(outputTwo) || (!StackUtil.isValid(this.inv.getStackInSlot(theSecondOutput)) || (this.inv.getStackInSlot(theSecondOutput).isItemEqual(outputTwo) && this.inv.getStackInSlot(theSecondOutput).getCount() <= this.inv.getStackInSlot(theSecondOutput).getMaxStackSize()-outputTwo.getCount())))){ - return true; - } + if ((!StackUtil.isValid(this.inv.getStackInSlot(theFirstOutput)) || (this.inv.getStackInSlot(theFirstOutput).isItemEqual(outputOne) && this.inv.getStackInSlot(theFirstOutput).getCount() <= this.inv.getStackInSlot(theFirstOutput).getMaxStackSize() - outputOne.getCount())) && (!StackUtil.isValid(outputTwo) || (!StackUtil.isValid(this.inv.getStackInSlot(theSecondOutput)) || (this.inv.getStackInSlot(theSecondOutput).isItemEqual(outputTwo) && this.inv.getStackInSlot(theSecondOutput).getCount() <= this.inv.getStackInSlot(theSecondOutput).getMaxStackSize() - outputTwo.getCount())))) { return true; } } } return false; } - private int getMaxCrushTime(){ + private int getMaxCrushTime() { return this.isDouble ? 150 : 100; } - public void finishCrushing(int theInput, int theFirstOutput, int theSecondOutput){ + public void finishCrushing(int theInput, int theFirstOutput, int theSecondOutput) { CrusherRecipe recipe = CrusherRecipeRegistry.getRecipeFromInput(inv.getStackInSlot(theInput)); - if(recipe == null) return; + if (recipe == null) return; ItemStack outputOne = recipe.getOutputOne(); - if(StackUtil.isValid(outputOne)){ - if(outputOne.getItemDamage() == Util.WILDCARD){ + if (StackUtil.isValid(outputOne)) { + if (outputOne.getItemDamage() == Util.WILDCARD) { outputOne.setItemDamage(0); } - if(!StackUtil.isValid(this.inv.getStackInSlot(theFirstOutput))){ + if (!StackUtil.isValid(this.inv.getStackInSlot(theFirstOutput))) { this.inv.setStackInSlot(theFirstOutput, outputOne.copy()); - } - else if(this.inv.getStackInSlot(theFirstOutput).getItem() == outputOne.getItem()){ + } else if (this.inv.getStackInSlot(theFirstOutput).getItem() == outputOne.getItem()) { this.inv.setStackInSlot(theFirstOutput, StackUtil.grow(this.inv.getStackInSlot(theFirstOutput), outputOne.getCount())); } } ItemStack outputTwo = recipe.getOutputTwo(); - if(StackUtil.isValid(outputTwo)){ - if(outputTwo.getItemDamage() == Util.WILDCARD){ + if (StackUtil.isValid(outputTwo)) { + if (outputTwo.getItemDamage() == Util.WILDCARD) { outputTwo.setItemDamage(0); } - int rand = this.world.rand.nextInt(100)+1; - if(rand <= recipe.getSecondChance()){ - if(!StackUtil.isValid(this.inv.getStackInSlot(theSecondOutput))){ + int rand = this.world.rand.nextInt(100) + 1; + if (rand <= recipe.getSecondChance()) { + if (!StackUtil.isValid(this.inv.getStackInSlot(theSecondOutput))) { this.inv.setStackInSlot(theSecondOutput, outputTwo.copy()); - } - else if(this.inv.getStackInSlot(theSecondOutput).getItem() == outputTwo.getItem()){ + } else if (this.inv.getStackInSlot(theSecondOutput).getItem() == outputTwo.getItem()) { this.inv.setStackInSlot(theSecondOutput, StackUtil.grow(this.inv.getStackInSlot(theSecondOutput), outputTwo.getCount())); } } } - this.inv.getStackInSlot(theInput).shrink(1); + this.inv.getStackInSlot(theInput).shrink(1); } - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } - public int getFirstTimeToScale(int i){ - return this.firstCrushTime*i/this.getMaxCrushTime(); + public int getFirstTimeToScale(int i) { + return this.firstCrushTime * i / this.getMaxCrushTime(); } - public int getSecondTimeToScale(int i){ - return this.secondCrushTime*i/this.getMaxCrushTime(); + public int getSecondTimeToScale(int i) { + return this.secondCrushTime * i / this.getMaxCrushTime(); } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || (slot == SLOT_OUTPUT_1_1 || slot == SLOT_OUTPUT_1_2 || slot == SLOT_OUTPUT_2_1 || slot == SLOT_OUTPUT_2_2); - } - - @Override - public void onButtonPressed(int buttonID, EntityPlayer player){ - if(buttonID == 0){ + public void onButtonPressed(int buttonID, EntityPlayer player) { + if (buttonID == 0) { this.isAutoSplit = !this.isAutoSplit; this.markDirty(); } } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java index e003f8011..aaabdbbb2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java @@ -10,15 +10,20 @@ package de.ellpeck.actuallyadditions.mod.tile; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.cyclops.commoncapabilities.capability.itemhandler.SlotlessItemHandlerConfig; import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.network.gui.INumberReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityFurnace; @@ -26,12 +31,8 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; -import org.cyclops.commoncapabilities.capability.itemhandler.SlotlessItemHandlerConfig; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class TileEntityInputter extends TileEntityInventoryBase implements IButtonReactor, INumberReactor{ +public class TileEntityInputter extends TileEntityInventoryBase implements IButtonReactor, INumberReactor { public static final int OKAY_BUTTON_ID = 133; private final SlotlessableItemHandlerWrapper wrapper = new SlotlessableItemHandlerWrapper(this.inv, null); @@ -53,54 +54,54 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt private int lastPullStart; private int lastPullEnd; - public TileEntityInputter(int slots, String name){ + public TileEntityInputter(int slots, String name) { super(slots, name); } - public TileEntityInputter(){ + public TileEntityInputter() { super(1, "inputter"); this.isAdvanced = false; } @Override - public void onNumberReceived(double number, int textID, EntityPlayer player){ - int text = (int)number; + public void onNumberReceived(double number, int textID, EntityPlayer player) { + int text = (int) number; - if(text != -1){ - if(textID == 0){ + if (text != -1) { + if (textID == 0) { this.slotToPutStart = Math.max(text, 0); } - if(textID == 1){ + if (textID == 1) { this.slotToPutEnd = Math.max(text, 0); } - if(textID == 2){ + if (textID == 2) { this.slotToPullStart = Math.max(text, 0); } - if(textID == 3){ + if (textID == 3) { this.slotToPullEnd = Math.max(text, 0); } } this.markDirty(); } - private boolean newPulling(){ - for(EnumFacing side : this.placeToPull.keySet()){ + private boolean newPulling() { + for (EnumFacing side : this.placeToPull.keySet()) { WorldUtil.doItemInteraction(this.placeToPull.get(side), this.wrapper, Integer.MAX_VALUE, this.slotToPullStart, this.slotToPullEnd, 0, 1, !this.isAdvanced ? null : this.leftFilter); - if(this.placeToPull instanceof TileEntityItemViewer){ + if (this.placeToPull instanceof TileEntityItemViewer) { break; } } return false; } - private boolean newPutting(){ - if(!this.isAdvanced || this.rightFilter.check(this.inv.getStackInSlot(0))){ - for(EnumFacing side : this.placeToPut.keySet()){ + private boolean newPutting() { + if (!this.isAdvanced || this.rightFilter.check(this.inv.getStackInSlot(0))) { + for (EnumFacing side : this.placeToPut.keySet()) { WorldUtil.doItemInteraction(this.wrapper, this.placeToPut.get(side), Integer.MAX_VALUE, 0, 1, this.slotToPutStart, this.slotToPutEnd, null); - if(this.placeToPut instanceof TileEntityItemViewer){ + if (this.placeToPut instanceof TileEntityItemViewer) { break; } } @@ -109,7 +110,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } @Override - public boolean shouldSaveDataOnChangeOrWorldStart(){ + public boolean shouldSaveDataOnChangeOrWorldStart() { return true; } @@ -117,29 +118,28 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt * Sets all of the relevant variables */ @Override - public void saveDataOnChangeOrWorldStart(){ + public void saveDataOnChangeOrWorldStart() { this.placeToPull.clear(); this.placeToPut.clear(); - if(this.sideToPull != -1){ + if (this.sideToPull != -1) { EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPull); BlockPos offset = this.pos.offset(side); - if(this.world.isBlockLoaded(offset)){ + if (this.world.isBlockLoaded(offset)) { TileEntity tile = this.world.getTileEntity(offset); - if(tile != null){ - for(EnumFacing facing : EnumFacing.values()){ + if (tile != null) { + for (EnumFacing facing : EnumFacing.values()) { IItemHandler normal = null; - if(tile.getClass() == TileEntityFurnace.class) - normal = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); - else if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)){ + if (tile.getClass() == TileEntityFurnace.class) normal = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); + else if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) { normal = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing); } Object slotless = null; - if(ActuallyAdditions.commonCapsLoaded){ - if(tile.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing)){ + if (ActuallyAdditions.commonCapsLoaded) { + if (tile.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing)) { slotless = tile.getCapability(SlotlessItemHandlerConfig.CAPABILITY, facing); } } @@ -147,10 +147,10 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt this.placeToPull.put(facing.getOpposite(), new SlotlessableItemHandlerWrapper(normal, slotless)); } - if(this.slotToPullEnd <= 0){ - if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){ + if (this.slotToPullEnd <= 0) { + if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) { IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); - if(cap != null){ + if (cap != null) { this.slotToPullEnd = cap.getSlots(); } } @@ -159,23 +159,23 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } } - if(this.sideToPut != -1){ + if (this.sideToPut != -1) { EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPut); BlockPos offset = this.pos.offset(side); - if(this.world.isBlockLoaded(offset)){ + if (this.world.isBlockLoaded(offset)) { TileEntity tile = this.world.getTileEntity(offset); - if(tile != null){ - for(EnumFacing facing : EnumFacing.values()){ + if (tile != null) { + for (EnumFacing facing : EnumFacing.values()) { IItemHandler normal = null; - if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)){ + if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) { normal = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing); } Object slotless = null; - if(ActuallyAdditions.commonCapsLoaded){ - if(tile.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing)){ + if (ActuallyAdditions.commonCapsLoaded) { + if (tile.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, facing)) { slotless = tile.getCapability(SlotlessItemHandlerConfig.CAPABILITY, facing); } } @@ -183,10 +183,10 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt this.placeToPut.put(facing.getOpposite(), new SlotlessableItemHandlerWrapper(normal, slotless)); } - if(this.slotToPutEnd <= 0){ - if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){ + if (this.slotToPutEnd <= 0) { + if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) { IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); - if(cap != null){ + if (cap != null) { this.slotToPutEnd = cap.getSlots(); } } @@ -197,44 +197,41 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } @Override - public void onButtonPressed(int buttonID, EntityPlayer player){ + public void onButtonPressed(int buttonID, EntityPlayer player) { this.leftFilter.onButtonPressed(buttonID); this.rightFilter.onButtonPressed(buttonID); //Reset the Slots - if(buttonID == 0 || buttonID == 1){ + if (buttonID == 0 || buttonID == 1) { this.slotToPutStart = 0; this.slotToPutEnd = 0; } - if(buttonID == 2 || buttonID == 3){ + if (buttonID == 2 || buttonID == 3) { this.slotToPullStart = 0; this.slotToPullEnd = 0; } - if(buttonID == 0){ + if (buttonID == 0) { this.sideToPut++; } - if(buttonID == 1){ + if (buttonID == 1) { this.sideToPut--; } - if(buttonID == 2){ + if (buttonID == 2) { this.sideToPull++; } - if(buttonID == 3){ + if (buttonID == 3) { this.sideToPull--; } - if(this.sideToPut >= 6){ + if (this.sideToPut >= 6) { this.sideToPut = -1; - } - else if(this.sideToPut < -1){ + } else if (this.sideToPut < -1) { this.sideToPut = 5; - } - else if(this.sideToPull >= 6){ + } else if (this.sideToPull >= 6) { this.sideToPull = -1; - } - else if(this.sideToPull < -1){ + } else if (this.sideToPull < -1) { this.sideToPull = 5; } @@ -243,9 +240,9 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("SideToPut", this.sideToPut); compound.setInteger("SlotToPut", this.slotToPutStart); compound.setInteger("SlotToPutEnd", this.slotToPutEnd); @@ -259,8 +256,8 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { this.sideToPut = compound.getInteger("SideToPut"); this.slotToPutStart = compound.getInteger("SlotToPut"); this.slotToPutEnd = compound.getInteger("SlotToPutEnd"); @@ -276,19 +273,19 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ + if (!this.world.isRemote) { //Is Block not powered by Redstone? - if(!this.isRedstonePowered){ - if(this.ticksElapsed%30 == 0){ - if(!(this.sideToPull == this.sideToPut && this.slotToPullStart == this.slotToPutStart && this.slotToPullEnd == this.slotToPutEnd)){ - if(!StackUtil.isValid(this.inv.getStackInSlot(0)) && this.sideToPull != -1 && this.placeToPull != null){ + if (!this.isRedstonePowered) { + if (this.ticksElapsed % 30 == 0) { + if (!(this.sideToPull == this.sideToPut && this.slotToPullStart == this.slotToPutStart && this.slotToPullEnd == this.slotToPutEnd)) { + if (!StackUtil.isValid(this.inv.getStackInSlot(0)) && this.sideToPull != -1 && this.placeToPull != null) { this.newPulling(); } - if(StackUtil.isValid(this.inv.getStackInSlot(0)) && this.sideToPut != -1 && this.placeToPut != null){ + if (StackUtil.isValid(this.inv.getStackInSlot(0)) && this.sideToPut != -1 && this.placeToPut != null) { this.newPutting(); } } @@ -296,7 +293,7 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } //Update the Client - if((this.sideToPut != this.lastPutSide || this.sideToPull != this.lastPullSide || this.slotToPullStart != this.lastPullStart || this.slotToPullEnd != this.lastPullEnd || this.slotToPutStart != this.lastPutStart || this.slotToPutEnd != this.lastPutEnd || this.leftFilter.needsUpdateSend() || this.rightFilter.needsUpdateSend()) && this.sendUpdateWithInterval()){ + if ((this.sideToPut != this.lastPutSide || this.sideToPull != this.lastPullSide || this.slotToPullStart != this.lastPullStart || this.slotToPullEnd != this.lastPullEnd || this.slotToPutStart != this.lastPutStart || this.slotToPutEnd != this.lastPutEnd || this.leftFilter.needsUpdateSend() || this.rightFilter.needsUpdateSend()) && this.sendUpdateWithInterval()) { this.lastPutSide = this.sideToPut; this.lastPullSide = this.sideToPull; this.lastPullStart = this.slotToPullStart; @@ -310,12 +307,12 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || i == 0; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || slot == 0; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || slot == 0; + public IRemover getRemover() { + return (slot, automation) -> !automation || slot == 0; } } \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInventoryBase.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInventoryBase.java index 73a1b7407..9148f874e 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInventoryBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInventoryBase.java @@ -11,6 +11,8 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -67,12 +69,12 @@ public abstract class TileEntityInventoryBase extends TileEntityBase { return this.inv; } - public boolean canInsert(int slot, ItemStack stack, boolean automation) { - return true; + public IAcceptor getAcceptor() { + return ItemStackHandlerAA.ACCEPT_TRUE; } - public boolean canExtract(int slot, ItemStack stack, boolean automation) { - return true; + public IRemover getRemover() { + return ItemStackHandlerAA.REMOVE_TRUE; } public int getMaxStackSize(int slot) { @@ -112,13 +114,13 @@ public abstract class TileEntityInventoryBase extends TileEntityBase { } @Override - public boolean canAccept(int slot, ItemStack stack, boolean fromAutomation) { - return TileEntityInventoryBase.this.canInsert(slot, stack, fromAutomation); + public IAcceptor getAcceptor() { + return TileEntityInventoryBase.this.getAcceptor(); } @Override - public boolean canRemove(int slot, boolean byAutomation) { - return TileEntityInventoryBase.this.canExtract(slot, this.getStackInSlot(slot), byAutomation); + public IRemover getRemover() { + return TileEntityInventoryBase.this.getRemover(); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemRepairer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemRepairer.java index 55cdc3322..c04c28d22 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemRepairer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemRepairer.java @@ -11,6 +11,8 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -20,7 +22,7 @@ import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class TileEntityItemRepairer extends TileEntityInventoryBase{ +public class TileEntityItemRepairer extends TileEntityInventoryBase { public static final int SLOT_INPUT = 0; public static final int SLOT_OUTPUT = 1; @@ -29,24 +31,21 @@ public class TileEntityItemRepairer extends TileEntityInventoryBase{ public int nextRepairTick; private int lastEnergy; - public TileEntityItemRepairer(){ + public TileEntityItemRepairer() { super(2, "repairer"); } - public static boolean canBeRepaired(ItemStack stack){ - if(StackUtil.isValid(stack)){ + public static boolean canBeRepaired(ItemStack stack) { + if (StackUtil.isValid(stack)) { Item item = stack.getItem(); - if(item != null){ - if(item.isRepairable() && item.getMaxDamage(stack) > 0){ + if (item != null) { + if (item.isRepairable() && item.getMaxDamage(stack) > 0) { return true; - } - else{ + } else { String reg = item.getRegistryName().toString(); - if(reg != null){ - for(String strg : ConfigStringListValues.REPAIRER_EXTRA_WHITELIST.getValue()){ - if(reg.equals(strg)){ - return true; - } + if (reg != null) { + for (String strg : ConfigStringListValues.REPAIRER_EXTRA_WHITELIST.getValue()) { + if (reg.equals(strg)) { return true; } } } } @@ -56,8 +55,8 @@ public class TileEntityItemRepairer extends TileEntityInventoryBase{ } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("NextRepairTick", this.nextRepairTick); } super.writeSyncableNBT(compound, type); @@ -65,8 +64,8 @@ public class TileEntityItemRepairer extends TileEntityInventoryBase{ } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ - if(type != NBTType.SAVE_BLOCK){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + if (type != NBTType.SAVE_BLOCK) { this.nextRepairTick = compound.getInteger("NextRepairTick"); } super.readSyncableNBT(compound, type); @@ -74,27 +73,26 @@ public class TileEntityItemRepairer extends TileEntityInventoryBase{ } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ + if (!this.world.isRemote) { ItemStack input = this.inv.getStackInSlot(SLOT_INPUT); - if(!StackUtil.isValid(this.inv.getStackInSlot(SLOT_OUTPUT)) && canBeRepaired(input)){ - if(input.getItemDamage() <= 0){ + if (!StackUtil.isValid(this.inv.getStackInSlot(SLOT_OUTPUT)) && canBeRepaired(input)) { + if (input.getItemDamage() <= 0) { this.inv.setStackInSlot(SLOT_OUTPUT, input.copy()); this.inv.setStackInSlot(SLOT_INPUT, StackUtil.getEmpty()); this.nextRepairTick = 0; - } - else{ - if(this.storage.getEnergyStored() >= ENERGY_USE){ + } else { + if (this.storage.getEnergyStored() >= ENERGY_USE) { this.nextRepairTick++; this.storage.extractEnergyInternal(ENERGY_USE, false); - if(this.nextRepairTick >= 4){ + if (this.nextRepairTick >= 4) { this.nextRepairTick = 0; - input.setItemDamage(input.getItemDamage()-1); + input.setItemDamage(input.getItemDamage() - 1); - if(input.hasTagCompound()){ + if (input.hasTagCompound()) { //TiCon un-break tools - if("tconstruct".equalsIgnoreCase(input.getItem().getRegistryName().getNamespace())){ + if ("tconstruct".equalsIgnoreCase(input.getItem().getRegistryName().getNamespace())) { NBTTagCompound stats = input.getTagCompound().getCompoundTag("Stats"); stats.removeTag("Broken"); } @@ -102,41 +100,38 @@ public class TileEntityItemRepairer extends TileEntityInventoryBase{ } } } - } - else{ + } else { this.nextRepairTick = 0; } - if(this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ + if (this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) { this.lastEnergy = this.storage.getEnergyStored(); } } } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || i == SLOT_INPUT; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || slot == SLOT_INPUT; } @SideOnly(Side.CLIENT) - public int getEnergyScaled(int i){ - return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored(); + public int getEnergyScaled(int i) { + return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored(); } - public int getItemDamageToScale(int i){ - if(StackUtil.isValid(this.inv.getStackInSlot(SLOT_INPUT))){ - return (this.inv.getStackInSlot(SLOT_INPUT).getMaxDamage()-this.inv.getStackInSlot(SLOT_INPUT).getItemDamage())*i/this.inv.getStackInSlot(SLOT_INPUT).getMaxDamage(); - } + public int getItemDamageToScale(int i) { + if (StackUtil.isValid(this.inv.getStackInSlot(SLOT_INPUT))) { return (this.inv.getStackInSlot(SLOT_INPUT).getMaxDamage() - this.inv.getStackInSlot(SLOT_INPUT).getItemDamage()) * i / this.inv.getStackInSlot(SLOT_INPUT).getMaxDamage(); } return 0; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || slot == SLOT_OUTPUT; + public IRemover getRemover() { + return (slot, automation) -> !automation || slot == SLOT_OUTPUT; } @Override - public IEnergyStorage getEnergyStorage(EnumFacing facing){ + public IEnergyStorage getEnergyStorage(EnumFacing facing) { return this.storage; } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java index dd1b7ab85..d9039c8a0 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java @@ -14,6 +14,7 @@ import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues; import de.ellpeck.actuallyadditions.mod.items.ItemDrill; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.Block; @@ -195,13 +196,8 @@ public class TileEntityMiner extends TileEntityInventoryBase implements IButtonR } @Override - public boolean canInsert(int slot, ItemStack stack, boolean fromAutomation) { - return !fromAutomation; - } - - @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation) { - return true; + public IAcceptor getAcceptor() { + return (stack, slot, automation) -> !automation; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomBreaker.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomBreaker.java index 15935eff0..8a73bd117 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomBreaker.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomBreaker.java @@ -10,11 +10,25 @@ package de.ellpeck.actuallyadditions.mod.tile; -public class TileEntityPhantomBreaker extends TileEntityPhantomPlacer{ +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; - public TileEntityPhantomBreaker(){ +public class TileEntityPhantomBreaker extends TileEntityPhantomPlacer { + + public TileEntityPhantomBreaker() { super(9, "phantomBreaker"); this.isBreaker = true; } + @Override + public IAcceptor getAcceptor() { + return ItemStackHandlerAA.ACCEPT_FALSE; + } + + @Override + public IRemover getRemover() { + return ItemStackHandlerAA.REMOVE_TRUE; + } + } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomItemface.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomItemface.java index 864ff9512..5357d5763 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomItemface.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomItemface.java @@ -11,33 +11,32 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.blocks.BlockPhantom; -import net.minecraft.item.ItemStack; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.items.CapabilityItemHandler; -public class TileEntityPhantomItemface extends TileEntityPhantomface{ +public class TileEntityPhantomItemface extends TileEntityPhantomface { - public TileEntityPhantomItemface(){ + public TileEntityPhantomItemface() { super("phantomface"); this.type = BlockPhantom.Type.FACE; } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || this.isBoundThingInRange(); + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation || this.isBoundThingInRange(); } @Override - public boolean isBoundThingInRange(){ - if(super.isBoundThingInRange()){ + public boolean isBoundThingInRange() { + if (super.isBoundThingInRange()) { TileEntity tile = this.world.getTileEntity(this.getBoundPosition()); - if(tile != null){ - for(EnumFacing facing : EnumFacing.values()){ - if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)){ - return true; - } + if (tile != null) { + for (EnumFacing facing : EnumFacing.values()) { + if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) { return true; } } } } @@ -45,12 +44,12 @@ public class TileEntityPhantomItemface extends TileEntityPhantomface{ } @Override - protected boolean isCapabilitySupported(Capability capability){ + protected boolean isCapabilitySupported(Capability capability) { return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || this.isBoundThingInRange(); + public IRemover getRemover() { + return (slot, automation) -> !automation || this.isBoundThingInRange(); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomPlacer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomPlacer.java index 75e9458dd..81b18db4e 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomPlacer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomPlacer.java @@ -13,6 +13,9 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.api.tile.IPhantomTile; import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.Block; @@ -24,7 +27,7 @@ import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; -public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements IPhantomTile, IButtonReactor{ +public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements IPhantomTile, IButtonReactor { public static final int RANGE = 3; public BlockPos boundPosition; @@ -34,90 +37,88 @@ public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements public int side; private int oldRange; - public TileEntityPhantomPlacer(int slots, String name){ + public TileEntityPhantomPlacer(int slots, String name) { super(slots, name); } - public TileEntityPhantomPlacer(){ + public TileEntityPhantomPlacer() { super(9, "phantomPlacer"); this.isBreaker = false; } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ + public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { super.writeSyncableNBT(compound, type); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { compound.setInteger("Range", this.range); - if(this.boundPosition != null){ + if (this.boundPosition != null) { compound.setInteger("xOfTileStored", this.boundPosition.getX()); compound.setInteger("yOfTileStored", this.boundPosition.getY()); compound.setInteger("zOfTileStored", this.boundPosition.getZ()); } - if(!this.isBreaker){ + if (!this.isBreaker) { compound.setInteger("Side", this.side); } } } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type){ + public void readSyncableNBT(NBTTagCompound compound, NBTType type) { super.readSyncableNBT(compound, type); - if(type != NBTType.SAVE_BLOCK){ + if (type != NBTType.SAVE_BLOCK) { int x = compound.getInteger("xOfTileStored"); int y = compound.getInteger("yOfTileStored"); int z = compound.getInteger("zOfTileStored"); this.range = compound.getInteger("Range"); - if(!(x == 0 && y == 0 && z == 0)){ + if (!(x == 0 && y == 0 && z == 0)) { this.boundPosition = new BlockPos(x, y, z); this.markDirty(); } - if(!this.isBreaker){ + if (!this.isBreaker) { this.side = compound.getInteger("Side"); } } } @Override - public void updateEntity(){ + public void updateEntity() { super.updateEntity(); - if(!this.world.isRemote){ + if (!this.world.isRemote) { this.range = TileEntityPhantomface.upgradeRange(RANGE, this.world, this.pos); - if(!this.hasBoundPosition()){ + if (!this.hasBoundPosition()) { this.boundPosition = null; } - if(this.isBoundThingInRange()){ - if(!this.isRedstonePowered && !this.isPulseMode){ - if(this.currentTime > 0){ + if (this.isBoundThingInRange()) { + if (!this.isRedstonePowered && !this.isPulseMode) { + if (this.currentTime > 0) { this.currentTime--; - if(this.currentTime <= 0){ + if (this.currentTime <= 0) { this.doWork(); } - } - else{ + } else { this.currentTime = 30; } } } - if(this.oldRange != this.range){ + if (this.oldRange != this.range) { this.oldRange = this.range; this.sendUpdate(); } - } - else{ - if(this.boundPosition != null){ + } else { + if (this.boundPosition != null) { this.renderParticles(); } } } @Override - public boolean hasBoundPosition(){ - if(this.boundPosition != null){ - if(this.world.getTileEntity(this.boundPosition) instanceof IPhantomTile || (this.getPos().getX() == this.boundPosition.getX() && this.getPos().getY() == this.boundPosition.getY() && this.getPos().getZ() == this.boundPosition.getZ() && this.world.provider.getDimension() == this.world.provider.getDimension())){ + public boolean hasBoundPosition() { + if (this.boundPosition != null) { + if (this.world.getTileEntity(this.boundPosition) instanceof IPhantomTile || (this.getPos().getX() == this.boundPosition.getX() && this.getPos().getY() == this.boundPosition.getY() && this.getPos().getZ() == this.boundPosition.getZ() && this.world.provider.getDimension() == this.world.provider.getDimension())) { this.boundPosition = null; return false; } @@ -126,95 +127,93 @@ public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements return false; } - private void doWork(){ - if(this.isBoundThingInRange()){ - if(this.isBreaker){ + private void doWork() { + if (this.isBoundThingInRange()) { + if (this.isBreaker) { Block blockToBreak = this.world.getBlockState(this.boundPosition).getBlock(); - if(blockToBreak != null && this.world.getBlockState(this.boundPosition).getBlockHardness(this.world, this.boundPosition) > -1.0F){ - NonNullList drops = NonNullList.create(); + if (blockToBreak != null && this.world.getBlockState(this.boundPosition).getBlockHardness(this.world, this.boundPosition) > -1.0F) { + NonNullList drops = NonNullList.create(); blockToBreak.getDrops(drops, world, pos, this.world.getBlockState(this.boundPosition), 0); - if(StackUtil.canAddAll(this.inv, drops, false)){ + if (StackUtil.canAddAll(this.inv, drops, false)) { this.world.playEvent(2001, this.boundPosition, Block.getStateId(this.world.getBlockState(this.boundPosition))); this.world.setBlockToAir(this.boundPosition); StackUtil.addAll(this.inv, drops, false); this.markDirty(); } } - } - else{ + } else { int theSlot = StackUtil.findFirstFilled(this.inv); - if(theSlot == -1) return; + if (theSlot == -1) return; inv.setStackInSlot(theSlot, WorldUtil.useItemAtSide(WorldUtil.getDirectionBySidesInOrder(this.side), this.world, this.boundPosition, inv.getStackInSlot(theSlot))); } } } - public void renderParticles(){ - if(this.world.rand.nextInt(2) == 0){ - double d1 = (double)((float)this.boundPosition.getY()+this.world.rand.nextFloat()); - int i1 = this.world.rand.nextInt(2)*2-1; - int j1 = this.world.rand.nextInt(2)*2-1; - double d4 = ((double)this.world.rand.nextFloat()-0.5D)*0.125D; - double d2 = (double)this.boundPosition.getZ()+0.5D+0.25D*(double)j1; - double d5 = (double)(this.world.rand.nextFloat()*1.0F*(float)j1); - double d0 = (double)this.boundPosition.getX()+0.5D+0.25D*(double)i1; - double d3 = (double)(this.world.rand.nextFloat()*1.0F*(float)i1); + public void renderParticles() { + if (this.world.rand.nextInt(2) == 0) { + double d1 = (double) ((float) this.boundPosition.getY() + this.world.rand.nextFloat()); + int i1 = this.world.rand.nextInt(2) * 2 - 1; + int j1 = this.world.rand.nextInt(2) * 2 - 1; + double d4 = ((double) this.world.rand.nextFloat() - 0.5D) * 0.125D; + double d2 = (double) this.boundPosition.getZ() + 0.5D + 0.25D * (double) j1; + double d5 = (double) (this.world.rand.nextFloat() * 1.0F * (float) j1); + double d0 = (double) this.boundPosition.getX() + 0.5D + 0.25D * (double) i1; + double d3 = (double) (this.world.rand.nextFloat() * 1.0F * (float) i1); this.world.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5); } } @Override - public boolean isBoundThingInRange(){ - return this.hasBoundPosition() && this.boundPosition.distanceSq(this.pos) <= this.range*this.range; + public boolean isBoundThingInRange() { + return this.hasBoundPosition() && this.boundPosition.distanceSq(this.pos) <= this.range * this.range; } @Override - public BlockPos getBoundPosition(){ + public BlockPos getBoundPosition() { return this.boundPosition; } @Override - public void setBoundPosition(BlockPos pos){ + public void setBoundPosition(BlockPos pos) { this.boundPosition = pos; } @Override - public int getGuiID(){ + public int getGuiID() { return GuiHandler.GuiTypes.PHANTOM_PLACER.ordinal(); } @Override - public int getRange(){ + public int getRange() { return this.range; } @Override - public boolean canInsert(int i, ItemStack stack, boolean automation){ - return !automation || !this.isBreaker; + public IAcceptor getAcceptor() { + return ItemStackHandlerAA.ACCEPT_TRUE; } @Override - public boolean canExtract(int slot, ItemStack stack, boolean automation){ - return !automation || this.isBreaker; + public IRemover getRemover() { + return ItemStackHandlerAA.REMOVE_FALSE; } @Override - public boolean isRedstoneToggle(){ + public boolean isRedstoneToggle() { return true; } @Override - public void activateOnPulse(){ + public void activateOnPulse() { this.doWork(); } @Override - public void onButtonPressed(int buttonID, EntityPlayer player){ - if(this.side+1 >= EnumFacing.values().length){ + public void onButtonPressed(int buttonID, EntityPlayer player) { + if (this.side + 1 >= EnumFacing.values().length) { this.side = 0; - } - else{ + } else { this.side++; } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPlacer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPlacer.java index 81c15d5e4..bb22c0b10 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPlacer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPlacer.java @@ -10,11 +10,19 @@ package de.ellpeck.actuallyadditions.mod.tile; -public class TileEntityPlacer extends TileEntityBreaker{ +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; - public TileEntityPlacer(){ +public class TileEntityPlacer extends TileEntityBreaker { + + public TileEntityPlacer() { super(9, "placer"); this.isPlacer = true; } + @Override + public IAcceptor getAcceptor() { + return ItemStackHandlerAA.ACCEPT_TRUE; + } + } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityRangedCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityRangedCollector.java index ff65c3365..a1440e60a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityRangedCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityRangedCollector.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.List; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; @@ -87,13 +88,8 @@ public class TileEntityRangedCollector extends TileEntityInventoryBase implement } @Override - public boolean canInsert(int i, ItemStack stack, boolean fromAutomation) { - return !fromAutomation; - } - - @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation) { - return true; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> !automation; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityXPSolidifier.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityXPSolidifier.java index 58204b3d5..8d95866dc 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityXPSolidifier.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityXPSolidifier.java @@ -10,10 +10,13 @@ package de.ellpeck.actuallyadditions.mod.tile; +import java.util.List; + import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; import de.ellpeck.actuallyadditions.mod.items.InitItems; import de.ellpeck.actuallyadditions.mod.items.ItemSolidifiedExperience; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; +import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.entity.item.EntityXPOrb; import net.minecraft.entity.player.EntityPlayer; @@ -22,8 +25,6 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.MathHelper; -import java.util.List; - public class TileEntityXPSolidifier extends TileEntityInventoryBase implements IButtonReactor { private static final int[] XP_MAP = new int[256]; @@ -162,13 +163,8 @@ public class TileEntityXPSolidifier extends TileEntityInventoryBase implements I } @Override - public boolean canInsert(int slot, ItemStack stack, boolean fromAutomation) { - return slot == 1 && stack.getItem() == InitItems.itemSolidifiedExperience; - } - - @Override - public boolean canExtract(int slot, ItemStack stack, boolean byAutomation) { - return true; + public IAcceptor getAcceptor() { + return (slot, stack, automation) -> slot == 1 && stack.getItem() == InitItems.itemSolidifiedExperience; } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/AwfulUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/AwfulUtil.java index 7df5ea108..772b6183f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/AwfulUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/AwfulUtil.java @@ -104,6 +104,7 @@ public final class AwfulUtil{ error += ("\n" + i++ + ": " + (k == null ? "null" : (k.getClass().getSimpleName() + " <- CLASS | INSTANCE -> " + k.toString() + ", "))); } error += "\n" + "The current side is: " + FMLCommonHandler.instance().getEffectiveSide(); + error += "\n" + "Report this to https://github.com/Ellpeck/ActuallyAdditions/issues"; throw new IllegalStateException(error); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemStackHandlerAA.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemStackHandlerAA.java index 58ba43a5e..b97ff658d 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemStackHandlerAA.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/ItemStackHandlerAA.java @@ -20,8 +20,32 @@ import net.minecraftforge.items.ItemStackHandler; */ public class ItemStackHandlerAA extends ItemStackHandler { - public ItemStackHandlerAA(int slots) { + public static final IAcceptor ACCEPT_TRUE = (a, b, c) -> true; + public static final IRemover REMOVE_TRUE = (a, b) -> true; + public static final IAcceptor ACCEPT_FALSE = (a, b, c) -> false; + public static final IRemover REMOVE_FALSE = (a, b) -> false; + + IAcceptor acceptor; + IRemover remover; + + public ItemStackHandlerAA(NonNullList stacks, IAcceptor acceptor, IRemover remover) { + super(stacks); + this.acceptor = acceptor; + this.remover = remover; + } + + public ItemStackHandlerAA(int slots, IAcceptor acceptor, IRemover remover) { super(slots); + this.acceptor = acceptor; + this.remover = remover; + } + + public ItemStackHandlerAA(NonNullList stacks) { + this(stacks, ACCEPT_TRUE, REMOVE_TRUE); + } + + public ItemStackHandlerAA(int slots) { + this(slots, ACCEPT_TRUE, REMOVE_TRUE); } public NonNullList getItems() { @@ -48,11 +72,33 @@ public class ItemStackHandlerAA extends ItemStackHandler { return super.extractItem(slot, amount, simulate); } - public boolean canAccept(int slot, ItemStack stack, boolean fromAutomation) { - return true; + public final boolean canAccept(int slot, ItemStack stack, boolean automation) { + IAcceptor acceptor = getAcceptor(); + try { + return acceptor.canAccept(slot, stack, automation); + } catch (NullPointerException e) { + e.printStackTrace(); + return false; + } } - public boolean canRemove(int slot, boolean byAutomation) { - return true; + public final boolean canRemove(int slot, boolean automation) { + return getRemover().canRemove(slot, automation); + } + + public IAcceptor getAcceptor() { + return acceptor; + } + + public IRemover getRemover() { + return remover; + } + + public static interface IAcceptor { + boolean canAccept(int slot, ItemStack stack, boolean automation); + } + + public static interface IRemover { + boolean canRemove(int slot, boolean automation); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/StackUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/StackUtil.java index f7ce76a3f..30a838e7e 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/StackUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/StackUtil.java @@ -10,7 +10,6 @@ package de.ellpeck.actuallyadditions.mod.util; -import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -23,7 +22,6 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.ItemHandlerHelper; public final class StackUtil { @@ -67,42 +65,6 @@ public final class StackUtil { return true; } - /** - * Checks if all provided itemstacks will fit in the handler. If you have an AA item handler, use the more sensitive below methods. - * @param inv The Item handler - * @param stacks The stacks to add - * @return If all stacks fit fully. If even one item would not fit, the method returns false. - */ - public static boolean canAddAll(IItemHandler inv, List stacks) { - - int slotMax = inv.getSlots(); - int counter = 0; - - for (ItemStack s : stacks = merge(stacks)) { - for (int i = 0; i < slotMax; i++) { - s = inv.insertItem(i, s, true); - if (s.isEmpty()) break; - } - if (s.isEmpty()) counter++; - } - return counter == stacks.size(); - } - - /** - * Adds all itemstacks in a list to an item handler. If you have an AA item handler, use the more sensitive below methods. - * @param inv The Item handler - * @param stacks The stacks to add - */ - public static void addAll(IItemHandler inv, List stacks) { - int slotMax = inv.getSlots(); - for (ItemStack s : stacks) { - for (int i = 0; i < slotMax; i++) { - s = inv.insertItem(i, s, false); - if (s.isEmpty()) break; - } - } - } - /** * Checks if all provided itemstacks will fit in the AA handler. Use addAll below to actually add the stacks. This is strictly a check function. * @param inv The AA Item handler @@ -111,13 +73,11 @@ public final class StackUtil { * @return If all stacks fit fully. If even one item would not fit, the method returns false. */ public static boolean canAddAll(ItemStackHandlerAA inv, List stacks, boolean fromAutomation) { - - int slotMax = inv.getSlots(); int counter = 0; - - for (ItemStack s : stacks = merge(stacks)) { - for (int i = 0; i < slotMax; i++) { - s = inv.insertItem(i, s, true, fromAutomation); + ItemStackHandlerAA dummy = testDummy(inv, 0, inv.getSlots()); + for (ItemStack s : stacks) { + for (int i = 0; i < dummy.getSlots(); i++) { + s = dummy.insertItem(i, s, false, fromAutomation); if (s.isEmpty()) break; } if (s.isEmpty()) counter++; @@ -152,10 +112,10 @@ public final class StackUtil { */ public static boolean canAddAll(ItemStackHandlerAA inv, List stacks, int slot, int endSlot, boolean fromAutomation) { int counter = 0; - - for (ItemStack s : stacks = merge(stacks)) { - for (int i = slot; i < endSlot; i++) { - s = inv.insertItem(i, s, true, fromAutomation); + ItemStackHandlerAA dummy = testDummy(inv, slot, endSlot); + for (ItemStack s : stacks) { + for (int i = 0; i < dummy.getSlots(); i++) { + s = dummy.insertItem(i, s, false, fromAutomation); if (s.isEmpty()) break; } if (s.isEmpty()) counter++; @@ -249,31 +209,14 @@ public final class StackUtil { } /** - * Combines every stack in the given list into larger stacks when possible. + * Constructs a clone of the given item handler, from the given slots. The new item handler will have the provided slot as slot 0. + * This is used for testing the ability to add all itemstacks, and should not be used for anything else. */ - public static List merge(List stacks) { - if (stacks.isEmpty()) return stacks; - - ItemStack[] array = stacks.toArray(new ItemStack[0]); - List list = new ArrayList<>(); - - while (!array[array.length - 1].isEmpty()) { - ItemStack merged = ItemStack.EMPTY; - for (int i = 0; i < array.length; i++) { - ItemStack stack = array[i]; - if (merged.isEmpty()) { - merged = stack.copy(); - array[i] = ItemStack.EMPTY; - } else if (ItemHandlerHelper.canItemStacksStack(merged, stack)) { - merged.grow(stack.getCount()); - array[i] = ItemStack.EMPTY; - } else break; - } - list.add(merged); - merged = ItemStack.EMPTY; - } - - return list; + public static ItemStackHandlerAA testDummy(ItemStackHandlerAA inv, int slot, int endSlot) { + NonNullList stacks = NonNullList.withSize(endSlot - slot, getEmpty()); + for (int i = slot; i < endSlot; i++) + stacks.set(i - slot, inv.getStackInSlot(i).copy()); + return new ItemStackHandlerAA(stacks, inv.getAcceptor(), inv.getRemover()); } }