Make any item consumable

Discussion in 'Plugin Development' started by Zettelkasten, Aug 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Zettelkasten

    Hello,

    is there a way of making any item consumable? So, for example, I want players to be able to eat wheat.
    I know I can use PlayerInteractEvent, but is there a way of having the "eating animation" where you have to hold down right click to eat (like when you eat normal food)?

    I think this is impossible to do, but I hope you can prove me wrong! :)
     
  2. Offline

    FerusGrim

    No. The "holding to completion" visual effect is all client-side.

    There are ways to simulate the effect of eating, but nothing will get around the player not being able to "hold down" the item.

    If you don't mind the above, as I stated, not being possible, you can use this code. Keep in mind that this is production code, and a lot of the information inside of it won't work for you, and will have to be suited to match your needs. (It's also, admittedly, not my best work)

    Code:java
    1. private void doHeal(final PlayerInteractEvent event) {
    2. final Player player = event.getPlayer();
    3. final UUID uuid = player.getUniqueId();
    4. final ItemStack item = event.getItem();
    5. if (player.getHealth() < player.getMaxHealth() && !fCooldown.contains(uuid)) {
    6. player.playSound(player.getLocation(), Sound.EAT, 1.0F, 1.0F);
    7. ParticleEffect.displayIconCrack(player.getLocation(), item.getType().getId(), 1.0F, 1.0F, 1.0F, 1.0F, 15);
    8. final Integer toHeal = foods.get(item.getType());
    9. if (item.getAmount() == 1) {
    10. player.getInventory().removeItem(new ItemStack(item.getType(), 1));
    11. player.updateInventory();
    12. event.setCancelled(true);
    13. } else {
    14. item.setAmount(item.getAmount() - 1);
    15. }
    16. if (player.getHealth() >= player.getMaxHealth() - toHeal) {
    17. player.setHealth(player.getMaxHealth());
    18. } else {
    19. player.setHealth(player.getHealth() + toHeal);
    20. }
    21. fCooldown.add(uuid);
    22. new BukkitRunnable() {
    23.  
    24. @Override
    25. public void run() {
    26. player.playSound(player.getLocation(), Sound.BURP, 1.0F, 1.0F);
    27. new BukkitRunnable() {
    28.  
    29. @Override
    30. public void run() {
    31. fCooldown.remove(uuid);
    32. }
    33. }.runTaskLater(plugin, 10);
    34. }
    35. }.runTaskLater(plugin, 20);
    36. }
    37. }


    TheMintyMate
    Try
    Code:
    [syntax=java]
    [/syntax]
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
    TheMintyMate likes this.
  3. Offline

    TheMintyMate

    hmm, its not that, I think my page has messed up, gonna refresh (kind of a pain :p)

    Zettelkasten

    Well, this thread suggests sending an animation packet to the player. Otherwise i suggest you do something
    like this (may not look/sound effective - untested):
    Code:
    player.getWorld().playEffect(player.getLocation(), Effect.STEP_SOUND, BLOCK);
    /* change "BLOCK" to the id of the item/block of what is being consumed - use http://minecraft-ids.grahamedgecombe.com/ if unsure how to get id type block/item and id shows next to it*/
    player.getWorld().playSound(player.getLocation(), Sound.EAT, 5, 5);
    /* change Sound.EAT to whatever you choose - or even just leave it*/
    
    This may not look very effective, but it's an idea nevertheless, if packets are]
    not the way to go.
    Hope this helped,
    - Minty

    Edit: finally, :p Had to delete prevous post

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page