Help with a funky glitch

Discussion in 'Plugin Development' started by justin_393, Apr 3, 2015.

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

    justin_393

    I'm trying to code a keepInventory item and I'm having some weird glitches. It works perfectly fine if you have multiple of the item in your inventory, but once you reach 1 it stops removing them and you keep 1 forever. The other bug is I made a custom crafting recipe for the item with CraftBook's built in /recp command, but once I make a recipe to use it it stops working. It keeps the display name and lore, but you don't keep your inventory. If you spawn in the item it works perfectly. I'm confused as to what is going on here. Here is my code.

    Spawning in the item:
    Show Spoiler
    Code:
    @Sub(permission = "minecraftplanetearth.admin", description = "Get Keep Inventory item", minArgs = 0, allowConsole = true)
        public void getKeepInv(CallInfo call) {
            Player p = call.getPlayer();
            String keepInv = "Keep Inventory";
    
            List<String> lores = new ArrayList<String>();
            lores.add("Keep this in your inventory");
            lores.add("To keep your items on death");
            lores.add("Will be removed on death");
    
            ItemStack kI = new ItemStack(Material.DIAMOND);
            ItemMeta im = kI.getItemMeta();
            im.setDisplayName(keepInv);
            im.setLore(lores);
            kI.setItemMeta(im);
            p.getInventory().addItem(kI);
        }


    Dying:
    Show Spoiler
    Code:
    @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            Player p = event.getEntity();
            UUID uuid = p.getUniqueId();
    
            if (event.getKeepInventory() == false) { 
                Inventory i = p.getInventory();
                for (ItemStack inven : i.getContents()) {
                    if (inven.getItemMeta().hasLore()
                            && inven.getItemMeta().hasDisplayName()) {
                        String name = inven.getItemMeta().getDisplayName();
                        List<String> lore = inven.getItemMeta().getLore();
                        if (lore.contains("Keep this in your inventory")
                                && ChatColor.stripColor(name).equals(
                                        "Keep Inventory")) {
                            event.getDrops().clear();
                            ItemStack item = inven;
                            item.setAmount(item.getAmount() - 1);
                            in.put(uuid, p.getInventory().getContents());
                            level.put(uuid, p.getLevel());
                            armor.put(uuid, p.getInventory().getArmorContents());
                            respawnPlayer.add(uuid);
    
                        }
                    }
                }
            }
    
        }


    And respawning
    Show Spoiler
    Code:
    @EventHandler
        public void onRespawn(PlayerRespawnEvent event) {
            Player p = event.getPlayer();
            UUID uuid = p.getUniqueId();
            if (respawnPlayer.contains(uuid)) {
    
                for (ItemStack item : in.get(uuid)) {
                    if (item != null) {
                        p.getInventory().addItem(item);
                    }
    
                }
    
                int l = level.get(uuid);
                p.setLevel(l);
    
                ItemStack[] ar = armor.get(uuid);
                p.getInventory().setArmorContents(ar);
                p.sendMessage(green
                        + "You kept your inventory due to your Keep Inventory item!");
                respawnPlayer.remove(uuid);
    
            }
        }
     
  2. Offline

    cnc4

    The first bug: you are using a setAmount wich probably wont delete your item, so check if the item amount is=1 and then just remove the item...
    For the secound bug, plz post your recipe code.
     
  3. Offline

    justin_393

    Like I said, I use craftbook. In game you can make a recipe in your inventory and put an item as the outcome. I did that and it works fine, gives me the item with the lore and name, but if I die with that one I lose everything.
     
  4. Offline

    cnc4

    Add syso to your code in die and spawn events and see where it stops
     
  5. Offline

    justin_393

    syso? I'm guessing you mean System.out ?
     
  6. Offline

    cnc4

    @justin_393

    yes, you can type syso and eclipse whould complete it to system.out.println();
     
  7. Offline

    justin_393

    Alright, I'll do that after I finish something else up.
     
  8. Offline

    nverdier

    @justin_393 Or use #getLogger() and then Logger#log(Level level, String msg)
     
Thread Status:
Not open for further replies.

Share This Page