Im trying to get it when a player clicks on a item in their inventory with another item it does something how would I do this?
Specifically ItemStack getCurrentItem() Gets the ItemStack currently in the clicked slot. ItemStack getCursor() Gets the current ItemStack on the cursor.
Barinade Alright so I did that but when I click on the item the code doesn't work Code:java package me.online.Scraps; import org.bukkit.ChatColor;import org.bukkit.Material;import org.bukkit.entity.Player;import org.bukkit.event.Listener;import org.bukkit.event.inventory.InventoryClickEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener{ public void onEnable() { getServer().getPluginManager().registerEvents(this, this); } public void onClick(InventoryClickEvent event){ Player player = (Player) event.getWhoClicked(); ItemStack item = event.getCursor(); ItemStack item1 = event.getCurrentItem(); if(item.getType() == Material.LEATHER){ if(item1.getType() == Material.WOOD_SWORD){ int max = item1.getType().getMaxDurability(); int dura = item1.getDurability(); double perc = dura/max; double newdura = perc + .05; double fin = newdura * max; String i = item1.getType().toString(); item1.setDurability((short) fin); player.getInventory().remove(item); String mess = ChatColor.BOLD + "Scrap and " + ChatColor.BOLD + i + ChatColor.BOLD + " Combined!"; player.sendMessage(ChatColor.AQUA + mess); } } }}
You forgot the annotation @EventHandler Also, not sure how true this is, but I think setting a double to an integer divided by another integer will result in a whole number, you might need to cast double, not exactly sure Edit: I was right, here's an output Code: int x = 10; int y = 24; double z = x/y; System.out.println(z); z = (double) x / (double) y; System.out.println(z); Console Code: 0.0 0.4166666666666667
Theres a problem now i get both the success message and the unsuccess one and it doesnt take my item im trying to combine: Code:java package me.online.Scraps; 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.inventory.InventoryClickEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener{ public void onEnable() { getServer().getPluginManager().registerEvents(this, this); } @SuppressWarnings("deprecation") @EventHandler public void onClick(InventoryClickEvent event){ Player player = (Player) event.getWhoClicked(); ItemStack item = event.getCursor(); ItemStack item1 = event.getCurrentItem(); if(item.getType() == Material.LEATHER){ if(item1.getType() == Material.WOOD_SWORD){ double o = (double) item1.getDurability()/ (double) item1.getType().getMaxDurability(); if(o <= .95){ int max = item1.getType().getMaxDurability(); int dura = item1.getDurability(); double perc = (double) dura / (double) max; double newdura = perc - .05; double fin = newdura * max; String i = item1.getType().toString(); String l = i.replace("_", " "); String k = l.toLowerCase(); item1.setDurability((short) fin); item.setType(Material.AIR); String mess = ChatColor.BOLD + "Scrap and " + ChatColor.BOLD + k + ChatColor.BOLD + " Combined!"; player.sendMessage(ChatColor.GREEN + mess); event.setCursor(null); } String j = item1.getType().toString().toLowerCase().replace("_", " "); String g = ChatColor.BOLD + "Your " + ChatColor.BOLD + j + ChatColor.BOLD + " is repaired as much as possible!"; player.sendMessage(ChatColor.RED + g); } }}}
Barinade long / int = long (large) long / long = long (small) int / long = long (small) int / int = int double / int = double double / double = double long / double = double