Custom code for item actions

Started by _seacheese on

Topic category: Help with modding (Java Edition)

Last seen on 19:31, 21. Mar 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Custom code for item actions

I'm making a mod where you use a hammer and nails. I've made the nails block, and gone to "triggers" and "on block right clicked" (by the hammer) and made a procedure to fasten a nail. 

I've made the code for it but don't know how to do block code. Is there a way to not do block coding and write in your own? if i click on generated code viewer i cannot edit the code there.

while i'm here, here is the code i wrote. i mean for it to take the hammer three hits until the nail dissapears. i also want to make it so the nail goes in a third of the way into the wood after each hit but i've got no idea how to do that. so here's my shot. it would be awesome if you could "proofread" the code to check if it'll work.

 

public class NailItem extends Item { private static final int MAX_HITS = 3;

 private int hitsRemaining;

 public ConstructionNailItem(Settings settings) { super(settings); this.hitsRemaining = MAX_HITS;

 } @Override public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) { ItemStack itemStack = user.getStackInHand(hand);

 // Check if the player is holding a hammer if (user.getMainHandStack().getItem() instanceof HammerItem || user.getOffHandStack().getItem() instanceof HammerItem) { if (hitsRemaining > 0) { // Reduce hits remaining hitsRemaining--;

 // Check if this was the last hit if (hitsRemaining == 0) { // Nail is fully fastened world.playSound(null, user.getBlockPos(), SoundEvents.BLOCK_ANVIL_PLACE, SoundCategory.BLOCKS, 1.0f, 1.0f);

 // Optionally, perform additional actions here } else { // Nail is partially fastened world.playSound(null, user.getBlockPos(), SoundEvents.BLOCK_METAL_HIT, SoundCategory.BLOCKS, 1.0f, 1.0f);

 } // Update the item stack tooltip to show remaining hits itemStack.getOrCreateTag().putInt("HitsRemaining", hitsRemaining);

 // Return success return TypedActionResult.success(itemStack);

 } else { // The nail is already fully fastened return TypedActionResult.fail(itemStack);

 } } else { // Player is not holding a hammer return TypedActionResult.fail(itemStack);


 

Last seen on 16:14, 26. Mar 2024
Joined Feb 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
there's a 'Custom Code…
Fri, 03/22/2024 - 15:53

there's a 'Custom Code Snippet:' procedure block in the Advanced procedure menu If that's any help?