mirror of
https://github.com/nkomarn/harbor.git
synced 2024-11-16 07:05:22 +01:00
Add BedEnter/BedLeave Events & continue refactoring
This commit is contained in:
parent
2f8544b9b1
commit
ece82c9e7f
@ -0,0 +1,37 @@
|
||||
package xyz.nkomarn.Harbor.listener;
|
||||
|
||||
import org.bukkit.World;
|
||||
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.PlayerBedLeaveEvent;
|
||||
import xyz.nkomarn.Harbor.task.Checker;
|
||||
import xyz.nkomarn.Harbor.util.Message;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@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)) {
|
||||
Message.SendChatMessage("messages.chat.sleeping");
|
||||
Message.SendActionbarMessage(world, "messages.actionbar.sleeping");
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onBedLeave(final PlayerBedLeaveEvent event) {
|
||||
final World world = event.getPlayer().getWorld();
|
||||
if (Checker.isNight(world) && !Checker.skippingWorlds.contains(world) && morePlayerNeeded(world)) {
|
||||
Message.SendChatMessage("messages.chat.left");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean morePlayerNeeded(final World world) {
|
||||
return Checker.getSleeping(world) > 0 && Checker.getNeeded(world) > 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package xyz.nkomarn.Harbor.task;
|
||||
|
||||
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;
|
||||
|
||||
public class AccelerateNightTask extends BukkitRunnable {
|
||||
private final World world;
|
||||
|
||||
public AccelerateNightTask(final World world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final long time = world.getTime();
|
||||
if (!(time >= 450 && time <= 1000)) {
|
||||
world.setTime(time + 60);
|
||||
} else {
|
||||
// Announce night skip and clear queue
|
||||
Message.SendRandomChatMessage("messages.chat.skipped");
|
||||
Checker.skippingWorlds.remove(world);
|
||||
|
||||
// Reset sleep statistic if phantoms are disabled
|
||||
if (!Config.getBoolean("features.phantoms")) {
|
||||
world.getPlayers().forEach(player -> player.setStatistic(Statistic.TIME_SINCE_REST, 0));
|
||||
}
|
||||
|
||||
// Clear weather
|
||||
if (Config.getBoolean("features.weather")) {
|
||||
world.setStorm(false);
|
||||
world.setThundering(false);
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,22 +1,20 @@
|
||||
package xyz.nkomarn.Harbor.task;
|
||||
|
||||
import net.md_5.bungee.api.ChatMessageType;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import xyz.nkomarn.Harbor.Harbor;
|
||||
import xyz.nkomarn.Harbor.util.Config;
|
||||
|
||||
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<World> skippingWorlds = new ArrayList<>();
|
||||
public static final List<World> skippingWorlds = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@ -27,14 +25,38 @@ public class Checker implements Runnable {
|
||||
.forEach(this::checkWorld);
|
||||
}
|
||||
|
||||
private void checkWorld(final World world) {
|
||||
final int sleeping = getSleeping(world).size(); // <- 0
|
||||
final int needed = getNeeded(world);
|
||||
public static int getSleeping(final World world) {
|
||||
return (int) world.getPlayers().stream().filter(Player::isSleeping).count();
|
||||
}
|
||||
|
||||
// Send actionbar notification
|
||||
if (sleeping > 0 && needed > 0 && Config.getBoolean("messages.actionbar.actionbar")) {
|
||||
world.getPlayers().forEach(this::sendActionBar);
|
||||
}
|
||||
public static int getNeeded(final World world) {
|
||||
return Math.max(0, (int) Math.ceil((getPlayers(world))
|
||||
* (Config.getDouble("values.percent") / 100)
|
||||
- Checker.getSleeping(world)));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private static List<Player> getExcluded(final World world) {
|
||||
return world.getPlayers().stream().filter(Checker::isExcluded).collect(toList());
|
||||
}
|
||||
|
||||
private 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) {
|
||||
@ -54,94 +76,8 @@ public class Checker implements Runnable {
|
||||
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).size()))
|
||||
.replace("[players]", String.valueOf(world.getPlayers().size()))
|
||||
.replace("[needed]", String.valueOf(getSkipAmount(world)))
|
||||
.replace("[more]", String.valueOf(getNeeded(world))))));
|
||||
}
|
||||
|
||||
private List<Player> getSleeping(final World world) {
|
||||
return world.getPlayers().stream().filter(Player::isSleeping).collect(toList());
|
||||
}
|
||||
|
||||
private int getSkipAmount(final World world) {
|
||||
return (int) Math.ceil(getPlayers(world) * (Config.getDouble("values.percent") / 100));
|
||||
}
|
||||
|
||||
private int getPlayers(final World world) {
|
||||
return Math.max(0, world.getPlayers().size() - getExcluded(world).size());
|
||||
}
|
||||
|
||||
private int getNeeded(final World world) {
|
||||
return Math.max(0, (int) Math.ceil((getPlayers(world))
|
||||
* (Config.getDouble("values.percent") / 100)
|
||||
- getSleeping(world).size()));
|
||||
}
|
||||
|
||||
private List<Player> getExcluded(final World world) {
|
||||
return world.getPlayers().stream().filter(this::isExcluded).collect(toList());
|
||||
}
|
||||
|
||||
private 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 String randomMessage(final String list) {
|
||||
final List<String> 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(final String message) {
|
||||
if (!Config.getBoolean("messages.chat.chat")) return;
|
||||
if (message.length() < 1) return;
|
||||
Bukkit.broadcastMessage(message);
|
||||
}
|
||||
|
||||
private void accelerateNight(final World world) {
|
||||
Bukkit.broadcastMessage(Config.getString("messages.chat.accelerateNight"));
|
||||
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final long time = world.getTime();
|
||||
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")) {
|
||||
world.getPlayers().forEach(player -> player.setStatistic(Statistic.TIME_SINCE_REST, 0));
|
||||
}
|
||||
|
||||
// Clear weather
|
||||
if (Config.getBoolean("features.weather")) {
|
||||
world.setStorm(false);
|
||||
world.setThundering(false);
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}.runTaskTimer(Harbor.instance, 20, 1);
|
||||
new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 20, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
53
src/main/java/xyz/nkomarn/Harbor/util/Message.java
Normal file
53
src/main/java/xyz/nkomarn/Harbor/util/Message.java
Normal file
@ -0,0 +1,53 @@
|
||||
package xyz.nkomarn.Harbor.util;
|
||||
|
||||
import net.md_5.bungee.api.ChatMessageType;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
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 String messageLocation) {
|
||||
sendChatMessage(Config.getString(messageLocation));
|
||||
}
|
||||
|
||||
public static void SendActionbarMessage(final World world, final String messageLocation) {
|
||||
if (Config.getBoolean("messages.actionbar.actionbar")) {
|
||||
final String message = Message.prepareMessageWithParams(Config.getString(messageLocation), world);
|
||||
world.getPlayers().forEach(p -> sendActionbarMessage(p, message));
|
||||
}
|
||||
}
|
||||
|
||||
public static void SendRandomChatMessage(final String messageListLocation) {
|
||||
final List<String> messages = Config.getList(messageListLocation);
|
||||
final int index = new Random().nextInt(messages.size());
|
||||
sendChatMessage(messages.get(index));
|
||||
}
|
||||
|
||||
private static void sendChatMessage(final String message) {
|
||||
if (Config.getBoolean("messages.chat.chat") && message.length() > 0) {
|
||||
Bukkit.broadcastMessage(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) {
|
||||
return ChatColor.translateAlternateColorCodes('&', message
|
||||
.replace("[sleeping]", String.valueOf(Checker.getSleeping(world)))
|
||||
.replace("[players]", String.valueOf(world.getPlayers().size()))
|
||||
.replace("[needed]", String.valueOf(getSkipAmount(world)))
|
||||
.replace("[more]", String.valueOf(Checker.getNeeded(world))));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user