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

158 lines
5.4 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.NaturesAura;
2018-10-18 13:34:37 +02:00
import de.ellpeck.naturesaura.blocks.tiles.TileEntityImpl;
2018-11-29 17:58:47 +01:00
import de.ellpeck.naturesaura.reg.ICreativeItem;
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;
2018-11-13 00:36:47 +01:00
import net.minecraft.block.Block;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.ContainerBlock;
2018-10-13 20:35:18 +02:00
import net.minecraft.block.material.Material;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
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;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockRenderType;
2018-11-04 16:38:09 +01:00
import net.minecraft.util.NonNullList;
2018-10-13 20:35:18 +02:00
import net.minecraft.util.ResourceLocation;
2018-10-18 13:34:37 +02:00
import net.minecraft.util.math.BlockPos;
2018-11-04 16:38:09 +01:00
import net.minecraft.world.IBlockAccess;
2018-10-13 20:35:18 +02:00
import net.minecraft.world.World;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import javax.annotation.Nullable;
2019-02-22 19:06:47 +01:00
import java.util.Random;
2018-10-13 20:35:18 +02:00
2019-10-20 22:30:49 +02:00
public class BlockContainerImpl extends ContainerBlock implements IModItem, ICreativeItem, IModelProvider {
2018-10-13 20:35:18 +02:00
private final String baseName;
private final Class<? extends TileEntity> tileClass;
private final String tileRegName;
public BlockContainerImpl(Material material, String baseName, Class<? extends TileEntity> tileClass, String tileReg) {
super(material);
this.baseName = baseName;
this.tileClass = tileClass;
this.tileRegName = tileReg;
2018-11-29 17:58:47 +01:00
ModRegistry.add(this);
2018-10-13 20:35:18 +02:00
}
@Nullable
@Override
2018-10-13 20:45:32 +02:00
public TileEntity createNewTileEntity(World world, int meta) {
2018-10-13 20:35:18 +02:00
try {
return this.tileClass.newInstance();
} catch (Exception e) {
return null;
}
}
@Override
public String getBaseName() {
return this.baseName;
}
@Override
public void onPreInit(FMLPreInitializationEvent event) {
}
@Override
public void onInit(FMLInitializationEvent event) {
GameRegistry.registerTileEntity(this.tileClass, new ResourceLocation(NaturesAura.MOD_ID, this.tileRegName));
}
@Override
public void onPostInit(FMLPostInitializationEvent event) {
}
@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
@Override
2019-10-20 22:30:49 +02:00
public void breakBlock(World worldIn, BlockPos pos, BlockState state) {
2018-10-18 13:34:37 +02:00
if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos);
2018-11-04 16:38:09 +01:00
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).dropInventory();
2018-10-18 13:34:37 +02:00
}
super.breakBlock(worldIn, pos, state);
}
2018-11-04 16:38:09 +01:00
@Override
2019-10-20 22:30:49 +02:00
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, BlockState state, int fortune) {
2018-11-04 16:38:09 +01:00
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
drops.add(((TileEntityImpl) tile).getDrop(state, fortune));
else
super.getDrops(drops, world, pos, state, fortune);
}
@Override
2019-10-20 22:30:49 +02:00
public boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest) {
2018-11-04 16:38:09 +01:00
return willHarvest || super.removedByPlayer(state, world, pos, player, false);
}
@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);
worldIn.setBlockToAir(pos);
}
@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
@Override
2019-10-20 22:30:49 +02:00
public void onBlockAdded(World worldIn, BlockPos pos, BlockState state) {
2018-11-13 00:36:47 +01:00
this.updateRedstoneState(worldIn, pos);
}
@Override
2019-10-20 22:30:49 +02:00
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos) {
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)
world.scheduleUpdate(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
public int tickRate(World worldIn) {
return 4;
}
2019-02-22 19:06:47 +01:00
@Override
2019-10-20 22:30:49 +02:00
public void updateTick(World worldIn, BlockPos pos, BlockState state, Random rand) {
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
}