package fi.gosu.mc.plugins.SleepDemocracy;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Created by Aikain on 7.5.2016.
 */
public class SleepDemocracy extends JavaPlugin implements Listener {
    private boolean SDEnable;
    private int SDPercent;

    @Override
    public void onEnable() {
        this.loadConfig();
        this.getServer().getPluginManager().registerEvents(this, this);
        this.getCommand("sdtoggle").setExecutor(this);
        this.getCommand("sdset").setExecutor(this);
        super.onEnable();
    }

    @Override
    public void onDisable() {
        super.onDisable();
    }

    public void loadConfig() {
        this.getConfig().options().copyDefaults(true);
        this.saveConfig();
        this.SDEnable = this.getConfig().getBoolean("SDEnable");
        this.SDPercent = this.getConfig().getInt("SDPercent");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        switch (cmd.getName()) {
            case "sdtoggle":
                if (sender.hasPermission("sleepdemocracy.sdtoggle")) {
                    if (args.length == 0) {
                        this.SDEnable = !this.SDEnable;
                    } else if (args.length == 1) {
                        if (args[0].equals("ON")) {
                            this.SDEnable = true;
                        } else if (args[1].equals("OFF")) {
                            this.SDEnable = false;
                        } else {
                            sender.sendMessage(cmd.getUsage());
                            return false;
                        }
                    } else {
                        sender.sendMessage(cmd.getUsage());
                        return false;
                    }
                    this.getConfig().set("SDEnable", Boolean.valueOf(this.SDEnable));
                    this.saveConfig();
                    sender.sendMessage((this.SDEnable ? "Enabled" : "Disabled") + " SleepDemocracy");
                    return true;
                }
                sender.sendMessage(cmd.getPermissionMessage());
                break;
            case "sdset":
                if (sender.hasPermission("sleepdemocracy.sdset")) {
                    if (args.length == 1) {
                        try {
                            int a = Integer.parseInt(args[0]);
                            if (a < 0 || a > 100) {
                                throw new Exception();
                            }
                            this.SDPercent = a;
                            this.getConfig().set("SDPercent", Integer.valueOf(a));
                            this.saveConfig();
                            sender.sendMessage("Set percent to " + args[0]);
                            return true;
                        } catch (Exception e) {
                            sender.sendMessage("Number invalid. Please enter a number 0 - 100.");
                        }
                    } else {
                        sender.sendMessage(cmd.getUsage());
                    }
                } else {
                    sender.sendMessage(cmd.getPermissionMessage());
                }
                break;
        }
        return false;
    }

    @EventHandler
    public void onBedEnter(PlayerBedEnterEvent event) {
        testSleeping();
    }

    @EventHandler
    public void onBedLeave(PlayerBedLeaveEvent event) {
        testSleeping();
    }

    @EventHandler
    public void onServerJoin(PlayerJoinEvent event) {
        testSleeping();
    }

    @EventHandler
    public void onServerLeave(PlayerQuitEvent event) {
        testSleeping();
    }

    @EventHandler
    public void onWorldChange(PlayerChangedWorldEvent event) {
        testSleeping();
    }

    private void testSleeping() {
        if (!this.SDEnable) return;
        Map<World, Integer> sleepers = new HashMap<>();
        for (Player player : Arrays.asList(Bukkit.getServer().getOnlinePlayers())) {
            sleepers.put(player.getWorld(), (sleepers.containsKey(player.getWorld()) ? sleepers.get(player.getWorld()) + 1 : 0));
        }
        for (Entry<World, Integer> entry : sleepers.entrySet()) {
            int currentPercent = 100 * entry.getValue() / entry.getKey().getPlayers().size();
            for (Player player : entry.getKey().getPlayers()) {
                player.sendMessage("Currently " + entry.getValue() + "% of " + entry.getKey().getName() + "'s players are sleeping out of " + this.SDPercent + "% needed.");
            }
            if (currentPercent > SDPercent) {
                entry.getKey().setTime(1000L);
            }
        }
    }
}

@@ -4241,127 +4241,114 @@
     
-Map%3CWorld, Integer%3E sleepers = new HashMap%3C%3E();%0A        for (Player player : Arrays.asList(Bukkit.getServer().getOnline
+for (World world : Bukkit.getWorlds()) %7B%0A            int i = 0;%0A            for (Player player : world.get
 Play
@@ -4353,17 +4353,16 @@
 ayers())
-)
  %7B%0A     
@@ -4372,124 +4372,40 @@
     
-sleepers.put(player.getWorld(), (sleepers.containsKey(player.getWorld()) ? sleepers.get(player.getWorld()) + 1 : 0))
+    if (player.isSleeping()) i++
 ;%0A  
@@ -4413,76 +4413,14 @@
     
- %7D%0A
      
-   for (Entry%3CWorld, Integer%3E entry : sleepers.entrySet()) %7B
+%7D
 %0A   
@@ -4459,41 +4459,17 @@
 0 * 
-entry.getValue() / entry.getKey()
+i / world
 .get
@@ -4519,30 +4519,21 @@
 layer : 
-entry.getKey()
+world
 .getPlay
@@ -4595,51 +4595,40 @@
 %22 + 
-entry.getValue() + %22%25 of %22 + entry.getKey()
+currentPercent + %22%25 of %22 + world
 .get
@@ -4783,22 +4783,13 @@
     
-entry.getKey()
+world
 .set

Fix getting online players v3