Optimization of this Plugin

Discussion in 'Spigot Plugin Development' started by TheWackyTV, Apr 23, 2017.

  1. Hi!

    I've developed this plugin a long time ago, and just watched some timings on it, and I think they can get better. I would like to see if some of you could take a look on the code, and see where I can optimize it, that would help me a lot, so I don't lag the server.

    Code (Text):
    public static HashMap<String, Long> comboStreak = new HashMap<>();
        public static HashMap<String, Integer> killStreaks = new HashMap<>();
        public static HashMap<String, Integer> streaks = new HashMap<>();
        public static HashMap<String, Integer> streakKills = new HashMap<>();
        public static HashMap<String, Integer> taskCounter = new HashMap<>();
        public static ArrayList<String> tasks = new ArrayList<>();

        JavaPlugin plugin = JavaPlugin.getPlugin(Core.class);

        @EventHandler
        public void kill(PlayerDeathEvent e) {
            Player killer = e.getEntity().getKiller();
            if(!streakKills.containsKey(killer.getName())) {
                streakKills.put(killer.getName(), 1);
                streaks.put(killer.getName(), 1);
                taskCounter.put(killer.getName(), 8);
            } else {
                streakKills.put(killer.getName(), streakKills.get(killer.getName()) + 1);
                taskCounter.put(killer.getName(), 8);
            }
            if(streakKills.get(killer.getName()) == 2) {
                killer.sendMessage("§8» §e§lDOUBLE KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 3) {
                killer.sendMessage("§8» §e§lTRIPLE KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 4) {
                killer.sendMessage("§8» §e§lQUAD KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 5) {
                killer.sendMessage("§8» §c§lFRENZY KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 6) {
                killer.sendMessage("§8» §c§lSUPER KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 7) {
                killer.sendMessage("§8» §c§lMEGA KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 8) {
                killer.sendMessage("§8» §c§lULTRA KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 9) {
                killer.sendMessage("§8» §c§lCHAIN KILL");
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 5, 5);
            } else if(streakKills.get(killer.getName()) == 10) {
                killer.sendMessage("§8» §4§lBRUTAL KILL");
                Manager.giveBrutal(killer);
                for(Player all : Bukkit.getOnlinePlayers()) {
                    all.playSound(all.getLocation(), Sound.ENDERDRAGON_GROWL, 3, 3);
                }
                Bukkit.getServer().broadcastMessage("§8[§3DarkStarPvP§8] §c" + killer.getName() + " §7har fået et §4§lBRUTAL KILL§7.");
            }
            if(!tasks.contains(killer.getName())) {
                tasks.add(killer.getName());
            }
        }

        @EventHandler
        public void killstreakEvent(PlayerDeathEvent e) {
            Player killer = e.getEntity().getKiller();

            // Resetting killstreak for dead player
            if(killStreaks.containsKey(e.getEntity().getName())) {
                killStreaks.remove(e.getEntity().getName());
            }



            if(!killStreaks.containsKey(killer.getName())) {
                killStreaks.put(killer.getName(), 1);
            } else {
                killStreaks.put(killer.getName(), killStreaks.get(killer.getName()) + 1);
                if(killStreaks.get(killer.getName()) == 5) {
                    killer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, 0));
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        all.sendMessage("§c" + killer.getName() + " §7har en killstreak på 5 kills!");
                        all.sendMessage("§8» §3Dusør tilføjet §7- §b75$");
                    }
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "consoledusor " + killer.getName() + " 75");
                } else if(killStreaks.get(killer.getName()) == 10) {
                    killer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, 0));
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        all.sendMessage("§c" + killer.getName() + " §7har en killstreak på 10 kills!");
                        all.sendMessage("§8» §3Dusør tilføjet §7- §b175$");
                    }
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "consoledusor " + killer.getName() + " 175");
                } else if(killStreaks.get(killer.getName()) == 15) {
                    killer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, 0));
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        all.sendMessage("§c" + killer.getName() + " §7har en killstreak på 15 kills!");
                        all.sendMessage("§8» §3Dusør tilføjet §7- §b250$");
                    }
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "consoledusor " + killer.getName() + " 250");
                } else if(killStreaks.get(killer.getName()) == 20) {
                    killer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, 0));
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        all.sendMessage("§c" + killer.getName() + " §7har en killstreak på 20 kills!");
                        all.sendMessage("§8» §3Dusør tilføjet §7- §b325$");
                    }
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "consoledusor " + killer.getName() + " 325");
                } else if(killStreaks.get(killer.getName()) == 25) {
                    killer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, 0));
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        all.sendMessage("§c" + killer.getName() + " §7har en killstreak på 25 kills!");
                        all.sendMessage("§8» §3Dusør tilføjet §7- §b400$");
                    }
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "consoledusor " + killer.getName() + " 400");
                } else if(killStreaks.get(killer.getName()) == 30) {
                    killer.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, 0));
                    for(Player all : Bukkit.getOnlinePlayers()) {
                        all.sendMessage("§c" + killer.getName() + " §7har en killstreak på 30 kills!");
                        all.sendMessage("§8» §3Dusør tilføjet §7- §b450$");
                    }
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "consoledusor " + killer.getName() + " 450");
                }
            }
        }

        @EventHandler
        public void leave(PlayerQuitEvent e) {
            killStreaks.remove(e.getPlayer().getName());
            comboStreak.remove(e.getPlayer().getName());
        }

    Code (Text):
    new BukkitRunnable() {
                @Override
                public void run() {
                    if (newKillstreak.taskCounter.size() > 0) {
                        for (String killerName : newKillstreak.streakKills.keySet()) { // line 33
                            if (newKillstreak.tasks.contains(killerName)) {
                                Player p = Bukkit.getPlayer(killerName);
                                if (p != null) {
                                    if (newKillstreak.taskCounter.get(p.getName()) > 0 && newKillstreak.taskCounter.containsKey(p.getName())) {
                                        newKillstreak.streaks.put(killerName, newKillstreak.streaks.get(killerName) + 1);
                                        newKillstreak.taskCounter.put(p.getName(), newKillstreak.taskCounter.get(p.getName()) - 1);
                                    } else {
                                        newKillstreak.streakKills.remove(killerName);
                                        newKillstreak.streaks.remove(killerName);
                                        newKillstreak.tasks.remove(killerName);
                                        newKillstreak.taskCounter.remove(p.getName());
                                    }
                                }
                            }
                        }
                    }
                }
            }.runTaskTimer(this, 20, 20);

    Thanks!
     
    • Funny Funny x 1
  2. Code (Java):
    for (String killerName : newKillstreak.streakKills.keySet()) { // line 33
                            if (newKillstreak.tasks.contains(killerName)) {
    This is unneeded. You're looping through a HashMap's keyset, and then check if the key is in the hashmap. Of course it is in there.
     
    • Informative Informative x 1
  3. Hi,

    I feel stupid for not seeing that line of code, I totally agree on your statement and I have no idea what I was thinking :)
     
    • Like Like x 1
  4. Remove that static? Make the variables private and with a getter?