Problem with the Additem-Method

Discussion in 'Plugin Development' started by Marv1nYT, Aug 4, 2019.

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

    Marv1nYT

    I have a problem, the player gets 2 instead of 1 item and I don't know why. Here the code:
    Code:
    killer.getInventory().addItem(createItem(Material.ENDER_PEARL, 1, "§8● §c§lEnderperle"));
    Code:
        private static ItemStack createItem(Material mat, int anzahl, String name) {
            ItemStack i = new ItemStack(mat, anzahl);
            ItemMeta m = i.getItemMeta();
            m.setDisplayName(name);
            i.setItemMeta(m);
            return i;
        }
     
  2. Offline

    The_Xman249

    Where is killer.getInventory().... Being called?
     
  3. Offline

    Marv1nYT

    How do u mean that ?
     
  4. Offline

    The_Xman249

    Could you show the method where you call the killer.getInventory()....?
     
  5. Offline

    Marv1nYT

    Here:
    Code:
        @SuppressWarnings({ "deprecation" })
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            e.setDroppedExp(0);
            e.getDrops().clear();
            Player p = (Player) e.getEntity();
            Player killer = p.getKiller();
            @SuppressWarnings("unused")
            API_SQLCoins api = new API_SQLCoins();
           
    
            if(killer instanceof Player && killer != null) {
           
                e.setDeathMessage(null);
               
                if(!death.contains(p)) {
                    for(Player all : Bukkit.getOnlinePlayers()) {
                            all.sendMessage(Main.prefix + this.Rang + p.getName() + " §r§7wurde von " + this.RangKiller + killer.getName() + " §r§7getötet.");
                            killer.sendTitle("§8● §c§lKILL §8●", "§7Du hast §c§l+ 30 Coins §7und §c§l+ 1 Kill §7bekommen.");   
                                
                            killer.getInventory().addItem(createItem(Material.ENDER_PEARL, 1, "§8● §c§lEnderperle"));
                              
                            p.sendTitle("§8● §c§lTOD §8●", "§7Du hast §c§l- 15 Coins §7und §c§l+ 1 Tod §7bekommen.");
                    }
                    death.add(p);
                    API_SQLCoins.addCoins(killer.getUniqueId().toString(), 30);
                    API_SQLCoins.removeCoins(p.getUniqueId().toString(), 15);
                   
                    PlayerScoreboard.CONST.update(killer, (byte) 3);
                    PlayerScoreboard.CONST.update(p, (byte) 3);
                   
                    if(Main.isMySQLEnabled()) {
                        if(!MySQL.playerExists(p.getUniqueId().toString())) {
                            MySQL.createPlayer(p.getUniqueId().toString());
                        }
                        if(!MySQL.playerExists(killer.getUniqueId().toString())) {
                            MySQL.createPlayer(killer.getUniqueId().toString());
                        }
                        if(killer != null) {
                            Stats.addKill(killer.getUniqueId().toString());
                        }
                        Stats.addDeath(p.getUniqueId().toString());
                       
                    } else {
                       
                        String playeruuid = p.getUniqueId().toString();
                        String killeruuid = killer.getUniqueId().toString();
                       
                        if(!ConfigStats.isRegistered(playeruuid)) {
                            ConfigStats.register(playeruuid, p.getName());
                        }
                        if(!ConfigStats.isRegistered(killeruuid)) {
                            ConfigStats.register(killeruuid, killer.getName());
                        }
                        if(killer != null) {
                            ConfigStats.addKill(killeruuid);
                        }
                        ConfigStats.addDeath(playeruuid);
                    }
                   
                    Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
                       
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                           
                            death.remove(p);
                           
                        }
                    }, 20);
                }
               
                killer.setHealth(20);
               
                Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
                   
                    @Override
                    public void run() {
                       
                        p.spigot().respawn();
                       
                    }
                }, 2);
               
            } else {
               
                e.setDeathMessage(null);
               
                if(Main.isMySQLEnabled()) {
                   
                    Stats.addDeath(p.getUniqueId().toString());
                   
                } else {
                    ConfigStats.addDeath(p.getUniqueId().toString());
                }
               
               
                if(!death.contains(p)) {
                    for(Player all : Bukkit.getOnlinePlayers()) {
    
                         all.sendMessage(Main.prefix + "§c" + p.getName() + " §7ist gestorben.");
               
                    }
                    PlayerScoreboard.CONST.update(p, (byte) 3);
                    death.add(p);
                    API_SQLCoins.removeCoins(p.getUniqueId().toString(), 15);
                    p.sendTitle("§8● §c§lTOD §8●", "§7Du hast §c§l- 15 Coins §7und §c§l+ 1 Tod §7bekommen.");
                    Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
                       
                        @Override
                        public void run() {
                            death.remove(p);
                           
                        }
                    }, 20);
                }
               
                Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
                   
                    @Override
                    public void run() {
                       
                        p.spigot().respawn();
                       
                    }
                }, 2);
            }
        }
     
  6. Offline

    The_Xman249

    Are you sure you are not registering the PlayerDeathEvent twice? If you are using InteliJ you can press ctrl+shift+f and search for where you are registering the Listener.
     
  7. Offline

    Marv1nYT

    I'm sure. The PlayerDeathEvent was registered only once.
     
  8. Offline

    Machine Maker

    @Marv1nYT So it’s possible that the event is called twice. Try unregistering the event and test it. (Nothing should happen) then register it but only have it print a message to the console. If you get two messages, the event gets called twice when a player dies. Some events do that.
     
Thread Status:
Not open for further replies.

Share This Page