Updating Inventory Problems And Other Stuff

Discussion in 'Plugin Development' started by yewtree8, Apr 18, 2014.

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

    yewtree8

    So I have tried for god knows how long to fix this problem but it seems to be recuring. when a player right clicks the piston, it gives them the message, but doesn't take the ingots and doesn't give them the random item, but when i relog, it gives the items and doesn't take the ingots, i tried player.updateInventory() but that's deprived, can someone take a look at my code and help me fix this? I've tried returning it and all sorts but nothing seems to work.

    Thanks

    - Mat



    Code \/



    Code:java
    1. package me.mat.mysterybox;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6. import java.util.Random;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.PlayerInventory;
    19.  
    20. public class BoxListener implements Listener {
    21.  
    22. public String Prefix = ChatColor.RED + "[" + ChatColor.GOLD + "Mystery" + ChatColor.DARK_AQUA + "Box" + ChatColor.RED + "] ";
    23.  
    24.  
    25.  
    26.  
    27. Random random = new Random();
    28. List<Material> itemMaterials = Arrays.asList(Material.DIRT, Material.STONE, Material.DIAMOND);
    29.  
    30.  
    31.  
    32. @EventHandler
    33. public void BoxInteract(PlayerInteractEvent event) {
    34. Player player = event.getPlayer();
    35. Action action = event.getAction();
    36. Inventory inv = player.getInventory();
    37. PlayerInventory pi = player.getInventory();
    38. Material block = event.getClickedBlock().getType();
    39. if(action == Action.RIGHT_CLICK_BLOCK) {
    40. if(block == Material.PISTON_BASE) {
    41. if (player.getInventory().contains(Material.GOLD_INGOT, 35)) {
    42.  
    43. player.sendMessage(Prefix + ChatColor.RED + "Thanks For Ya Business Kid");
    44.  
    45. player.getInventory().removeItem(new ItemStack(Material.GOLD_INGOT, 35));
    46.  
    47.  
    48.  
    49. player.getInventory().addItem(new ItemStack(itemMaterials.get(this.random.nextInt(itemMaterials.size()))));
    50.  
    51. }
    52.  
    53.  
    54.  
    55. else if (!player.getInventory().contains(Material.GOLD_INGOT, 35)) {
    56.  
    57. player.sendMessage(Prefix + ChatColor.RED + "You Do Not Have 35 Gold Ingots");
    58.  
    59. }
    60.  
    61. } return;
    62.  
    63. } return;
    64.  
    65. }
    66.  
    67. }
    68.  
     
  2. I would either use player.updateInventory() (who needs Bukkit deprecations when you can yolo) or make schedule a 5 tick delayed task that removes the item then adds the new item.

    Change "Player player = event.getPlayer()" into "final Player player = event.getPlayer()"
    Where you're adding the items, replace it with:

    Code:
    player.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
        public void run() {
            if (player != null && player.isOnline()) {
                player.getInventory().removeItem(new ItemStack(Material.GOLD_INGOT, 35));
                player.getInventory().addItem(new ItemStack(itemMaterials.get(random.nextInt(itemMaterials.size()))));
            }
        }
    }, 2L);
     
  3. Offline

    yewtree8


    KingFaris11


    how would i make a delayed task? just add them to an array list and run it using the scheduler? I'm not too good with that could you show me how i would do that in my code?



    OMG I YOLO'd it and it worked :DDD I cannot thank you enough, I am so happy right now :)))))

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Already did so. Edited.
     
  5. Offline

    yewtree8

    Tip Of The Day: When In Doubt, Yolo Bukkit Deprications.
     
Thread Status:
Not open for further replies.

Share This Page