Okay, so I am concurrently making a soup core, in doing this I need to make it when you right click a soup you increase health by 2.5 hp... Code: Code: @EventHandler public void onConsume(PlayerInteractEvent event){ Player p = event.getPlayer(); if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) { if (p.getHealth() == 20) { event.setCancelled(false); return; } else { p.getInventory().getItemInHand().setType(Material.AIR); double health = p.getHealth(); p.setHealth(health + 2.5); return; } } }
Can you show us the whole class with this code in, and also does the plugin show up when you do /plugins EDIT: I tested your code, it could be improved a little, however it did work and gave me the 2.5 hearts.
Class: Code: package me.d0tjar.soups.listeners; import me.d0tjar.soups.main.main; 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; public class soupConsume implements Listener { private main plugin; public soupConsume(main pl){ plugin = pl; } @EventHandler public void onConsume(PlayerInteractEvent event){ Player p = event.getPlayer(); if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) { if (p.getHealth() == 20) { event.setCancelled(false); return; } else { p.getInventory().getItemInHand().setType(Material.AIR); double health = p.getHealth(); p.setHealth(health + 2.5); return; } } } }
Code: package me.d0tjar.soups.main; import me.d0tjar.soups.commands.*; import me.d0tjar.soups.commands.gamemodes.*; import me.d0tjar.soups.commands.staff.*; import me.d0tjar.soups.events.*; import me.d0tjar.soups.items.*; import me.d0tjar.soups.listeners.*; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; public class main extends JavaPlugin { private String Key = this.getConfig().getString("Server.License"); private String customerName = "null"; public void onEnable(){ ConsoleCommandSender m = Bukkit.getConsoleSender(); PluginManager pm = Bukkit.getServer().getPluginManager(); // Commands: getCommand("events").setExecutor(new commandEvents(this)); getCommand("fly").setExecutor(new commandFly()); getCommand("heal").setExecutor(new commandHeal()); // Gamemode commands: getCommand("gmc").setExecutor(new commandGmc()); getCommand("gma").setExecutor(new commandGma()); getCommand("gms").setExecutor(new commandGms()); // Staff Commands: getCommand("broadcast").setExecutor(new commandBroadcast(this)); getCommand("invsee").setExecutor(new commandInvsee()); getCommand("staff").setExecutor(new commandStaff(this)); getCommand("vanish").setExecutor(new commandVanish(this)); getCommand("discord").setExecutor(new commandDiscord(this)); // Server Info Commands: getCommand("motd").setExecutor(new commandMotd(this)); getCommand("soups").setExecutor(new commandSoups()); getCommand("store").setExecutor(new commandStore(this)); getCommand("teamspeak").setExecutor(new commandTeamspeak(this)); getCommand("twitter").setExecutor(new commandTwitter(this)); getCommand("website").setExecutor(new commandWebsite(this)); // Command Listeners: pm.registerEvents(new commandStaff(this), this); pm.registerEvents(new commandVanish(this), this); // Listeners pm.registerEvents(new items(this), this); pm.registerEvents(new eventMenu(this), this); pm.registerEvents(new eventKitMenu(this), this); pm.registerEvents(new joinMessage(this), this); pm.registerEvents(new flyChecker(this), this); pm.registerEvents(new antiHunger(this), this); pm.registerEvents(new staffAbilities(this), this); setupConfig(); if (this.checkLicense()) { m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Waking up §6Soups"); m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2License '§9" + this.getConfig().getString("Server.License") + "§2' is valid!"); m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Registered to: " + customerName); m.sendMessage(""); } else { m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Waking up §6Soups"); m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2License '§c" + this.getConfig().getString("Server.License") + "§2' is §4not §2valid!"); m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Disabling plugin..."); m.sendMessage(""); Bukkit.getPluginManager().disablePlugin(this); } } @Override public void onDisable() { ConsoleCommandSender m = Bukkit.getConsoleSender(); if (this.checkLicense()) { m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Powering down §6Soups"); m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Good bye " + customerName); } else { m.sendMessage(""); m.sendMessage("§8[§6Soups§8] §2Powering down §6Soups"); m.sendMessage(""); Bukkit.getPluginManager().disablePlugin(this); } } public void setupConfig(){ getConfig().options().copyDefaults(true); saveConfig(); } } Removed my method for License but yeah that's my main.
It seems you haven't registered your listener on your onEnable Try adding this: pm.registerEvents(new soupConsume(), this);
LMAO I actually thought It was there, I dead ass was sitting there like yeah I see it... My excuse = tired A new issue has dug its way into the equation... Code: @EventHandler public void onConsume(PlayerInteractEvent event){ Player p = event.getPlayer(); if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) { if (p.getHealth() == 20) { event.setCancelled(false); return; } else { p.getInventory().setItemInHand(new ItemStack(Material.AIR)); double health = p.getHealth(); p.setHealth(health + 2.5); return; } } } Doesn't remove the concurrent item in the hand. If I do p.getInventory().remove(p.getItemInHand()); It gets rid of ALL the soups in the inventory, therefore I am lost with what to do... EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Instead of using remove(), you should set the amount of item instead p.getInventory().getItemInHand().setAmount(p.getInventory().getItemInHand().getAmount() - 1);
Code: package me.d0tjar.soups.listeners; import me.d0tjar.soups.main.main; 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.inventory.ItemStack; public class soupConsume implements Listener { public soupConsume(main main){ } @EventHandler public void onConsume(PlayerInteractEvent event){ Player p = event.getPlayer(); if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) { if (p.getHealth() == 20) { event.setCancelled(false); return; } else { p.getInventory().setItemInHand(new ItemStack(Material.AIR)); double health = p.getHealth(); p.setHealth(health + 2.5); return; } } } }
I tried it, and it works for me, and I fixed the code a little. Does this work for you? Btw what version you running? Code: @EventHandler public void onConsume(PlayerInteractEvent event){ Player p = event.getPlayer(); Action a = event.getAction(); if(a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) { if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) { if (p.getHealth() == 20) { event.setCancelled(true); } else { p.getInventory().getItemInHand().setAmount((p.getItemInHand().getAmount() - 1)); double health = p.getHealth() + 2.5; if(health >= 20) { p.setHealth(20); } else p.setHealth(health); } } } }
Does the event get called? Maybe add like a broadcast message above and test right clicking to see if it works
@D0tJar, you registered the event in your main class? (Just checking) Change it to Code: p.getItemInHand().getType.equals(Material.MUSHROOM_SOUP) Makes all the difference in the world.
@D0tJar Call player#updateInventory(); after you take the item. If this doesn't work, you should try to create a new itemstack that is a copy of the current stack. ItemStack is = player#getItemInHand(); Then call is#setAmount(player#getItemInHand()#getAmount()-1); then set the players hand to that item. this code is purely theoretical and untested. I haven't played around with itemstacks for a while so it's possible that getAmount is not a valid method. But it should be named similar if not.