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

186 lines
7.3 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;
2021-12-19 15:32:45 +01:00
import de.ellpeck.naturesaura.blocks.tiles.ITickableBlockEntity;
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;
2022-06-27 15:24:04 +02:00
import net.minecraft.util.RandomSource;
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.RenderShape;
2021-12-04 15:40:09 +01:00
import net.minecraft.world.level.block.entity.BlockEntity;
2021-12-19 15:32:45 +01:00
import net.minecraft.world.level.block.entity.BlockEntityTicker;
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;
2023-07-08 12:32:27 +02:00
import net.minecraft.world.level.storage.loot.LootParams;
2021-12-06 14:38:12 +01:00
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
2018-10-13 20:35:18 +02:00
import javax.annotation.Nullable;
2021-12-19 15:32:45 +01:00
import java.lang.reflect.InvocationTargetException;
2020-01-23 16:05:52 +01:00
import java.util.List;
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-19 15:32:45 +01:00
private final Class<? extends BlockEntity> tileClass;
private final ModTileType<? extends BlockEntity> tileType;
2018-10-13 20:35:18 +02:00
2021-12-19 15:32:45 +01:00
public BlockContainerImpl(String baseName, Class<? extends BlockEntity> tileClass, Block.Properties properties) {
2019-11-04 19:08:49 +01:00
super(properties);
2018-10-13 20:35:18 +02:00
this.baseName = baseName;
2021-12-19 15:32:45 +01:00
this.tileClass = tileClass;
this.tileType = new ModTileType<>(this::createBlockEntity, this);
2018-10-13 20:35:18 +02:00
2022-06-27 15:24:04 +02:00
ModRegistry.ALL_ITEMS.add(this);
ModRegistry.ALL_ITEMS.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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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) {
2021-12-19 15:32:45 +01:00
return this.createBlockEntity(pos, state);
}
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
if (ITickableBlockEntity.class.isAssignableFrom(this.tileClass))
return ITickableBlockEntity.createTickerHelper(type, this.tileType.type);
return null;
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
2023-07-08 12:32:27 +02:00
public List<ItemStack> getDrops(BlockState state, LootParams.Builder builder) {
2021-12-15 16:30:22 +01:00
var drops = super.getDrops(state, builder);
2020-01-21 21:04:44 +01:00
var tile = builder.getOptionalParameter(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
2024-03-10 11:29:12 +01:00
public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
2023-02-15 23:45:50 +01:00
var tile = level.getBlockEntity(pos);
if (tile instanceof BlockEntityImpl impl)
impl.dropInventory();
2024-03-10 11:29:12 +01:00
return super.playerWillDestroy(level, pos, state, player);
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
2022-06-27 15:24:04 +02:00
public void tick(BlockState state, ServerLevel levelIn, BlockPos pos, RandomSource random) {
2021-12-04 15:40:09 +01:00
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
}
}
2021-12-19 15:32:45 +01:00
private BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
try {
return this.tileClass.getConstructor(BlockPos.class, BlockState.class).newInstance(pos, state);
2023-07-08 12:32:27 +02:00
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
2021-12-19 15:32:45 +01:00
throw new IllegalStateException("Cannot construct block entity from class " + this.tileClass, e);
}
}
2024-03-10 11:29:12 +01:00
2023-07-08 12:32:27 +02:00
}