Jump to content

Recommended Posts

Posted

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?

Posted

You can’t create a new item, this usually crashed the game - you want an ItemStack. I would just copy the code from ItemHoe

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.