Im a little lost can anybody help?

Discussion in 'Plugin Development' started by 15987632, Mar 30, 2014.

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

    15987632

    Soo i wanted to make a plugin that when you right click obsidian with an empty buckit it gives you a lava bucket. I dont know how to make it if you only click an obsidian block and i dont know how to make it so the obsidian gets set to air after the right click. This is what i have come up with

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onClick(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. Player player = event.getPlayer();
    6. if (player.getItemInHand() != null) {
    7. if (player.getItemInHand() == Material.BUCKET) {
    8. int amount = player.getItemInHand().getAmount();
    9. int inv = player.getInventory().getSize();
    10. PlayerInventory inven = event.getPlayer().getInventory();
    11. if (amount == 1) {
    12. player.getItemInHand().setType(Material.LAVA_BUCKET);
    13. }
    14. if (amount > 1) {
    15. if (inv == 36) {
    16. event.getPlayer().sendMessage(ChatColor.DARK_RED + "Please open up an inventory slot.");
    17. }
    18.  
    19.  
    20.  
    21. if (inv < 36) {
    22. player.getItemInHand().setAmount(amount - 1);
    23. inven.addItem(Material.LAVA_BUCKET, 1)
    24. player.updateInventory();
    25.  
    26.  
    27. }
    28.  
    29.  
    30. }
    31.  
    32.  
    33. }
    34. }
    35. }
    36. }
     
  2. Offline

    Azubuso

    15987632
    To check if material being clicked is obsidian:
    Code:java
    1. if (event.getClickedBlock().getType() == Material.OBSIDIAN) {}
    2. // To set it to air once clicked
    3. event.getClickedBlock().setType(Material.AIR);
     
  3. Offline

    15987632

    Azubuso ok so now i have this

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onClick(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (event.getClickedBlock().getType() == Material.OBSIDIAN) {
    6.  
    7. Player player = event.getPlayer();
    8. if (player.getItemInHand() != null) {
    9. if (player.getItemInHand() == Material.BUCKET) {
    10. int amount = player.getItemInHand().getAmount();
    11. int inv = player.getInventory().getSize();
    12. PlayerInventory inven = event.getPlayer().getInventory();
    13. if (amount == 1) {
    14. player.getItemInHand().setType(Material.LAVA_BUCKET);
    15. event.getClickedBlock().setType(Material.AIR);
    16. }
    17. if (amount > 1) {
    18. if (inv == 36) {
    19. event.getPlayer().sendMessage(ChatColor.DARK_RED + "Please open up an inventory slot.");
    20. }
    21.  
    22.  
    23.  
    24. if (inv < 36) {
    25. player.getItemInHand().setAmount(amount - 1);
    26. inven.addItem(Material.LAVA_BUCKET, 1)
    27. player.updateInventory();
    28. event.getClickedBlock().setType(Material.AIR);
    29.  
    30.  
    31. }
    32.  
    33.  
    34. }
    35.  
    36.  
    37. }
    38. }
    39. }
    40.  
    41. }
    42. }


    eclipse gives me errors on lines 9 and 26
    and none of this works idk why
     
  4. Offline

    Fred12i12i

    fix line 9:
    Code:java
    1. int blockId = player.getItemInHand().getType().getId();
    2. if (blockId == 49){


    fix line 26:
    Code:java
    1. ItemStack lava = new ItemStack(Material.LAVA_BUCKET, 1);
    2. player.getInventory().addItem(lava);
     
  5. Offline

    15987632

    Fred12i12i the errors are gone but the plugin still does not work
     
  6. Offline

    Azubuso

    15987632
    At line 9 it needs to be:
    Code:java
    1. player.getItemInHand().getType() == Material.BUCKET
    2. // Not
    3. player.getItemInHand() == Material.BUCKET

    As for line 26 do what Fred12i12i wrote! ;)
     
  7. Offline

    Fred12i12i

    opps
    if(blockId == 49){
    should be
    if(blockId == 325){
     
  8. Offline

    15987632

    Azubuso Fred12i12i ok the plugin relatively works now when u have only one bucket in the stack and right click a block of obsidian it gives u a lava bucket but places the lava where you are looking as soon as you get the bucket. and when you have more than 1 bucket in a stack it just reads you the message Please open up an inventory slot.
    this is the current code

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onClick(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (event.getClickedBlock().getType() == Material.OBSIDIAN) {
    6.  
    7. Player player = event.getPlayer();
    8. if (player.getItemInHand() != null) {
    9. if (player.getItemInHand().getType() == Material.BUCKET) {
    10. int amount = player.getItemInHand().getAmount();
    11. int inv = player.getInventory().getSize();
    12. if (amount == 1) {
    13. player.getItemInHand().setType(Material.LAVA_BUCKET);
    14. event.getClickedBlock().setType(Material.AIR);
    15. }
    16. if (amount > 1) {
    17. if (inv == 36) {
    18. event.getPlayer().sendMessage(ChatColor.DARK_RED + "Please open up an inventory slot.");
    19. }
    20.  
    21.  
    22.  
    23. if (inv < 36) {
    24. player.getItemInHand().setAmount(amount - 1);
    25. ItemStack lava = new ItemStack(Material.LAVA_BUCKET, 1);
    26. player.getInventory().addItem(lava);
    27. player.updateInventory();
    28. event.getClickedBlock().setType(Material.AIR);
    29.  
    30.  
    31. }
    32.  
    33.  
    34. }
    35.  
    36.  
    37. }
    38. }
    39. }
    40.  
    41. }
    42. }




     
  9. Offline

    JBoss925

    You want to check if the inventory contains a certain amount of buckets rather than checking if they have an itemstack with just 1 bucket in it.
     
  10. Offline

    15987632

    JBoss925 why the inventory? I want to check how many are in the persons hand
     
  11. Offline

    JBoss925

    Ok get the slot they have highlighted then check that Itemstack to see if it contains 1 or more buckets. Then adjust the amount left accordingly.
     
  12. Offline

    15987632

    JBoss925 that's what I tried to do based on how many items a plæer had in a stack and based on if a players inv is full but I think I messed up the part based on if there inv was full
     
  13. Offline

    15987632

  14. Offline

    coasterman10

    It seems you are just getting the size of the inventory, which will always be 36 for a player. What you want to do is loop through the inventory and check that at least one item is null or material equals air.

    Code:java
    1. private static boolean hasOpenSlot(Inventory inv) {
    2. for (ItemStack stack : inv)
    3. if (stack == null)
    4. return true;
    5. else if (stack.getType() == Material.AIR)
    6. return true;
    7. return false;
    8. }
     
Thread Status:
Not open for further replies.

Share This Page