diff --git a/Harbor/.classpath b/Harbor/.classpath index 39fab5f..745a1a7 100644 --- a/Harbor/.classpath +++ b/Harbor/.classpath @@ -2,6 +2,7 @@ + diff --git a/Harbor/config.yml b/Harbor/config.yml index 3c96d15..c99bffc 100644 --- a/Harbor/config.yml +++ b/Harbor/config.yml @@ -24,6 +24,8 @@ features: skipNight: true # Clear weather when skipping night clearWeather: true + # Toggle exclusion of operators/players with permission "harbor.bypass" from sleep count + bypass: false # Display debug information in console debug: false @@ -58,4 +60,4 @@ messages: permission: "&7Insufficient permissions." # Version identifier (do not change) -version: 1.4 +version: 1.4.2 diff --git a/Harbor/plugin.yml b/Harbor/plugin.yml index edf373f..c3d2f69 100644 --- a/Harbor/plugin.yml +++ b/Harbor/plugin.yml @@ -1,5 +1,5 @@ name: Harbor -version: 1.4 +version: 1.4.2 main: techtoolbox.Harbor.Main 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: TechToolbox diff --git a/Harbor/src/techtoolbox/Harbor/Listeners/BedEnterEvent.java b/Harbor/src/techtoolbox/Harbor/Listeners/BedEnterEvent.java index b1bd53f..9278ec3 100644 --- a/Harbor/src/techtoolbox/Harbor/Listeners/BedEnterEvent.java +++ b/Harbor/src/techtoolbox/Harbor/Listeners/BedEnterEvent.java @@ -4,56 +4,128 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerBedEnterEvent; +import org.bukkit.event.player.PlayerBedEnterEvent.BedEnterResult; import techtoolbox.Harbor.Main; public class BedEnterEvent implements Listener { - @EventHandler + + String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + + @EventHandler(priority = EventPriority.HIGH) public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) { - Main.bypassers.clear(); - for (Player player : Bukkit.getServer().getOnlinePlayers()) { - if (player.hasPermission("harbor.bypass")) { - Main.bypassers.add(player.getName()); - } - } - - if (!event.getPlayer().hasPermission("harbor.bypass")) { - Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf((Main.worlds.get(event.getPlayer().getWorld())).intValue() + 1)); - - // Chat messages - if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.sleeping").length() != 0)) { - Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.sleeping") - .replace("[sleeping]", String.valueOf(Main.worlds.get(event.getPlayer().getWorld()))) - .replace("[online]", String.valueOf(event.getPlayer().getWorld().getPlayers().size() - Main.bypassers.size())) - .replace("[player]", event.getPlayer().getName()) - .replace("[needed]", String.valueOf(Math.max(0, Math.round(event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size() - (Main.worlds.get(event.getPlayer().getWorld())).intValue())))))); - } + + // TEMPORARY SOLUTION FOR 1.13.2: CHECK IF BEDENTERRESULT WORKS, IF NOT THEN RUN 1.13.1 CODE + try { + if (event.getBedEnterResult() == BedEnterResult.OK) { + Main.bypassers.clear(); + if (Main.plugin.getConfig().getBoolean("features.bypass")) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + if (player.hasPermission("harbor.bypass")) { + Main.bypassers.add(player.getName()); + } + } + } + + if (!event.getPlayer().hasPermission("harbor.bypass")) { + try { + Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf((Main.worlds.get(event.getPlayer().getWorld())).intValue() + 1)); + } + catch (Exception exception) { + // Create value if it doesn't exist already + Main.worlds.put(event.getPlayer().getWorld(), 1); + } + + // Chat messages + if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.sleeping").length() != 0)) { + Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.sleeping") + .replace("[sleeping]", String.valueOf(Main.worlds.get(event.getPlayer().getWorld()))) + .replace("[online]", String.valueOf(event.getPlayer().getWorld().getPlayers().size() - Main.bypassers.size())) + .replace("[player]", event.getPlayer().getName()) + .replace("[needed]", String.valueOf(Math.max(0, Math.round(event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size() - (Main.worlds.get(event.getPlayer().getWorld())).intValue())))))); + } - // Skip night feature - if (Main.plugin.getConfig().getBoolean("features.skipNight") && ((Main.worlds.get(event.getPlayer().getWorld())).intValue() >= event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size()) && (event.getPlayer().getWorld().getPlayers().size() > 1)) { - Bukkit.getServer().getWorld(event.getPlayer().getWorld().getName()).setTime(1000L); - - // Weather clear feature - if (Main.plugin.getConfig().getBoolean("features.clearWeather")) { - event.getPlayer().getWorld().setStorm(false); - event.getPlayer().getWorld().setThundering(false); - } - - // Night skipped message - if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.skipped").length() != 0)) { - Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.skipped"))); - } + // Skip night feature + if (Main.plugin.getConfig().getBoolean("features.skipNight") && ((Main.worlds.get(event.getPlayer().getWorld())).intValue() >= event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size()) && (event.getPlayer().getWorld().getPlayers().size() > 1)) { + Bukkit.getServer().getWorld(event.getPlayer().getWorld().getName()).setTime(1000L); + + // Weather clear feature + if (Main.plugin.getConfig().getBoolean("features.clearWeather")) { + event.getPlayer().getWorld().setStorm(false); + event.getPlayer().getWorld().setThundering(false); + } + + // Night skipped message + if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.skipped").length() != 0)) { + Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.skipped"))); + } + } + } + else if (Main.plugin.getConfig().getString("messages.chat.bypass").length() != 0) { + event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.bypass"))); + } + + // Reset value if below zero (plugin glitch) + if ((Main.worlds.get(event.getPlayer().getWorld())).intValue() < 0) { + Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf(0)); + } } } - else if (Main.plugin.getConfig().getString("messages.chat.bypass").length() != 0) { - event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.bypass"))); - } - - // Reset value if below zero (plugin glitch) - if ((Main.worlds.get(event.getPlayer().getWorld())).intValue() < 0) { - Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf(0)); + catch (NoSuchMethodError e){ + Main.bypassers.clear(); + if (Main.plugin.getConfig().getBoolean("features.bypass")) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + if (player.hasPermission("harbor.bypass")) { + Main.bypassers.add(player.getName()); + } + } + } + + if (!event.getPlayer().hasPermission("harbor.bypass")) { + try { + Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf((Main.worlds.get(event.getPlayer().getWorld())).intValue() + 1)); + } + catch (Exception exception) { + // Create value if it doesn't exist already + Main.worlds.put(event.getPlayer().getWorld(), 1); + } + + // Chat messages + if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.sleeping").length() != 0)) { + Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.sleeping") + .replace("[sleeping]", String.valueOf(Main.worlds.get(event.getPlayer().getWorld()))) + .replace("[online]", String.valueOf(event.getPlayer().getWorld().getPlayers().size() - Main.bypassers.size())) + .replace("[player]", event.getPlayer().getName()) + .replace("[needed]", String.valueOf(Math.max(0, Math.round(event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size() - (Main.worlds.get(event.getPlayer().getWorld())).intValue())))))); + } + + // Skip night feature + if (Main.plugin.getConfig().getBoolean("features.skipNight") && ((Main.worlds.get(event.getPlayer().getWorld())).intValue() >= event.getPlayer().getWorld().getPlayers().size() * Float.parseFloat(Main.plugin.getConfig().getString("values.percent")) - Main.bypassers.size()) && (event.getPlayer().getWorld().getPlayers().size() > 1)) { + Bukkit.getServer().getWorld(event.getPlayer().getWorld().getName()).setTime(1000L); + + // Weather clear feature + if (Main.plugin.getConfig().getBoolean("features.clearWeather")) { + event.getPlayer().getWorld().setStorm(false); + event.getPlayer().getWorld().setThundering(false); + } + + // Night skipped message + if (Main.plugin.getConfig().getBoolean("messages.chat.chat") && (Main.plugin.getConfig().getString("messages.chat.skipped").length() != 0)) { + Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.skipped"))); + } + } + } + else if (Main.plugin.getConfig().getString("messages.chat.bypass").length() != 0) { + event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("messages.chat.bypass"))); + } + + // Reset value if below zero (plugin glitch) + if ((Main.worlds.get(event.getPlayer().getWorld())).intValue() < 0) { + Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf(0)); + } } } } diff --git a/Harbor/src/techtoolbox/Harbor/Listeners/BedLeaveEvent.java b/Harbor/src/techtoolbox/Harbor/Listeners/BedLeaveEvent.java index 4d2622b..bbbfe3d 100644 --- a/Harbor/src/techtoolbox/Harbor/Listeners/BedLeaveEvent.java +++ b/Harbor/src/techtoolbox/Harbor/Listeners/BedLeaveEvent.java @@ -3,20 +3,24 @@ package techtoolbox.Harbor.Listeners; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerBedLeaveEvent; import techtoolbox.Harbor.Main; public class BedLeaveEvent implements Listener { - @EventHandler + + @EventHandler(priority = EventPriority.HIGH) public void onPlayerBedLeave(PlayerBedLeaveEvent event) { Main.bypassers.clear(); - for (Player player : Bukkit.getServer().getOnlinePlayers()) { - if (player.hasPermission("harbor.bypass")) { - Main.bypassers.add(player.getName()); - } - } + if (Main.plugin.getConfig().getBoolean("features.bypass")) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + if (player.hasPermission("harbor.bypass")) { + Main.bypassers.add(player.getName()); + } + } + } if (!event.getPlayer().hasPermission("harbor.bypass")) { Main.worlds.put(event.getPlayer().getWorld(), Integer.valueOf((Main.worlds.get(event.getPlayer().getWorld())).intValue() - 1)); diff --git a/Harbor/src/techtoolbox/Harbor/Listeners/JoinEvent.java b/Harbor/src/techtoolbox/Harbor/Listeners/JoinEvent.java index 95d1047..cdc0e99 100644 --- a/Harbor/src/techtoolbox/Harbor/Listeners/JoinEvent.java +++ b/Harbor/src/techtoolbox/Harbor/Listeners/JoinEvent.java @@ -12,9 +12,11 @@ public class JoinEvent implements Listener { @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Main.bypassers.clear(); - for (Player player : Bukkit.getServer().getOnlinePlayers()) { - if (player.hasPermission("harbor.bypass")) { - Main.bypassers.add(player.getName()); + if (Main.plugin.getConfig().getBoolean("features.bypass")) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + if (player.hasPermission("harbor.bypass")) { + Main.bypassers.add(player.getName()); + } } } } diff --git a/Harbor/src/techtoolbox/Harbor/Listeners/QuitEvent.java b/Harbor/src/techtoolbox/Harbor/Listeners/QuitEvent.java index e94c794..630c915 100644 --- a/Harbor/src/techtoolbox/Harbor/Listeners/QuitEvent.java +++ b/Harbor/src/techtoolbox/Harbor/Listeners/QuitEvent.java @@ -12,9 +12,11 @@ public class QuitEvent implements Listener{ @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { Main.bypassers.clear(); - for (Player player : Bukkit.getServer().getOnlinePlayers()) { - if (player.hasPermission("harbor.bypass")) { - Main.bypassers.add(player.getName()); + if (Main.plugin.getConfig().getBoolean("features.bypass")) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + if (player.hasPermission("harbor.bypass")) { + Main.bypassers.add(player.getName()); + } } } } diff --git a/Harbor/src/techtoolbox/Harbor/Main.java b/Harbor/src/techtoolbox/Harbor/Main.java index 94b4f84..47354a2 100644 --- a/Harbor/src/techtoolbox/Harbor/Main.java +++ b/Harbor/src/techtoolbox/Harbor/Main.java @@ -7,7 +7,10 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.plugin.java.JavaPlugin; import techtoolbox.Harbor.Actionbar.Actionbar; @@ -27,7 +30,7 @@ import techtoolbox.Harbor.Listeners.QuitEvent; public class Main extends JavaPlugin implements Listener { public static Main plugin; - public static String version = "1.4"; + public static String version = "1.4.2"; public static HashMap worlds = new HashMap(); public static ArrayList bypassers = new ArrayList(); public static Actionbar actionbar; @@ -41,20 +44,11 @@ public class Main extends JavaPlugin implements Listener { Bukkit.getPluginManager().registerEvents(new BedEnterEvent(), this); Bukkit.getPluginManager().registerEvents(new BedLeaveEvent(), this); - // Detect old configuration version - try { - if (Float.valueOf(getConfig().getString("version")) > Float.valueOf(version)) { - Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.miscellaneous.prefix") + "&7Configuration version is incompatible. Running Harbor version " + version + " while the configuration version is " + getConfig().getString("version") + ".")); - } - } - catch (Exception e) { - // Config older than version 1.4 - Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.miscellaneous.prefix") + "&7Incompatible configuration. Delete the current configuration to generate a valid one.")); - } - // Fill up worlds list - for (int i = 0; Bukkit.getServer().getWorlds().size() > i; i++) { - worlds.put(Bukkit.getServer().getWorlds().get(i), Integer.valueOf(0)); + if (getConfig().getBoolean("features.bypass")) { + for (int i = 0; Bukkit.getServer().getWorlds().size() > i; i++) { + worlds.put(Bukkit.getServer().getWorlds().get(i), Integer.valueOf(0)); + } } // Start actionbar message check @@ -112,7 +106,8 @@ public class Main extends JavaPlugin implements Listener { } Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.miscellaneous.prefix") + plugin.getConfig().getString("messages.miscellaneous.running").replace("[version]", version))); - + + // Actionbar if (version.equals("v1_8_R1")) { actionbar = new Actionbar_1_8_R1(); }else if (version.equals("v1_8_R2")) {