TechReborn/src/main/java/techreborn/tiles/TileScrapboxinator.java

193 lines
4.3 KiB
Java
Raw Normal View History

package techreborn.tiles;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import reborncore.api.power.EnumPowerTier;
import reborncore.api.tile.IInventoryProvider;
2016-10-08 21:46:16 +02:00
import reborncore.common.IWrenchable;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.powerSystem.TilePowerAcceptor;
import reborncore.common.util.Inventory;
import techreborn.api.ScrapboxList;
import techreborn.init.ModBlocks;
import techreborn.init.ModItems;
import java.util.Random;
2016-03-15 09:13:05 +01:00
2016-10-08 21:46:16 +02:00
public class TileScrapboxinator extends TilePowerAcceptor implements IWrenchable, IInventoryProvider, ISidedInventory {
public Inventory inventory = new Inventory(6, "TileScrapboxinator", 64, this);
public int capacity = 1000;
public int cost = 20;
public int progress;
public int time = 200;
public int chance = 4;
public int random;
public int input1 = 0;
public int output = 1;
2016-10-08 21:46:16 +02:00
public TileScrapboxinator() {
super(1);
}
2016-10-08 21:46:16 +02:00
public int gaugeProgressScaled(int scale) {
return (progress * scale) / time;
}
@Override
2016-10-08 21:46:16 +02:00
public void updateEntity() {
boolean burning = isBurning();
boolean updateInventory = false;
2016-10-08 21:46:16 +02:00
if (getEnergy() <= cost && canOpen()) {
if (getEnergy() > cost) {
updateInventory = true;
}
}
2016-10-08 21:46:16 +02:00
if (isBurning() && canOpen()) {
updateState();
progress++;
2016-10-08 21:46:16 +02:00
if (progress >= time) {
progress = 0;
recycleItems();
updateInventory = true;
}
2016-10-08 21:46:16 +02:00
} else {
progress = 0;
updateState();
}
2016-10-08 21:46:16 +02:00
if (burning != isBurning()) {
updateInventory = true;
}
2016-10-08 21:46:16 +02:00
if (updateInventory) {
markDirty();
}
}
2016-10-08 21:46:16 +02:00
public void recycleItems() {
2016-11-19 13:50:08 +01:00
if (this.canOpen() && !world.isRemote) {
int random = new Random().nextInt(ScrapboxList.stacks.size());
ItemStack out = ScrapboxList.stacks.get(random).copy();
2016-10-08 21:46:16 +02:00
if (getStackInSlot(output) == null) {
useEnergy(cost);
setInventorySlotContents(output, out);
}
2016-11-19 13:50:08 +01:00
if (getStackInSlot(input1).getCount() > 1) {
useEnergy(cost);
this.decrStackSize(input1, 1);
2016-10-08 21:46:16 +02:00
} else {
useEnergy(cost);
setInventorySlotContents(input1, ItemStack.EMPTY);
}
}
}
2016-10-08 21:46:16 +02:00
public boolean canOpen() {
return getStackInSlot(input1) != ItemStack.EMPTY && getStackInSlot(output) == ItemStack.EMPTY;
}
2016-10-08 21:46:16 +02:00
public boolean isBurning() {
return getEnergy() > cost;
}
2016-10-08 21:46:16 +02:00
public void updateState() {
2016-11-19 13:50:08 +01:00
IBlockState blockState = world.getBlockState(pos);
2016-10-08 21:46:16 +02:00
if (blockState.getBlock() instanceof BlockMachineBase) {
BlockMachineBase blockMachineBase = (BlockMachineBase) blockState.getBlock();
if (blockState.getValue(BlockMachineBase.ACTIVE) != progress > 0)
2016-11-19 13:50:08 +01:00
blockMachineBase.setActive(progress > 0, world, pos);
}
}
@Override
2016-10-08 21:46:16 +02:00
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) {
return false;
}
@Override
2016-10-08 21:46:16 +02:00
public EnumFacing getFacing() {
return getFacingEnum();
}
@Override
2016-10-08 21:46:16 +02:00
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
}
@Override
2016-10-08 21:46:16 +02:00
public float getWrenchDropRate() {
return 1.0F;
}
@Override
2016-10-08 21:46:16 +02:00
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.SCRAPBOXINATOR, 1);
}
2016-10-08 21:46:16 +02:00
public boolean isComplete() {
return false;
}
// ISidedInventory
@Override
2016-10-08 21:46:16 +02:00
public int[] getSlotsForFace(EnumFacing side) {
return side == EnumFacing.DOWN ? new int[] { 0, 1, 2 } : new int[] { 0, 1, 2 };
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canInsertItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
if (slotIndex == 2)
return false;
2016-10-08 21:46:16 +02:00
if (slotIndex == 1) {
if (itemStack.getItem() == ModItems.SCRAP_BOX) {
return true;
}
}
return isItemValidForSlot(slotIndex, itemStack);
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canExtractItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
return slotIndex == 2;
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxPower() {
return capacity;
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canAcceptEnergy(EnumFacing direction) {
return true;
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canProvideEnergy(EnumFacing direction) {
return false;
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxOutput() {
return 0;
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxInput() {
return 32;
}
@Override
2016-10-08 21:46:16 +02:00
public EnumPowerTier getTier() {
return EnumPowerTier.MEDIUM;
}
@Override
public Inventory getInventory() {
return inventory;
}
}