Skip to content

Instantly share code, notes, and snippets.

/block.java Secret

Created May 28, 2016
Embed
What would you like to do?
package com.koopamillion.blocks;
import java.util.List;
import java.util.Random;
import com.koopamillion.Main.MainRegistry;
import com.koopamillion.api.IWrenchable;
import com.koopamillion.creativetabs.MCreativeTabs;
import com.koopamillion.lib.Names;
import com.koopamillion.lib.RefStrings;
import com.koopamillion.tile_entity.TileEntityElectricFurnace;
import com.koopamillion.tile_entity.TileEntityElectricPounder;
import com.koopamillion.util.LogHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPistonBase;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class BlockMachines extends Block implements IWrenchable{
private IIcon[] icons = new IIcon[3];
private static boolean keepInventory;
private Random rand = new Random();
public BlockMachines(){
super(Material.iron);
this.setCreativeTab(MCreativeTabs.tabBlocks);
this.setBlockName(Names.MACHINES);
this.setHardness(3.5F);
this.setResistance(17.5F);
this.setStepSound(soundTypeMetal);
this.setHarvestLevel("pickaxe", 1);
this.setTickRandomly(true);
lightValue = 0;
}
public TileEntity createTileEntity(World world, int meta){
switch(meta){
case 0:
return new TileEntityElectricFurnace();
}
return null;
}
public boolean hasTileEntity(int meta){
return meta == 0;
}
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ){
if(!world.isRemote){
int meta = world.getBlockMetadata(x, y, z);
if(meta == 0){
player.openGui(MainRegistry.modInstance, 2, world, x, y, z);
return true;
}
}
return true;
}
public void getSubBlocks(Item item, MCreativeTabs creativeTab, List list){
list.add(new ItemStack(item, 1, 0));
}
public int damageDropped(int meta){
return meta;
}
public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z){
int meta = world.getBlockMetadata(x, y, z);
return new ItemStack(this, 1, meta);
}
public void registerBlockIcons(IIconRegister iconRegister){
icons[0] = iconRegister.registerIcon(RefStrings.MODID + ":ordinalMachineSide");
icons[1] = iconRegister.registerIcon(RefStrings.MODID + ":electricFurnaceOff");
icons[2] = iconRegister.registerIcon(RefStrings.MODID + ":electricFurnaceOn");
}
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemstack){
int meta = world.getBlockMetadata(x, y, z);
if(meta == 0){
TileEntityElectricFurnace tileentity = (TileEntityElectricFurnace) world.getTileEntity(x, y, z);
tileentity.rotation = BlockPistonBase.determineOrientation(world, x, y, z, entity);
}
}
public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side)
{
int meta = world.getBlockMetadata(x, y, z);
if(meta == 0)
{
TileEntityElectricFurnace tileentity = (TileEntityElectricFurnace) world.getTileEntity(x, y, z);
if(side == tileentity.rotation)
{
if(tileentity.isSmelting)
{
lightValue = 10;
return icons[2];
}
else
{
lightValue = 0;
return icons[1];
}
}
}
return icons[0];
}
public IIcon getIcon(int side, int meta)
{
if(meta == 0 && side == 3)
{
return icons[1];
}
return icons[0];
}
@Override
public void wrenchShiftRightClick(World world, int x, int y, int z) {
if(!world.isRemote && world.getTileEntity(x, y, z) != null){
ItemStack itemstack = new ItemStack(Item.getItemFromBlock(this));
dropInventory(world, x, y, z);
world.setBlockToAir(x, y, z);
world.removeTileEntity(x, y, z);
world.notifyBlockChange(x, y, z, this);
dropBlockAsItem(world, x, y, z, itemstack);
}
}
protected void dropInventory(World world, int x, int y, int z)
{
TileEntity tileEntity = world.getTileEntity(x, y, z);
if (!(tileEntity instanceof IInventory))
{
return;
}
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack itemStack = inventory.getStackInSlot(i);
if (itemStack != null && itemStack.stackSize > 0)
{
Random rand = new Random();
float dX = rand.nextFloat() * 0.8F + 0.1F;
float dY = rand.nextFloat() * 0.8F + 0.1F;
float dZ = rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, itemStack.copy());
if (itemStack.hasTagCompound())
{
entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
}
float factor = 0.05F;
entityItem.motionX = rand.nextGaussian() * factor;
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
entityItem.motionZ = rand.nextGaussian() * factor;
world.spawnEntityInWorld(entityItem);
itemStack.stackSize = 0;
}
}
}
@Override
public void wrenchRightClick(World world, int x, int y, int z) {
}
@Override
public void wrenchCutterRightClick(World world, int x, int y, int z) {
// TODO Auto-generated method stub
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment