1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-12-19 14:57:38 +01:00

🔨 Refactoring some stuff

This commit is contained in:
Mykyta 2019-10-09 07:05:09 -07:00
parent 3500efa178
commit e525fca31c
No known key found for this signature in database
GPG Key ID: C147E30C19EA3570
7 changed files with 103 additions and 105 deletions

View File

@ -1,2 +1,2 @@
mvn package
yes | cp -rf target/*.jar ../../Documents/Spigot/1.14.4/plugins
cp -rf target/*.jar ../../Documents/Spigot/1.14.4/plugins

View File

@ -7,19 +7,21 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import xyz.nkomarn.Harbor.task.Checker;
import xyz.nkomarn.Harbor.util.Message;
import xyz.nkomarn.Harbor.util.Messages;
public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGH)
// Removed for testing
/*@EventHandler(priority = EventPriority.HIGH)
public void onBedEnter(final PlayerBedEnterEvent event) {
if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) {
return;
}
final World world = event.getPlayer().getWorld();
if (morePlayerNeeded(world, 1)) {
Message.SendChatMessage(world, "messages.chat.sleeping", event.getPlayer().getDisplayName(), 1);
Message.SendActionbarMessage(world, "messages.actionbar.sleeping", event.getPlayer().getDisplayName(), 1);
Message.sendChatMessage(world, "messages.chat.sleeping", event.getPlayer().getDisplayName(), 1);
Message.sendActionbarMessage(world, "messages.actionbar.sleeping", event.getPlayer().getDisplayName(), 0);
}
}
@ -27,7 +29,7 @@ public class PlayerListener implements Listener {
public void onBedLeave(final PlayerBedLeaveEvent event) {
final World world = event.getPlayer().getWorld();
if (Checker.isNight(world) && !Checker.skippingWorlds.contains(world) && morePlayerNeeded(world, 0)) {
Message.SendChatMessage(world, "messages.chat.left", event.getPlayer().getDisplayName(), 0);
Message.sendChatMessage(world, "messages.chat.left", event.getPlayer().getDisplayName(), 0);
}
}
@ -35,5 +37,6 @@ public class PlayerListener implements Listener {
final int sleeping = Checker.getSleeping(world) + change;
final int needed = Checker.getNeeded(world) - change;
return sleeping > 0 && needed > 0;
}
}*/
}

View File

@ -1,10 +1,12 @@
package xyz.nkomarn.Harbor.task;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;
import xyz.nkomarn.Harbor.util.Config;
import xyz.nkomarn.Harbor.util.Message;
import xyz.nkomarn.Harbor.util.Messages;
public class AccelerateNightTask extends BukkitRunnable {
private final World world;
@ -17,10 +19,13 @@ public class AccelerateNightTask extends BukkitRunnable {
public void run() {
final long time = world.getTime();
if (!(time >= 450 && time <= 1000)) {
world.setTime(time + Config.getInteger("values.timeSkipInterval"));
world.setTime(time + Config.getInteger("values.interval"));
world.getPlayers().forEach(player -> Messages.sendActionBarMessage(player,
ChatColor.GREEN + "Current world time: " + time));
} else {
// Announce night skip and clear queue
Message.SendRandomChatMessage(world, "messages.chat.skipped");
Bukkit.getServer().broadcastMessage("Harbor - skipped");
// Message.sendRandomChatMessage(world, "messages.chat.skipped");
Checker.skippingWorlds.remove(world);
// Reset sleep statistic if phantoms are disabled

View File

@ -4,9 +4,10 @@ import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.World;
import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.Harbor;
import xyz.nkomarn.Harbor.util.Config;
import xyz.nkomarn.Harbor.util.Message;
import xyz.nkomarn.Harbor.util.Messages;
import java.util.ArrayList;
import java.util.List;
@ -20,66 +21,73 @@ public class Checker implements Runnable {
@Override
public void run() {
Bukkit.getOnlinePlayers()
.stream()
.map(Player::getWorld).distinct()
.filter(this::validForCheckWorld)
.forEach(this::checkWorld);
.stream()
.map(Player::getWorld).distinct()
.filter(this::validateWorld)
.forEach(this::checkWorld);
}
public static int getSleeping(final World world) {
return world.getPlayers().stream().filter(Player::isSleeping).collect(toList()).size();
private void checkWorld(final World world) {
final int sleeping = getSleeping(world).size();
final int needed = getNeeded(world);
// Send actionbar sleeping notification
if (sleeping > 0 && needed > 0 && Config.getBoolean("messages.actionbar.actionbar")) {
world.getPlayers().forEach(player -> Messages.sendActionBarMessage(player,
Config.getString("messages.actionbar.sleeping")));
} else if (needed == 0 && sleeping > 0) {
world.getPlayers().forEach(player -> Messages.sendActionBarMessage(player,
Config.getString("messages.actionbar.everyone")));
}
// Check if world is applicable for skipping
if (needed == 0 && sleeping > 0) {
skippingWorlds.add(world);
new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 0L, 1);
}
}
public static int getNeeded(final World world) {
return Math.max(0, (int) Math.ceil((getPlayers(world))
* (Config.getDouble("values.percent") / 100)
- Checker.getSleeping(world)));
private boolean validateWorld(final World world) {
return !isBlacklisted(world)
&& !skippingWorlds.contains(world)
&& isNight(world);
}
private boolean isBlacklisted(final World world) {
return Config.getList("blacklist").contains(world.getName());
}
private boolean isNight(final World world) {
return world.getTime() > 12950 || world.getTime() < 23950;
}
public static List<Player> getSleeping(final World world) {
return world.getPlayers().stream().filter(Player::isSleeping).collect(toList());
}
public static int getSkipAmount(final World world) {
return (int) Math.ceil(getPlayers(world) * (Config.getDouble("values.percent") / 100));
}
public static int getPlayers(final World world) {
return Math.max(0, world.getPlayers().size() - getExcluded(world).size());
}
public static boolean isNight(final World world) {
return world.getTime() > 12950 || world.getTime() < 23950;
public static int getNeeded(final World world) {
return Math.max(0, (int) Math.ceil((getPlayers(world))
* (Config.getDouble("values.percent") / 100)
- getSleeping(world).size()));
}
private static List<Player> getExcluded(final World world) {
return world.getPlayers().stream().filter(Checker::isExcluded).collect(toList());
}
private static boolean isExcluded(final Player p) {
public static boolean isExcluded(final Player p) {
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 void checkWorld(final World world) {
final int sleeping = getSleeping(world);
final int needed = getNeeded(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 boolean validForCheckWorld(final World world) {
return notBlacklisted(world)
&& isNight(world)
&& !skippingWorlds.contains(world);
}
private boolean notBlacklisted(final World world) {
return !Config.getList("blacklist").contains(world.getName());
}
private void accelerateNight(final World world) {
Message.SendChatMessage(world, "messages.chat.accelerateNight", "", 0);
Message.SendActionbarMessage(world, "messages.actionbar.everyone", "", 0);
new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 0L, 1);
}
}

View File

@ -1,53 +0,0 @@
package xyz.nkomarn.Harbor.util;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.task.Checker;
import java.util.List;
import java.util.Random;
public class Message {
public static void SendChatMessage(final World world, final String messageLocation, final String playerName, final int change) {
if (Config.getBoolean("messages.chat.chat")) {
final String message = prepareMessageWithParams(Config.getString(messageLocation), world, playerName, change);
world.getPlayers().forEach(p -> sendChatMessage(p, message));
}
}
public static void SendActionbarMessage(final World world, final String messageLocation, final String playerName, final int change) {
if (Config.getBoolean("messages.actionbar.actionbar")) {
final String message = prepareMessageWithParams(Config.getString(messageLocation), world, playerName, change);
world.getPlayers().forEach(p -> sendActionbarMessage(p, message));
}
}
public static void SendRandomChatMessage(final World world, final String messageListLocation) {
final List<String> messages = Config.getList(messageListLocation);
final int index = new Random().nextInt(messages.size());
world.getPlayers().forEach(p -> sendChatMessage(p, messages.get(index)));
}
private static void sendChatMessage(final Player player, final String message) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
private static void sendActionbarMessage(final Player player, final String message) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
}
private static int getSkipAmount(final World world) {
return (int) Math.ceil(Checker.getPlayers(world) * (Config.getDouble("values.percent") / 100));
}
private static String prepareMessageWithParams(final String message, final World world, final String playerName, final int change) {
return ChatColor.translateAlternateColorCodes('&', message
.replace("[sleeping]", String.valueOf(Checker.getSleeping(world) + change))
.replace("[needed]", String.valueOf(getSkipAmount(world)))
.replace("[more]", String.valueOf(Checker.getNeeded(world) - change))
.replace("[player]", playerName));
}
}

View File

@ -0,0 +1,35 @@
package xyz.nkomarn.Harbor.util;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.Player;
import xyz.nkomarn.Harbor.task.Checker;
import java.util.List;
import java.util.Random;
public class Messages {
public static void sendRandomChatMessage(final World world, final String messageList) {
final List<String> messages = Config.getList(messageList);
final int index = new Random().nextInt(messages.size());
world.getPlayers().forEach(p -> sendChatMessage(p, messages.get(index)));
}
private static void sendChatMessage(final Player player, final String message) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
public static void sendActionBarMessage(final Player player, final String message) {
final World world = player.getWorld();
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(
ChatColor.translateAlternateColorCodes('&', message
.replace("[sleeping]", String.valueOf(Checker.getSleeping(world).size()))
.replace("[players]", String.valueOf(Checker.getPlayers(world)))
.replace("[needed]", String.valueOf(Checker.getSkipAmount(world)))
.replace("[more]", String.valueOf(Checker.getNeeded(world))))));
}
}

View File

@ -11,7 +11,7 @@
values:
timer: 2 # How often to run the clock task (used to detect sleep, AFK players, time actionbar, etc.)
percent: 100 # Percent of players that need to sleep to skip night (must be between 0 to 100)
timeSkipInterval: 60 # Time skip interval that is added when the night get accelerated.
interval: 60 # Time skip interval that is added when the night get accelerated.
features:
skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent)