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

176 lines
6.5 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.blocks;
2018-10-18 13:34:37 +02:00
import de.ellpeck.naturesaura.blocks.tiles.TileEntityImpl;
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;
import net.minecraft.entity.player.PlayerEntity;
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;
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;
2018-10-13 20:35:18 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
2018-10-18 13:34:37 +02:00
import net.minecraft.util.math.BlockPos;
2019-11-04 19:08:49 +01:00
import net.minecraft.world.IBlockReader;
2020-01-23 16:05:52 +01:00
import net.minecraft.world.IWorld;
2018-10-13 20:35:18 +02:00
import net.minecraft.world.World;
2020-01-28 18:08:56 +01:00
import net.minecraft.world.server.ServerWorld;
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
2020-01-29 00:40:28 +01:00
public class BlockContainerImpl extends ContainerBlock implements IModItem {
2018-10-13 20:35:18 +02:00
private final String baseName;
2020-01-22 01:32:26 +01:00
private final ModTileType<? extends TileEntity> tileType;
2018-10-13 20:35:18 +02:00
2020-01-22 01:32:26 +01:00
public BlockContainerImpl(String baseName, Supplier<TileEntity> 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
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
if (this.hasWaterlogging() && stateIn.get(BlockStateProperties.WATERLOGGED))
worldIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(worldIn));
return super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos);
}
@Override
@Nullable
public BlockState getStateForPlacement(BlockItemUseContext context) {
if (this.hasWaterlogging()) {
2020-09-22 03:17:02 +02:00
FluidState state = context.getWorld().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
2020-01-21 23:02:39 +01:00
public TileEntity createNewTileEntity(IBlockReader worldIn) {
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
public void onPlayerDestroy(IWorld worldIn, BlockPos pos, BlockState state) {
super.onPlayerDestroy(worldIn, pos, state);
}
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
2020-01-23 16:05:52 +01:00
TileEntity tile = builder.get(LootParameters.BLOCK_ENTITY);
if (tile instanceof TileEntityImpl) {
for (ItemStack stack : drops) {
if (stack.getItem() != this.asItem())
continue;
((TileEntityImpl) tile).modifyDrop(stack);
break;
}
}
return drops;
}
2018-11-04 16:38:09 +01:00
2020-01-23 16:05:52 +01:00
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).dropInventory();
}
super.onReplaced(state, worldIn, pos, newState, isMoving);
}
2018-11-04 16:38:09 +01:00
@Override
2019-10-20 22:30:49 +02:00
public void harvestBlock(World worldIn, PlayerEntity player, BlockPos pos, BlockState state, @Nullable TileEntity te, ItemStack stack) {
2018-11-04 16:38:09 +01:00
super.harvestBlock(worldIn, player, pos, state, te, stack);
2020-01-21 21:04:44 +01:00
worldIn.setBlockState(pos, Blocks.AIR.getDefaultState());
2018-11-04 16:38:09 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
2018-11-04 16:38:09 +01:00
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).loadDataOnPlace(stack);
}
2018-11-13 00:36:47 +01:00
2020-01-23 19:20:47 +01:00
@Override
public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
2018-11-13 00:36:47 +01:00
this.updateRedstoneState(worldIn, pos);
}
@Override
2020-01-23 19:20:47 +01:00
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
2018-11-13 00:36:47 +01:00
this.updateRedstoneState(worldIn, pos);
}
private void updateRedstoneState(World world, BlockPos pos) {
if (!world.isRemote) {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityImpl) {
TileEntityImpl impl = (TileEntityImpl) tile;
2019-01-27 13:57:34 +01:00
int newPower = world.getRedstonePowerFromNeighbors(pos);
2019-03-19 17:21:06 +01:00
if (impl.redstonePower != newPower)
2020-09-22 03:17:02 +02:00
world.getPendingBlockTicks().scheduleTick(pos, this, 4);
2018-11-13 00:36:47 +01:00
}
}
}
2019-02-22 19:06:47 +01:00
@Override
2020-01-28 18:08:56 +01:00
public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) {
2019-02-22 19:06:47 +01:00
if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos);
2019-03-19 17:21:06 +01:00
if (tile instanceof TileEntityImpl) {
TileEntityImpl impl = (TileEntityImpl) tile;
int newPower = worldIn.getRedstonePowerFromNeighbors(pos);
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
}