Skip to content

Commit

Permalink
feat: make bio-machines retain their fuel when destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Jul 18, 2023
1 parent 98e8877 commit 8ec768d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected Iterable<Block> getKnownBlocks() {
return blocks;
}

protected static LootTable.Builder createNameableBioMachineTable(Block block) {
return LootTable.lootTable().withPool(applyExplosionCondition(block, LootPool.lootPool().setRolls(ConstantValue.exactly(1))
.add(LootItem.lootTableItem(block)
.apply(CopyNameFunction.copyName(CopyNameFunction.NameSource.BLOCK_ENTITY))
.apply(CopyNbtFunction.copyData(ContextNbtProvider.BLOCK_ENTITY).copy("Fuel", "BlockEntityTag.Fuel"))
)));
}

protected static LootTable.Builder dropWithInventory(Block block) {
return LootTable.lootTable().withPool(applyExplosionCondition(block, LootPool.lootPool().setRolls(ConstantValue.exactly(1))
.add(LootItem.lootTableItem(block)
Expand Down Expand Up @@ -73,10 +81,10 @@ protected void addTables() {
dropSelf(ModBlocks.MAW_HOPPER.get());
add(ModBlocks.STORAGE_SAC.get(), ModBlockLoot::dropWithInventory);

add(ModBlocks.BIO_FORGE.get(), BlockLoot::createNameableBlockEntityTable);
add(ModBlocks.BIO_LAB.get(), BlockLoot::createNameableBlockEntityTable);
add(ModBlocks.DIGESTER.get(), BlockLoot::createNameableBlockEntityTable);
add(ModBlocks.DECOMPOSER.get(), BlockLoot::createNameableBlockEntityTable);
add(ModBlocks.BIO_FORGE.get(), ModBlockLoot::createNameableBioMachineTable);
add(ModBlocks.BIO_LAB.get(), ModBlockLoot::createNameableBioMachineTable);
add(ModBlocks.DIGESTER.get(), ModBlockLoot::createNameableBioMachineTable);
add(ModBlocks.DECOMPOSER.get(), ModBlockLoot::createNameableBioMachineTable);

add(ModBlocks.FLESHKIN_CHEST.get(), BlockLoot::createNameableBlockEntityTable);
add(ModBlocks.FLESHKIN_DOOR.get(), ModBlockLoot::dropWithOwnableData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.github.elenterius.biomancy.block.bioforge;

import com.github.elenterius.biomancy.chat.ComponentUtil;
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.ModBlockEntities;
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.styles.TextStyles;
import com.github.elenterius.biomancy.util.SoundUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
Expand All @@ -29,6 +38,9 @@
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.List;

public class BioForgeBlock extends BaseEntityBlock {

public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
Expand Down Expand Up @@ -118,4 +130,19 @@ public BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}

@Override
public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<Component> tooltip, TooltipFlag flag) {
int fuelAmount = getFuelAmount(stack);
if (fuelAmount > 0) {
tooltip.add(ComponentUtil.emptyLine());
DecimalFormat df = ClientTextUtil.getDecimalFormatter("#,###,###");
tooltip.add(ComponentUtil.translatable("tooltip.biomancy.nutrients_fuel").withStyle(ChatFormatting.GRAY));
tooltip.add(ComponentUtil.literal("%s/%s u".formatted(df.format(fuelAmount), df.format(BioForgeBlockEntity.MAX_FUEL))).withStyle(TextStyles.NUTRIENTS));
}
}

public static int getFuelAmount(ItemStack stack) {
CompoundTag tag = BlockItem.getBlockEntityData(stack);
return tag != null && tag.contains("Fuel") ? tag.getShort("Fuel") : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

import com.github.elenterius.biomancy.block.HorizontalFacingMachineBlock;
import com.github.elenterius.biomancy.block.entity.MachineBlockEntity;
import com.github.elenterius.biomancy.chat.ComponentUtil;
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.ModBlockEntities;
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.styles.TextStyles;
import com.github.elenterius.biomancy.util.SoundUtil;
import com.github.elenterius.biomancy.util.VoxelShapeUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.RenderShape;
Expand All @@ -30,6 +39,8 @@
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Stream;

public class BioLabBlock extends HorizontalFacingMachineBlock {
Expand Down Expand Up @@ -111,4 +122,19 @@ public void animateTick(BlockState state, Level level, BlockPos pos, RandomSourc
}
}

@Override
public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<Component> tooltip, TooltipFlag flag) {
int fuelAmount = getFuelAmount(stack);
if (fuelAmount > 0) {
tooltip.add(ComponentUtil.emptyLine());
DecimalFormat df = ClientTextUtil.getDecimalFormatter("#,###,###");
tooltip.add(ComponentUtil.translatable("tooltip.biomancy.nutrients_fuel").withStyle(ChatFormatting.GRAY));
tooltip.add(ComponentUtil.literal("%s/%s u".formatted(df.format(fuelAmount), df.format(BioLabBlockEntity.MAX_FUEL))).withStyle(TextStyles.NUTRIENTS));
}
}

public static int getFuelAmount(ItemStack stack) {
CompoundTag tag = BlockItem.getBlockEntityData(stack);
return tag != null && tag.contains("Fuel") ? tag.getCompound("Fuel").getInt("Amount") : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

import com.github.elenterius.biomancy.block.HorizontalFacingMachineBlock;
import com.github.elenterius.biomancy.block.entity.MachineBlockEntity;
import com.github.elenterius.biomancy.chat.ComponentUtil;
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.ModBlockEntities;
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.styles.TextStyles;
import com.github.elenterius.biomancy.util.SoundUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -29,6 +38,8 @@
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Stream;

public class DecomposerBlock extends HorizontalFacingMachineBlock {
Expand Down Expand Up @@ -92,4 +103,19 @@ public void animateTick(BlockState state, Level level, BlockPos pos, RandomSourc
}
}

@Override
public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<Component> tooltip, TooltipFlag flag) {
int fuelAmount = getFuelAmount(stack);
if (fuelAmount > 0) {
tooltip.add(ComponentUtil.emptyLine());
DecimalFormat df = ClientTextUtil.getDecimalFormatter("#,###,###");
tooltip.add(ComponentUtil.translatable("tooltip.biomancy.nutrients_fuel").withStyle(ChatFormatting.GRAY));
tooltip.add(ComponentUtil.literal("%s/%s u".formatted(df.format(fuelAmount), df.format(DecomposerBlockEntity.MAX_FUEL))).withStyle(TextStyles.NUTRIENTS));
}
}

public static int getFuelAmount(ItemStack stack) {
CompoundTag tag = BlockItem.getBlockEntityData(stack);
return tag != null && tag.contains("Fuel") ? tag.getCompound("Fuel").getInt("Amount") : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

import com.github.elenterius.biomancy.block.HorizontalFacingMachineBlock;
import com.github.elenterius.biomancy.block.entity.MachineBlockEntity;
import com.github.elenterius.biomancy.chat.ComponentUtil;
import com.github.elenterius.biomancy.client.util.ClientTextUtil;
import com.github.elenterius.biomancy.init.ModBlockEntities;
import com.github.elenterius.biomancy.init.ModSoundEvents;
import com.github.elenterius.biomancy.styles.TextStyles;
import com.github.elenterius.biomancy.util.SoundUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
Expand All @@ -33,6 +41,9 @@
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.List;

public class DigesterBlock extends HorizontalFacingMachineBlock {

protected static final VoxelShape SHAPE = createShape();
Expand Down Expand Up @@ -135,4 +146,19 @@ else if (stack.getUseAnimation() == UseAnim.EAT) {
return false;
}

@Override
public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<Component> tooltip, TooltipFlag flag) {
int fuelAmount = getFuelAmount(stack);
if (fuelAmount > 0) {
tooltip.add(ComponentUtil.emptyLine());
DecimalFormat df = ClientTextUtil.getDecimalFormatter("#,###,###");
tooltip.add(ComponentUtil.translatable("tooltip.biomancy.nutrients_fuel").withStyle(ChatFormatting.GRAY));
tooltip.add(ComponentUtil.literal("%s/%s u".formatted(df.format(fuelAmount), df.format(DigesterBlockEntity.MAX_FUEL))).withStyle(TextStyles.NUTRIENTS));
}
}

public static int getFuelAmount(ItemStack stack) {
CompoundTag tag = BlockItem.getBlockEntityData(stack);
return tag != null && tag.contains("Fuel") ? tag.getCompound("Fuel").getInt("Amount") : 0;
}
}

0 comments on commit 8ec768d

Please sign in to comment.