From bc2977d6ce1acc2cf0b7ee195549d5803cedbc4f Mon Sep 17 00:00:00 2001 From: Roland von Rotz Date: Mon, 23 Sep 2019 09:11:19 +0200 Subject: [PATCH 1/4] Make Harbor work again --- src/main/java/xyz/nkomarn/Harbor/Harbor.java | 8 +- .../nkomarn/Harbor/command/HarborCommand.java | 11 +- .../java/xyz/nkomarn/Harbor/task/Checker.java | 128 ++++++++++-------- src/main/resources/config.yml | 1 + 4 files changed, 79 insertions(+), 69 deletions(-) diff --git a/src/main/java/xyz/nkomarn/Harbor/Harbor.java b/src/main/java/xyz/nkomarn/Harbor/Harbor.java index e6a8901..8eccbd6 100644 --- a/src/main/java/xyz/nkomarn/Harbor/Harbor.java +++ b/src/main/java/xyz/nkomarn/Harbor/Harbor.java @@ -17,13 +17,15 @@ public class Harbor extends JavaPlugin { public void onEnable() { instance = this; saveDefaultConfig(); - Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, - new Checker(), 0L, Config.getInteger("values.timer") * 20); + if (Config.getBoolean("features.skip")) { + Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, + new Checker(), 0L, Config.getInteger("values.timer") * 20); + } getCommand("harbor").setExecutor(new HarborCommand()); // bStats - Metrics metrics = new Metrics(this); + final Metrics metrics = new Metrics(this); // Essentials hook essentials = (Essentials) Bukkit.getServer().getPluginManager().getPlugin("Essentials"); diff --git a/src/main/java/xyz/nkomarn/Harbor/command/HarborCommand.java b/src/main/java/xyz/nkomarn/Harbor/command/HarborCommand.java index a57c688..18b83a3 100644 --- a/src/main/java/xyz/nkomarn/Harbor/command/HarborCommand.java +++ b/src/main/java/xyz/nkomarn/Harbor/command/HarborCommand.java @@ -5,19 +5,18 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.nkomarn.Harbor.util.Updater; public class HarborCommand implements CommandExecutor { int changeTimeTask = 0; @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - Player player = (Player) commandSender; - World world = player.getWorld(); + public boolean onCommand(final CommandSender commandSender, final Command command, final String s, final String[] strings) { + final Player player = (Player) commandSender; + final World world = player.getWorld(); - Updater.check(); - Updater.upgrade(); + //Updater.check(); + //Updater.upgrade(); return true; } diff --git a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java index d2ebfa4..c968eb7 100644 --- a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java +++ b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java @@ -11,89 +11,98 @@ import xyz.nkomarn.Harbor.util.Config; import java.util.ArrayList; import java.util.List; import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; public class Checker implements Runnable { - private static List skippingWorlds = new ArrayList<>(); + private static final List skippingWorlds = new ArrayList<>(); @Override public void run() { - for (World world : Bukkit.getWorlds()) { + Bukkit.getOnlinePlayers() + .stream() + .map(Player::getWorld).distinct() + .filter(this::validForCheckWorld) + .forEach(this::checkWorld); + } - // Check for blacklisted worlds - if (Config.getList("blacklist").contains(world.getName())) return; + private void checkWorld(final World world) { + final int sleeping = getSleeping(world).size(); // <- 0 + final int needed = getNeeded(world); - // Check if the night is already being skipped - if (skippingWorlds.contains(world)) return; + // Send actionbar notification + if (sleeping > 0 && needed > 0 && Config.getBoolean("messages.actionbar.actionbar")) { + world.getPlayers().forEach(this::sendActionBar); + } - int sleeping = getSleeping(world).size(); - int needed = getNeeded(world); - - // Send actionbar notification - if (getSleeping(world).size() > 0 && getNeeded(world) > 0) { - for (Player player : world.getPlayers()) { - sendActionBar(player, Config.getString("messages.actionbar.sleeping")); - } - } - - // Check if world is applicable for skipping - if (Config.getBoolean("features.skip") && getNeeded(world) == 0 && getSleeping(world).size() > 0) { - - // Rapidly accelerate time until it's day - skippingWorlds.add(world); - accelerateNight(world); - - } + // Check if world is applicable for skipping + if (needed == 0 && sleeping > 0) { + // Rapidly accelerate time until it's day + skippingWorlds.add(world); + accelerateNight(world); } } - private void sendActionBar(Player player, String message) { - World world = player.getWorld(); - - player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( - ChatColor.translateAlternateColorCodes('&', message - .replace("[sleeping]", String.valueOf(getSleeping(world))) - .replace("[players]", String.valueOf(world.getPlayers().size())) - .replace("[needed]", String.valueOf(getSkipAmount(world))) - .replace("[more]", String.valueOf(getNeeded(world)))))); + private boolean validForCheckWorld(final World world) { + return notBlacklisted(world) + && isNight(world) + && !skippingWorlds.contains(world); } - private List getSleeping(World world) { - List sleeping = new ArrayList<>(); - for (Player player : world.getPlayers()) { + private boolean notBlacklisted(final World world) { + return !Config.getList("blacklist").contains(world.getName()); + } + + private boolean isNight(final World world) { + return world.getTime() > 12950 || world.getTime() < 23950; + } + + private void sendActionBar(final Player player) { + final World world = player.getWorld(); + final String message = Config.getString("messages.actionbar.sleeping"); + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( + ChatColor.translateAlternateColorCodes('&', message + .replace("[sleeping]", String.valueOf(getSleeping(world))) + .replace("[players]", String.valueOf(world.getPlayers().size())) + .replace("[needed]", String.valueOf(getSkipAmount(world))) + .replace("[more]", String.valueOf(getNeeded(world)))))); + } + + private List getSleeping(final World world) { + final List sleeping = new ArrayList<>(); + for (final Player player : world.getPlayers()) { if (player.isSleeping()) sleeping.add(player); } return sleeping; } - private int getSkipAmount(World world) { + private int getSkipAmount(final World world) { return (int) (getPlayers(world) * (Config.getDouble("values.percent") / 100)); } - private int getPlayers(World world) { + private int getPlayers(final World world) { return Math.max(0, world.getPlayers().size() - getExcluded(world).size()); } - - private int getNeeded(World world) { + private int getNeeded(final World world) { return Math.max(0, (int) Math.ceil((getPlayers(world)) - * (Config.getDouble("values.percent") / 100) - - getSleeping(world).size())); + * (Config.getDouble("values.percent") / 100) + - getSleeping(world).size())); } - private ArrayList getExcluded(World w) { - ArrayList a = new ArrayList<>(); + private ArrayList getExcluded(final World w) { + final ArrayList a = new ArrayList<>(); w.getPlayers().forEach(p -> { if (isExcluded(p)) a.add(p); }); return a; } - private boolean isExcluded(Player p) { + private boolean isExcluded(final Player p) { boolean s = false; - if (Config.getBoolean("features.ignore")) if (p.getGameMode() == GameMode.SURVIVAL) s = false; else s = true; - if (Config.getBoolean("features.bypass")) if (p.hasPermission("harbor.bypass")) s = true; else s = false; + if (Config.getBoolean("features.ignore")) if (p.getGameMode() == GameMode.SURVIVAL) s = false; + else s = true; + if (Config.getBoolean("features.bypass")) if (p.hasPermission("harbor.bypass")) s = true; + else s = false; // Essentials AFK detection if (Harbor.essentials != null) { @@ -103,39 +112,38 @@ public class Checker implements Runnable { return s; } - private String randomMessage(String list) { - List messages = Config.getList(list); - Random random = new Random(); - int index = random.nextInt(messages.size()); + private String randomMessage(final String list) { + final List messages = Config.getList(list); + final Random random = new Random(); + final int index = random.nextInt(messages.size()); return ChatColor.translateAlternateColorCodes('&', messages.get(index)); } - private void sendChatMessage(String message) { + private void sendChatMessage(final String message) { if (!Config.getBoolean("messages.chat.chat")) return; if (message.length() < 1) return; Bukkit.broadcastMessage(message); } - private void accelerateNight(World world) { - Bukkit.broadcastMessage("Harbor - Accelerating time."); + private void accelerateNight(final World world) { + Bukkit.broadcastMessage(Config.getString("messages.chat.accelerateNight")); new BukkitRunnable() { @Override public void run() { - long time = world.getTime(); + final long time = world.getTime(); if (!(time >= 450 && time <= 1000)) { world.setTime(time + 60); - } - else { + } else { // Announce night skip and clear queue - sendChatMessage("messages.chat.skipped"); + sendChatMessage(randomMessage("messages.chat.skipped")); skippingWorlds.remove(world); // Reset sleep statistic if phantoms are disabled if (!Config.getBoolean("features.phantoms")) { - for (Player player : world.getPlayers()) { + for (final Player player : world.getPlayers()) { player.setStatistic(Statistic.TIME_SINCE_REST, 0); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f9f9344..122c88b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -30,6 +30,7 @@ messages: - "&eArghh, it's so bright outside." sleeping: "&e[player] is now sleeping ([sleeping]/[online], [needed] more needed to skip)." # Display which player went to bed left: "&e[player] got out of bed ([sleeping]/[online], [needed] more needed to skip)." # Display when a player left their bed + accelerateNight: "Die Nacht wird beschleunigt" # Display when the night get accelerated. actionbar: actionbar: true # Enable/disable actionbar messages sleeping: "&e[sleeping] out of [needed] players are sleeping ([more] more needed to skip)." # Shown when some players are in bed From a1357648ff6a71be457312fa107fc29baeb7dea5 Mon Sep 17 00:00:00 2001 From: Roland von Rotz Date: Mon, 23 Sep 2019 09:13:37 +0200 Subject: [PATCH 2/4] Englisch message --- src/main/resources/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 122c88b..02a5bd3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -30,7 +30,7 @@ messages: - "&eArghh, it's so bright outside." sleeping: "&e[player] is now sleeping ([sleeping]/[online], [needed] more needed to skip)." # Display which player went to bed left: "&e[player] got out of bed ([sleeping]/[online], [needed] more needed to skip)." # Display when a player left their bed - accelerateNight: "Die Nacht wird beschleunigt" # Display when the night get accelerated. + accelerateNight: "Harbor - Accelerating time." # Display when the night get accelerated. actionbar: actionbar: true # Enable/disable actionbar messages sleeping: "&e[sleeping] out of [needed] players are sleeping ([more] more needed to skip)." # Shown when some players are in bed From 5d5710af914985914682b3bcb5ddb2decf9e247f Mon Sep 17 00:00:00 2001 From: Roland von Rotz Date: Sun, 29 Sep 2019 16:48:08 +0200 Subject: [PATCH 3/4] Perhaps fixing needed count --- .../java/xyz/nkomarn/Harbor/task/Checker.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java index c968eb7..34a9c65 100644 --- a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java +++ b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java @@ -12,6 +12,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import static java.util.stream.Collectors.toList; + public class Checker implements Runnable { private static final List skippingWorlds = new ArrayList<>(); @@ -61,22 +63,18 @@ public class Checker implements Runnable { final String message = Config.getString("messages.actionbar.sleeping"); player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText( ChatColor.translateAlternateColorCodes('&', message - .replace("[sleeping]", String.valueOf(getSleeping(world))) + .replace("[sleeping]", String.valueOf(getSleeping(world).size())) .replace("[players]", String.valueOf(world.getPlayers().size())) .replace("[needed]", String.valueOf(getSkipAmount(world))) .replace("[more]", String.valueOf(getNeeded(world)))))); } private List getSleeping(final World world) { - final List sleeping = new ArrayList<>(); - for (final Player player : world.getPlayers()) { - if (player.isSleeping()) sleeping.add(player); - } - return sleeping; + return world.getPlayers().stream().filter(Player::isSleeping).collect(toList()); } private int getSkipAmount(final World world) { - return (int) (getPlayers(world) * (Config.getDouble("values.percent") / 100)); + return (int) Math.ceil(getPlayers(world) * (Config.getDouble("values.percent") / 100)); } private int getPlayers(final World world) { @@ -89,12 +87,8 @@ public class Checker implements Runnable { - getSleeping(world).size())); } - private ArrayList getExcluded(final World w) { - final ArrayList a = new ArrayList<>(); - w.getPlayers().forEach(p -> { - if (isExcluded(p)) a.add(p); - }); - return a; + private List getExcluded(final World world) { + return world.getPlayers().stream().filter(this::isExcluded).collect(toList()); } private boolean isExcluded(final Player p) { From 1b75e8879916b331aa9ae8ec4b79bca1ee4561e1 Mon Sep 17 00:00:00 2001 From: Roland von Rotz Date: Sun, 29 Sep 2019 16:58:58 +0200 Subject: [PATCH 4/4] Refactoring --- .../java/xyz/nkomarn/Harbor/task/Checker.java | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java index 34a9c65..576482a 100644 --- a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java +++ b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java @@ -92,18 +92,10 @@ public class Checker implements Runnable { } private boolean isExcluded(final Player p) { - boolean s = false; - if (Config.getBoolean("features.ignore")) if (p.getGameMode() == GameMode.SURVIVAL) s = false; - else s = true; - if (Config.getBoolean("features.bypass")) if (p.hasPermission("harbor.bypass")) s = true; - else s = false; - - // Essentials AFK detection - if (Harbor.essentials != null) { - if (Harbor.essentials.getUser(p).isAfk()) s = true; - } - - return s; + final boolean excludedByGameMode = Config.getBoolean("features.ignore") && p.getGameMode() != GameMode.SURVIVAL; + final boolean excludedByPermission = Config.getBoolean("features.bypass") && p.hasPermission("harbor.bypass"); + final boolean excludedByAfk = Harbor.essentials != null && Harbor.essentials.getUser(p).isAfk(); // Essentials AFK detection + return excludedByGameMode || excludedByPermission || excludedByAfk; } private String randomMessage(final String list) { @@ -130,16 +122,13 @@ public class Checker implements Runnable { if (!(time >= 450 && time <= 1000)) { world.setTime(time + 60); } else { - // Announce night skip and clear queue sendChatMessage(randomMessage("messages.chat.skipped")); skippingWorlds.remove(world); // Reset sleep statistic if phantoms are disabled if (!Config.getBoolean("features.phantoms")) { - for (final Player player : world.getPlayers()) { - player.setStatistic(Statistic.TIME_SINCE_REST, 0); - } + world.getPlayers().forEach(player -> player.setStatistic(Statistic.TIME_SINCE_REST, 0)); } // Clear weather