Move common code for iron machines into abstract class
This commit is contained in:
parent
fbfa24b6ff
commit
7c4564b5ab
7 changed files with 289 additions and 381 deletions
|
@ -68,14 +68,14 @@ public abstract class GenericMachineBlockEntity extends PowerAcceptorBlockEntity
|
||||||
checkTier();
|
checkTier();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getProgressScaled(final int scale) {
|
public int getProgressScaled(int scale) {
|
||||||
if (crafter != null && crafter.currentTickTime != 0) {
|
if (crafter != null && crafter.currentTickTime != 0) {
|
||||||
return crafter.currentTickTime * scale / crafter.currentNeededTicks;
|
return crafter.currentTickTime * scale / crafter.currentNeededTicks;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TilePowerAcceptor
|
// PowerAcceptorBlockEntity
|
||||||
@Override
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
super.tick();
|
super.tick();
|
||||||
|
@ -115,7 +115,7 @@ public abstract class GenericMachineBlockEntity extends PowerAcceptorBlockEntity
|
||||||
return new ItemStack(toolDrop, 1);
|
return new ItemStack(toolDrop, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ItemHandlerProvider
|
// InventoryProvider
|
||||||
@Override
|
@Override
|
||||||
public RebornInventory<?> getInventory() {
|
public RebornInventory<?> getInventory() {
|
||||||
return inventory;
|
return inventory;
|
||||||
|
|
|
@ -0,0 +1,207 @@
|
||||||
|
package techreborn.blockentity.machine.iron;
|
||||||
|
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
|
||||||
|
import net.minecraft.block.entity.BlockEntityType;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
|
import reborncore.api.IToolDrop;
|
||||||
|
import reborncore.api.blockentity.InventoryProvider;
|
||||||
|
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
||||||
|
import reborncore.common.blocks.BlockMachineBase;
|
||||||
|
import reborncore.common.util.RebornInventory;
|
||||||
|
import techreborn.config.TechRebornConfig;
|
||||||
|
|
||||||
|
public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop {
|
||||||
|
|
||||||
|
public RebornInventory<?> inventory;
|
||||||
|
public int burnTime;
|
||||||
|
public int totalBurnTime;
|
||||||
|
public int progress;
|
||||||
|
public int totalCookingTime;
|
||||||
|
int fuelSlot;
|
||||||
|
Block toolDrop;
|
||||||
|
boolean active = false;
|
||||||
|
|
||||||
|
public AbstractIronMachineBlockEntity(BlockEntityType<?> blockEntityTypeIn, int fuelSlot, Block toolDrop) {
|
||||||
|
super(blockEntityTypeIn);
|
||||||
|
this.fuelSlot = fuelSlot;
|
||||||
|
// default value for vanilla smelting recipes is 200
|
||||||
|
this.totalCookingTime = (int) (200 / TechRebornConfig.cookingScale);
|
||||||
|
this.toolDrop = toolDrop;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that we have all inputs and can put output into slot
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected abstract boolean canSmelt();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn ingredients into the appropriate smelted
|
||||||
|
* item in the output slot
|
||||||
|
*/
|
||||||
|
protected abstract void smelt();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of ticks that the supplied fuel item will keep the
|
||||||
|
* furnace burning, or 0 if the item isn't fuel
|
||||||
|
* @param stack Itemstack of fuel
|
||||||
|
* @return Integer Number of ticks
|
||||||
|
*/
|
||||||
|
private int getItemBurnTime(ItemStack stack) {
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return (int) (AbstractFurnaceBlockEntity.createFuelTimeMap().getOrDefault(stack.getItem(), 0) * TechRebornConfig.fuelScale);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns remaining fraction of fuel burn time
|
||||||
|
* @param scale Scale to use for burn time
|
||||||
|
* @return int scaled remaining fuel burn time
|
||||||
|
*/
|
||||||
|
public int getBurnTimeRemainingScaled(int scale) {
|
||||||
|
if (totalBurnTime == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return burnTime * scale / totalBurnTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns crafting progress
|
||||||
|
* @param scale Scale to use for crafting progress
|
||||||
|
* @return int Scaled crafting progress
|
||||||
|
*/
|
||||||
|
public int getProgressScaled(int scale) {
|
||||||
|
if (totalCookingTime > 0) {
|
||||||
|
return progress * scale / totalCookingTime;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if Iron Machine is burning fuel thus can do work
|
||||||
|
* @return Boolean True if machine is burning
|
||||||
|
*/
|
||||||
|
public boolean isBurning() {
|
||||||
|
return burnTime > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateState() {
|
||||||
|
BlockState state = world.getBlockState(pos);
|
||||||
|
if (state.getBlock() instanceof BlockMachineBase) {
|
||||||
|
BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock();
|
||||||
|
if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0)
|
||||||
|
blockMachineBase.setActive(burnTime > 0, world, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MachineBaseBlockEntity
|
||||||
|
@Override
|
||||||
|
public void fromTag(CompoundTag compoundTag) {
|
||||||
|
super.fromTag(compoundTag);
|
||||||
|
burnTime = compoundTag.getInt("BurnTime");
|
||||||
|
totalBurnTime = compoundTag.getInt("TotalBurnTime");
|
||||||
|
progress = compoundTag.getInt("Progress");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompoundTag toTag(CompoundTag compoundTag) {
|
||||||
|
super.toTag(compoundTag);
|
||||||
|
compoundTag.putInt("BurnTime", burnTime);
|
||||||
|
compoundTag.putInt("TotalBurnTime", totalBurnTime);
|
||||||
|
compoundTag.putInt("Progress", progress);
|
||||||
|
return compoundTag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick() {
|
||||||
|
super.tick();
|
||||||
|
if(world.isClient){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean isBurning = isBurning();
|
||||||
|
if (isBurning) {
|
||||||
|
--burnTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isBurning && canSmelt()) {
|
||||||
|
burnTime = totalBurnTime = getItemBurnTime(inventory.getInvStack(fuelSlot));
|
||||||
|
if (burnTime > 0) {
|
||||||
|
// Fuel slot
|
||||||
|
ItemStack fuelStack = inventory.getInvStack(fuelSlot);
|
||||||
|
if (fuelStack.getItem().hasRecipeRemainder()) {
|
||||||
|
inventory.setInvStack(fuelSlot, new ItemStack(fuelStack.getItem().getRecipeRemainder()));
|
||||||
|
} else if (fuelStack.getCount() > 1) {
|
||||||
|
inventory.shrinkSlot(fuelSlot, 1);
|
||||||
|
} else if (fuelStack.getCount() == 1) {
|
||||||
|
inventory.setInvStack(fuelSlot, ItemStack.EMPTY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBurning() && canSmelt()) {
|
||||||
|
++progress;
|
||||||
|
if (progress == totalCookingTime) {
|
||||||
|
progress = 0;
|
||||||
|
smelt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
progress = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBurning != isBurning()) {
|
||||||
|
inventory.setChanged();
|
||||||
|
updateState();
|
||||||
|
}
|
||||||
|
if (inventory.hasChanged()) {
|
||||||
|
markDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canBeUpgraded() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// InventoryProvider
|
||||||
|
@Override
|
||||||
|
public RebornInventory<?> getInventory() {
|
||||||
|
return inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IToolDrop
|
||||||
|
@Override
|
||||||
|
public ItemStack getToolDrop(PlayerEntity entityPlayer) {
|
||||||
|
return new ItemStack(toolDrop);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBurnTime() {
|
||||||
|
return this.burnTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBurnTime(int burnTime) {
|
||||||
|
this.burnTime = burnTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalBurnTime() {
|
||||||
|
return this.totalBurnTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalBurnTime(int totalBurnTime) {
|
||||||
|
this.totalBurnTime = totalBurnTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getProgress() {
|
||||||
|
return progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProgress(int progress) {
|
||||||
|
this.progress = progress;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,19 +24,11 @@
|
||||||
|
|
||||||
package techreborn.blockentity.machine.iron;
|
package techreborn.blockentity.machine.iron;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
|
||||||
import net.minecraft.util.math.Direction;
|
|
||||||
import reborncore.api.IToolDrop;
|
|
||||||
import reborncore.api.blockentity.InventoryProvider;
|
|
||||||
import reborncore.client.containerBuilder.IContainerProvider;
|
import reborncore.client.containerBuilder.IContainerProvider;
|
||||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||||
import reborncore.client.containerBuilder.builder.ContainerBuilder;
|
import reborncore.client.containerBuilder.builder.ContainerBuilder;
|
||||||
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
|
||||||
import reborncore.common.blocks.BlockMachineBase;
|
|
||||||
import reborncore.common.crafting.RebornRecipe;
|
import reborncore.common.crafting.RebornRecipe;
|
||||||
import reborncore.common.crafting.ingredient.RebornIngredient;
|
import reborncore.common.crafting.ingredient.RebornIngredient;
|
||||||
import reborncore.common.util.RebornInventory;
|
import reborncore.common.util.RebornInventory;
|
||||||
|
@ -44,93 +36,15 @@ import techreborn.init.ModRecipes;
|
||||||
import techreborn.init.TRBlockEntities;
|
import techreborn.init.TRBlockEntities;
|
||||||
import techreborn.init.TRContent;
|
import techreborn.init.TRContent;
|
||||||
|
|
||||||
public class IronAlloyFurnaceBlockEntity extends MachineBaseBlockEntity
|
public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity implements IContainerProvider {
|
||||||
implements IToolDrop, InventoryProvider, IContainerProvider {
|
|
||||||
|
|
||||||
public RebornInventory<IronAlloyFurnaceBlockEntity> inventory = new RebornInventory<>(4, "IronAlloyFurnaceBlockEntity", 64, this);
|
|
||||||
public int burnTime;
|
|
||||||
public int totalBurnTime;
|
|
||||||
public int progress;
|
|
||||||
int input1 = 0;
|
int input1 = 0;
|
||||||
int input2 = 1;
|
int input2 = 1;
|
||||||
int output = 2;
|
int output = 2;
|
||||||
int fuel = 3;
|
|
||||||
|
|
||||||
public IronAlloyFurnaceBlockEntity() {
|
public IronAlloyFurnaceBlockEntity() {
|
||||||
super(TRBlockEntities.IRON_ALLOY_FURNACE);
|
super(TRBlockEntities.IRON_ALLOY_FURNACE, 3, TRContent.Machine.IRON_ALLOY_FURNACE.block);
|
||||||
}
|
this.inventory = new RebornInventory<>(4, "IronAlloyFurnaceBlockEntity", 64, this);
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the number of ticks that the supplied fuel item will keep the
|
|
||||||
* furnace burning, or 0 if the item isn't fuel
|
|
||||||
* @param stack Itemstack of fuel
|
|
||||||
* @return Integer Number of ticks
|
|
||||||
*/
|
|
||||||
public static int getItemBurnTime(ItemStack stack) {
|
|
||||||
if (stack.isEmpty()) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return AbstractFurnaceBlockEntity.createFuelTimeMap().getOrDefault(stack.getItem(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromTag(CompoundTag compoundTag) {
|
|
||||||
super.fromTag(compoundTag);
|
|
||||||
burnTime = compoundTag.getInt("BurnTime");
|
|
||||||
totalBurnTime = compoundTag.getInt("TotalBurnTime");
|
|
||||||
progress = compoundTag.getInt("Progress");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompoundTag toTag(CompoundTag compoundTag) {
|
|
||||||
super.toTag(compoundTag);
|
|
||||||
compoundTag.putInt("BurnTime", burnTime);
|
|
||||||
compoundTag.putInt("TotalBurnTime", totalBurnTime);
|
|
||||||
compoundTag.putInt("Progress", progress);
|
|
||||||
return compoundTag;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void tick() {
|
|
||||||
super.tick();
|
|
||||||
if(world.isClient){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isBurning = isBurning();
|
|
||||||
boolean updateInventory = false;
|
|
||||||
if (isBurning) {
|
|
||||||
--burnTime;
|
|
||||||
}
|
|
||||||
if (burnTime != 0 || !inventory.getInvStack(input1).isEmpty() && !inventory.getInvStack(fuel).isEmpty()) {
|
|
||||||
if (burnTime == 0 && canSmelt()) {
|
|
||||||
totalBurnTime = burnTime = getItemBurnTime(inventory.getInvStack(fuel));
|
|
||||||
if (burnTime > 0) {
|
|
||||||
updateInventory = true;
|
|
||||||
if (!inventory.getInvStack(fuel).isEmpty()) {
|
|
||||||
inventory.shrinkSlot(fuel, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isBurning() && canSmelt()) {
|
|
||||||
++progress;
|
|
||||||
if (progress == 200) {
|
|
||||||
progress = 0;
|
|
||||||
smeltItem();
|
|
||||||
updateInventory = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
progress = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isBurning != isBurning()) {
|
|
||||||
updateInventory = true;
|
|
||||||
updateState();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (updateInventory) {
|
|
||||||
markDirty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasAllInputs(RebornRecipe recipeType) {
|
public boolean hasAllInputs(RebornRecipe recipeType) {
|
||||||
|
@ -150,7 +64,8 @@ public class IronAlloyFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canSmelt() {
|
@Override
|
||||||
|
protected boolean canSmelt() {
|
||||||
if (inventory.getInvStack(input1).isEmpty() || inventory.getInvStack(input2).isEmpty()) {
|
if (inventory.getInvStack(input1).isEmpty() || inventory.getInvStack(input2).isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -172,11 +87,8 @@ public class IronAlloyFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
return result <= inventory.getStackLimit() && result <= inventory.getInvStack(output).getMaxCount();
|
return result <= inventory.getStackLimit() && result <= inventory.getInvStack(output).getMaxCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Turn one item from the furnace source stack into the appropriate smelted
|
protected void smelt() {
|
||||||
* item in the furnace result stack
|
|
||||||
*/
|
|
||||||
public void smeltItem() {
|
|
||||||
if (!canSmelt()) {
|
if (!canSmelt()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -212,87 +124,16 @@ public class IronAlloyFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Furnace isBurning
|
|
||||||
* @return Boolean True if furnace is burning
|
|
||||||
*/
|
|
||||||
public boolean isBurning() {
|
|
||||||
return burnTime > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBurnTimeRemainingScaled(int scale) {
|
|
||||||
if (totalBurnTime == 0) {
|
|
||||||
totalBurnTime = 200;
|
|
||||||
}
|
|
||||||
|
|
||||||
return burnTime * scale / totalBurnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCookProgressScaled(int scale) {
|
|
||||||
return progress * scale / 200;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateState() {
|
|
||||||
BlockState state = world.getBlockState(pos);
|
|
||||||
if (state.getBlock() instanceof BlockMachineBase) {
|
|
||||||
BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock();
|
|
||||||
if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0)
|
|
||||||
blockMachineBase.setActive(burnTime > 0, world, pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Direction getFacing() {
|
|
||||||
return getFacingEnum();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ItemStack getToolDrop(PlayerEntity entityPlayer) {
|
|
||||||
return TRContent.Machine.IRON_ALLOY_FURNACE.getStack();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RebornInventory<IronAlloyFurnaceBlockEntity> getInventory() {
|
|
||||||
return inventory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBurnTime() {
|
|
||||||
return burnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBurnTime(int burnTime) {
|
|
||||||
this.burnTime = burnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTotalBurnTime() {
|
|
||||||
return totalBurnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTotalBurnTime(int currentItemBurnTime) {
|
|
||||||
this.totalBurnTime = currentItemBurnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCookTime() {
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCookTime(int cookTime) {
|
|
||||||
this.progress = cookTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
|
public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
|
||||||
return new ContainerBuilder("alloyfurnace").player(player.inventory).inventory().hotbar()
|
return new ContainerBuilder("alloyfurnace").player(player.inventory).inventory().hotbar()
|
||||||
.addInventory().blockEntity(this)
|
.addInventory().blockEntity(this)
|
||||||
.slot(0, 47, 17)
|
.slot(0, 47, 17)
|
||||||
.slot(1, 65, 17)
|
.slot(1, 65, 17)
|
||||||
.outputSlot(2, 116, 35).fuelSlot(3, 56, 53).syncIntegerValue(this::getBurnTime, this::setBurnTime)
|
.outputSlot(2, 116, 35).fuelSlot(3, 56, 53)
|
||||||
.syncIntegerValue(this::getCookTime, this::setCookTime)
|
.syncIntegerValue(this::getBurnTime, this::setBurnTime)
|
||||||
.syncIntegerValue(this::getTotalBurnTime, this::setTotalBurnTime).addInventory().create(this, syncID);
|
.syncIntegerValue(this::getProgress, this::setProgress)
|
||||||
}
|
.syncIntegerValue(this::getTotalBurnTime, this::setTotalBurnTime)
|
||||||
|
.addInventory().create(this, syncID);
|
||||||
@Override
|
|
||||||
public boolean canBeUpgraded() {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,6 @@ package techreborn.blockentity.machine.iron;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
|
|
||||||
import net.minecraft.entity.ExperienceOrbEntity;
|
import net.minecraft.entity.ExperienceOrbEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
@ -35,104 +33,36 @@ import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.recipe.RecipeType;
|
import net.minecraft.recipe.RecipeType;
|
||||||
import net.minecraft.recipe.SmeltingRecipe;
|
import net.minecraft.recipe.SmeltingRecipe;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import reborncore.api.blockentity.InventoryProvider;
|
|
||||||
import reborncore.client.containerBuilder.IContainerProvider;
|
import reborncore.client.containerBuilder.IContainerProvider;
|
||||||
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
import reborncore.client.containerBuilder.builder.BuiltContainer;
|
||||||
import reborncore.client.containerBuilder.builder.ContainerBuilder;
|
import reborncore.client.containerBuilder.builder.ContainerBuilder;
|
||||||
import reborncore.common.blocks.BlockMachineBase;
|
|
||||||
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
|
||||||
import reborncore.common.util.IInventoryAccess;
|
|
||||||
import reborncore.common.util.RebornInventory;
|
import reborncore.common.util.RebornInventory;
|
||||||
import reborncore.common.util.ItemUtils;
|
|
||||||
import techreborn.init.TRBlockEntities;
|
import techreborn.init.TRBlockEntities;
|
||||||
|
import techreborn.init.TRContent;
|
||||||
import techreborn.utils.RecipeUtils;
|
import techreborn.utils.RecipeUtils;
|
||||||
|
|
||||||
public class IronFurnaceBlockEntity extends MachineBaseBlockEntity
|
public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity implements IContainerProvider {
|
||||||
implements InventoryProvider, IContainerProvider {
|
|
||||||
|
|
||||||
public RebornInventory<IronFurnaceBlockEntity> inventory = new RebornInventory<>(3, "IronFurnaceBlockEntity", 64, this, getInvetoryAccess());
|
|
||||||
public int burnTime;
|
|
||||||
public int totalBurnTime;
|
|
||||||
public int progress;
|
|
||||||
public int fuelScale = 160;
|
|
||||||
int inputSlot = 0;
|
int inputSlot = 0;
|
||||||
int outputSlot = 1;
|
int outputSlot = 1;
|
||||||
int fuelSlot = 2;
|
|
||||||
boolean active = false;
|
|
||||||
public float experience;
|
public float experience;
|
||||||
|
|
||||||
public IronFurnaceBlockEntity() {
|
public IronFurnaceBlockEntity() {
|
||||||
super(TRBlockEntities.IRON_FURNACE);
|
super(TRBlockEntities.IRON_FURNACE, 2, TRContent.Machine.IRON_FURNACE.block);
|
||||||
|
this.inventory = new RebornInventory<>(3, "IronFurnaceBlockEntity", 64, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IInventoryAccess<IronFurnaceBlockEntity> getInvetoryAccess(){
|
public void handleGuiInputFromClient(PlayerEntity playerIn) {
|
||||||
return (slotID, stack, face, direction, blockEntity) -> {
|
if (playerIn instanceof ServerPlayerEntity) {
|
||||||
if(direction == IInventoryAccess.AccessDirection.INSERT){
|
ServerPlayerEntity player = (ServerPlayerEntity) playerIn;
|
||||||
boolean isFuel = AbstractFurnaceBlockEntity.canUseAsFuel(stack);
|
int totalExperience = (int) experience;
|
||||||
if(isFuel){
|
while (totalExperience > 0) {
|
||||||
ItemStack fuelSlotStack = blockEntity.inventory.getInvStack(blockEntity.fuelSlot);
|
int expToDrop = ExperienceOrbEntity.roundToOrbSize(totalExperience);
|
||||||
if(fuelSlotStack.isEmpty() || ItemUtils.isItemEqual(stack, fuelSlotStack, true, true) && fuelSlotStack.getMaxCount() != fuelSlotStack.getCount()){
|
totalExperience -= expToDrop;
|
||||||
return slotID == blockEntity.fuelSlot;
|
player.world.spawnEntity(new ExperienceOrbEntity(player.world, player.x, player.y + 0.5D, player.z + 0.5D, expToDrop));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return slotID != blockEntity.outputSlot;
|
experience = 0;
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public int gaugeProgressScaled(int scale) {
|
|
||||||
return progress * scale / fuelScale;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int gaugeFuelScaled(int scale) {
|
|
||||||
if (totalBurnTime == 0) {
|
|
||||||
totalBurnTime = burnTime;
|
|
||||||
if (totalBurnTime == 0) {
|
|
||||||
totalBurnTime = fuelScale;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return burnTime * scale / totalBurnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cookItems() {
|
|
||||||
if (!canSmelt()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ItemStack inputStack = inventory.getInvStack(inputSlot);
|
|
||||||
ItemStack outputStack = getResultFor(inputStack);
|
|
||||||
float recipeExp = getExperienceFor(inputStack);
|
|
||||||
|
|
||||||
if (inventory.getInvStack(outputSlot).isEmpty()) {
|
|
||||||
inventory.setInvStack(outputSlot, outputStack.copy());
|
|
||||||
} else if (inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack)) {
|
|
||||||
inventory.getInvStack(outputSlot).increment(outputStack.getCount());
|
|
||||||
}
|
|
||||||
if (inputStack.getCount() > 1) {
|
|
||||||
inventory.shrinkSlot(inputSlot, 1);
|
|
||||||
} else {
|
|
||||||
inventory.setInvStack(inputSlot, ItemStack.EMPTY);
|
|
||||||
}
|
|
||||||
experience += recipeExp;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean canSmelt() {
|
|
||||||
if (inventory.getInvStack(inputSlot).isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ItemStack outputStack = getResultFor(inventory.getInvStack(inputSlot));
|
|
||||||
if (outputStack.isEmpty())
|
|
||||||
return false;
|
|
||||||
if (inventory.getInvStack(outputSlot).isEmpty())
|
|
||||||
return true;
|
|
||||||
if (!inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack))
|
|
||||||
return false;
|
|
||||||
int result = inventory.getInvStack(outputSlot).getCount() + outputStack.getCount();
|
|
||||||
return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBurning() {
|
|
||||||
return burnTime > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemStack getResultFor(ItemStack stack) {
|
private ItemStack getResultFor(ItemStack stack) {
|
||||||
|
@ -151,130 +81,58 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleGuiInputFromClient(PlayerEntity playerIn) {
|
// AbstractIronMachineBlockEntity
|
||||||
if (playerIn instanceof ServerPlayerEntity) {
|
@Override
|
||||||
ServerPlayerEntity player = (ServerPlayerEntity) playerIn;
|
protected void smelt() {
|
||||||
int totalExperience = (int) experience;
|
if (!canSmelt()) {
|
||||||
while (totalExperience > 0) {
|
return;
|
||||||
int expToDrop = ExperienceOrbEntity.roundToOrbSize(totalExperience);
|
|
||||||
totalExperience -= expToDrop;
|
|
||||||
player.world.spawnEntity(new ExperienceOrbEntity(player.world, player.x, player.y + 0.5D, player.z + 0.5D, expToDrop));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
experience = 0;
|
|
||||||
}
|
}
|
||||||
|
ItemStack inputStack = inventory.getInvStack(inputSlot);
|
||||||
|
ItemStack resultStack = getResultFor(inputStack);
|
||||||
|
|
||||||
private void updateState() {
|
if (inventory.getInvStack(outputSlot).isEmpty()) {
|
||||||
BlockState state = world.getBlockState(pos);
|
inventory.setInvStack(outputSlot, resultStack.copy());
|
||||||
if (state.getBlock() instanceof BlockMachineBase) {
|
} else if (inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(resultStack)) {
|
||||||
BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock();
|
inventory.getInvStack(outputSlot).increment(resultStack.getCount());
|
||||||
if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0)
|
}
|
||||||
blockMachineBase.setActive(burnTime > 0, world, pos);
|
experience += getExperienceFor(inputStack);
|
||||||
|
if (inputStack.getCount() > 1) {
|
||||||
|
inventory.shrinkSlot(inputSlot, 1);
|
||||||
|
} else {
|
||||||
|
inventory.setInvStack(inputSlot, ItemStack.EMPTY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MachineBaseBlockEntity
|
@Override
|
||||||
|
protected boolean canSmelt() {
|
||||||
|
if (inventory.getInvStack(inputSlot).isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ItemStack outputStack = getResultFor(inventory.getInvStack(inputSlot));
|
||||||
|
if (outputStack.isEmpty())
|
||||||
|
return false;
|
||||||
|
if (inventory.getInvStack(outputSlot).isEmpty())
|
||||||
|
return true;
|
||||||
|
if (!inventory.getInvStack(outputSlot).isItemEqualIgnoreDamage(outputStack))
|
||||||
|
return false;
|
||||||
|
int result = inventory.getInvStack(outputSlot).getCount() + outputStack.getCount();
|
||||||
|
return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromTag(CompoundTag compoundTag) {
|
public void fromTag(CompoundTag compoundTag) {
|
||||||
super.fromTag(compoundTag);
|
super.fromTag(compoundTag);
|
||||||
experience = compoundTag.getFloat("Experience");
|
experience = compoundTag.getFloat("Experience");
|
||||||
burnTime = compoundTag.getInt("BurnTime");
|
|
||||||
totalBurnTime = compoundTag.getInt("TotalBurnTime");
|
|
||||||
progress = compoundTag.getInt("Progress");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompoundTag toTag(CompoundTag compoundTag) {
|
public CompoundTag toTag(CompoundTag compoundTag) {
|
||||||
super.toTag(compoundTag);
|
super.toTag(compoundTag);
|
||||||
compoundTag.putFloat("Experience", experience);
|
compoundTag.putFloat("Experience", experience);
|
||||||
compoundTag.putInt("BurnTime", burnTime);
|
|
||||||
compoundTag.putInt("TotalBurnTime", totalBurnTime);
|
|
||||||
compoundTag.putInt("Progress", progress);
|
|
||||||
return compoundTag;
|
return compoundTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void tick() {
|
|
||||||
super.tick();
|
|
||||||
if(world.isClient){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
boolean isBurning = isBurning();
|
|
||||||
boolean updateInventory = false;
|
|
||||||
if (isBurning) {
|
|
||||||
--burnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (burnTime <= 0 && canSmelt()) {
|
|
||||||
burnTime = totalBurnTime = (int) (AbstractFurnaceBlockEntity.createFuelTimeMap().getOrDefault(inventory.getInvStack(fuelSlot).getItem(), 0) * 1.25);
|
|
||||||
if (burnTime > 0) {
|
|
||||||
// Fuel slot
|
|
||||||
ItemStack fuelStack = inventory.getInvStack(fuelSlot);
|
|
||||||
if (fuelStack.getItem().hasRecipeRemainder()) {
|
|
||||||
inventory.setInvStack(fuelSlot, new ItemStack(fuelStack.getItem().getRecipeRemainder()));
|
|
||||||
} else if (fuelStack.getCount() > 1) {
|
|
||||||
inventory.shrinkSlot(fuelSlot, 1);
|
|
||||||
} else if (fuelStack.getCount() == 1) {
|
|
||||||
inventory.setInvStack(fuelSlot, ItemStack.EMPTY);
|
|
||||||
}
|
|
||||||
updateInventory = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isBurning() && canSmelt()) {
|
|
||||||
progress++;
|
|
||||||
if (progress >= fuelScale) {
|
|
||||||
progress = 0;
|
|
||||||
cookItems();
|
|
||||||
updateInventory = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
progress = 0;
|
|
||||||
}
|
|
||||||
if (isBurning != isBurning()) {
|
|
||||||
updateInventory = true;
|
|
||||||
updateState();
|
|
||||||
}
|
|
||||||
if (updateInventory) {
|
|
||||||
markDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canBeUpgraded() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// InventoryProvider
|
|
||||||
@Override
|
|
||||||
public RebornInventory<IronFurnaceBlockEntity> getInventory() {
|
|
||||||
return this.inventory;
|
|
||||||
}
|
|
||||||
|
|
||||||
// IContainerProvider
|
// IContainerProvider
|
||||||
public int getBurnTime() {
|
|
||||||
return this.burnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBurnTime(int burnTime) {
|
|
||||||
this.burnTime = burnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTotalBurnTime() {
|
|
||||||
return this.totalBurnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTotalBurnTime(int totalBurnTime) {
|
|
||||||
this.totalBurnTime = totalBurnTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getProgress() {
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setProgress(int progress) {
|
|
||||||
this.progress = progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getExperience() {
|
public float getExperience() {
|
||||||
return experience;
|
return experience;
|
||||||
}
|
}
|
||||||
|
@ -286,7 +144,8 @@ public class IronFurnaceBlockEntity extends MachineBaseBlockEntity
|
||||||
@Override
|
@Override
|
||||||
public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
|
public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
|
||||||
return new ContainerBuilder("ironfurnace").player(player.inventory).inventory().hotbar()
|
return new ContainerBuilder("ironfurnace").player(player.inventory).inventory().hotbar()
|
||||||
.addInventory().blockEntity(this).fuelSlot(2, 56, 53).slot(0, 56, 17).outputSlot(1, 116, 35)
|
.addInventory().blockEntity(this)
|
||||||
|
.fuelSlot(2, 56, 53).slot(0, 56, 17).outputSlot(1, 116, 35)
|
||||||
.syncIntegerValue(this::getBurnTime, this::setBurnTime)
|
.syncIntegerValue(this::getBurnTime, this::setBurnTime)
|
||||||
.syncIntegerValue(this::getProgress, this::setProgress)
|
.syncIntegerValue(this::getProgress, this::setProgress)
|
||||||
.syncIntegerValue(this::getTotalBurnTime, this::setTotalBurnTime)
|
.syncIntegerValue(this::getTotalBurnTime, this::setTotalBurnTime)
|
||||||
|
|
|
@ -58,7 +58,7 @@ public class GuiAlloyFurnace extends GuiBase<BuiltContainer> {
|
||||||
super.drawForeground(mouseX, mouseY);
|
super.drawForeground(mouseX, mouseY);
|
||||||
GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
|
GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
|
||||||
|
|
||||||
builder.drawProgressBar(this, blockEntity.getCookProgressScaled(100), 100, 85, 36, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
|
builder.drawProgressBar(this, blockEntity.getProgressScaled(100), 100, 85, 36, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
|
||||||
builder.drawBurnBar(this, blockEntity.getBurnTimeRemainingScaled(100), 100, 56, 36, mouseX, mouseY, layer);
|
builder.drawBurnBar(this, blockEntity.getBurnTimeRemainingScaled(100), 100, 56, 36, mouseX, mouseY, layer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,7 @@ public class GuiIronFurnace extends GuiBase<BuiltContainer> {
|
||||||
super.drawForeground(mouseX, mouseY);
|
super.drawForeground(mouseX, mouseY);
|
||||||
final GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
|
final GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
|
||||||
|
|
||||||
builder.drawProgressBar(this, blockEntity.gaugeProgressScaled(100), 100, 85, 36, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
|
builder.drawProgressBar(this, blockEntity.getProgressScaled(100), 100, 85, 36, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
|
||||||
builder.drawBurnBar(this, blockEntity.gaugeFuelScaled(100), 100, 56, 36, mouseX, mouseY, layer);
|
builder.drawBurnBar(this, blockEntity.getBurnTimeRemainingScaled(100), 100, 56, 36, mouseX, mouseY, layer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,10 +241,6 @@ public class TechRebornConfig {
|
||||||
@Config(config = "items", category = "upgrades", key = "energy_storage", comment = "Energy storage behavior extra power")
|
@Config(config = "items", category = "upgrades", key = "energy_storage", comment = "Energy storage behavior extra power")
|
||||||
public static double energyStoragePower = 40_000;
|
public static double energyStoragePower = 40_000;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Machines
|
// Machines
|
||||||
@Config(config = "machines", category = "grinder", key = "GrinderInput", comment = "Grinder Max Input (Value in EU)")
|
@Config(config = "machines", category = "grinder", key = "GrinderInput", comment = "Grinder Max Input (Value in EU)")
|
||||||
public static int grinderMaxInput = 32;
|
public static int grinderMaxInput = 32;
|
||||||
|
@ -465,6 +461,11 @@ public class TechRebornConfig {
|
||||||
@Config(config = "machines", category = "solid_canning_machine", key = "solidCanningMachineMaxInput", comment = "Solid Canning Machine Max Energy (Value in EU)")
|
@Config(config = "machines", category = "solid_canning_machine", key = "solidCanningMachineMaxInput", comment = "Solid Canning Machine Max Energy (Value in EU)")
|
||||||
public static int solidCanningMachineMaxEnergy = 1_000;
|
public static int solidCanningMachineMaxEnergy = 1_000;
|
||||||
|
|
||||||
|
@Config(config = "machines", category = "iron_machine", key = "fuel_scale", comment = "Multiplier for vanilla furnace item burn time")
|
||||||
|
public static double fuelScale = 1.25;
|
||||||
|
|
||||||
|
@Config(config = "machines", category = "iron_machine", key = "cooking_scale", comment = "Multiplier for vanilla furnace item cook time")
|
||||||
|
public static double cookingScale = 1.25;
|
||||||
|
|
||||||
// Misc
|
// Misc
|
||||||
@Config(config = "misc", category = "general", key = "IC2TransformersStyle", comment = "Input from dots side, output from other sides, like in IC2.")
|
@Config(config = "misc", category = "general", key = "IC2TransformersStyle", comment = "Input from dots side, output from other sides, like in IC2.")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue