How to make a food item plant seeds?

Started by Fox_trot on

Topic category: Help with Minecraft modding (Java Edition)

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to make a food item plant seeds?

I'm back again!

I'm making a plant that is similar to that of the sweet berry bush. I want the sweet berry to have the same affect where it is both edible and the seed for the bush. As of right now there is no variable that I know of on a food for when it is right-clicked on a block, right now I have it as when it is right-clicked which spawns the plant right where the player is standing but also is edible at the same time. 

Maybe the developers could add a right-clicked on block event to the food item.

Thanks

I suggest you check our…
Wed, 09/25/2019 - 16:07

I suggest you check our tutorials collection playlist on our YouTube channel which contains many examples and tutorials that can help you get started with MCreator: https://www.youtube.com/playlist?list=PLAeL-oIFIEngE6jRgFYeFMfuj8WQsO3Ei

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hi Klemen,  I have looked at…
Thu, 09/26/2019 - 08:39

Hi Klemen, 

I have looked at the tutorials and they've helped me a lot with other elements, however because I have my seed as a food and not an item there is no right clicked on block event. Is there a procedure saying, if player is looking at grass block, place plant at that block.

Thanks

Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
If you want your food to be…
Thu, 09/26/2019 - 09:57

If you want your food to be plantable, you can create an item element that triggers the "Right-clicked on block" procedure, then copy the code from that item to your food element. The code you'll have to copy should look like this:

@Override
public EnumActionResult onItemUseFirst(EntityPlayer entity, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
	ItemStack itemstack = entity.getHeldItem(hand);
	int x = pos.getX();
	int y = pos.getY();
	int z = pos.getZ();
	{
	java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
		$_dependencies.put("entity", entity);
		$_dependencies.put("x", x);
		$_dependencies.put("y", y);
		$_dependencies.put("z", z);
		$_dependencies.put("world", world);
	MCreatorYourProcedure.executeProcedure($_dependencies);
	}
	return EnumActionResult.PASS;
}

After copying the code, you should press ctrl+w to add the required imports (or you'll get compilation errors), lock the food element and delete the item element.

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thanks for the help!…
Thu, 09/26/2019 - 11:12

Thanks for the help!

Unfortunately, after following your steps it still doesn't work when I right click. 

Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
try replacing this: public…
Thu, 09/26/2019 - 11:30

try replacing this:

public EnumActionResult onItemUseFirst(EntityPlayer entity, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {

with this, after pasting the code:

public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {

 

Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
*sorry, replace it with this…
Thu, 09/26/2019 - 11:32

*sorry, replace it with this:

public EnumActionResult onItemUse(EntityPlayer entity, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {

 

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I've tried again both ways…
Thu, 09/26/2019 - 11:44

I've tried again both ways but now it still doesn't work. Here is the code:

 

public class MCreatorSwampBerries extends Elementsoxenfox_mod.ModElement {
    @GameRegistry.ObjectHolder("oxenfox_mod:swampberries")
    public static final Item block = null;

    public MCreatorSwampBerries(Elementsoxenfox_mod instance) {
        super(instance, 1);
    }

    @Override
    public void initElements() {
        elements.items.add(() -> new ItemFoodCustom());
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void registerModels(ModelRegistryEvent event) {
        ModelLoader.setCustomModelResourceLocation(block, 0, new ModelResourceLocation("oxenfox_mod:swampberries", "inventory"));
    }

    public static class ItemFoodCustom extends ItemFood {
        public ItemFoodCustom() {
            super(2, 0.3F, false);
            setUnlocalizedName("swampberries");
            setRegistryName("swampberries");
            setCreativeTab(CreativeTabs.FOOD);
            setMaxStackSize(64);
        }

        @Override
        public int getMaxItemUseDuration(ItemStack stack) {
            return 16;
        }

        @Override
        public EnumAction getItemUseAction(ItemStack par1ItemStack) {
            return EnumAction.EAT;
        }

        @Override
        public EnumActionResult onItemUseFirst(EntityPlayer entity, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, 
        EnumHand hand) {
            ItemStack itemstack = entity.getHeldItem(hand);
            int x = pos.getX();
            int y = pos.getY();
            int z = pos.getZ();
            {
                java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
                $_dependencies.put("x", x);
                $_dependencies.put("y", y);
                $_dependencies.put("z", z);
                $_dependencies.put("world", world);
                MCreatorSpawnBerryBush.executeProcedure($_dependencies);
            }

        @Override
        protected void onFoodEaten(ItemStack itemStack, World world, EntityPlayer entity) {
            super.onFoodEaten(itemStack, world, entity);
            int x = (int) entity.posX;
            int y = (int) entity.posY;
            int z = (int) entity.posZ;
            {
                java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
                $_dependencies.put("entity", entity);
                MCreatorSwampBerryEaten.executeProcedure($_dependencies);
            }
        }
    }
}
 

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm getting compilations…
Thu, 09/26/2019 - 11:46

I'm getting compilations errors so what do you mean by ctr+w. I;ve done it in the code and it add boxes with negative signs in them, is that what you mean?

Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
you're missing a } after…
Thu, 09/26/2019 - 11:48

you're missing a } after MCreatorSpawnBerryBush.executeProcedure($_dependencies);

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I've added a } but it still…
Thu, 09/26/2019 - 11:53

I've added a } but it still doesn't compile :(

        @Override
        public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer entity, EnumHand hand) {
            ActionResult<ItemStack> ar = super.onItemRightClick(world, entity, hand);
            ItemStack itemstack = ar.getResult();
            int x = (int) entity.posX;
            int y = (int) entity.posY;
            int z = (int) entity.posZ;
            {
                java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
                $_dependencies.put("x", x);
                $_dependencies.put("y", y);
                $_dependencies.put("z", z);
                $_dependencies.put("world", world);
                MCreatorSpawnBerryBush.executeProcedure($_dependencies);  }
            }
        }

        @Override
        protected void onFoodEaten(ItemStack itemStack, World world, EntityPlayer entity) {
            super.onFoodEaten(itemStack, world, entity);
            int x = (int) entity.posX;
            int y = (int) entity.posY;
            int z = (int) entity.posZ;
            {
                java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
                $_dependencies.put("entity", entity);
                MCreatorSwampBerryEaten.executeProcedure($_dependencies);  
            }
        }
    }
}
 

Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
now there's one too many. It…
Thu, 09/26/2019 - 11:55

now there's one too many. It wasn't there in the code you posted before

 

Joined Nov 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
hm, that's the mod file, not…
Thu, 09/26/2019 - 12:25

hm, that's the mod file, not the workspace.
Try again by:

  1. unlocking the element
  2. trigger the "plant the bush" procedure in "Item right click"
  3. now lock the code again, replace
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer entity, EnumHand hand) {
          ActionResult<ItemStack> ar = super.onItemRightClick(world, entity, hand);
          ItemStack itemstack = ar.getResult();
          int x = (int) entity.posX;
          int y = (int) entity.posY;
          int z = (int) entity.posZ;
          {
               java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
               $_dependencies.put("x", x);
               $_dependencies.put("y", y);
               $_dependencies.put("z", z);
               $_dependencies.put("world", world);
               MCreatorSpawnBerryBush.executeProcedure($_dependencies);
          }
    }

    with

    ​@Override
    public EnumActionResult onItemUse(EntityPlayer entity, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    	ItemStack itemstack = entity.getHeldItem(hand);
    	int x = pos.getX();
    	int y = pos.getY();
    	int z = pos.getZ();
    	{
    	java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
    		$_dependencies.put("entity", entity);
    		$_dependencies.put("x", x);
    		$_dependencies.put("y", y);
    		$_dependencies.put("z", z);
    		$_dependencies.put("world", world);
    	MCreatorSpawnBerryBush.executeProcedure($_dependencies);
    	}
    	return EnumActionResult.PASS;
    }
  4. press ctrl+w and save the code
Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Whoops I am such a cluts…
Thu, 09/26/2019 - 12:42

Whoops I am such a cluts sometimes...

Anyway, IT WORKED!

I had to make it y + 1 as it was replacing the block but other than that it's perfect!

I forgot to do step 2 so I presume that's why it wouldn't compile for all those attempts.

But nonetheless, thank you very very much for helping me out, very much appreciated!