diff --git a/src/main/java/appeng/core/features/AEBlockFeatureHandler.java b/src/main/java/appeng/core/features/AEBlockFeatureHandler.java index 5bd5b2337..c4d134e0e 100644 --- a/src/main/java/appeng/core/features/AEBlockFeatureHandler.java +++ b/src/main/java/appeng/core/features/AEBlockFeatureHandler.java @@ -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 itemBlockClass = this.featured.getItemBlockClass(); + // Class 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 ); } } } diff --git a/src/main/java/appeng/core/features/BlockDefinition.java b/src/main/java/appeng/core/features/BlockDefinition.java index a1a912ee5..3908cbe95 100644 --- a/src/main/java/appeng/core/features/BlockDefinition.java +++ b/src/main/java/appeng/core/features/BlockDefinition.java @@ -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 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 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 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 itemCtor = itemclass.getConstructor( ctorArgClasses ); + return itemCtor.newInstance( ObjectArrays.concat( block, itemCtorArgs ) ); + } + catch ( Throwable t ) + { + return null; + } + } } diff --git a/src/main/java/appeng/core/features/ItemDefinition.java b/src/main/java/appeng/core/features/ItemDefinition.java index 2162b7994..28aa143cc 100644 --- a/src/main/java/appeng/core/features/ItemDefinition.java +++ b/src/main/java/appeng/core/features/ItemDefinition.java @@ -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; } diff --git a/src/main/java/appeng/core/features/TileDefinition.java b/src/main/java/appeng/core/features/TileDefinition.java index fcd573d99..813e43331 100644 --- a/src/main/java/appeng/core/features/TileDefinition.java +++ b/src/main/java/appeng/core/features/TileDefinition.java @@ -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; }