Solved Dragon Kills Counter Plugin

Discussion in 'Plugin Development' started by Gonmarte, Aug 23, 2015.

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

    Gonmarte

    Im making a dragon kills counter plugin.

    When i make /dragonkills should show how many dragons i have killed. I was testing and i killed 1 dragon with my account and when i put the command it shows that i killed 1 dragon. But when i test the plugin in a account that hasnt killed a dragon it shows 1 dragon kill too. All the accounts[​IMG] has the same number of dragon kills.
    Can u help me guys?

    Here is my Code

    MAIN CLASS :
    Code:
    package me.gonmarte;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
      
        @Override
        public void onEnable() {
          
            getLogger().info("O Plugin foi ativado - by Gonmarte");
          
            new Listeners(this);
          
            this.getConfig().addDefault("DragonKills", 0);
          
            this.getConfig().options().copyDefaults(true);
          
            saveConfig();
          
        }
      
        @Override
        public void onDisable() {
          
            getLogger().info("O Plugin foi desativado - by Gonmarte");
            
            saveConfig();
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            Player player = (Player) sender;
          
            if (cmd.getName().equalsIgnoreCase("dragonkills") && sender instanceof Player) {
              
                if (args.length == 0) {
                  
                    player.sendMessage(ChatColor.WHITE + "Tu ja mataste " + this.getConfig().getInt("DragonKills") + ChatColor.DARK_PURPLE + " Ender Dragons!");
                  
                }else if(args[0].equalsIgnoreCase("help")) {
                      
                        player.sendMessage(ChatColor.BLACK + "--------------- "  + ChatColor.DARK_PURPLE + "DragonKills Help " + ChatColor.BLACK +  "---------------");
                      
                        player.sendMessage(ChatColor.WHITE + "/dragonkills" + ChatColor.BLACK +  " -> " + ChatColor.WHITE + "Dis quantos Dragões já mataste.")
                      
                        player.sendMessage(ChatColor.BLACK + "-----------------------------------------");
                      
                    }
                  
                }
              
            }
            return false;
          
        }
      
    }
    LISTENERS :
    Code:
    package me.gonmarte;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.EnderDragon;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    
    public class Listeners implements Listener{
      
        Main configBetter;
      
        public Listeners(Main plugin) {
          
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
          
            configBetter = plugin;
          
        }
    
        @EventHandler
      
        public void onDragonKill(EntityDeathEvent e) {
          
            Entity deadEntity = e.getEntity();
          
            Entity killer = e.getEntity().getKiller();
          
            if(deadEntity instanceof EnderDragon && killer instanceof Player) {
              
                Player player = (Player) killer;
              
                int killcount = configBetter.getConfig().getInt("DragonKills");
              
                configBetter.getConfig().set("DragonKills", killcount + 1);
              
                player.sendMessage(ChatColor.WHITE + "Boa! Acabaste De Matar um Dragão!" );
              
              
              
              
            }
          
        }
      
    }
     
    Last edited by a moderator: Apr 10, 2016
  2. @Gonmarte Well, you are saving it to a config under the path "DragonKills", no matter which player does the kill.
    You need to create a section inside the config for each player, so when a player joins, check if he already is in the config and if he isn't, add him to the config under the path <uuid>.dragonKills and set it to 0.

    So at the end it could look like this:
    Code:
    069a79f4-44e9-4726-a5be-fca90e38aaf5:
      dragonKills: 3
    61699b2e-d327-4a01-9f1e-0ea8c3f06bc6:
      dragonKills: 1
    bd346dd5-ac1c-427d-87e8-73bdd4bf3e13:
      dragonKills: 0
    player:
      dragonKills: 5
    .
    .
    .
     
    Gonmarte likes this.
  3. Offline

    mine-care

    First check, then cast.
     
    Gonmarte likes this.
  4. Offline

    Gonmarte

    @Lionhard

    Thank you so much i did it =)



    Ty i wont forget =)

    EDIT by Timtower: merged posts
     
    Last edited: Aug 26, 2015
    Lionhard and mine-care like this.
  5. Gonmarte likes this.
Thread Status:
Not open for further replies.

Share This Page