SteamPowered/src/main/java/com/teammoeg/steampowered/content/burner/BurnerTileEntity.java
2021-10-24 16:55:17 -07:00

195 lines
7.3 KiB
Java

/*
* Copyright (c) 2021 TeamMoeg
*
* This file is part of Steam Powered.
*
* Steam Powered is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* Steam Powered is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Steam Powered. If not, see <https://www.gnu.org/licenses/>.
*/
package com.teammoeg.steampowered.content.burner;
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
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.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import java.util.List;
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_);
}
// Easy, easy
public void readCustomNBT(CompoundNBT nbt) {
inv.deserializeNBT(nbt.getCompound("inv"));
HURemain = nbt.getInt("hu");
}
// Easy, easy
public void writeCustomNBT(CompoundNBT nbt) {
nbt.put("inv", inv.serializeNBT());
nbt.putInt("hu", HURemain);
}
@Override
public void load(BlockState state, CompoundNBT nbt) {
super.load(state, nbt);
readCustomNBT(nbt);
}
@Override
public CompoundNBT save(CompoundNBT nbt) {
super.save(nbt);
writeCustomNBT(nbt);
return nbt;
}
@Override
public SUpdateTileEntityPacket getUpdatePacket() {
CompoundNBT nbt = new CompoundNBT();
this.writeCustomNBT(nbt);
return new SUpdateTileEntityPacket(this.getBlockPos(), 3, nbt);
}
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
this.readCustomNBT(pkt.getTag());
}
@Override
public CompoundNBT getUpdateTag() {
CompoundNBT nbt = super.getUpdateTag();
writeCustomNBT(nbt);
return nbt;
}
@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() {
if (level != null && level.isClientSide) {
BlockState state = this.level.getBlockState(this.worldPosition);
if (state.getValue(BurnerBlock.LIT)) {
double d0 = (double)getBlockPos().getX() + 0.5D;
double d1 = (double)getBlockPos().getY();
double d2 = (double)getBlockPos().getZ() + 0.5D;
if (this.level.getRandom().nextDouble() < 0.1D) {
this.level.playLocalSound(d0, d1, d2, SoundEvents.FURNACE_FIRE_CRACKLE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
}
Direction direction = state.getValue(BurnerBlock.FACING);
Direction.Axis direction$axis = direction.getAxis();
double d3 = 0.52D;
double d4 = this.level.getRandom().nextDouble() * 0.6D - 0.3D;
double d5 = direction$axis == Direction.Axis.X ? (double)direction.getStepX() * 0.52D : d4;
double d6 = this.level.getRandom().nextDouble() * 6.0D / 16.0D;
double d7 = direction$axis == Direction.Axis.Z ? (double)direction.getStepZ() * 0.52D : d4;
this.level.addParticle(ParticleTypes.SMOKE, d0 + d5, d1 + d6, d2 + d7, 0.0D, 0.0D, 0.0D);
this.level.addParticle(ParticleTypes.FLAME, d0 + d5, d1 + d6, d2 + d7, 0.0D, 0.0D, 0.0D);
}
}
if (level != null && !level.isClientSide) {
BlockState state = this.level.getBlockState(this.worldPosition);
int emit = getHuPerTick();
while (HURemain < emit && this.consumeFuel()) ;
if (HURemain < emit) {
if (HURemain > 0) {
emitHeat(HURemain);
HURemain = 0;
}
this.level.setBlockAndUpdate(this.worldPosition, state.setValue(BurnerBlock.LIT, false));
} else {
HURemain -= emit;
emitHeat(emit);
this.level.setBlockAndUpdate(this.worldPosition, state.setValue(BurnerBlock.LIT, true));
}
this.level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
this.setChanged();
}
}
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.GOLD)));
tooltip.add(componentSpacing.plainCopy().append(new TranslationTextComponent("tooltip.steampowered.burner.item", inv.getStackInSlot(0).getCount(), inv.getStackInSlot(0).getItem().getName(inv.getStackInSlot(0))).withStyle(TextFormatting.GRAY)));
return true;
}
/*
* HU per tick max, 10HU=1 steam
* */
protected abstract int getHuPerTick();
}