Applied-Energistics-2-tiler.../src/main/java/appeng/items/parts/ItemFacade.java

342 lines
8.7 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.items.parts;
2014-09-24 02:26:27 +02:00
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockGlass;
import net.minecraft.block.BlockStainedGlass;
2016-08-28 12:10:40 +02:00
import net.minecraft.block.state.IBlockState;
2014-09-24 02:26:27 +02:00
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
2016-08-28 12:10:40 +02:00
import net.minecraft.util.EnumBlockRenderType;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
2014-09-24 02:26:27 +02:00
import net.minecraft.world.World;
import net.minecraftforge.common.property.IExtendedBlockState;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.AEApi;
import appeng.api.exceptions.MissingDefinition;
2014-09-24 02:26:27 +02:00
import appeng.api.parts.IAlphaPassItem;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation;
2016-08-28 12:10:40 +02:00
import appeng.core.AELog;
2014-09-24 02:26:27 +02:00
import appeng.core.FacadeConfig;
2016-08-28 12:10:40 +02:00
import appeng.decorative.solid.BlockQuartzOre;
2014-09-24 02:26:27 +02:00
import appeng.facade.FacadePart;
import appeng.facade.IFacadeItem;
import appeng.items.AEBaseItem;
2014-09-24 02:26:27 +02:00
public class ItemFacade extends AEBaseItem implements IFacadeItem, IAlphaPassItem
{
private static final String TAG_ITEM_ID = "item";
private static final String TAG_DAMAGE = "damage";
private List<ItemStack> subTypes = null;
public ItemFacade()
{
2014-12-29 15:13:47 +01:00
this.setHasSubtypes( true );
2014-09-24 02:26:27 +02:00
}
@Override
public EnumActionResult onItemUseFirst( final ItemStack is, final EntityPlayer player, final World world, final BlockPos pos, final EnumFacing side, final float hitX, final float hitY, final float hitZ, final EnumHand hand )
2014-09-24 02:26:27 +02:00
{
return AEApi.instance().partHelper().placeBus( is, pos, side, player, hand, world );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public String getItemStackDisplayName( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
try
{
2015-09-30 14:24:40 +02:00
final ItemStack in = this.getTextureItem( is );
if( in != null )
{
return super.getItemStackDisplayName( is ) + " - " + in.getDisplayName();
}
}
2015-09-30 14:24:40 +02:00
catch( final Throwable ignored )
{
2014-09-24 02:26:27 +02:00
}
2014-09-24 02:26:27 +02:00
return super.getItemStackDisplayName( is );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
protected void getCheckedSubItems( final Item sameItem, final CreativeTabs creativeTab, final List<ItemStack> itemStacks )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.calculateSubTypes();
itemStacks.addAll( this.subTypes );
2014-09-24 02:26:27 +02:00
}
private void calculateSubTypes()
{
if( this.subTypes == null )
2014-09-24 02:26:27 +02:00
{
this.subTypes = new ArrayList<ItemStack>( 1000 );
for( final Object blk : Block.REGISTRY )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final Block b = (Block) blk;
2014-09-24 02:26:27 +02:00
try
{
2015-09-30 14:24:40 +02:00
final Item item = Item.getItemFromBlock( b );
if( item == null )
{
continue;
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final List<ItemStack> tmpList = new ArrayList<ItemStack>( 100 );
2014-09-24 02:26:27 +02:00
b.getSubBlocks( item, b.getCreativeTabToDisplayOn(), tmpList );
2015-09-30 14:24:40 +02:00
for( final ItemStack l : tmpList )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final ItemStack facade = this.createFacadeForItem( l, false );
if( facade != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.subTypes.add( facade );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
2014-09-24 02:26:27 +02:00
{
// just absorb..
}
}
if( FacadeConfig.instance().hasChanged() )
2015-04-29 02:30:53 +02:00
{
FacadeConfig.instance().save();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
private static boolean hasSimpleModel( IBlockState blockState )
2016-08-28 12:10:40 +02:00
{
if( blockState.getRenderType() != EnumBlockRenderType.MODEL || blockState instanceof IExtendedBlockState )
2016-08-28 12:10:40 +02:00
{
return false;
}
return blockState.isFullCube();
2016-08-28 12:10:40 +02:00
}
2015-09-30 14:24:40 +02:00
public ItemStack createFacadeForItem( final ItemStack l, final boolean returnItem )
2014-09-24 02:26:27 +02:00
{
if( l == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final Block b = Block.getBlockFromItem( l.getItem() );
if( b == null || l.hasTagCompound() )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final int metadata = l.getItem().getMetadata( l.getItemDamage() );
2014-09-24 02:26:27 +02:00
2016-08-28 12:10:40 +02:00
final boolean hasTile = b.hasTileEntity( b.getDefaultState() );
final boolean enableGlass = b instanceof BlockGlass || b instanceof BlockStainedGlass;
final boolean disableOre = b instanceof BlockQuartzOre;
// Try to get the block state based on the item stack's meta. If this fails, don't consider it for a facade
// This for example fails for Pistons because they hardcoded an invalid meta value in vanilla
IBlockState blockState;
try
{
blockState = b.getStateFromMeta( metadata );
}
catch( Exception e )
{
AELog.debug( e, "Cannot create a facade for " + b.getRegistryName() );
return null;
}
final boolean defaultValue = ( b.isFullyOpaque( blockState ) && hasSimpleModel( blockState ) && !b.getTickRandomly() && !hasTile && !disableOre ) || enableGlass;
if( FacadeConfig.instance().checkEnabled( b, metadata, defaultValue ) )
2014-09-24 02:26:27 +02:00
{
if( returnItem )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return l;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final ItemStack is = new ItemStack( this );
final NBTTagCompound data = new NBTTagCompound();
data.setString( TAG_ITEM_ID, l.getItem().getRegistryName().toString() );
data.setInteger( TAG_DAMAGE, l.getItemDamage() );
2014-09-24 02:26:27 +02:00
is.setTagCompound( data );
return is;
}
return null;
}
@Override
2015-09-30 14:24:40 +02:00
public FacadePart createPartFromItemStack( final ItemStack is, final AEPartLocation side )
{
2015-09-30 14:24:40 +02:00
final ItemStack in = this.getTextureItem( is );
if( in != null )
2015-04-29 02:30:53 +02:00
{
return new FacadePart( is, side );
2015-04-29 02:30:53 +02:00
}
return null;
}
@Override
public ItemStack getTextureItem( ItemStack is )
{
NBTTagCompound nbt = is.getTagCompound();
if( nbt == null )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
ResourceLocation itemId;
int itemDamage;
// Handle legacy facades
if( nbt.hasKey( "x" ) )
{
int[] data = nbt.getIntArray( "x" );
if( data.length != 2 )
{
return null;
}
Item item = Item.REGISTRY.getObjectById( data[0] );
if ( item == null ) {
return null;
}
itemId = item.getRegistryName();
itemDamage = data[1];
} else {
// First item is numeric item id, second is damage
itemId = new ResourceLocation( nbt.getString( TAG_ITEM_ID ) );
itemDamage = nbt.getInteger( TAG_DAMAGE );
}
Item baseItem = Item.REGISTRY.getObject( itemId );
if( baseItem == null )
{
return null;
}
return new ItemStack( baseItem, 1, itemDamage );
}
@Override
public IBlockState getTextureBlockState( ItemStack is )
{
ItemStack baseItemStack = getTextureItem( is );
if( baseItemStack == null )
{
return Blocks.GLASS.getDefaultState();
}
2015-12-24 02:03:16 +01:00
Block block = Block.getBlockFromItem( baseItemStack.getItem() );
if( block == null )
{
return Blocks.GLASS.getDefaultState();
}
int metadata = baseItemStack.getItem().getMetadata( baseItemStack );
try
{
return block.getStateFromMeta( metadata );
}
catch( Exception e )
{
AELog.warn( "Block %s has broken getStateFromMeta method for meta %d", block.getRegistryName().toString(), baseItemStack.getItemDamage() );
return Blocks.GLASS.getDefaultState();
}
}
public List<ItemStack> getFacades()
{
this.calculateSubTypes();
return this.subTypes;
}
public ItemStack getCreativeTabIcon()
2014-09-24 02:26:27 +02:00
{
this.calculateSubTypes();
if( this.subTypes.isEmpty() )
2015-04-29 02:30:53 +02:00
{
return new ItemStack( Items.CAKE );
2015-04-29 02:30:53 +02:00
}
return this.subTypes.get( 0 );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public ItemStack createFromIDs( final int[] ids )
2014-09-24 02:26:27 +02:00
{
ItemStack facadeStack = AEApi.instance().definitions().items().facade().maybeStack( 1 )
.orElseThrow( () -> new MissingDefinition( "Tried to create a facade, while facades are being deactivated." ) );
2014-09-24 02:26:27 +02:00
// Convert back to a registry name...
Item item = Item.REGISTRY.getObjectById( ids[0] );
if ( item == null ) {
return null;
}
final NBTTagCompound facadeTag = new NBTTagCompound();
facadeTag.setString( TAG_ITEM_ID, item.getRegistryName().toString() );
facadeTag.setInteger( TAG_DAMAGE, ids[1] );
facadeStack.setTagCompound( facadeTag );
2014-09-24 02:26:27 +02:00
return facadeStack;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean useAlphaPass( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
IBlockState blockState = this.getTextureBlockState( is );
2014-09-24 02:26:27 +02:00
if( blockState == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return false;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
Block blk = blockState.getBlock();
return blk.canRenderInLayer( BlockRenderLayer.TRANSLUCENT );
2014-09-24 02:26:27 +02:00
}
}