added basis of boiler and burner

This commit is contained in:
khj xiaogu 2021-10-23 18:19:44 +08:00
parent 479851577d
commit b60a5bfecf
No known key found for this signature in database
GPG key ID: DEA172814EAFF426
4 changed files with 344 additions and 0 deletions

View file

@ -0,0 +1,84 @@
package com.teammoeg.steampowered.block.burner;
import com.simibubi.create.content.contraptions.components.flywheel.FlywheelTileEntity;
import com.simibubi.create.repack.registrate.util.entry.TileEntityEntry;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
public class Burner extends Block {
public static final BooleanProperty LIT = BlockStateProperties.LIT;
public static final DirectionProperty FACING=BlockStateProperties.FACING;
public final TileEntityEntry<FlywheelTileEntity> te;
public Burner(Properties props,TileEntityEntry<FlywheelTileEntity> te) {
super(props);
this.te=te;
}
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Direction facing = context.getClickedFace();
return this.defaultBlockState().setValue(FACING, facing.getAxis().isVertical() ? context.getHorizontalDirection().getOpposite() : facing).setValue(LIT, Boolean.valueOf(false));
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return te.create();
}
@Override
public void stepOn(World w, BlockPos p, Entity e) {
if(w.getBlockState(p).getValue(LIT)==true)
if(e instanceof LivingEntity)
e.hurt(DamageSource.HOT_FLOOR,2);
}
protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder.add(LIT).add(FACING));
}
@Override
public ActionResultType use(BlockState bs, World w, BlockPos bp,PlayerEntity pe,Hand h, BlockRayTraceResult br) {
if(pe.getItemInHand(h).isEmpty()) {
IItemHandler cap=w.getBlockEntity(bp).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).resolve().get();
ItemStack is=cap.getStackInSlot(0);
if(!is.isEmpty()) {
pe.setItemInHand(h,cap.extractItem(0,is.getCount(),false));
return ActionResultType.SUCCESS;
}
}else if(ForgeHooks.getBurnTime(pe.getItemInHand(h))!=0) {
IItemHandler cap=w.getBlockEntity(bp).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).resolve().get();
pe.setItemInHand(h,cap.insertItem(0,pe.getItemInHand(h),false));
return ActionResultType.SUCCESS;
}
return ActionResultType.PASS;
}
}

View file

@ -0,0 +1,136 @@
package com.teammoeg.steampowered.tileentity.boiler;
import java.util.List;
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import com.teammoeg.steampowered.FluidRegistry;
import com.teammoeg.steampowered.SteamPowered;
import com.teammoeg.steampowered.tileentity.burner.IHeatReceiver;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import net.minecraftforge.fluids.capability.templates.FluidTank;
public abstract class BoilerTileEntity extends TileEntity implements IHeatReceiver,ITickableTileEntity,IHaveGoggleInformation{
private FluidTank in=new FluidTank(10000) {
@Override
public boolean isFluidValid(FluidStack stack) {
return stack.getFluid()==Fluids.WATER;
}
};
private FluidTank out=new FluidTank(10000);
private IFluidHandler ft=new IFluidHandler() {
@Override
public int getTanks() {
return 2;
}
@Override
public FluidStack getFluidInTank(int tank) {
switch(tank) {
case 0:return in.getFluid();
case 1:return out.getFluid();
default:return null;
}
}
@Override
public int getTankCapacity(int tank) {
return 10000;
}
@Override
public boolean isFluidValid(int tank, FluidStack stack) {
if(tank==0&&stack.getFluid()==Fluids.WATER)
return true;
return false;
}
@Override
public int fill(FluidStack resource, FluidAction action) {
return in.fill(resource, action);
}
@Override
public FluidStack drain(FluidStack resource, FluidAction action) {
return out.drain(resource, action);
}
@Override
public FluidStack drain(int maxDrain, FluidAction action) {
return out.drain(maxDrain, action);
}
};
int heatreceived;
private LazyOptional<IFluidHandler> holder = LazyOptional.of(() -> ft);
public BoilerTileEntity(TileEntityType<?> p_i48289_1_) {
super(p_i48289_1_);
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
in.readFromNBT(nbt.getCompound("in"));
out.readFromNBT(nbt.getCompound("out"));
heatreceived=nbt.getInt("hu");
super.deserializeNBT(nbt);
}
@Override
public CompoundNBT serializeNBT() {
CompoundNBT cnbt= super.serializeNBT();
cnbt.put("in",in.writeToNBT(new CompoundNBT()));
cnbt.put("out",out.writeToNBT(new CompoundNBT()));
cnbt.putInt("hu",heatreceived);
return cnbt;
}
@Override
public void tick() {
int consume=Math.min(getHUPerTick(),heatreceived);
heatreceived=0;
consume=Math.min(this.in.drain(consume/12,FluidAction.EXECUTE).getAmount()*12,consume);
this.out.fill(new FluidStack(FluidRegistry.steam.get().getFluid(),consume),FluidAction.EXECUTE);
}
@Override
public void commitHeat(float value) {
heatreceived=(int) value;
}
public boolean addToGoggleTooltip(List<ITextComponent> tooltip, boolean isPlayerSneaking) {
this.containedFluidTooltip(tooltip, isPlayerSneaking,LazyOptional.of(()->in));
this.containedFluidTooltip(tooltip, isPlayerSneaking,LazyOptional.of(()->out));
return true;
}
protected abstract int getHUPerTick();
@Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
if (!this.holder.isPresent()) {
this.refreshCapability();
}
return cap == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY ? holder.cast() : super.getCapability(cap,side);
}
private void refreshCapability() {
LazyOptional<IFluidHandler> oldCap = this.holder;
this.holder = LazyOptional.of(() -> {
return this.ft;
});
oldCap.invalidate();
}
}

View file

@ -0,0 +1,119 @@
package com.teammoeg.steampowered.tileentity.burner;
import java.util.List;
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import com.teammoeg.steampowered.block.burner.Burner;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.templates.FluidTank;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
public abstract class BurnerTileEntity extends TileEntity implements ITickableTileEntity,IHaveGoggleInformation {
private ItemStackHandler inv=new ItemStackHandler() {
@Override
public boolean isItemValid(int slot, ItemStack stack) {
if(ForgeHooks.getBurnTime(stack)!=0)return true;
return false;
}
};
int HURemain;
private LazyOptional<IItemHandler> holder = LazyOptional.of(() -> inv);
public BurnerTileEntity(TileEntityType<?> p_i48289_1_) {
super(p_i48289_1_);
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
inv.deserializeNBT(nbt.getCompound("inv"));
HURemain=nbt.getInt("hu");
super.deserializeNBT(nbt);
}
@Override
public CompoundNBT serializeNBT() {
CompoundNBT cnbt= super.serializeNBT();
cnbt.put("inv",inv.serializeNBT());
cnbt.putInt("hu",HURemain);
return cnbt;
}
@Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
if (!this.holder.isPresent()) {
this.refreshCapability();
}
return cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? holder.cast() : super.getCapability(cap,side);
}
private void refreshCapability() {
LazyOptional<IItemHandler> oldCap = this.holder;
this.holder = LazyOptional.of(() -> {
return this.inv;
});
oldCap.invalidate();
}
@Override
public void tick() {
int emit=getHuPerTick();
while(HURemain<emit&&this.consumeFuel());
if(HURemain<emit) {
if(HURemain>0) {
emitHeat(HURemain);
HURemain=0;
}
this.getBlockState().setValue(Burner.LIT,false);
}else {
HURemain-=emit;
emitHeat(emit);
this.getBlockState().setValue(Burner.LIT,true);
}
}
private boolean consumeFuel() {
int time=ForgeHooks.getBurnTime(inv.getStackInSlot(0),IRecipeType.SMELTING);
if(time<=0)return false;
inv.getStackInSlot(0).shrink(1);
HURemain+=time*24;//2.4HU/t
return true;
}
protected void emitHeat(float value) {
TileEntity receiver=level.getBlockEntity(this.getBlockPos().above());
if(receiver instanceof IHeatReceiver) {
((IHeatReceiver) receiver).commitHeat(value);
}
}
@Override
public boolean addToGoggleTooltip(List<ITextComponent> tooltip, boolean isPlayerSneaking) {
tooltip.add(componentSpacing.plainCopy().append(new TranslationTextComponent("tooltip.steampowered.burner.hu",HURemain).withStyle(TextFormatting.GREEN)));
tooltip.add(componentSpacing.plainCopy().append(new TranslationTextComponent("tooltip.steampowered.burner.item",inv.getStackInSlot(0).getItem().getName(inv.getStackInSlot(0)),inv.getStackInSlot(0).getCount()).withStyle(TextFormatting.GREEN)));
return true;
}
/*
* HU per tick max, 10HU=1 steam
* */
protected abstract int getHuPerTick();
}

View file

@ -0,0 +1,5 @@
package com.teammoeg.steampowered.tileentity.burner;
public interface IHeatReceiver {
public void commitHeat(float value);
}