Applied-Energistics-2-tiler.../items/parts/ItemFacade.java

192 lines
4.6 KiB
Java
Raw Normal View History

package appeng.items.parts;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockGlass;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
2014-02-09 02:34:52 +01:00
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
2014-02-01 23:24:13 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.AEApi;
import appeng.block.solids.OreQuartz;
import appeng.client.render.BusRenderer;
2014-02-01 23:24:13 +01:00
import appeng.core.FacadeConfig;
import appeng.core.features.AEFeature;
import appeng.facade.FacadePart;
import appeng.facade.IFacadeItem;
import appeng.items.AEBaseItem;
import appeng.util.Platform;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class ItemFacade extends AEBaseItem implements IFacadeItem
{
public ItemFacade() {
super( ItemFacade.class );
setfeature( EnumSet.of( AEFeature.Facades ) );
setHasSubtypes( true );
if ( Platform.isClient() )
2014-02-09 02:34:52 +01:00
MinecraftForgeClient.registerItemRenderer( this, BusRenderer.instance );
}
@Override
@SideOnly(Side.CLIENT)
public int getSpriteNumber()
{
return 0;
}
@Override
public boolean onItemUse(ItemStack is, EntityPlayer player, World w, int x, int y, int z, int side, float hitX, float hitY, float hitZ)
{
return AEApi.instance().partHelper().placeBus( is, x, y, z, side, player, w );
}
@Override
public FacadePart createPartFromItemStack(ItemStack is, ForgeDirection side)
{
ItemStack in = getTextureItem( is );
if ( in != null )
return new FacadePart( is, side );
return null;
}
List<ItemStack> subTypes = null;
2014-02-01 23:24:13 +01:00
public ItemStack getCreativeTabIcon()
{
calculateSubTypes();
if ( subTypes.isEmpty() )
2014-02-09 02:34:52 +01:00
return new ItemStack( Items.cake );
2014-02-01 23:24:13 +01:00
return subTypes.get( 0 );
}
@Override
2014-02-09 02:34:52 +01:00
public void getSubItems(Item number, CreativeTabs tab, List list)
{
calculateSubTypes();
list.addAll( subTypes );
}
public ItemStack createFromInts(int[] ids)
{
ItemStack is = new ItemStack( AEApi.instance().items().itemFacade.item() );
NBTTagCompound data = new NBTTagCompound();
2014-01-26 07:44:50 +01:00
data.setIntArray( "x", ids.clone() );
is.setTagCompound( data );
return is;
}
@Override
public ItemStack getTextureItem(ItemStack is)
{
Block blk = getBlock( is );
if ( blk != null )
return new ItemStack( blk, 1, getMeta( is ) );
return null;
}
private void calculateSubTypes()
{
if ( subTypes == null )
{
subTypes = new ArrayList();
2014-02-09 02:34:52 +01:00
for (Object blk : Block.blockRegistry)
{
2014-02-09 02:34:52 +01:00
Block b = (Block) blk;
2014-02-01 23:24:13 +01:00
if ( b != null
&& FacadeConfig.instance.checkEnabled( b, b.isOpaqueCube() && !b.getTickRandomly() && !(b instanceof OreQuartz)
|| b instanceof BlockGlass ) )
{
try
{
2014-02-09 02:34:52 +01:00
Item item = Item.getItemFromBlock( b );
List<ItemStack> tmpList = new ArrayList();
2014-02-09 02:34:52 +01:00
b.getSubBlocks( item, b.getCreativeTabToDisplayOn(), tmpList );
for (ItemStack l : tmpList)
{
2014-02-15 04:16:05 +01:00
if ( l.hasTagCompound() )
continue;
ItemStack is = new ItemStack( this );
NBTTagCompound data = new NBTTagCompound();
int[] ds = new int[2];
2014-02-09 02:34:52 +01:00
ds[0] = Item.getIdFromItem( l.getItem() );
ds[1] = l.getItem().getMetadata( l.getItemDamage() );
data.setIntArray( "x", ds );
is.setTagCompound( data );
subTypes.add( is );
}
}
catch (Throwable t)
{
// just absorb..
}
}
}
2014-02-01 23:24:13 +01:00
if ( FacadeConfig.instance.hasChanged() )
FacadeConfig.instance.save();
}
2014-02-01 23:24:13 +01:00
}
@Override
public Block getBlock(ItemStack is)
{
NBTTagCompound data = is.getTagCompound();
if ( data != null )
{
int[] blk = data.getIntArray( "x" );
if ( blk != null && blk.length == 2 )
2014-02-09 02:34:52 +01:00
return Block.getBlockById( blk[0] );
}
2014-02-09 02:34:52 +01:00
return Blocks.glass;
}
@Override
public int getMeta(ItemStack is)
{
NBTTagCompound data = is.getTagCompound();
if ( data != null )
{
int[] blk = data.getIntArray( "x" );
if ( blk != null && blk.length == 2 )
return blk[1];
}
return 0;
}
@Override
2014-02-09 02:34:52 +01:00
public String getItemStackDisplayName(ItemStack is)
{
try
{
ItemStack in = getTextureItem( is );
if ( in != null )
{
2014-02-09 02:34:52 +01:00
return super.getItemStackDisplayName( is ) + " - " + in.getDisplayName();
}
}
catch (Throwable t)
{
}
2014-02-09 02:34:52 +01:00
return super.getItemStackDisplayName( is );
}
}