Hey guys Just a quick question. is there or is there a Player Eat Event? IF so pleaso post if not please post a workaround. THanks
This event is called if a entity is getting back health: EntityRegainHealthEvent This event is called, if the entities food level is changed: FoodLevelChangeEvent
Unfortunately, I don't think that you can get the food item used for FoodLevelChangeEvent. I think your best bet is to listen to PlayerInteractEvent and check the item in their hand to see if it is soup. If it is, remove the soup, add a bowl to their inventory, and give them health. A downside to this might be players could be able to instantly consume soup instead of having to go through the animation. However, I still believe that it is the best option you have.
This is what you have to do, alternatively you could calculate the amount if ticks it takes for the animation to complete and add a delayed task that heals them after the animation. The problem is just that it will still heal them even if they just tap the right button and dont complete the animation. But you could possibly add a check to see if the soupbowl is still in their inventory before you heal them, if it is, dont heal them, if its gone, heal them.
so... how would one check what item the player has in hand? :S this might be a realy noob question but i just started dealing with plugin (actualy java as a whole) like today so... yeah D: anyways i'v been trying to use player.getItemInHand(), which seems to be an ItemStack type of thing. how do i compare it with Material.APPLE for example? or realy, any kind of apple there is xD
Code: ItemStack item = player.getItemInHand(); if (item.getType == Material.APPLE){ //do stuff } I doubt that will work because obviously if the food has been eaten, there won't be anything left in his hand.
@EnvisionRed oh didn't know there was a .getType() for .getItemInHand()! nice thanks \o/ however it still doesn't work D: basicaly as i check the item he has in hand when he right-clicks (starts eating) he should still have the item in his hand right? because i want to get the time when he actualy finished eating it (number of that item in his inv decreases) so i trigger something (since there's no PlayerEatEvent)... okay so this is what i have so far: Code: public void onPlayerClick(PlayerInteractEntityEvent event) { Player player = event.getPlayer(); if(player.getItemInHand().getType() == Material.APPLE) { player.sendMessage("eating"); } } the "eating" is just for testing purposes. it doesn't show up so... something is wrong? EDIT: or perhaps i could simply test when his FoodLevel changes and test if it changed to something higher then what he had before? can that be done? would skip a lot of tests
I don't think PlayerInteractEntityEvent is what you need here, because he could right click on air or a block, which aren't entities. Code: public void onPlayerClick(PlayerInteractEvent event){ Player player = event.getPlayer(); if ((event.getAction() == RIGHT_CLICK_BLOCK) || (event.getAction() == RIGHT_CLICK_AIR)){ if (player.getItemInHand().getType() == MATERIAL.APPLE) { //do stuff } } }
EnvisionRed oh haha that makes sense xD only problem i get now is it not recognizing RIGHT_CLICK_BLOCK and RIGHT_CLICK_AIR do i need to import anything else that's not on this list?: Code: import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerMoveEvent;
@EnvisionRed right so what i have now is this: Code: public void onPlayerClick(PlayerInteractEvent event) { Player player = event.getPlayer(); if((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.RIGHT_CLICK_AIR)) { if(player.getItemInHand().getType() == Material.APPLE) { player.sendMessage("eating"); } } } however when i right click with an apple it doesn't send the test message. does the apple change to another kind entity or something like that when someone starts eating it? :S btw i must ask... does me putting u'r name at the beggining of the reply helps or... u know... annoy? i realy dunno cuz i'm new to actualy discussing plugins so maybe u guys don't use it that way? maybe i should stop that EDIT: lol nvm! i was missing the @EventHandler -.- idk what it does but fixed it thank you very much for your support \o/
No, it works the way you think it does. It also gives me an alert that I was tagged in a post so I know that someone has responded to me. Two questions for you: 1. Have you added the eventhandler tag? 2. Have you registered events from it in your main class?
@EnvisionRed right so ugh, if eventhandler tag is this -> @EventHandler so yeah i'v JUST added it and it now works xD but ugh... i have no idea what the second question is about hahaha btw, i was wandering, shouldn't Code: if(event.getAction() != null){} work instead of the big Code: if((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.RIGHT_CLICK_AIR)){} ?
No, because there's other actions that are possible, including leftclick events. You don't leftclick to eat food... About the registering as a listener part, in your onEnable add: Code: getServer().getPluginManager().registerEvents(new Nameofyoureventsclasshere, this);
@EnvisionRed oh yeah ofc well thanks again for all the support \o/ that's a new hope on my dev path. woot!
Just saying, i think if (player.getItemInHand().getType() == MATERIAL.APPLE) { should come first, simple because it would cause less lag (rule it out quicker, less IF statements)
I would have just suggested listening for a FoodLevelChangeEvent, then checking the item in the player's hand to see it was mushroom stew.
this is the code i used for one of my plugins a while ago for cookies. could be adapted for any other food although i overdid the timing a little :/ should probs be 90L. Code:java @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)public void onPlayerInteractEvent(PlayerInteractEvent e){//get all the relative values for comparationfinal int l = e.getPlayer().getFoodLevel();final Player p = e.getPlayer();final int a = p.getItemInHand().getAmount();ItemStack i = e.getPlayer().getItemInHand();if(i.getType().equals(Material.COOKIE)){//schedule a task to see if they have eaten the cookie(maybe the time could be a little faster idk)plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){public void run(){if(p.getFoodLevel() > l && p.getItemInHand().getAmount() < a){//do your shizzle}},100L);}}}
The FoodLevelChangeEvent triggers after you consume the item, so it's impossible to see which item you ate. I tried that and it's not working. This is my code: Code:java @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)public void onPlayerInteractEvent(PlayerInteractEvent e) {final Player p = e.getPlayer();final int l = p.getFoodLevel();final int a = p.getItemInHand().getAmount();final int s = p.getInventory().getHeldItemSlot();final ItemStack i = p.getItemInHand();p.sendMessage("test1");if(i.getType() == Material.PUMPKIN_PIE) {p.sendMessage("test2");plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {p.sendMessage("test3");if(p.getFoodLevel() > l && p.getItemInHand().getAmount() < a && s == p.getInventory().getHeldItemSlot()) {p.setFoodLevel(p.getFoodLevel() + 2);p.sendMessage("You have eaten an apple pie!");} else {p.sendMessage("You have not eaten an apple pie!");}}}, 100L);}} I put the test messages for debug purposes. The code doesn't make it to test3. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Anyway now there is an event for player eat: http://jd.bukkit.org/rb/apidocs/org/bukkit/event/player/PlayerItemConsumeEvent.html
TheChugBug Mate, I know The Gaming Grunts already said this, but I don't want a notification about a post I made two Christmases ago.