NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockContainerImpl.java

178 lines
6.7 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.blocks;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityImpl;
2018-10-13 20:35:18 +02:00
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.ModRegistry;
2020-01-22 01:32:26 +01:00
import de.ellpeck.naturesaura.reg.ModTileType;
2020-01-21 21:04:44 +01:00
import net.minecraft.block.*;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.LivingEntity;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
2020-09-22 03:17:02 +02:00
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BlockItemUseContext;
2018-10-13 20:35:18 +02:00
import net.minecraft.item.ItemStack;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.IBlockReader;
import net.minecraft.level.ILevel;
import net.minecraft.level.Level;
import net.minecraft.level.server.ServerLevel;
2020-09-22 03:17:02 +02:00
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameters;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Direction;
2018-10-18 13:34:37 +02:00
import net.minecraft.util.math.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
2018-10-13 20:35:18 +02:00
import javax.annotation.Nullable;
2020-01-23 16:05:52 +01:00
import java.util.List;
2019-02-22 19:06:47 +01:00
import java.util.Random;
2020-01-22 01:32:26 +01:00
import java.util.function.Supplier;
2018-10-13 20:35:18 +02:00
2021-12-04 15:40:09 +01:00
public class BlockContainerImpl extends BaseEntityBlock implements IModItem {
2018-10-13 20:35:18 +02:00
private final String baseName;
2021-12-04 15:40:09 +01:00
private final ModTileType<? extends BlockEntity> tileType;
2018-10-13 20:35:18 +02:00
2021-12-04 15:40:09 +01:00
public BlockContainerImpl(String baseName, Supplier<BlockEntity> tileSupplier, Block.Properties properties) {
2019-11-04 19:08:49 +01:00
super(properties);
2018-10-13 20:35:18 +02:00
this.baseName = baseName;
2020-01-22 01:32:26 +01:00
this.tileType = new ModTileType<>(tileSupplier, this);
2018-10-13 20:35:18 +02:00
2018-11-29 17:58:47 +01:00
ModRegistry.add(this);
2020-01-22 01:32:26 +01:00
ModRegistry.add(this.tileType);
2020-02-07 15:22:30 +01:00
if (this.hasWaterlogging())
this.setDefaultState(this.stateContainer.getBaseState().with(BlockStateProperties.WATERLOGGED, false));
}
protected boolean hasWaterlogging() {
return false;
}
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
if (this.hasWaterlogging())
builder.add(BlockStateProperties.WATERLOGGED);
}
@Override
2020-09-22 03:17:02 +02:00
public FluidState getFluidState(BlockState state) {
return this.hasWaterlogging() && state.get(BlockStateProperties.WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
}
@Override
2021-12-04 15:40:09 +01:00
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, ILevel levelIn, BlockPos currentPos, BlockPos facingPos) {
if (this.hasWaterlogging() && stateIn.get(BlockStateProperties.WATERLOGGED))
2021-12-04 15:40:09 +01:00
levelIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(levelIn));
return super.updatePostPlacement(stateIn, facing, facingState, levelIn, currentPos, facingPos);
}
@Override
@Nullable
public BlockState getStateForPlacement(BlockItemUseContext context) {
if (this.hasWaterlogging()) {
2021-12-04 15:40:09 +01:00
FluidState state = context.getLevel().getFluidState(context.getPos());
return this.getDefaultState().with(BlockStateProperties.WATERLOGGED, state.isTagged(FluidTags.WATER) && state.getLevel() == 8);
}
return super.getStateForPlacement(context);
2018-10-13 20:35:18 +02:00
}
@Nullable
@Override
2021-12-04 15:40:09 +01:00
public BlockEntity createNewBlockEntity(IBlockReader levelIn) {
2020-01-22 01:32:26 +01:00
return this.tileType.type.create();
2018-10-13 20:35:18 +02:00
}
@Override
public String getBaseName() {
return this.baseName;
}
@Override
2019-10-20 22:30:49 +02:00
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
2018-10-13 20:35:18 +02:00
}
2018-10-18 13:34:37 +02:00
2020-01-23 16:05:52 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void onPlayerDestroy(ILevel levelIn, BlockPos pos, BlockState state) {
super.onPlayerDestroy(levelIn, pos, state);
2020-01-23 16:05:52 +01:00
}
2018-11-04 16:38:09 +01:00
@Override
2020-01-21 21:04:44 +01:00
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
2020-01-23 16:05:52 +01:00
List<ItemStack> drops = super.getDrops(state, builder);
2020-01-21 21:04:44 +01:00
2021-12-04 15:40:09 +01:00
BlockEntity tile = builder.get(LootParameters.BLOCK_ENTITY);
if (tile instanceof BlockEntityImpl) {
2020-01-23 16:05:52 +01:00
for (ItemStack stack : drops) {
if (stack.getItem() != this.asItem())
continue;
2021-12-04 15:40:09 +01:00
((BlockEntityImpl) tile).modifyDrop(stack);
2020-01-23 16:05:52 +01:00
break;
}
}
return drops;
}
2018-11-04 16:38:09 +01:00
2020-01-23 16:05:52 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void onReplaced(BlockState state, Level levelIn, BlockPos pos, BlockState newState, boolean isMoving) {
2020-01-23 16:05:52 +01:00
if (state.getBlock() != newState.getBlock()) {
2021-12-04 15:40:09 +01:00
BlockEntity tile = levelIn.getBlockEntity(pos);
if (tile instanceof BlockEntityImpl)
((BlockEntityImpl) tile).dropInventory();
2020-01-23 16:05:52 +01:00
}
2021-12-04 15:40:09 +01:00
super.onReplaced(state, levelIn, pos, newState, isMoving);
2020-01-23 16:05:52 +01:00
}
2018-11-04 16:38:09 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void harvestBlock(Level levelIn, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity te, ItemStack stack) {
super.harvestBlock(levelIn, player, pos, state, te, stack);
levelIn.setBlockState(pos, Blocks.AIR.getDefaultState());
2018-11-04 16:38:09 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public void onBlockPlacedBy(Level levelIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
BlockEntity tile = levelIn.getBlockEntity(pos);
if (tile instanceof BlockEntityImpl)
((BlockEntityImpl) tile).loadDataOnPlace(stack);
2018-11-04 16:38:09 +01:00
}
2018-11-13 00:36:47 +01:00
2020-01-23 19:20:47 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void onBlockAdded(BlockState state, Level levelIn, BlockPos pos, BlockState oldState, boolean isMoving) {
this.updateRedstoneState(levelIn, pos);
2018-11-13 00:36:47 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public void neighborChanged(BlockState state, Level levelIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
this.updateRedstoneState(levelIn, pos);
2018-11-13 00:36:47 +01:00
}
2021-12-04 15:40:09 +01:00
private void updateRedstoneState(Level level, BlockPos pos) {
if (!level.isClientSide) {
BlockEntity tile = level.getBlockEntity(pos);
if (tile instanceof BlockEntityImpl) {
BlockEntityImpl impl = (BlockEntityImpl) tile;
int newPower = level.getRedstonePowerFromNeighbors(pos);
2019-03-19 17:21:06 +01:00
if (impl.redstonePower != newPower)
2021-12-04 15:40:09 +01:00
level.getPendingBlockTicks().scheduleTick(pos, this, 4);
2018-11-13 00:36:47 +01:00
}
}
}
2019-02-22 19:06:47 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void tick(BlockState state, ServerLevel levelIn, BlockPos pos, Random random) {
if (!levelIn.isClientSide) {
BlockEntity tile = levelIn.getBlockEntity(pos);
if (tile instanceof BlockEntityImpl) {
BlockEntityImpl impl = (BlockEntityImpl) tile;
int newPower = levelIn.getRedstonePowerFromNeighbors(pos);
2019-03-19 17:21:06 +01:00
if (impl.redstonePower != newPower)
2019-03-20 20:51:24 +01:00
impl.onRedstonePowerChange(newPower);
2019-03-19 17:21:06 +01:00
}
2019-02-22 19:06:47 +01:00
}
}
2018-10-13 20:35:18 +02:00
}