Inventory Event

Discussion in 'Plugin Development' started by Onlineids, Mar 21, 2014.

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

    Onlineids

    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?
     
  2. Offline

    AtomSponge

  3. Offline

    Barinade

    Onlineids likes this.
  4. Offline

    Onlineids

    Barinade
    Alright so I did that but when I click on the item the code doesn't work

    Code:java
    1. package me.online.Scraps;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.inventory.InventoryClickEvent;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener{
    12.  
    13. public void onEnable() {
    14. getServer().getPluginManager().registerEvents(this, this);
    15. }
    16. public void onClick(InventoryClickEvent event){
    17. Player player = (Player) event.getWhoClicked();
    18. ItemStack item = event.getCursor();
    19. ItemStack item1 = event.getCurrentItem();
    20. if(item.getType() == Material.LEATHER){
    21. if(item1.getType() == Material.WOOD_SWORD){
    22. int max = item1.getType().getMaxDurability();
    23. int dura = item1.getDurability();
    24. double perc = dura/max;
    25. double newdura = perc + .05;
    26. double fin = newdura * max;
    27. String i = item1.getType().toString();
    28. item1.setDurability((short) fin);
    29. player.getInventory().remove(item);
    30. String mess = ChatColor.BOLD + "Scrap and " + ChatColor.BOLD + i + ChatColor.BOLD + " Combined!";
    31. player.sendMessage(ChatColor.AQUA + mess);
    32. }
    33. }
    34. }
    35. }
    36.  
     
  5. Offline

    Barinade

    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
     
    Mattkx4 and Onlineids like this.
  6. Offline

    Onlineids

    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
    1. package me.online.Scraps;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.inventory.InventoryClickEvent;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin implements Listener{
    13.  
    14. public void onEnable() {
    15. getServer().getPluginManager().registerEvents(this, this);
    16. }
    17. @SuppressWarnings("deprecation")
    18. @EventHandler
    19. public void onClick(InventoryClickEvent event){
    20. Player player = (Player) event.getWhoClicked();
    21. ItemStack item = event.getCursor();
    22. ItemStack item1 = event.getCurrentItem();
    23. if(item.getType() == Material.LEATHER){
    24. if(item1.getType() == Material.WOOD_SWORD){
    25. double o = (double) item1.getDurability()/ (double) item1.getType().getMaxDurability();
    26. if(o <= .95){
    27. int max = item1.getType().getMaxDurability();
    28. int dura = item1.getDurability();
    29. double perc = (double) dura / (double) max;
    30. double newdura = perc - .05;
    31. double fin = newdura * max;
    32. String i = item1.getType().toString();
    33. String l = i.replace("_", " ");
    34. String k = l.toLowerCase();
    35. item1.setDurability((short) fin);
    36. item.setType(Material.AIR);
    37. String mess = ChatColor.BOLD + "Scrap and " + ChatColor.BOLD + k + ChatColor.BOLD + " Combined!";
    38. player.sendMessage(ChatColor.GREEN + mess);
    39. event.setCursor(null);
    40.  
    41.  
    42. }
    43.  
    44. String j = item1.getType().toString().toLowerCase().replace("_", " ");
    45. String g = ChatColor.BOLD + "Your " + ChatColor.BOLD + j + ChatColor.BOLD + " is repaired as much as possible!";
    46. player.sendMessage(ChatColor.RED + g);
    47. }
    48. }
    49. }
    50. }
    51.  
    52.  
    53.  
    54.  
     
  7. Offline

    Barinade

    add "return;" after event.setCursor(null)
     
  8. Offline

    MRPS

    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
     
Thread Status:
Not open for further replies.

Share This Page