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

243 lines
7.5 KiB
Java
Raw Normal View History

/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2017 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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.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.registration.RebornRegistry;
import reborncore.common.registration.impl.ConfigRegistry;
import reborncore.common.util.Inventory;
import techreborn.api.ScrapboxList;
import techreborn.client.container.IContainerProvider;
import techreborn.client.container.builder.BuiltContainer;
import techreborn.client.container.builder.ContainerBuilder;
import techreborn.init.ModBlocks;
import techreborn.init.ModItems;
import techreborn.lib.ModInfo;
import java.util.Random;
2016-03-15 09:13:05 +01:00
@RebornRegistry(modID = ModInfo.MOD_ID)
public class TileScrapboxinator extends TilePowerAcceptor
implements IWrenchable, IInventoryProvider, ISidedInventory, IContainerProvider {
@ConfigRegistry(config = "machines", category = "scrapboxinator", key = "ScrapboxinatorMaxInput", comment = "Scrapboxinator Max Input (Value in EU)")
public static int maxInput = 32;
@ConfigRegistry(config = "machines", category = "scrapboxinator", key = "ScrapboxinatorMaxEnergy", comment = "Scrapboxinator Max Energy (Value in EU)")
public static int maxEnergy = 1000;
@ConfigRegistry(config = "machines", category = "scrapboxinator", key = "ScrapboxinatorEnergyCost", comment = "Scrapboxinator Energy Cost (Value in EU)")
public static int cost = 20;
@ConfigRegistry(config = "machines", category = "scrapboxinator", key = "ScrapboxinatorRunTime", comment = "Scrapboxinator Run Time")
public static int runTime = 200;
// @ConfigRegistry(config = "machines", category = "scrapboxinator", key = "ScrapboxinatorWrenchDropRate", comment = "Scrapboxinator Wrench Drop Rate")
public static float wrenchDropRate = 1.0F;
public Inventory inventory = new Inventory(6, "TileScrapboxinator", 64, this);
public int progress;
public int input1 = 0;
public int output = 1;
2016-10-08 21:46:16 +02:00
public TileScrapboxinator() {
2017-06-09 17:31:19 +02:00
super();
}
2017-01-08 13:10:13 +01:00
public int gaugeProgressScaled(final int scale) {
return this.progress * scale / runTime;
}
@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;
if (this.getEnergy() <= cost && this.canOpen()) {
if (this.getEnergy() > 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 >= runTime) {
2017-01-08 13:10:13 +01:00
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(cost);
2017-01-08 13:10:13 +01:00
this.setInventorySlotContents(this.output, out);
}
2017-01-08 13:10:13 +01:00
if (this.getStackInSlot(this.input1).getCount() > 1) {
this.useEnergy(cost);
2017-01-08 13:10:13 +01:00
this.decrStackSize(this.input1, 1);
2016-10-08 21:46:16 +02:00
} else {
this.useEnergy(cost);
2017-01-08 13:10:13 +01:00
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() {
return this.getEnergy() > 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 wrenchDropRate;
}
@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
2017-04-11 20:12:32 +02:00
public double getBaseMaxPower() {
return maxEnergy;
}
@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
2017-04-11 20:12:32 +02:00
public double getBaseMaxOutput() {
return 0;
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxInput() {
return maxInput;
}
@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;
}
@Override
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("scrapboxinator").player(player.inventory).inventory(8, 84).hotbar(8, 142)
.addInventory().tile(this).filterSlot(0, 56, 34, stack -> stack.getItem() == ModItems.SCRAP_BOX)
.outputSlot(1, 116, 34).upgradeSlot(2, 152, 8).upgradeSlot(3, 152, 26).upgradeSlot(4, 152, 44)
.upgradeSlot(5, 152, 62).syncEnergyValue().syncIntegerValue(this::getProgress, this::setProgress)
.addInventory().create();
}
}