Fixes api refactoring causing items to be null.

This happens as nothing is registered with minecraft at this point, so it
will always return null for the ItemBlock.
This commit is contained in:
yueh 2015-04-02 14:33:39 +02:00
parent 6bd4472229
commit c699ed44aa
4 changed files with 84 additions and 8 deletions

View file

@ -21,13 +21,12 @@ package appeng.core.features;
import java.util.EnumSet;
import cpw.mods.fml.common.registry.GameRegistry;
import com.google.common.base.Optional;
import cpw.mods.fml.common.registry.GameRegistry;
import appeng.api.definitions.ITileDefinition;
import appeng.block.AEBaseBlock;
import appeng.block.AEBaseItemBlock;
import appeng.core.CommonHelper;
import appeng.core.CreativeTab;
import appeng.util.Platform;
@ -77,9 +76,13 @@ public final class AEBlockFeatureHandler implements IFeatureHandler
CommonHelper.proxy.bindTileEntitySpecialRenderer( this.featured.getTileEntityClass(), this.featured );
}
Class<? extends AEBaseItemBlock> itemBlockClass = this.featured.getItemBlockClass();
// Class<? extends AEBaseItemBlock> itemBlockClass = this.featured.getItemBlockClass();
GameRegistry.registerBlock( this.featured, itemBlockClass, "tile." + name );
final String registryName = "tile." + name;
// Bypass the forge magic with null to register our own itemblock later.
GameRegistry.registerBlock( this.featured, null, registryName );
GameRegistry.registerItem( this.definition.maybeItem().get(), registryName );
}
}
}

View file

@ -19,15 +19,19 @@
package appeng.core.features;
import java.lang.reflect.Constructor;
import com.google.common.base.Optional;
import com.google.common.collect.ObjectArrays;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.world.IBlockAccess;
import com.google.common.base.Optional;
import appeng.api.definitions.IBlockDefinition;
import appeng.block.AEBaseBlock;
public class BlockDefinition extends ItemDefinition implements IBlockDefinition
@ -37,7 +41,9 @@ public class BlockDefinition extends ItemDefinition implements IBlockDefinition
public BlockDefinition( Block block, ActivityState state )
{
super( Item.getItemFromBlock( block ), state );
super( constructItemFromBlock( block ), state );
assert block != null;
this.block = block;
this.enabled = state == ActivityState.Enabled;
}
@ -79,4 +85,68 @@ public class BlockDefinition extends ItemDefinition implements IBlockDefinition
{
return this.enabled && world.getBlock( x, y, z ) == this.block;
}
/**
* Create an {@link ItemBlock} from a {@link Block} to register it later as {@link Item}
*
* @param block
* @return
*/
private static Item constructItemFromBlock( Block block )
{
final Class<? extends ItemBlock> itemclass = getItemBlockConstructor( block );
return constructItemBlock( block, itemclass );
}
/**
* 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
*/
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.
*
* Shamelessly stolen from the forge magic.
*
* TODO: throw an exception instead of returning null? As this could cause issue later on.
*
* @param block the block to create the {@link ItemBlock} from
* @param itemclass the class used to construct it.
* @return an {@link Item} for the block. Actually always a sub type of {@link ItemBlock}
*/
private static Item constructItemBlock( Block block, Class<? extends ItemBlock> itemclass )
{
try
{
Object[] itemCtorArgs = new Object[] {};
Class<?>[] ctorArgClasses = new Class<?>[itemCtorArgs.length + 1];
ctorArgClasses[0] = Block.class;
for ( int idx = 1; idx < ctorArgClasses.length; idx++ )
{
ctorArgClasses[idx] = itemCtorArgs[idx - 1].getClass();
}
Constructor<? extends ItemBlock> itemCtor = itemclass.getConstructor( ctorArgClasses );
return itemCtor.newInstance( ObjectArrays.concat( block, itemCtorArgs ) );
}
catch ( Throwable t )
{
return null;
}
}
}

View file

@ -36,6 +36,8 @@ public class ItemDefinition implements IItemDefinition
public ItemDefinition( Item item, ActivityState state )
{
assert item != null;
this.item = item;
this.enabled = state == ActivityState.Enabled;
}

View file

@ -34,6 +34,7 @@ public final class TileDefinition extends BlockDefinition implements ITileDefini
public TileDefinition( AEBaseBlock block, ActivityState state )
{
super( block, state );
assert !block.hasBlockTileEntity() || block.getTileEntityClass() != null;
this.block = block;
}