I'm adapting this tutorial (which is quite old) to a newer Forge version. One of the things you do in it is make a "wheat and steel" item. This is a damageable item that plants a seed. Tutorial suggests as an extension that we add the ability to hoe the ground first before attempting to place the seed. I thought I would try to just reuse the implementations of ItemHoe and ItemSeeds. Here is my code (Scala).
package com.fizzixnerd.mymod
import net.minecraft.creativetab.CreativeTabs
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.{Item, ItemHoe, ItemSeeds, ItemStack}
import net.minecraft.util.{EnumActionResult, EnumFacing, EnumHand, ResourceLocation}
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.minecraftforge.common.IPlantable
import net.minecraftforge.fml.common.registry.GameRegistry
object ItemWheatAndSteel extends Item {
setRegistryName("wheat_and_steel")
maxStackSize = 1
setMaxDamage(64)
setCreativeTab(CreativeTabs.TOOLS)
private val virtualSeeds = GameRegistry.findRegistry(classOf[Item]).getValue(new ResourceLocation(("minecraft:wheat_seeds")))
override
def onItemUse(player: EntityPlayer,
world: World,
pos: BlockPos,
hand: EnumHand,
side: EnumFacing,
hitX: Float,
hitY: Float,
hitZ: Float): EnumActionResult = {
val itemStack = player.getHeldItem(hand)
if (!player.canPlayerEdit(pos.offset(side), side, itemStack)) {
EnumActionResult.FAIL
} else {
// Create a virtual hoe to till the ground first.
val virtualHoe = new ItemHoe(Item.ToolMaterial.IRON)
val success = virtualHoe.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ)
if (success == EnumActionResult.SUCCESS) {
val success = virtualSeeds.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ)
if (success == EnumActionResult.SUCCESS) {
itemStack.damageItem(1, player)
}
success
} else {
success
}
}
}
}
This does not give the desired behavior. After using the item once, it disappears from the players inventory.
What happens is that inside ItemSeeds class is that at the end of planting the seed, it decrements the number of items the player is holding, whatever that happens to be.
Is there a way to reuse the behavior of ItemSeeds and ItemHoe without reimplementing them directly in my own class?