2015-08-29 14:33:25 +02:00
|
|
|
/*
|
|
|
|
* This file ("BlockContainerBase.java") is part of the Actually Additions Mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2015-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-03 20:15:07 +01:00
|
|
|
package ellpeck.actuallyadditions.blocks.base;
|
2014-12-20 21:34:07 +01:00
|
|
|
|
2015-12-03 20:15:07 +01:00
|
|
|
import cpw.mods.fml.common.registry.GameRegistry;
|
|
|
|
import ellpeck.actuallyadditions.creative.CreativeTab;
|
2015-12-13 00:54:25 +01:00
|
|
|
import ellpeck.actuallyadditions.tile.IEnergySaver;
|
2015-12-03 18:40:43 +01:00
|
|
|
import ellpeck.actuallyadditions.tile.TileEntityBase;
|
2015-03-07 12:51:28 +01:00
|
|
|
import ellpeck.actuallyadditions.tile.TileEntityInventoryBase;
|
2015-12-03 20:15:07 +01:00
|
|
|
import ellpeck.actuallyadditions.util.ModUtil;
|
2015-11-22 18:58:23 +01:00
|
|
|
import ellpeck.actuallyadditions.util.Util;
|
2015-12-03 18:40:43 +01:00
|
|
|
import net.minecraft.block.Block;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.block.BlockContainer;
|
|
|
|
import net.minecraft.block.material.Material;
|
2015-12-13 00:54:25 +01:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2015-12-13 00:54:25 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-05-25 17:00:54 +02:00
|
|
|
import net.minecraft.inventory.Container;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
2015-12-03 20:15:07 +01:00
|
|
|
import net.minecraft.item.EnumRarity;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-05-25 17:00:54 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2014-12-20 21:34:07 +01:00
|
|
|
public abstract class BlockContainerBase extends BlockContainer{
|
|
|
|
|
2015-12-03 20:15:07 +01:00
|
|
|
private String name;
|
|
|
|
|
|
|
|
public BlockContainerBase(Material material, String name){
|
|
|
|
super(material);
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
this.register();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void register(){
|
|
|
|
this.setBlockName(ModUtil.MOD_ID_LOWER+"."+this.getBaseName());
|
|
|
|
GameRegistry.registerBlock(this, this.getItemBlock(), this.getBaseName());
|
|
|
|
if(this.shouldAddCreative()){
|
|
|
|
this.setCreativeTab(CreativeTab.instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean shouldAddCreative(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getBaseName(){
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Class<? extends ItemBlockBase> getItemBlock(){
|
|
|
|
return ItemBlockBase.class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public EnumRarity getRarity(ItemStack stack){
|
|
|
|
return EnumRarity.common;
|
2014-12-20 21:34:07 +01:00
|
|
|
}
|
|
|
|
|
2015-06-18 13:14:57 +02:00
|
|
|
public void dropInventory(World world, int x, int y, int z){
|
2015-06-28 03:12:32 +02:00
|
|
|
if(!world.isRemote){
|
2015-09-13 19:39:02 +02:00
|
|
|
TileEntity aTile = world.getTileEntity(x, y, z);
|
|
|
|
if(aTile instanceof TileEntityInventoryBase){
|
|
|
|
TileEntityInventoryBase tile = (TileEntityInventoryBase)aTile;
|
|
|
|
if(tile.getSizeInventory() > 0){
|
|
|
|
for(int i = 0; i < tile.getSizeInventory(); i++){
|
|
|
|
this.dropSlotFromInventory(i, tile, world, x, y, z);
|
2015-06-18 13:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-20 21:34:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 17:00:54 +02:00
|
|
|
|
2015-09-13 19:39:02 +02:00
|
|
|
public void dropSlotFromInventory(int i, TileEntityInventoryBase tile, World world, int x, int y, int z){
|
|
|
|
ItemStack stack = tile.getStackInSlot(i);
|
|
|
|
if(stack != null && stack.stackSize > 0){
|
2015-11-22 18:58:23 +01:00
|
|
|
float dX = Util.RANDOM.nextFloat()*0.8F+0.1F;
|
|
|
|
float dY = Util.RANDOM.nextFloat()*0.8F+0.1F;
|
|
|
|
float dZ = Util.RANDOM.nextFloat()*0.8F+0.1F;
|
2015-09-13 19:39:02 +02:00
|
|
|
EntityItem entityItem = new EntityItem(world, x+dX, y+dY, z+dZ, stack.copy());
|
2015-10-02 16:48:01 +02:00
|
|
|
if(stack.hasTagCompound()){
|
|
|
|
entityItem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy());
|
|
|
|
}
|
2015-09-13 19:39:02 +02:00
|
|
|
float factor = 0.05F;
|
2015-11-22 18:58:23 +01:00
|
|
|
entityItem.motionX = Util.RANDOM.nextGaussian()*factor;
|
|
|
|
entityItem.motionY = Util.RANDOM.nextGaussian()*factor+0.2F;
|
|
|
|
entityItem.motionZ = Util.RANDOM.nextGaussian()*factor;
|
2015-09-13 19:39:02 +02:00
|
|
|
world.spawnEntityInWorld(entityItem);
|
|
|
|
}
|
|
|
|
tile.setInventorySlotContents(i, null);
|
|
|
|
}
|
|
|
|
|
2015-05-25 17:00:54 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasComparatorInputOverride(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getComparatorInputOverride(World world, int x, int y, int z, int meta){
|
|
|
|
TileEntity tile = world.getTileEntity(x, y, z);
|
2015-10-03 10:16:18 +02:00
|
|
|
if(tile instanceof IInventory){
|
|
|
|
return Container.calcRedstoneFromInventory((IInventory)tile);
|
|
|
|
}
|
2015-05-25 17:00:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-03 18:40:43 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNeighborBlockChange(World world, int x, int y, int z, Block block){
|
|
|
|
this.updateRedstoneState(world, x, y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBlockAdded(World world, int x, int y, int z){
|
|
|
|
this.updateRedstoneState(world, x, y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateRedstoneState(World world, int x, int y, int z){
|
|
|
|
TileEntity tile = world.getTileEntity(x, y, z);
|
|
|
|
if(tile instanceof TileEntityBase){
|
|
|
|
((TileEntityBase)tile).setRedstonePowered(world.isBlockIndirectlyGettingPowered(x, y, z));
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer player){
|
|
|
|
if(!player.capabilities.isCreativeMode){
|
|
|
|
this.dropBlockAsItem(world, x, y, z, meta, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune){
|
|
|
|
ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
|
|
|
|
|
|
|
|
TileEntity tile = world.getTileEntity(x, y, z);
|
|
|
|
if(tile != null){
|
|
|
|
ItemStack stack = new ItemStack(this.getItemDropped(metadata, Util.RANDOM, fortune), 1, this.damageDropped(metadata));
|
|
|
|
|
|
|
|
if(tile instanceof IEnergySaver){
|
|
|
|
int energy = ((IEnergySaver)tile).getEnergy();
|
|
|
|
if(energy > 0){
|
|
|
|
if(stack.getTagCompound() == null){
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
}
|
|
|
|
stack.getTagCompound().setInteger("Energy", energy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drops.add(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
return drops;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack){
|
|
|
|
if(stack.getTagCompound() != null){
|
|
|
|
TileEntity tile = world.getTileEntity(x, y, z);
|
|
|
|
if(tile instanceof IEnergySaver){
|
|
|
|
((IEnergySaver)tile).setEnergy(stack.getTagCompound().getInteger("Energy"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 21:34:07 +01:00
|
|
|
}
|