2014-11-18 16:47:30 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* 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>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package appeng.core.features;
|
|
|
|
|
|
|
|
|
2015-04-02 14:33:39 +02:00
|
|
|
import java.lang.reflect.Constructor;
|
2015-05-02 15:33:02 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
import com.google.common.base.Optional;
|
|
|
|
import com.google.common.base.Preconditions;
|
2015-04-02 14:33:39 +02:00
|
|
|
|
2014-11-18 16:47:30 +01:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.item.Item;
|
2015-01-03 02:53:14 +01:00
|
|
|
import net.minecraft.item.ItemBlock;
|
2014-11-18 16:47:30 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.world.IBlockAccess;
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
import appeng.api.definitions.IBlockDefinition;
|
2015-04-02 14:33:39 +02:00
|
|
|
import appeng.block.AEBaseBlock;
|
2014-11-18 16:47:30 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
|
|
|
|
public class BlockDefinition extends ItemDefinition implements IBlockDefinition
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
|
|
|
private final Block block;
|
|
|
|
private final boolean enabled;
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
public BlockDefinition( Block block, ActivityState state )
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
2015-04-02 14:33:39 +02:00
|
|
|
super( constructItemFromBlock( block ), state );
|
2015-04-26 09:57:42 +02:00
|
|
|
|
|
|
|
Preconditions.checkNotNull( block );
|
|
|
|
Preconditions.checkNotNull( state );
|
2015-04-02 14:33:39 +02:00
|
|
|
|
2014-11-18 16:47:30 +01:00
|
|
|
this.block = block;
|
2015-01-03 02:53:14 +01:00
|
|
|
this.enabled = state == ActivityState.Enabled;
|
2014-11-18 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-26 09:57:42 +02:00
|
|
|
public final Optional<Block> maybeBlock()
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
return Optional.of( this.block );
|
2014-11-18 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-26 09:57:42 +02:00
|
|
|
public final Optional<ItemBlock> maybeItemBlock()
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.enabled )
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
2015-01-03 02:53:14 +01:00
|
|
|
return Optional.of( new ItemBlock( this.block ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Optional.absent();
|
2014-11-18 16:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-03 02:53:14 +01:00
|
|
|
public final Optional<ItemStack> maybeStack( int stackSize )
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.enabled )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
return Optional.of( new ItemStack( this.block ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
2014-11-18 16:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-03 02:53:14 +01:00
|
|
|
public final boolean isSameAs( IBlockAccess world, int x, int y, int z )
|
2014-11-18 16:47:30 +01:00
|
|
|
{
|
|
|
|
return this.enabled && world.getBlock( x, y, z ) == this.block;
|
|
|
|
}
|
2015-04-02 14:33:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an {@link ItemBlock} from a {@link Block} to register it later as {@link Item}
|
|
|
|
*
|
2015-05-02 15:33:02 +02:00
|
|
|
* @param block to be constructed from
|
|
|
|
*
|
|
|
|
* @return item from block
|
2015-04-02 14:33:39 +02:00
|
|
|
*/
|
2015-05-02 15:33:02 +02:00
|
|
|
@Nonnull
|
2015-04-02 14:33:39 +02:00
|
|
|
private static Item constructItemFromBlock( Block block )
|
|
|
|
{
|
2015-05-02 15:33:02 +02:00
|
|
|
final Class<? extends ItemBlock> itemClass = getItemBlockConstructor( block );
|
|
|
|
|
|
|
|
return constructItemBlock( block, itemClass );
|
2015-04-02 14:33:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cosntructor to use.
|
|
|
|
*
|
|
|
|
* Either {@link ItemBlock} or in case of an {@link AEBaseBlock} the class returned by
|
|
|
|
* AEBaseBlock.getItemBlockClass().
|
|
|
|
*
|
|
|
|
* @param block the block used to determine the used constructor.
|
|
|
|
* @return a {@link Class} extending ItemBlock
|
|
|
|
*/
|
2015-05-02 15:33:02 +02:00
|
|
|
@Nonnull
|
2015-04-02 14:33:39 +02:00
|
|
|
private static Class<? extends ItemBlock> getItemBlockConstructor( Block block )
|
|
|
|
{
|
|
|
|
if ( block instanceof AEBaseBlock )
|
|
|
|
{
|
|
|
|
final AEBaseBlock aeBaseBlock = ( AEBaseBlock ) block;
|
|
|
|
return aeBaseBlock.getItemBlockClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ItemBlock.class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually construct an instance of {@link Item} with the block and earlier determined constructor.
|
|
|
|
*
|
|
|
|
* @param block the block to create the {@link ItemBlock} from
|
2015-05-02 15:33:02 +02:00
|
|
|
* @param itemClass the class used to construct it.
|
2015-04-02 14:33:39 +02:00
|
|
|
* @return an {@link Item} for the block. Actually always a sub type of {@link ItemBlock}
|
|
|
|
*/
|
2015-05-02 15:33:02 +02:00
|
|
|
@Nonnull
|
|
|
|
private static Item constructItemBlock( Block block, Class<? extends ItemBlock> itemClass )
|
2015-04-02 14:33:39 +02:00
|
|
|
{
|
2015-05-02 15:33:02 +02:00
|
|
|
assert block != null;
|
|
|
|
assert itemClass != null;
|
|
|
|
|
2015-04-02 14:33:39 +02:00
|
|
|
try
|
|
|
|
{
|
2015-05-02 15:33:02 +02:00
|
|
|
final Constructor<? extends ItemBlock> itemConstructor = itemClass.getConstructor( Block.class );
|
|
|
|
|
|
|
|
return itemConstructor.newInstance( block );
|
2015-04-02 14:33:39 +02:00
|
|
|
}
|
2015-05-02 15:33:02 +02:00
|
|
|
catch( InstantiationException e )
|
2015-04-02 14:33:39 +02:00
|
|
|
{
|
2015-05-02 15:33:02 +02:00
|
|
|
e.printStackTrace();
|
2015-04-02 14:33:39 +02:00
|
|
|
}
|
2015-05-02 15:33:02 +02:00
|
|
|
catch( IllegalAccessException e )
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch( InvocationTargetException e )
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch( NoSuchMethodException e )
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalStateException( "Tried to construct an ItemBlock from Block " + block.getUnlocalizedName() + " and Class<Item>" + itemClass );
|
2015-04-02 14:33:39 +02:00
|
|
|
}
|
2014-11-18 16:47:30 +01:00
|
|
|
}
|