This commit is contained in:
modmuss50 2019-10-18 19:47:45 +01:00
parent 572ad1ecb4
commit 6151757914

View file

@ -1,231 +1,231 @@
/* /*
* This file is part of TechReborn, licensed under the MIT License (MIT). * This file is part of TechReborn, licensed under the MIT License (MIT).
* *
* Copyright (c) 2018 TechReborn * Copyright (c) 2018 TechReborn
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in all * The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software. * copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * 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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
package techreborn.blockentity.machine.iron; package techreborn.blockentity.machine.iron;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.AbstractFurnaceBlockEntity; import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
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.nbt.CompoundTag;
import reborncore.api.IToolDrop; import reborncore.api.IToolDrop;
import reborncore.api.blockentity.InventoryProvider; import reborncore.api.blockentity.InventoryProvider;
import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.blocks.BlockMachineBase; import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.util.RebornInventory; import reborncore.common.util.RebornInventory;
import techreborn.config.TechRebornConfig; import techreborn.config.TechRebornConfig;
public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop { public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop {
public RebornInventory<?> inventory; public RebornInventory<?> inventory;
public int burnTime; public int burnTime;
public int totalBurnTime; public int totalBurnTime;
public int progress; public int progress;
public int totalCookingTime; public int totalCookingTime;
int fuelSlot; int fuelSlot;
Block toolDrop; Block toolDrop;
boolean active = false; boolean active = false;
public AbstractIronMachineBlockEntity(BlockEntityType<?> blockEntityTypeIn, int fuelSlot, Block toolDrop) { public AbstractIronMachineBlockEntity(BlockEntityType<?> blockEntityTypeIn, int fuelSlot, Block toolDrop) {
super(blockEntityTypeIn); super(blockEntityTypeIn);
this.fuelSlot = fuelSlot; this.fuelSlot = fuelSlot;
// default value for vanilla smelting recipes is 200 // default value for vanilla smelting recipes is 200
this.totalCookingTime = (int) (200 / TechRebornConfig.cookingScale); this.totalCookingTime = (int) (200 / TechRebornConfig.cookingScale);
this.toolDrop = toolDrop; this.toolDrop = toolDrop;
} }
/** /**
* Checks that we have all inputs and can put output into slot * Checks that we have all inputs and can put output into slot
* @return * @return
*/ */
protected abstract boolean canSmelt(); protected abstract boolean canSmelt();
/** /**
* Turn ingredients into the appropriate smelted * Turn ingredients into the appropriate smelted
* item in the output slot * item in the output slot
*/ */
protected abstract void smelt(); protected abstract void smelt();
/** /**
* Returns the number of ticks that the supplied fuel item will keep the * Returns the number of ticks that the supplied fuel item will keep the
* furnace burning, or 0 if the item isn't fuel * furnace burning, or 0 if the item isn't fuel
* @param stack Itemstack of fuel * @param stack Itemstack of fuel
* @return Integer Number of ticks * @return Integer Number of ticks
*/ */
private int getItemBurnTime(ItemStack stack) { private int getItemBurnTime(ItemStack stack) {
if (stack.isEmpty()) { if (stack.isEmpty()) {
return 0; return 0;
} }
return (int) (AbstractFurnaceBlockEntity.createFuelTimeMap().getOrDefault(stack.getItem(), 0) * TechRebornConfig.fuelScale); return (int) (AbstractFurnaceBlockEntity.createFuelTimeMap().getOrDefault(stack.getItem(), 0) * TechRebornConfig.fuelScale);
} }
/** /**
* Returns remaining fraction of fuel burn time * Returns remaining fraction of fuel burn time
* @param scale Scale to use for burn time * @param scale Scale to use for burn time
* @return int scaled remaining fuel burn time * @return int scaled remaining fuel burn time
*/ */
public int getBurnTimeRemainingScaled(int scale) { public int getBurnTimeRemainingScaled(int scale) {
if (totalBurnTime == 0) { if (totalBurnTime == 0) {
return 0; return 0;
} }
return burnTime * scale / totalBurnTime; return burnTime * scale / totalBurnTime;
} }
/** /**
* Returns crafting progress * Returns crafting progress
* @param scale Scale to use for crafting progress * @param scale Scale to use for crafting progress
* @return int Scaled crafting progress * @return int Scaled crafting progress
*/ */
public int getProgressScaled(int scale) { public int getProgressScaled(int scale) {
if (totalCookingTime > 0) { if (totalCookingTime > 0) {
return progress * scale / totalCookingTime; return progress * scale / totalCookingTime;
} }
return 0; return 0;
} }
/** /**
* Returns true if Iron Machine is burning fuel thus can do work * Returns true if Iron Machine is burning fuel thus can do work
* @return Boolean True if machine is burning * @return Boolean True if machine is burning
*/ */
public boolean isBurning() { public boolean isBurning() {
return burnTime > 0; return burnTime > 0;
} }
private void updateState() { private void updateState() {
BlockState state = world.getBlockState(pos); BlockState state = world.getBlockState(pos);
if (state.getBlock() instanceof BlockMachineBase) { if (state.getBlock() instanceof BlockMachineBase) {
BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock(); BlockMachineBase blockMachineBase = (BlockMachineBase) state.getBlock();
if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0) if (state.get(BlockMachineBase.ACTIVE) != burnTime > 0)
blockMachineBase.setActive(burnTime > 0, world, pos); blockMachineBase.setActive(burnTime > 0, world, pos);
} }
} }
// MachineBaseBlockEntity // MachineBaseBlockEntity
@Override @Override
public void fromTag(CompoundTag compoundTag) { public void fromTag(CompoundTag compoundTag) {
super.fromTag(compoundTag); super.fromTag(compoundTag);
burnTime = compoundTag.getInt("BurnTime"); burnTime = compoundTag.getInt("BurnTime");
totalBurnTime = compoundTag.getInt("TotalBurnTime"); totalBurnTime = compoundTag.getInt("TotalBurnTime");
progress = compoundTag.getInt("Progress"); progress = compoundTag.getInt("Progress");
} }
@Override @Override
public CompoundTag toTag(CompoundTag compoundTag) { public CompoundTag toTag(CompoundTag compoundTag) {
super.toTag(compoundTag); super.toTag(compoundTag);
compoundTag.putInt("BurnTime", burnTime); compoundTag.putInt("BurnTime", burnTime);
compoundTag.putInt("TotalBurnTime", totalBurnTime); compoundTag.putInt("TotalBurnTime", totalBurnTime);
compoundTag.putInt("Progress", progress); compoundTag.putInt("Progress", progress);
return compoundTag; return compoundTag;
} }
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
if(world.isClient){ if(world.isClient){
return; return;
} }
boolean isBurning = isBurning(); boolean isBurning = isBurning();
if (isBurning) { if (isBurning) {
--burnTime; --burnTime;
} }
if (!isBurning && canSmelt()) { if (!isBurning && canSmelt()) {
burnTime = totalBurnTime = getItemBurnTime(inventory.getInvStack(fuelSlot)); burnTime = totalBurnTime = getItemBurnTime(inventory.getInvStack(fuelSlot));
if (burnTime > 0) { if (burnTime > 0) {
// Fuel slot // Fuel slot
ItemStack fuelStack = inventory.getInvStack(fuelSlot); ItemStack fuelStack = inventory.getInvStack(fuelSlot);
if (fuelStack.getItem().hasRecipeRemainder()) { if (fuelStack.getItem().hasRecipeRemainder()) {
inventory.setInvStack(fuelSlot, new ItemStack(fuelStack.getItem().getRecipeRemainder())); inventory.setInvStack(fuelSlot, new ItemStack(fuelStack.getItem().getRecipeRemainder()));
} else if (fuelStack.getCount() > 1) { } else if (fuelStack.getCount() > 1) {
inventory.shrinkSlot(fuelSlot, 1); inventory.shrinkSlot(fuelSlot, 1);
} else if (fuelStack.getCount() == 1) { } else if (fuelStack.getCount() == 1) {
inventory.setInvStack(fuelSlot, ItemStack.EMPTY); inventory.setInvStack(fuelSlot, ItemStack.EMPTY);
} }
} }
} }
if (isBurning() && canSmelt()) { if (isBurning() && canSmelt()) {
++progress; ++progress;
if (progress == totalCookingTime) { if (progress == totalCookingTime) {
progress = 0; progress = 0;
smelt(); smelt();
} }
} else { } else if(!canSmelt()) {
progress = 0; progress = 0;
} }
if (isBurning != isBurning()) { if (isBurning != isBurning()) {
inventory.setChanged(); inventory.setChanged();
updateState(); updateState();
} }
if (inventory.hasChanged()) { if (inventory.hasChanged()) {
markDirty(); markDirty();
} }
} }
@Override @Override
public boolean canBeUpgraded() { public boolean canBeUpgraded() {
return false; return false;
} }
// InventoryProvider // InventoryProvider
@Override @Override
public RebornInventory<?> getInventory() { public RebornInventory<?> getInventory() {
return inventory; return inventory;
} }
// IToolDrop // IToolDrop
@Override @Override
public ItemStack getToolDrop(PlayerEntity entityPlayer) { public ItemStack getToolDrop(PlayerEntity entityPlayer) {
return new ItemStack(toolDrop); return new ItemStack(toolDrop);
} }
public int getBurnTime() { public int getBurnTime() {
return this.burnTime; return this.burnTime;
} }
public void setBurnTime(int burnTime) { public void setBurnTime(int burnTime) {
this.burnTime = burnTime; this.burnTime = burnTime;
} }
public int getTotalBurnTime() { public int getTotalBurnTime() {
return this.totalBurnTime; return this.totalBurnTime;
} }
public void setTotalBurnTime(int totalBurnTime) { public void setTotalBurnTime(int totalBurnTime) {
this.totalBurnTime = totalBurnTime; this.totalBurnTime = totalBurnTime;
} }
public int getProgress() { public int getProgress() {
return progress; return progress;
} }
public void setProgress(int progress) { public void setProgress(int progress) {
this.progress = progress; this.progress = progress;
} }
} }