NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockContainerImpl.java
2018-11-04 16:38:09 +01:00

120 lines
4 KiB
Java

package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityImpl;
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.IModelProvider;
import de.ellpeck.naturesaura.reg.ModRegistry;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
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;
public class BlockContainerImpl extends BlockContainer implements IModItem, IModelProvider {
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;
ModRegistry.addItemOrBlock(this);
}
@Nullable
@Override
public TileEntity createNewTileEntity(World world, int meta) {
try {
return this.tileClass.newInstance();
} catch (Exception e) {
return null;
}
}
@Override
public String getBaseName() {
return this.baseName;
}
@Override
public boolean shouldAddCreative() {
return true;
}
@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
public EnumBlockRenderType getRenderType(IBlockState state) {
return EnumBlockRenderType.MODEL;
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).dropInventory();
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
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
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
return willHarvest || super.removedByPlayer(state, world, pos, player, false);
}
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) {
super.harvestBlock(worldIn, player, pos, state, te, stack);
worldIn.setBlockToAir(pos);
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).loadDataOnPlace(stack);
}
}