diff --git a/plugin.yml b/plugin.yml index cf07618..f019bd2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -4,6 +4,7 @@ main: mykyta.Harbor.Harbor description: Ahoy, matey! Harbor is a Spigot plugin that redefines how sleep works in your server, making it easier for all the online players to get in bed quick and skip through the night! author: Mykyta (TechToolbox) website: https://mykyta.tk +api-version: 1.13 commands: harbor: diff --git a/src/main/java/mykyta/Harbor/Commands/Sleeping.java b/src/main/java/mykyta/Harbor/Commands/Sleeping.java index b4d0d95..3ca66e4 100644 --- a/src/main/java/mykyta/Harbor/Commands/Sleeping.java +++ b/src/main/java/mykyta/Harbor/Commands/Sleeping.java @@ -15,10 +15,13 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; import mykyta.Harbor.Config; -import mykyta.Harbor.EncodingUtils; import mykyta.Harbor.Util; public class Sleeping implements CommandExecutor { + + private Inventory gui; + + @SuppressWarnings("deprecation") @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (sender instanceof Player) { @@ -26,19 +29,19 @@ public class Sleeping implements CommandExecutor { World world = player.getWorld(); Config config = new Config(); ArrayList sleeping = Util.sleeping.get(world); - int slots = (((sleeping.size()) % 9)) * 9; //FIXME bad brok bad - System.out.println(slots); - Inventory gui = Bukkit.createInventory(player, slots, config.getString("gui.sleeping")); + int slots = Math.min(54, ((sleeping.size() - 1) / 9 + 1) * 9); + gui = Bukkit.createInventory(player, slots, config.getString("gui.sleeping")); + if (sleeping.size() > 0) sleeping.forEach(p -> { - ItemStack item = new ItemStack(Material.LEGACY_SKULL_ITEM, 1, (short) 3); //FIXME deprecated - SkullMeta skull = (SkullMeta) item.getItemMeta(); - skull.setDisplayName(p.getName()); + ItemStack item = new ItemStack(Material.PLAYER_HEAD, 1); + SkullMeta meta = (SkullMeta) item.getItemMeta(); + meta.setDisplayName(ChatColor.GRAY + p.getName()); /*ArrayList lore = new ArrayList(); lore.add("Custom head"); skull.setLore(lore); */ - skull.setOwner(p.getName()); - item.setItemMeta(skull); + meta.setOwner(p.getName()); + item.setItemMeta(meta); gui.setItem(sleeping.indexOf(p), item); }); @@ -62,11 +65,6 @@ public class Sleeping implements CommandExecutor { else Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix") + "There aren't any currently sleeping players in &o" + args[0] + "&r.")); } } - /* - StringBuilder sb = new StringBuilder(); - ArrayList sleeping = Util.sleeping.get(w); - Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix"))); - */ } return true; } diff --git a/src/main/java/mykyta/Harbor/Config.java b/src/main/java/mykyta/Harbor/Config.java index b8ae263..28535f2 100644 --- a/src/main/java/mykyta/Harbor/Config.java +++ b/src/main/java/mykyta/Harbor/Config.java @@ -3,11 +3,15 @@ package mykyta.Harbor; import java.util.logging.Logger; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.ConsoleCommandSender; public class Config { private Logger log = Bukkit.getLogger(); - private String error = "An error occured while trying to read the configuration. The plugin may not function correctly as a result."; + private Config config = new Config(); + private String error = "An error occured while trying to read the configuration. Harbor may not function correctly as a result."; private static Harbor harbor; + private ConsoleCommandSender c = Bukkit.getServer().getConsoleSender(); /** * Sets main class instance for accessing configuration @@ -22,8 +26,8 @@ public class Config { * @param e Exception generated from reading configuration */ private void error(Exception e) { - log.severe(error); - if (Util.debug) System.err.println(e); + c.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix") + error)); + if (Util.debug) e.printStackTrace(); } /** diff --git a/src/main/java/mykyta/Harbor/EncodingUtils.java b/src/main/java/mykyta/Harbor/EncodingUtils.java deleted file mode 100644 index c062f19..0000000 --- a/src/main/java/mykyta/Harbor/EncodingUtils.java +++ /dev/null @@ -1,130 +0,0 @@ -package mykyta.Harbor; - -import java.nio.charset.Charset; - -import org.bukkit.ChatColor; - -/** - * Utility to hide data using encoding with ChatColor - * @see https://gist.github.com/filoghost/f53ecb7b014c40b66bdc - */ -public class EncodingUtils { - // String constants. - private static final String SEQUENCE_HEADER = "" + ChatColor.RESET + ChatColor.BOLD + ChatColor.RESET; - private static final String SEQUENCE_FOOTER = "" + ChatColor.RESET + ChatColor.STRIKETHROUGH + ChatColor.RESET; - - - public static String encodeString(String hiddenString) { - return quote(stringToColors(hiddenString)); - } - - public static boolean hasHiddenString(String input) { - if (input == null) return false; - - return input.indexOf(SEQUENCE_HEADER) > -1 && input.indexOf(SEQUENCE_FOOTER) > -1; - } - - public static String extractHiddenString(String input) { - return colorsToString(extract(input)); - } - - - public static String replaceHiddenString(String input, String hiddenString) { - if (input == null) return null; - - int start = input.indexOf(SEQUENCE_HEADER); - int end = input.indexOf(SEQUENCE_FOOTER); - - if (start < 0 || end < 0) { - return null; - } - - return input.substring(0, start + SEQUENCE_HEADER.length()) + stringToColors(hiddenString) + input.substring(end, input.length()); - } - - /** - * Internal stuff. - */ - private static String quote(String input) { - if (input == null) return null; - return SEQUENCE_HEADER + input + SEQUENCE_FOOTER; - } - - private static String extract(String input) { - if (input == null) return null; - - int start = input.indexOf(SEQUENCE_HEADER); - int end = input.indexOf(SEQUENCE_FOOTER); - - if (start < 0 || end < 0) { - return null; - } - - return input.substring(start + SEQUENCE_HEADER.length(), end); - } - - private static String stringToColors(String normal) { - if (normal == null) return null; - - byte[] bytes = normal.getBytes(Charset.forName("UTF-8")); - char[] chars = new char[bytes.length * 4]; - - for (int i = 0; i < bytes.length; i++) { - char[] hex = byteToHex(bytes[i]); - chars[i * 4] = ChatColor.COLOR_CHAR; - chars[i * 4 + 1] = hex[0]; - chars[i * 4 + 2] = ChatColor.COLOR_CHAR; - chars[i * 4 + 3] = hex[1]; - } - - return new String(chars); - } - - private static String colorsToString(String colors) { - if (colors == null) return null; - - colors = colors.toLowerCase().replace("" + ChatColor.COLOR_CHAR, ""); - - if (colors.length() % 2 != 0) { - colors = colors.substring(0, (colors.length() / 2) * 2); - } - - char[] chars = colors.toCharArray(); - byte[] bytes = new byte[chars.length / 2]; - - for (int i = 0; i < chars.length; i += 2) { - bytes[i / 2] = hexToByte(chars[i], chars[i + 1]); - } - - return new String(bytes, Charset.forName("UTF-8")); - } - - private static int hexToUnsignedInt(char c) { - if (c >= '0' && c <= '9') { - return c - 48; - } else if (c >= 'a' && c <= 'f') { - return c - 87; - } else { - throw new IllegalArgumentException("Invalid hex char: out of range"); - } - } - - private static char unsignedIntToHex(int i) { - if (i >= 0 && i <= 9) { - return (char) (i + 48); - } else if (i >= 10 && i <= 15) { - return (char) (i + 87); - } else { - throw new IllegalArgumentException("Invalid hex int: out of range"); - } - } - - private static byte hexToByte(char hex1, char hex0) { - return (byte) (((hexToUnsignedInt(hex1) << 4) | hexToUnsignedInt(hex0)) + Byte.MIN_VALUE); - } - - private static char[] byteToHex(byte b) { - int unsignedByte = (int) b - Byte.MIN_VALUE; - return new char[]{unsignedIntToHex((unsignedByte >> 4) & 0xf), unsignedIntToHex(unsignedByte & 0xf)}; - } -} \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/Events/BedEnter.java b/src/main/java/mykyta/Harbor/Events/BedEnter.java index b61295f..b27fa1d 100644 --- a/src/main/java/mykyta/Harbor/Events/BedEnter.java +++ b/src/main/java/mykyta/Harbor/Events/BedEnter.java @@ -30,26 +30,26 @@ public class BedEnter implements Listener { } if (event.getBedEnterResult() == BedEnterResult.OK) { - World w = event.getPlayer().getWorld(); - ArrayList included = util.getIncluded(w); - int excluded = w.getPlayers().size() - included.size(); + Player p = event.getPlayer(); + World w = p.getWorld(); + ArrayList excluded = util.getExcluded(w); - if (included.contains(event.getPlayer())) { - util.add(w, event.getPlayer()); + if (!excluded.contains(p)) { + util.add(w, p); // Chat messages if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.sleeping").length() != 0)) { Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.sleeping") .replace("[sleeping]", String.valueOf(util.getSleeping(w)))) - .replace("[online]", String.valueOf(included.size())) - .replace("[player]", event.getPlayer().getName()) - .replace("[needed]", String.valueOf(util.getNeeded(w) - excluded))); + .replace("[online]", String.valueOf(util.getOnline(w) - excluded.size())) + .replace("[player]", p.getName()) + .replace("[needed]", String.valueOf(util.getNeeded(w) - excluded.size()))); } // Skip night if possible - util.skip(w, excluded, util.getNeeded(w)); + util.skip(w); } - else if (config.getString("messages.chat.bypass").length() != 0) event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass"))); + else if (config.getString("messages.chat.bypass").length() != 0) p.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.bypass"))); } } } \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/Events/BedLeave.java b/src/main/java/mykyta/Harbor/Events/BedLeave.java index b306143..af4768c 100644 --- a/src/main/java/mykyta/Harbor/Events/BedLeave.java +++ b/src/main/java/mykyta/Harbor/Events/BedLeave.java @@ -19,23 +19,23 @@ public class BedLeave implements Listener { public void onPlayerBedLeave(PlayerBedLeaveEvent event) { Util util = new Util(); Config config = new Config(); - World w = event.getPlayer().getWorld(); - - ArrayList included = util.getIncluded(w); - int excluded = w.getPlayers().size() - included.size(); + Player p = event.getPlayer(); + World w = p.getWorld(); + + ArrayList excluded = util.getExcluded(w); // Decrement the sleeping count if player isn't excluded - if (included.contains(event.getPlayer())) { - util.remove(w, event.getPlayer()); + if (!excluded.contains(p)) { + util.remove(w, p); } // Send a chat message when player gets out of bed (if it's not day) - if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.left").length() != 0) && !(w.getTime() > 0 && w.getTime() < 12300) && included.contains(event.getPlayer())) { + if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.left").length() != 0) && !(w.getTime() > 0 && w.getTime() < 12300) && !excluded.contains(p)) { Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.left") .replace("[sleeping]", String.valueOf(util.getSleeping(w)))) - .replace("[online]", String.valueOf(included.size())) + .replace("[online]", String.valueOf(util.getOnline(w) - excluded.size())) .replace("[player]", event.getPlayer().getName()) - .replace("[needed]", String.valueOf(util.getNeeded(w) - excluded))); + .replace("[needed]", String.valueOf(util.getNeeded(w) - excluded.size()))); } } } \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/Events/GUIClick.java b/src/main/java/mykyta/Harbor/Events/GUIClick.java index 76e3168..0182cb0 100644 --- a/src/main/java/mykyta/Harbor/Events/GUIClick.java +++ b/src/main/java/mykyta/Harbor/Events/GUIClick.java @@ -3,33 +3,18 @@ package mykyta.Harbor.Events; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import mykyta.Harbor.EncodingUtils; -import mykyta.Harbor.GUI; -import mykyta.Harbor.Util; -import mykyta.Harbor.GUI.GUIType; +import mykyta.Harbor.Config; public class GUIClick implements Listener { + @SuppressWarnings("deprecation") @EventHandler public void onGUIClick(InventoryClickEvent event) { + Config config = new Config(); String title = event.getInventory().getName(); - if (EncodingUtils.hasHiddenString(title)) { - // Extract hidden data - String raw = EncodingUtils.extractHiddenString(title); - JSONParser parser = new JSONParser(); - JSONObject json = new JSONObject(); - try {json = (JSONObject) parser.parse(raw);} - catch (Exception e) {if (Util.debug) e.printStackTrace();} - String type = json.get("GUIType").toString(); - - System.out.println("type"); - if (type.equals("SLEEPING")) { - event.setCancelled(true); - return; - } - } - else System.out.println("asdfasd");; + + if (title.equals(config.getString("gui.sleeping"))) { + event.setCancelled(true); + } } } \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/Events/PlayerLeave.java b/src/main/java/mykyta/Harbor/Events/PlayerLeave.java index 02681cc..9c9dd35 100644 --- a/src/main/java/mykyta/Harbor/Events/PlayerLeave.java +++ b/src/main/java/mykyta/Harbor/Events/PlayerLeave.java @@ -1,6 +1,7 @@ package mykyta.Harbor.Events; import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; @@ -11,9 +12,18 @@ public class PlayerLeave implements Listener { @EventHandler public void onPlayerLeave(PlayerQuitEvent event) { Util util = new Util(); - World w = event.getPlayer().getWorld(); - int excluded = (w.getPlayers().size() - 1) - (util.getIncluded(w).size() - 1); - util.skip(event.getPlayer().getWorld(), excluded, util.getNeededDecremented(w)); - Util.activity.remove(event.getPlayer()); + Player p = event.getPlayer(); + World w = p.getWorld(); + + new java.util.Timer().schedule( + new java.util.TimerTask() { + @Override + public void run() { + if (w.getPlayers().size() > 0 && Math.max(0, util.getNeeded(w) - util.getExcluded(w).size()) == 0) util.skip(w); + Util.activity.remove(p); + } + }, + 1000 + ); } } \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/GUI.java b/src/main/java/mykyta/Harbor/GUI.java deleted file mode 100644 index 6155327..0000000 --- a/src/main/java/mykyta/Harbor/GUI.java +++ /dev/null @@ -1,43 +0,0 @@ -package mykyta.Harbor; - -import org.bukkit.Bukkit; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -/** - * A custom Inventory wrapper for Harbor GUIs - * @see https://www.spigotmc.org/threads/custom-inventory-types.150414/ - */ -public class GUI { - - //TODO ADD JAVADOC COMMENTS ALL OVER THIS FILE - - public enum GUIType { - SLEEPING, BLACKLIST - } - - private final Inventory inventory; - private final GUIType type; - - public GUI(Inventory inventory, GUIType type) { - this.inventory = inventory; - this.type = type; - } - - public GUI(int size, String name, GUIType type) { - this.inventory = Bukkit.createInventory(null, size, name); - this.type = type; - } - - public Inventory getInventory() { - return this.inventory; - } - - public GUIType getType() { - return this.type; - } - - public void setItem(int slot, ItemStack itemstack) { - this.inventory.setItem(slot, itemstack); - } -} \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/GUIType.java b/src/main/java/mykyta/Harbor/GUIType.java deleted file mode 100644 index fad3dd2..0000000 --- a/src/main/java/mykyta/Harbor/GUIType.java +++ /dev/null @@ -1,5 +0,0 @@ -package mykyta.Harbor; - -enum GUIType { - MENU, SETTINGS; -} \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/Harbor.java b/src/main/java/mykyta/Harbor/Harbor.java index ee3272b..736dcda 100644 --- a/src/main/java/mykyta/Harbor/Harbor.java +++ b/src/main/java/mykyta/Harbor/Harbor.java @@ -21,9 +21,6 @@ import mykyta.Harbor.Events.PlayerLeave; import mykyta.Harbor.Events.Spawn; public class Harbor extends JavaPlugin { - private Logger log = Bukkit.getLogger(); - private Updater updater = new Updater(); - public void onEnable() { Config config = new Config(); Util util = new Util(); @@ -40,26 +37,17 @@ public class Harbor extends JavaPlugin { Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this); Bukkit.getPluginManager().registerEvents(new PlayerLeave(), this); util.setupNMS(); - - // Enable debugging if set in configuration - if (this.getConfig().getBoolean("debug")) Util.debug = true; - - // Start timer task Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Task(), 0L, config.getInteger("values.clock") * 20); - - // Check for updates + if (this.getConfig().getBoolean("debug")) Util.debug = true; if (this.getConfig().getBoolean("features.notifier")) { if (Util.debug) Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("messages.miscellaneous.prefix")) + "Checking for new updates..."); + Updater updater = new Updater(); updater.check(); } - - // FIXME Initialize HashMap items for every world for (World w : Bukkit.getServer().getWorlds()) { ArrayList sleeping = new ArrayList(); Util.sleeping.put(w, sleeping); } - - //FIXME temp add players to the count on realad Bukkit.getServer().getWorlds().forEach(w -> { w.getPlayers().forEach(p -> { Util.activity.put(p, System.currentTimeMillis()); diff --git a/src/main/java/mykyta/Harbor/Task.java b/src/main/java/mykyta/Harbor/Task.java index eded16a..fdd8871 100644 --- a/src/main/java/mykyta/Harbor/Task.java +++ b/src/main/java/mykyta/Harbor/Task.java @@ -1,9 +1,9 @@ package mykyta.Harbor; -import java.util.ArrayList; +import java.util.concurrent.TimeUnit; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; +import org.bukkit.ChatColor; public class Task implements Runnable { @@ -13,32 +13,19 @@ public class Task implements Runnable { @Override public void run() { Bukkit.getServer().getWorlds().forEach(w -> { - // Display a title if it's time to sleep if (w.getTime() >= 12516 && w.getTime() <= 12547) w.getPlayers().forEach(p -> { util.sendTitle(p, config.getString("messages.title.evening.top"), config.getString("messages.title.evening.bottom")); }); - - // Fix time if players leave bed naturally - /*if (w.getTime() >= 0 && w.getTime() <= 100) { - ArrayList list = new ArrayList(); - Util.sleeping.put(w, list); - }*/ - - // Send actionbar if someone's sleeping + if (util.getSleeping(w) > 0 && Math.max(0, util.getNeeded(w) - util.getExcluded(w).size()) == 0) { + util.skip(w); + } if (util.getSleeping(w) > 0 && util.getSleeping(w) < w.getPlayers().size()) {w.getPlayers().forEach(p -> {util.sendActionbar(p, config.getString("messages.actionbar.sleeping"), w);});} else if (util.getSleeping(w) == w.getPlayers().size()) {w.getPlayers().forEach(p -> {util.sendActionbar(p, config.getString("messages.actionbar.everyone"), w);});} + /*if (TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - Util.activity.get(p)) > config.getInteger("values.timeout")) { + ///TODO custom afk prefix + p.setPlayerListName(ChatColor.GRAY + "[AFK] - " + ChatColor.RESET + p.getDisplayName()); + System.out.println(p.getName() + " is AFK."); + }*/ }); - - /* - // TODO if player hasnt moved for x minutes then put in afk - Bukkit.getServer().getWorlds().forEach(w -> { - w.getPlayers().forEach(p -> { - if (TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - Util.activity.get(p)) > config.getInteger("values.timeout")) { - ///TODO custom afk prefix - p.setPlayerListName(ChatColor.GRAY + "[AFK] - " + ChatColor.RESET + p.getDisplayName()); - System.out.println(p.getName() + " is AFK."); - } - }); - });*/ } } \ No newline at end of file diff --git a/src/main/java/mykyta/Harbor/Updater.java b/src/main/java/mykyta/Harbor/Updater.java index c3db968..bec4e09 100644 --- a/src/main/java/mykyta/Harbor/Updater.java +++ b/src/main/java/mykyta/Harbor/Updater.java @@ -12,6 +12,10 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.ConsoleCommandSender; + public class Updater { private String latest = ""; @@ -20,12 +24,15 @@ public class Updater { * @see https://spiget.org/ */ public boolean check() { + ConsoleCommandSender c = Bukkit.getServer().getConsoleSender(); + Util util = new Util(); + Config config = new Config(); + try { URL url = new URL("https://api.spiget.org/v2/resources/60088/versions"); URLConnection request = url.openConnection(); request.connect(); - Util util = new Util(); ArrayList releases = new ArrayList(); JsonParser parser = new JsonParser(); JsonElement element = parser.parse(new InputStreamReader((InputStream) request.getContent())); @@ -36,23 +43,22 @@ public class Updater { releases.add(id.get("name").getAsString()); } - // TODO Beautify console messages if (util.version.equals(releases.get(releases.size() - 1))) { - System.out.println("Running latest version."); + if (Util.debug) c.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix")) + "Currently running the latest version of Harbor."); return false; } else if (!releases.contains(util.version)) { - System.out.println("Hmm... looks like you're using some sort of time travel technology. Welp, at least you don't have updates to worry about any time soon."); + if (config.getBoolean("features.notifier")) c.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix")) + "You have a version of Harbor that is newer than the latest available version. Great work!"); return false; } else { latest = releases.get(releases.size() - 1); - System.out.println("Running an outdated version! Latest is " + latest); + if (config.getBoolean("features.notifier")) c.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix")) + "Currently running an outdated version of Harbor. The latest available release is version " + latest + "."); return true; } } catch (IOException e) { - System.out.println("Failed to check for updates."); + if (Util.debug) c.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix")) + "Failed to check for updated releases of Harbor."); return false; } } diff --git a/src/main/java/mykyta/Harbor/Util.java b/src/main/java/mykyta/Harbor/Util.java index 69115af..6aee099 100644 --- a/src/main/java/mykyta/Harbor/Util.java +++ b/src/main/java/mykyta/Harbor/Util.java @@ -18,7 +18,8 @@ public class Util { public static HashMap> sleeping = new HashMap>(); public static HashMap activity = new HashMap(); public static ArrayList afk = new ArrayList(); - public String version = "1.5"; + + public String version = "1.1"; public static boolean debug = false; private Logger log = Bukkit.getLogger(); private static NMS nms; @@ -31,7 +32,7 @@ public class Util { String version = ""; try {version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];} catch (ArrayIndexOutOfBoundsException e) { - log.severe("Could not get server version. The plugin may not function correctly as a result."); + Bukkit.getServer().getConsoleSender().sendMessage(config.getString("messages.miscellaneous.prefix") + "Could not get server version. The plugin may not function correctly as a result."); if (Util.debug) System.err.println(e); } Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.prefix") + config.getString("messages.miscellaneous.running").replace("[version]", version))); @@ -58,58 +59,64 @@ public class Util { public void remove(World world, Player player) { ArrayList list = Util.sleeping.get(world); list.remove(player); - Util.sleeping.put(world, list); } /** * Fetch the sleeping count for a world * @param world World to fetch count for */ - public int getSleeping(World world) { - return Util.sleeping.get(world).size(); + public int getSleeping(World w) { + return Util.sleeping.get(w).size(); } /** * Fetch the amount of players needed to skip night * @param world World to fetch count for */ - public int getNeeded(World world) { - return Math.max(0, (int) Math.ceil(world.getPlayers().size() * (config.getDouble("values.percent") / 100) - this.getSleeping(world))); + public int getNeeded(World w) { + //FIXME i think its broke + return Math.max(0, (int) Math.ceil(w.getPlayers().size() * (config.getDouble("values.percent") / 100) - this.getSleeping(w))); } /** - * Fetch the amount of players needed to skip night (minus one) - * @param world World to fetch count for + * Get players in a world (returns zero if negative) + * @param world World to check player count for */ - public int getNeededDecremented(World world) { - return Math.max(0, (int) Math.ceil((world.getPlayers().size() - 1) * (config.getDouble("values.percent") / 100) - this.getSleeping(world))); + public int getOnline(World w) { + return Math.max(0, w.getPlayers().size()); } - + /** - * Fetch the included players in a world + * Fetch the excluded players in a world * @param world World to fetch count for */ - public ArrayList getIncluded(World world) { - ArrayList players = new ArrayList(); - world.getPlayers().forEach(p -> { - if (this.isIncluded(p)) players.add(p); + public ArrayList getExcluded(World w) { + ArrayList excluded = new ArrayList(); + w.getPlayers().forEach(p -> { + if (this.isExcluded(p)) excluded.add(p); }); - return players; + return excluded; } - /** - * Returns true if player should be included in count + * Returns whether or not a player should be excluded from the sleep count * @param player Target player */ - public boolean isIncluded(Player p) { + public boolean isExcluded(Player p) { boolean state = true; - if (config.getBoolean("features.ignore")) if (p.getGameMode() == GameMode.SURVIVAL) state = true; else state = false; - if (config.getBoolean("features.bypass")) if (p.hasPermission("harbor.bypass")) state = false; else state = true; - if (TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - activity.get(p)) > config.getInteger("values.timeout")) state = false; + if (config.getBoolean("features.ignore")) if (p.getGameMode() == GameMode.SURVIVAL) state = false; else state = true; + if (config.getBoolean("features.bypass")) if (p.hasPermission("harbor.bypass")) state = true; else state = false; + if (TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - activity.get(p)) > config.getInteger("values.timeout")) state = true; return state; } + /** + * Returns if player is considered AFK + */ + public void isAFK(Player p) { + + } + /** * Sends an actionbar message to the given player * @param player Player to show actionbar to @@ -127,13 +134,12 @@ public class Util { * @param world World to fetch information for */ public void sendActionbar(Player p, String message, World w) { - ArrayList included = this.getIncluded(w); - int excluded = w.getPlayers().size() - included.size(); + ArrayList excluded = this.getExcluded(w); nms.sendActionbar(p, message .replace("[sleeping]", String.valueOf(this.getSleeping(w))) - .replace("[online]", String.valueOf(included.size())) - .replace("[needed]", String.valueOf(this.getNeeded(w) - excluded))); + .replace("[online]", String.valueOf(w.getPlayers().size() - excluded.size())) + .replace("[needed]", String.valueOf(this.getNeeded(w) - excluded.size()))); } /** @@ -158,9 +164,8 @@ public class Util { * Skips the night in the specified world (if possible) * @param World to return value for */ - public void skip(World w, int excluded, int needed) { - if (config.getBoolean("features.skip") && (needed - excluded) == 0) { - System.out.println("set time"); + public void skip(World w) { + if (config.getBoolean("features.skip") && this.getNeeded(w) - this.getExcluded(w).size() == 0) { w.setTime(1000L); // Set weather to clear @@ -172,7 +177,6 @@ public class Util { // Display messages if (config.getBoolean("messages.chat.chat") && (config.getString("messages.chat.skipped").length() != 0)) Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.chat.skipped"))); if (config.getBoolean("features.title")) { - System.out.println("sent message"); w.getPlayers().forEach(p -> { this.sendTitle(p, config.getString("messages.title.morning.top"), config.getString("messages.title.morning.bottom")); });