Solved Prevent Main Thread From Behing Halted while executed events

Discussion in 'Plugin Development' started by kameronn, Jan 7, 2017.

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

    kameronn

    No matter what i seem to do this happens I even tried running it async and that bearly helps
    I save data on death and it delays everything until it's finished
     
    Last edited: Jan 7, 2017
  2. Offline

    Zombie_Striker

    @kameronn
    There are only two things that cause lag: having to complete many processing operations and having a poor CPU. Post your code. Either you will need to reduce the amount of operations done in the event or you may need to get a better CPU.
     
  3. Offline

    kameronn

    @Zombie_Striker
    For example what happens is when a player dies, the messages that are suppossed to be sent are delayed and what also happens is other players cant do anything until the player death event is finished which takes a good 1-2 seconds

    Event thats delaying everything:
    HTML:
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            Bukkit.getScheduler().runTask(PotentiaMC.getInstance(), new Runnable() {
                @Override
                public void run() {
                    Player killed = event.getEntity();
                    PlayerData killeddata = datamanager.getData(killed.getUniqueId());
    
                    killed.getWorld().strikeLightningEffect(killed.getLocation());
    
                    killeddata.getPlayerData().set("stats.deaths", killeddata.getDeaths() + 1);
                    killeddata.getPlayerData().set("stats.killstreak", 0);
                    event.setDeathMessage("");
                    scoreboard.setScoreboard(killed, killeddata);
    
                    if (!(killed.getKiller() instanceof Player)) {
                        return;
                    }
    
                    if (killed.getKiller() instanceof Player) {
                        Player killer = killed.getKiller();
    
                        PlayerData killerdata = datamanager.getData(killer.getUniqueId());
                        double COIN_MULTIPLIER = 2.2;
    
                        killerdata.getPlayerData().set("stats.kills", killerdata.getKills() + 1);
                        killerdata.getPlayerData().set("stats.killstreak", killerdata.getKillstreak() + 1);
    
                        double pointsDropped = killeddata.getElo() * 0.025;
                        double newElo = killerdata.getElo() + pointsDropped;
    
                        double coinsRecieved = 100 * COIN_MULTIPLIER + pointsDropped;
    
                        killerdata.getPlayerData().set("stats.elo", newElo);
                        killerdata.getPlayerData().set("stats.balance", killerdata.getBalance() + coinsRecieved);
                        killeddata.getPlayerData().set("stats.elo", killeddata.getElo() - pointsDropped);
    
                        killed.sendMessage(Strings.PREFIX.getResult() + " " + ChatColor.GOLD + "");
    
                        killer.sendMessage(translate("&7You have gained &5" + pointsDropped
                                + " elo points &7for dropping &5" + killed.getName() + "."));
    
                        killer.sendMessage(translate("&7You now have &5" + killerdata.getElo() + " elo points &7and &5"
                                + killerdata.getBalance() + " credits."));
    
                        String path = killerdata.getPlayerData().getString("kits.current");
                        String playstyle = killerdata.getPlayerData().getString("kits.size." + path + ".playstyle");
                        String kitname = killerdata.getPlayerData().getString("kits.size." + path + ".name");
    
                        if (kitname.contains("&")) {
                            kitname = ChatColor.translateAlternateColorCodes('&', kitname);
                        }
    
                        if (!kitname.contains("&")) {
                            kitname = "&d" + kitname;
                        }
    
                        killed.sendMessage(translate("&7You were killed by &5" + killer.getName() + "."));
                        killed.sendMessage(translate("&7Using the the kit " + kitname + "."));
                        killed.sendMessage(translate("&7They were using the playstyle &5" + playstyle + "."));
                        killed.sendMessage(translate("&7They had &5" + playstyleLeft(playstyle, killer) + " &7left."));
                        killed.sendMessage(translate("&7You now have &5" + killeddata.getElo() + " elo points &7and &5"
                                + killeddata.getBalance() + " credits."));
    
                        killerdata.save();
                       
                        scoreboard.setScoreboard(killer, killerdata);
    
                    }
    
    
                }
            });
    
        }
    PlayerData class:
    HTML:
    package potentiamc.org.playerdata;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    import potentiamc.org.PotentiaMC;
    
    public class PlayerData {
    
        private File file;
        private YamlConfiguration configuration;
        private UUID uuid;
    
        private PotentiaMC core = PotentiaMC.getInstance();
    
        public PlayerData(UUID uuid) {
            this.file = new File(core.getDataFolder() + File.separator + "data", uuid.toString() + ".yml");
            this.uuid = uuid;
    
            this.configuration = YamlConfiguration.loadConfiguration(this.file);
    
            if (!file.exists()) {
                Bukkit.getScheduler().runTask(PotentiaMC.getInstance(), new Runnable() {
                    @Override
                    public void run() {
                        createFile();
                    }
    
                });
            }
        }
       
        public void reload() {
            Bukkit.getScheduler().runTask(PotentiaMC.getInstance(), new Runnable() {
                @Override
                public void run() {
                    configuration = YamlConfiguration.loadConfiguration(file);
                }
            });
        }
        public void createFile() {
            Bukkit.getScheduler().runTask(PotentiaMC.getInstance(), new Runnable() {
                @Override
                public void run() {
                    try {
                        file.createNewFile();
                        configuration = YamlConfiguration.loadConfiguration(file);
                        setDefaults();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        public
        void setDefaults() {
            configuration.createSection("kits");
    
            configuration.set("kits.size.kit1.name", "Kit #1");
            configuration.set("kits.size.kit1.playstyle", "souper");
            configuration.set("kits.size.kit1.abilitypoints", Double.valueOf(0));
    
            configuration.set("kits.current", "kit1");
    
            configuration.createSection("stats");
            configuration.set("stats.kills", Integer.valueOf(0));
            configuration.set("stats.deaths", Integer.valueOf(0));
            configuration.set("stats.killstreak", Integer.valueOf(0));
            configuration.set("stats.elo", Double.valueOf(1000));
            configuration.set("stats.balance", Integer.valueOf(0));
    
            configuration.createSection("abilities");
    
            configuration.createSection("crates");
    
            Random random = new Random();
    
            int min = 7;
            int max = 13;
    
            configuration.set("crates.chests", Integer.valueOf(random.nextInt(max - min + 1) + min));
    
            save();
        }
    
        public void save() {
            if (configuration != null && file != null) {
    
                Bukkit.getScheduler().runTask(PotentiaMC.getInstance(), new Runnable() {
                    @Override
                    public void run() {
                        try {
                            configuration.save(file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    
    PlayerData Cache Class:
    Code:
    package potentiamc.org.managers;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.UUID;
    
    import potentiamc.org.playerdata.PlayerData;
    
    public class DataManager {
    
        private static HashSet<PlayerData> data = new HashSet<>();
       
        public HashSet<PlayerData> getHashSet() {
            return data;
        }
    
        public static void addData(UUID uuid) {
            if (getData(uuid) == null) {
                PlayerData playerData = new PlayerData(uuid);
                data.add(playerData);
            }
    
        }
    
        public static PlayerData getData(UUID uuid) {       
            for(PlayerData playerdata : data) {
                if(playerdata.getUUID().equals(uuid)) {
                    return playerdata;
                }
            }
           
            return null;
        }
    
        public static boolean containsData(UUID uuid) {
            boolean contains = data.contains(uuid);
            return contains;
        }
    
        public static Collection<PlayerData> getAllData() {
            return data;
        }
       
        public static void removeData(UUID uuid) {
            if(containsData(uuid)) {
                data.remove(uuid);
            }
        }
    
    }
    
     
  4. Offline

    Zombie_Striker

    @kameronn
    How did you run the code asynchronously? Can you post what you did then?
     
  5. Offline

    kameronn

    In the playerdata class I changed the runTask to runTaskAsync and on the player death event i did some things for the same code.

    I also removed the runnables and the server is still freezing everytime someone times, or anything happens (pretty much when the data is loaded)

    EDIT: Nevermind did some tests and the reason the server is lagging is because of the scoreboard
     
    Zombie_Striker likes this.
  6. Offline

    Zombie_Striker

    @kameronn
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page