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

167 lines
6.4 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;
2021-12-06 14:38:12 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
2021-12-04 15:40:09 +01:00
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape;
2021-12-04 15:40:09 +01:00
import net.minecraft.world.level.block.entity.BlockEntity;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
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;
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-06 14:38:12 +01:00
private final ModTileType<BlockEntity> tileType;
2018-10-13 20:35:18 +02:00
2021-12-06 14:38:12 +01:00
public BlockContainerImpl(String baseName, BlockEntityType.BlockEntitySupplier<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())
2021-12-06 14:38:12 +01:00
this.registerDefaultState(this.stateDefinition.any().setValue(BlockStateProperties.WATERLOGGED, false));
}
protected boolean hasWaterlogging() {
return false;
}
@Override
2021-12-06 14:38:12 +01:00
protected void createBlockStateDefinition(StateDefinition.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) {
2021-12-06 14:38:12 +01:00
return this.hasWaterlogging() && state.getValue(BlockStateProperties.WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
2021-12-06 14:38:12 +01:00
public BlockState updateShape(BlockState stateIn, Direction facing, BlockState facingState, LevelAccessor levelIn, BlockPos currentPos, BlockPos facingPos) {
if (this.hasWaterlogging() && stateIn.getValue(BlockStateProperties.WATERLOGGED))
levelIn.scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickDelay(levelIn));
return super.updateShape(stateIn, facing, facingState, levelIn, currentPos, facingPos);
}
@Override
@Nullable
2021-12-06 14:38:12 +01:00
public BlockState getStateForPlacement(BlockPlaceContext context) {
if (this.hasWaterlogging()) {
2021-12-15 16:30:22 +01:00
var state = context.getLevel().getFluidState(context.getClickedPos());
2021-12-06 14:38:12 +01:00
return this.defaultBlockState().setValue(BlockStateProperties.WATERLOGGED, state.is(FluidTags.WATER) && state.getAmount() == 8);
}
return super.getStateForPlacement(context);
2018-10-13 20:35:18 +02:00
}
2021-12-06 14:38:12 +01:00
@org.jetbrains.annotations.Nullable
2018-10-13 20:35:18 +02:00
@Override
2021-12-06 14:38:12 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return this.tileType.type.create(pos, state);
2018-10-13 20:35:18 +02:00
}
@Override
public String getBaseName() {
return this.baseName;
}
@Override
2021-12-06 14:38:12 +01:00
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
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) {
2021-12-15 16:30:22 +01:00
var drops = super.getDrops(state, builder);
2020-01-21 21:04:44 +01:00
2021-12-15 16:30:22 +01:00
var tile = builder.getParameter(LootContextParams.BLOCK_ENTITY);
2021-12-04 15:40:09 +01:00
if (tile instanceof BlockEntityImpl) {
2021-12-15 16:30:22 +01:00
for (var stack : drops) {
2020-01-23 16:05:52 +01:00
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-06 14:38:12 +01:00
public void onPlace(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-15 16:30:22 +01:00
var tile = levelIn.getBlockEntity(pos);
2021-12-04 15:40:09 +01:00
if (tile instanceof BlockEntityImpl)
((BlockEntityImpl) tile).dropInventory();
2020-01-23 16:05:52 +01:00
}
2021-12-06 14:38:12 +01:00
super.onPlace(state, levelIn, pos, newState, isMoving);
2020-01-23 16:05:52 +01:00
}
2018-11-04 16:38:09 +01:00
@Override
2021-12-06 14:38:12 +01:00
public void playerDestroy(Level levelIn, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity te, ItemStack stack) {
super.playerDestroy(levelIn, player, pos, state, te, stack);
levelIn.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
2018-11-04 16:38:09 +01:00
}
@Override
2021-12-06 14:38:12 +01:00
public void setPlacedBy(Level levelIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
2021-12-15 16:30:22 +01:00
var tile = levelIn.getBlockEntity(pos);
2021-12-04 15:40:09 +01:00
if (tile instanceof BlockEntityImpl)
((BlockEntityImpl) tile).loadDataOnPlace(stack);
2018-11-04 16:38:09 +01:00
}
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) {
2021-12-15 16:30:22 +01:00
var tile = level.getBlockEntity(pos);
2021-12-06 14:38:12 +01:00
if (tile instanceof BlockEntityImpl impl) {
2021-12-15 16:30:22 +01:00
var newPower = level.getBestNeighborSignal(pos);
2019-03-19 17:21:06 +01:00
if (impl.redstonePower != newPower)
2021-12-06 14:38:12 +01:00
level.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) {
2021-12-15 16:30:22 +01:00
var tile = levelIn.getBlockEntity(pos);
2021-12-06 14:38:12 +01:00
if (tile instanceof BlockEntityImpl impl) {
2021-12-15 16:30:22 +01:00
var newPower = levelIn.getBestNeighborSignal(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
}