Odd Item Glitch.

Discussion in 'Plugin Development' started by HyrulesLegend, Jul 16, 2014.

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

    HyrulesLegend

    Ok.
    In my plugin, there's a GUI which gives the user a kit. The kit contains a Diamond Sword and the rest of the inventory full of soup. However, only the soup that lands on the hotbar actually works. (Fast Soup Plugin: http://dev.bukkit.org/bukkit-plugins/beastsoup/). We know it's not the soup plugin, as we tested with different soup plugins. Also, the soup problem only occurs when you receive the kit from the GUI.

    Code:
    Code:java
    1. package me.kitpvp.GUI;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import me.kitpvp.SonicKit.Main;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.inventory.InventoryClickEvent;
    15. import org.bukkit.inventory.Inventory;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.inventory.PlayerInventory;
    18. import org.bukkit.potion.PotionEffect;
    19.  
    20. public class PvPGUI implements Listener {
    21.  
    22. private Main plugin;
    23. public PvPGUI(Main plugin) {
    24. this.plugin = plugin;
    25. }
    26.  
    27. List<String> apvp = new ArrayList<String>();
    28.  
    29.  
    30. @EventHandler
    31. public void onGUISelect(InventoryClickEvent e) {
    32. Player p = (Player) e.getWhoClicked();
    33. if(e.isLeftClick()){
    34.  
    35. if(e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "PvP")){
    36.  
    37.  
    38. if(e.getCurrentItem().getType().equals(Material.DIAMOND_SWORD)){
    39.  
    40. Inventory inv = p.getInventory();
    41. if(plugin.kit.contains(p.getName())) {
    42. p.sendMessage(ChatColor.RED + "You can only use one kit per life!");
    43. } else {
    44.  
    45. plugin.kit.add(p.getName());
    46. inv.clear();
    47. //Remove Potion Effects
    48. for (PotionEffect effect : p.getActivePotionEffects())
    49. p.removePotionEffect(effect.getType());
    50.  
    51. // Send the player a message
    52. String pvp = plugin.getConfig().getString("pvpmsg");
    53. String prefixmsg = plugin.getConfig().getString("prefixmsg");
    54.  
    55. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + "" + prefixmsg + ChatColor.GOLD + "] " + ChatColor.GOLD + " " + pvp);
    56.  
    57. // List Items
    58. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    59.  
    60.  
    61.  
    62. // List armor
    63. ItemStack helm = new ItemStack(Material.IRON_HELMET);
    64. ItemStack chest = new ItemStack(Material.IRON_CHESTPLATE);
    65. ItemStack legs = new ItemStack(Material.IRON_LEGGINGS);
    66. ItemStack boots = new ItemStack(Material.IRON_BOOTS);
    67.  
    68. // Enchant any items
    69. sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    70.  
    71. // Add any potion effects
    72.  
    73. // Add items to inventory (weapons first.)
    74. inv.addItem(sword);
    75.  
    76. // List & add Soup (Make the "<=35>" less if you have more than 1 item)
    77.  
    78. ItemStack getSoup = new ItemStack(Material.MUSHROOM_SOUP, 1);
    79. for(int i=1; i <=35; i++)
    80. inv.addItem(getSoup);
    81. p.getOpenInventory().close();
    82.  
    83.  
    84.  
    85. //Put armor on player
    86. ((PlayerInventory) inv).setHelmet(helm);
    87. ((PlayerInventory)inv).setChestplate(chest);
    88. ((PlayerInventory)inv).setLeggings(legs);
    89. ((PlayerInventory)inv).setBoots(boots);
    90.  
    91. p.updateInventory();
    92.  
    93.  
    94. }
    95.  
    96. }
    97. }
    98. }
    99. }
    100. }


    Anyone have any ideas?

    Anyone got any advice?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    PurePlugins

  3. Offline

    HyrulesLegend

    Updated the code, still doesn't work. I'm not sure why it does this, It's as the soup is not actually there, cause when I hit the ground with the soup in hand, it goes away.
     
  4. Offline

    PurePlugins

    @HyrulesLegend Also can you put your code in Java format so I can read it easier xD

    @HyrulesLegend So when you click on the item in the inventory it will close the inventory and give you the sword and soups, but when a user is killed it only drops the items that are in his hotbar?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    HyrulesLegend likes this.
  5. Offline

    JBoss925

    It's because you're looping too many times. You're trying to add soup when there is no space and so it glitches for some odd reason. Loop with the other item(s) in mind.
     
    PurePlugins likes this.
  6. Offline

    PurePlugins

    JBoss925 I can't believe I missed that >.<
     
  7. Offline

    JBoss925

    Lol it's all good. I've missed worse. Like stack traces xP
     
  8. Offline

    HyrulesLegend

    JBoss925 PurePlugins
    Not sure what you're saying, what's wrong with the loop?
    EDIT: I just tested with my anti death drop off.. all the soup dropped from his inventory..
    EDIT #2 (xD): That exact loop and the soup in the kit works perfectly fine in all my other kits, just not anything in the GUI.
     
  9. Offline

    PurePlugins

    @HyrulesLegend Ok so with your loop, you are looping over all 35 slots in the inventory right? but you aren't taking into account the items that the user may have on him
     
  10. Offline

    HyrulesLegend

    Inventory is cleared before hand.
     
  11. Offline

    PurePlugins

    HyrulesLegend this will return the number of empty slots in the players inv, this is what you want to loop over :3
    Code:java
    1. public int getEmptySlots(Inventory inventory) {
    2. int i = 0;
    3. for (ItemStack is : inventory.getContents()) {
    4. if (is == null)
    5. continue;
    6. i++;
    7. }
    8. return i;
    9. }
     
  12. Offline

    HyrulesLegend

    Problem Solved! I created a separate method to give me the items and it worked. :p
     
Thread Status:
Not open for further replies.

Share This Page