mirror of
https://github.com/nkomarn/harbor.git
synced 2024-12-20 07:17:39 +01:00
🔨 Refactoring some stuff
This commit is contained in:
parent
3500efa178
commit
e525fca31c
2
build.sh
2
build.sh
@ -1,2 +1,2 @@
|
|||||||
mvn package
|
mvn package
|
||||||
yes | cp -rf target/*.jar ../../Documents/Spigot/1.14.4/plugins
|
cp -rf target/*.jar ../../Documents/Spigot/1.14.4/plugins
|
@ -7,19 +7,21 @@ import org.bukkit.event.Listener;
|
|||||||
import org.bukkit.event.player.PlayerBedEnterEvent;
|
import org.bukkit.event.player.PlayerBedEnterEvent;
|
||||||
import org.bukkit.event.player.PlayerBedLeaveEvent;
|
import org.bukkit.event.player.PlayerBedLeaveEvent;
|
||||||
import xyz.nkomarn.Harbor.task.Checker;
|
import xyz.nkomarn.Harbor.task.Checker;
|
||||||
import xyz.nkomarn.Harbor.util.Message;
|
import xyz.nkomarn.Harbor.util.Messages;
|
||||||
|
|
||||||
public class PlayerListener implements Listener {
|
public class PlayerListener implements Listener {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGH)
|
// Removed for testing
|
||||||
|
|
||||||
|
/*@EventHandler(priority = EventPriority.HIGH)
|
||||||
public void onBedEnter(final PlayerBedEnterEvent event) {
|
public void onBedEnter(final PlayerBedEnterEvent event) {
|
||||||
if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) {
|
if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final World world = event.getPlayer().getWorld();
|
final World world = event.getPlayer().getWorld();
|
||||||
if (morePlayerNeeded(world, 1)) {
|
if (morePlayerNeeded(world, 1)) {
|
||||||
Message.SendChatMessage(world, "messages.chat.sleeping", event.getPlayer().getDisplayName(), 1);
|
Message.sendChatMessage(world, "messages.chat.sleeping", event.getPlayer().getDisplayName(), 1);
|
||||||
Message.SendActionbarMessage(world, "messages.actionbar.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) {
|
public void onBedLeave(final PlayerBedLeaveEvent event) {
|
||||||
final World world = event.getPlayer().getWorld();
|
final World world = event.getPlayer().getWorld();
|
||||||
if (Checker.isNight(world) && !Checker.skippingWorlds.contains(world) && morePlayerNeeded(world, 0)) {
|
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 sleeping = Checker.getSleeping(world) + change;
|
||||||
final int needed = Checker.getNeeded(world) - change;
|
final int needed = Checker.getNeeded(world) - change;
|
||||||
return sleeping > 0 && needed > 0;
|
return sleeping > 0 && needed > 0;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package xyz.nkomarn.Harbor.task;
|
package xyz.nkomarn.Harbor.task;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Statistic;
|
import org.bukkit.Statistic;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
import xyz.nkomarn.Harbor.util.Config;
|
import xyz.nkomarn.Harbor.util.Config;
|
||||||
import xyz.nkomarn.Harbor.util.Message;
|
import xyz.nkomarn.Harbor.util.Messages;
|
||||||
|
|
||||||
public class AccelerateNightTask extends BukkitRunnable {
|
public class AccelerateNightTask extends BukkitRunnable {
|
||||||
private final World world;
|
private final World world;
|
||||||
@ -17,10 +19,13 @@ public class AccelerateNightTask extends BukkitRunnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
final long time = world.getTime();
|
final long time = world.getTime();
|
||||||
if (!(time >= 450 && time <= 1000)) {
|
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 {
|
} else {
|
||||||
// Announce night skip and clear queue
|
// 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);
|
Checker.skippingWorlds.remove(world);
|
||||||
|
|
||||||
// Reset sleep statistic if phantoms are disabled
|
// Reset sleep statistic if phantoms are disabled
|
||||||
|
@ -4,9 +4,10 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import xyz.nkomarn.Harbor.Harbor;
|
import xyz.nkomarn.Harbor.Harbor;
|
||||||
import xyz.nkomarn.Harbor.util.Config;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -22,64 +23,71 @@ public class Checker implements Runnable {
|
|||||||
Bukkit.getOnlinePlayers()
|
Bukkit.getOnlinePlayers()
|
||||||
.stream()
|
.stream()
|
||||||
.map(Player::getWorld).distinct()
|
.map(Player::getWorld).distinct()
|
||||||
.filter(this::validForCheckWorld)
|
.filter(this::validateWorld)
|
||||||
.forEach(this::checkWorld);
|
.forEach(this::checkWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getSleeping(final World world) {
|
private void checkWorld(final World world) {
|
||||||
return world.getPlayers().stream().filter(Player::isSleeping).collect(toList()).size();
|
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")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getNeeded(final World world) {
|
// Check if world is applicable for skipping
|
||||||
return Math.max(0, (int) Math.ceil((getPlayers(world))
|
if (needed == 0 && sleeping > 0) {
|
||||||
* (Config.getDouble("values.percent") / 100)
|
skippingWorlds.add(world);
|
||||||
- Checker.getSleeping(world)));
|
new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 0L, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
public static int getPlayers(final World world) {
|
||||||
return Math.max(0, world.getPlayers().size() - getExcluded(world).size());
|
return Math.max(0, world.getPlayers().size() - getExcluded(world).size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNight(final World world) {
|
public static int getNeeded(final World world) {
|
||||||
return world.getTime() > 12950 || world.getTime() < 23950;
|
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) {
|
private static List<Player> getExcluded(final World world) {
|
||||||
return world.getPlayers().stream().filter(Checker::isExcluded).collect(toList());
|
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 excludedByGameMode = Config.getBoolean("features.ignore") && p.getGameMode() != GameMode.SURVIVAL;
|
||||||
final boolean excludedByPermission = Config.getBoolean("features.bypass") && p.hasPermission("harbor.bypass");
|
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
|
final boolean excludedByAfk = Harbor.essentials != null && Harbor.essentials.getUser(p).isAfk(); // Essentials AFK detection
|
||||||
return excludedByGameMode || excludedByPermission || excludedByAfk;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
35
src/main/java/xyz/nkomarn/Harbor/util/Messages.java
Normal file
35
src/main/java/xyz/nkomarn/Harbor/util/Messages.java
Normal 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))))));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
values:
|
values:
|
||||||
timer: 2 # How often to run the clock task (used to detect sleep, AFK players, time actionbar, etc.)
|
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)
|
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:
|
features:
|
||||||
skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent)
|
skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent)
|
||||||
|
Loading…
Reference in New Issue
Block a user