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.IModelProvider;
|
|
|
|
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;
|
2018-10-13 20:35:18 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
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;
|
2020-01-23 19:20:47 +01:00
|
|
|
import net.minecraft.world.IWorldReader;
|
2018-10-13 20:35:18 +02:00
|
|
|
import net.minecraft.world.World;
|
2020-01-23 16:05:52 +01:00
|
|
|
import net.minecraft.world.storage.loot.LootContext;
|
|
|
|
import net.minecraft.world.storage.loot.LootParameters;
|
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
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
public class BlockContainerImpl extends ContainerBlock implements IModItem, IModelProvider {
|
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);
|
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-01-23 19:20:47 +01:00
|
|
|
world.getPendingBlockTicks().scheduleTick(pos, this, this.tickRate(world));
|
2018-11-13 00:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 19:06:47 +01:00
|
|
|
|
2019-03-19 17:21:06 +01:00
|
|
|
@Override
|
2020-01-23 19:20:47 +01:00
|
|
|
public int tickRate(IWorldReader worldIn) {
|
2019-03-19 17:21:06 +01:00
|
|
|
return 4;
|
2020-01-23 19:20:47 +01:00
|
|
|
}
|
2019-03-19 17:21:06 +01:00
|
|
|
|
2019-02-22 19:06:47 +01:00
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public void tick(BlockState state, World 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
|
|
|
}
|