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

203 lines
4.8 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;
2017-01-08 13:10:13 +01:00
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;
2017-01-08 13:10:13 +01:00
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);
}
2017-01-08 13:10:13 +01:00
public int gaugeProgressScaled(final int scale) {
return this.progress * scale / this.time;
}
@Override
2016-10-08 21:46:16 +02:00
public void updateEntity() {
2017-01-08 13:10:13 +01:00
final boolean burning = this.isBurning();
boolean updateInventory = false;
2017-01-08 13:10:13 +01:00
if (this.getEnergy() <= this.cost && this.canOpen()) {
if (this.getEnergy() > this.cost) {
updateInventory = true;
}
}
2017-01-08 13:10:13 +01:00
if (this.isBurning() && this.canOpen()) {
this.updateState();
2017-01-08 13:10:13 +01:00
this.progress++;
if (this.progress >= this.time) {
this.progress = 0;
this.recycleItems();
updateInventory = true;
}
2016-10-08 21:46:16 +02:00
} else {
2017-01-08 13:10:13 +01:00
this.progress = 0;
this.updateState();
}
2017-01-08 13:10:13 +01:00
if (burning != this.isBurning()) {
updateInventory = true;
}
2016-10-08 21:46:16 +02:00
if (updateInventory) {
2017-01-08 13:10:13 +01:00
this.markDirty();
}
}
2016-10-08 21:46:16 +02:00
public void recycleItems() {
2017-01-08 13:10:13 +01:00
if (this.canOpen() && !this.world.isRemote) {
final int random = new Random().nextInt(ScrapboxList.stacks.size());
final ItemStack out = ScrapboxList.stacks.get(random).copy();
if (this.getStackInSlot(this.output) == null) {
this.useEnergy(this.cost);
this.setInventorySlotContents(this.output, out);
}
2017-01-08 13:10:13 +01:00
if (this.getStackInSlot(this.input1).getCount() > 1) {
this.useEnergy(this.cost);
this.decrStackSize(this.input1, 1);
2016-10-08 21:46:16 +02:00
} else {
2017-01-08 13:10:13 +01:00
this.useEnergy(this.cost);
this.setInventorySlotContents(this.input1, ItemStack.EMPTY);
}
}
}
2016-10-08 21:46:16 +02:00
public boolean canOpen() {
2017-01-08 13:10:13 +01:00
return this.getStackInSlot(this.input1) != ItemStack.EMPTY && this.getStackInSlot(this.output) == ItemStack.EMPTY;
}
2016-10-08 21:46:16 +02:00
public boolean isBurning() {
2017-01-08 13:10:13 +01:00
return this.getEnergy() > this.cost;
}
2016-10-08 21:46:16 +02:00
public void updateState() {
2017-01-08 13:10:13 +01:00
final IBlockState blockState = this.world.getBlockState(this.pos);
2016-10-08 21:46:16 +02:00
if (blockState.getBlock() instanceof BlockMachineBase) {
2017-01-08 13:10:13 +01:00
final BlockMachineBase blockMachineBase = (BlockMachineBase) blockState.getBlock();
if (blockState.getValue(BlockMachineBase.ACTIVE) != this.progress > 0)
blockMachineBase.setActive(this.progress > 0, this.world, this.pos);
}
}
@Override
2017-01-08 13:10:13 +01:00
public boolean wrenchCanSetFacing(final EntityPlayer entityPlayer, final EnumFacing side) {
return false;
}
@Override
2016-10-08 21:46:16 +02:00
public EnumFacing getFacing() {
2017-01-08 13:10:13 +01:00
return this.getFacingEnum();
}
@Override
2017-01-08 13:10:13 +01:00
public boolean wrenchCanRemove(final EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
}
@Override
2016-10-08 21:46:16 +02:00
public float getWrenchDropRate() {
return 1.0F;
}
@Override
2017-01-08 13:10:13 +01:00
public ItemStack getWrenchDrop(final EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.SCRAPBOXINATOR, 1);
}
2016-10-08 21:46:16 +02:00
public boolean isComplete() {
return false;
}
// ISidedInventory
@Override
2017-01-08 13:10:13 +01:00
public int[] getSlotsForFace(final EnumFacing side) {
return side == EnumFacing.DOWN ? new int[] { 0, 1, 2 } : new int[] { 0, 1, 2 };
}
@Override
2017-01-08 13:10:13 +01:00
public boolean canInsertItem(final int slotIndex, final ItemStack itemStack, final 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;
}
}
2017-01-08 13:10:13 +01:00
return this.isItemValidForSlot(slotIndex, itemStack);
}
@Override
2017-01-08 13:10:13 +01:00
public boolean canExtractItem(final int slotIndex, final ItemStack itemStack, final EnumFacing side) {
return slotIndex == 2;
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxPower() {
2017-01-08 13:10:13 +01:00
return this.capacity;
}
@Override
2017-01-08 13:10:13 +01:00
public boolean canAcceptEnergy(final EnumFacing direction) {
return true;
}
@Override
2017-01-08 13:10:13 +01:00
public boolean canProvideEnergy(final 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() {
2017-01-08 13:10:13 +01:00
return this.inventory;
}
public int getProgress() {
return this.progress;
}
public void setProgress(final int progress) {
this.progress = progress;
}
}