1
0
mirror of https://github.com/nkomarn/harbor.git synced 2025-01-05 07:07:34 +01:00

Clean up + more Javadocs

We're getting closer to a final, stable. *bug-free* build of refactored Harbor.
This commit is contained in:
Mykyta Komarn 2020-08-19 17:08:41 -07:00
parent 54c062816f
commit c2a32f27e8
8 changed files with 134 additions and 122 deletions

View File

@ -17,6 +17,7 @@ import xyz.nkomarn.harbor.util.Metrics;
import xyz.nkomarn.harbor.util.PlayerManager;
import java.util.Arrays;
import java.util.Optional;
public class Harbor extends JavaPlugin {
@ -27,12 +28,13 @@ public class Harbor extends JavaPlugin {
private Essentials essentials;
public void onEnable() {
PluginManager pluginManager = getServer().getPluginManager();
config = new Config(this);
checker = new Checker(this);
messages = new Messages(this);
playerManager = new PlayerManager(this);
PluginManager pluginManager = getServer().getPluginManager();
essentials = (Essentials) pluginManager.getPlugin("Essentials");
Arrays.asList(
messages,
@ -43,10 +45,9 @@ public class Harbor extends JavaPlugin {
getCommand("harbor").setExecutor(new HarborCommand(this));
essentials = (Essentials) getServer().getPluginManager().getPlugin("Essentials");
if (essentials == null) {
getLogger().info("Essentials not present- registering fallback AFK detection system.");
// TODO IDK LOL getServer().getPluginManager().registerEvents(new PlayerManager.AfkListener(), this);
playerManager.registerFallbackListeners();
}
if (config.getBoolean("metrics")) {
@ -90,8 +91,8 @@ public class Harbor extends JavaPlugin {
return playerManager;
}
@Nullable
public Essentials getEssentials() {
return essentials;
@NotNull
public Optional<Essentials> getEssentials() {
return Optional.ofNullable(essentials);
}
}

View File

@ -28,7 +28,7 @@ public class HarborCommand implements TabExecutor {
String prefix = ChatColor.translateAlternateColorCodes('&', config.getString("messages.miscellaneous.chat-prefix"));
if (args.length < 1 || !sender.hasPermission("harbor.admin")) {
sender.sendMessage(prefix + "Harbor version " + harbor.getVersion() + " by TechToolbox (@nkomarn).");
sender.sendMessage(prefix + "Harbor " + harbor.getVersion() + " by TechToolbox (@nkomarn).");
return true;
}
@ -51,8 +51,8 @@ public class HarborCommand implements TabExecutor {
if (checker.isSkipping(world)) {
sender.sendMessage(prefix + "This world's time is already being accelerated.");
} else {
checker.forceSkip(world);
sender.sendMessage(prefix + "Forcing night skip in your world.");
checker.forceSkip(world);
}
return true;

View File

@ -8,8 +8,6 @@ import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.jetbrains.annotations.NotNull;
import xyz.nkomarn.harbor.Harbor;
import xyz.nkomarn.harbor.task.Checker;
import xyz.nkomarn.harbor.util.Config;
import xyz.nkomarn.harbor.util.PlayerManager;
import java.util.concurrent.TimeUnit;
@ -30,27 +28,14 @@ public class BedListener implements Listener {
return;
}
Checker checker = harbor.getChecker();
Player player = event.getPlayer();
if (checker.isSkipping(player.getWorld())) {
return;
}
if (checker.isVanished(player)) {
if (isMessageSilenced(player)) {
return;
}
Bukkit.getScheduler().runTaskLater(harbor, () -> {
Config config = harbor.getConfiguration();
int cooldown = config.getInteger("messages.chat.message-cooldown");
if (!(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - playerManager.getCooldown(player)) > cooldown)) {
return;
}
playerManager.setCooldown(player, System.currentTimeMillis());
harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), config.getString("messages.chat.player-sleeping")
harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), harbor.getConfiguration().getString("messages.chat.player-sleeping")
.replace("[player]", event.getPlayer().getName())
.replace("[displayname]", event.getPlayer().getDisplayName()));
}, 1);
@ -58,29 +43,34 @@ public class BedListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void onBedLeave(PlayerBedLeaveEvent event) {
Checker checker = harbor.getChecker();
Player player = event.getPlayer();
if (checker.isSkipping(player.getWorld())) {
return;
}
if (checker.isVanished(player)) {
if (isMessageSilenced(event.getPlayer())) {
return;
}
Bukkit.getScheduler().runTaskLater(harbor, () -> {
Config config = harbor.getConfiguration();
int cooldown = config.getInteger("messages.chat.message-cooldown");
if (!(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - playerManager.getCooldown(player)) > cooldown)) {
return;
}
playerManager.setCooldown(player, System.currentTimeMillis());
harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), config.getString("messages.chat.player-left-bed")
playerManager.setCooldown(event.getPlayer(), System.currentTimeMillis());
harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), harbor.getConfiguration().getString("messages.chat.player-left-bed")
.replace("[player]", event.getPlayer().getName())
.replace("[displayname]", event.getPlayer().getDisplayName()));
}, 1);
}
/**
* Checks if a message should be silenced from chat (i.e. if the player is under cooldown).
*
* @param player The player context.
* @return Whether the message should be silenced.
*/
private boolean isMessageSilenced(@NotNull Player player) {
if (harbor.getChecker().isSkipping(player.getWorld())) {
return true;
}
if (harbor.getChecker().isVanished(player)) {
return true;
}
int cooldown = harbor.getConfiguration().getInteger("messages.chat.message-cooldown");
return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - playerManager.getCooldown(player)) < cooldown;
}
}

View File

@ -6,9 +6,7 @@ import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import xyz.nkomarn.harbor.Harbor;
import xyz.nkomarn.harbor.listener.BedListener;
import xyz.nkomarn.harbor.util.Config;
import xyz.nkomarn.harbor.util.Messages;
public class AccelerateNightTask extends BukkitRunnable {
@ -46,10 +44,8 @@ public class AccelerateNightTask extends BukkitRunnable {
int dayTime = Math.max(150, config.getInteger("night-skip.daytime-ticks"));
int sleeping = checker.getSleepingPlayers(world).size();
if (config.getBoolean("night-skip.proportional-acceleration")) {
if (sleeping != 0) {
timeRate = Math.min(timeRate, Math.round(timeRate / world.getPlayers().size() * sleeping));
}
if (config.getBoolean("night-skip.proportional-acceleration") && sleeping != 0) {
timeRate = Math.min(timeRate, Math.round(timeRate / world.getPlayers().size() * sleeping));
}
if (time >= (dayTime - timeRate * 1.5) && time <= dayTime) {
@ -61,8 +57,9 @@ public class AccelerateNightTask extends BukkitRunnable {
harbor.getPlayerManager().clearCooldowns();
harbor.getMessages().sendRandomChatMessage(world, "messages.chat.night-skipped");
cancel();
} else {
world.setTime(time + (int) timeRate);
return;
}
world.setTime(time + (int) timeRate);
}
}

View File

@ -72,20 +72,12 @@ public class Checker extends BukkitRunnable {
double sleepingPercentage = Math.min(1, (double) sleeping / getSkipAmount(world));
messages.sendActionBarMessage(world, config.getString("messages.actionbar.players-sleeping"));
messages.sendBossBarMessage(
world,
config.getString("messages.bossbar.players-sleeping.message"),
config.getString("messages.bossbar.players-sleeping.color"),
sleepingPercentage
);
messages.sendBossBarMessage(world, config.getString("messages.bossbar.players-sleeping.message"),
config.getString("messages.bossbar.players-sleeping.color"), sleepingPercentage);
} else if (needed == 0) {
messages.sendActionBarMessage(world, config.getString("messages.actionbar.night-skipping"));
messages.sendBossBarMessage(
world,
config.getString("messages.bossbar.night-skipping.message"),
config.getString("messages.bossbar.night-skipping.color"),
1
);
messages.sendBossBarMessage(world, config.getString("messages.bossbar.night-skipping.message"),
config.getString("messages.bossbar.night-skipping.color"), 1);
if (!config.getBoolean("night-skip.enabled")) {
return;
@ -207,7 +199,7 @@ public class Checker extends BukkitRunnable {
* @param player The player to check.
* @return Whether the given player is excluded.
*/
private boolean isExcluded(final Player player) {
private boolean isExcluded(@NotNull Player player) {
ConfigurationSection exclusions = harbor.getConfig().getConfigurationSection("exclusions");
if (exclusions == null) {

View File

@ -1,5 +1,6 @@
package xyz.nkomarn.harbor.util;
import com.google.common.base.Enums;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
@ -18,17 +19,13 @@ import org.jetbrains.annotations.NotNull;
import xyz.nkomarn.harbor.Harbor;
import xyz.nkomarn.harbor.task.Checker;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.*;
public class Messages implements Listener {
private final Harbor harbor;
private final Config config;
private final Random random;
private final HashMap<UUID, BossBar> bossBars;
public Messages(@NotNull Harbor harbor) {
@ -46,6 +43,12 @@ public class Messages implements Listener {
}
}
/**
* Sends a message to all players in a given world.
*
* @param world The world context.
* @param message The message to send.
*/
public void sendWorldChatMessage(@NotNull World world, @NotNull String message) {
if (!config.getBoolean("messages.chat.enabled") || message.length() < 1) {
return;
@ -57,6 +60,12 @@ public class Messages implements Listener {
}
}
/**
* Sends an actionbar message to all players in a given world.
*
* @param world The world context.
* @param message The message to send.
*/
public void sendActionBarMessage(@NotNull World world, @NotNull String message) {
if (!config.getBoolean("messages.actionbar.enabled") || message.length() < 1) {
return;
@ -68,6 +77,12 @@ public class Messages implements Listener {
}
}
/**
* Selects a random message from a string list and sends it to the given world.
*
* @param world The world context.
* @param listLocation The location of the message list in the configuration.
*/
public void sendRandomChatMessage(@NotNull World world, @NotNull String listLocation) {
List<String> messages = config.getStringList(listLocation);
@ -78,6 +93,14 @@ public class Messages implements Listener {
sendWorldChatMessage(world, messages.get(random.nextInt(Math.max(0, messages.size()))));
}
/**
* Sets the message for the given world's bossbar.
*
* @param world The world in which the bossbar exists.
* @param message The message to set.
* @param color The bossbar color to set.
* @param percentage The bossbar percentage to set.
*/
public void sendBossBarMessage(@NotNull World world, @NotNull String message, @NotNull String color, double percentage) {
if (!config.getBoolean("messages.bossbar.enabled") || message.length() < 1) {
return;
@ -86,7 +109,6 @@ public class Messages implements Listener {
BossBar bar = bossBars.get(world.getUID());
if (bar == null) {
System.out.println("bar null");
return;
}
@ -96,11 +118,18 @@ public class Messages implements Listener {
}
bar.setTitle(harbor.getMessages().prepareMessage(world, message));
bar.setColor(getBarColor(color));
bar.setColor(Enums.getIfPresent(BarColor.class, color).or(BarColor.BLUE));
bar.setProgress(percentage);
world.getPlayers().forEach(bar::addPlayer);
}
/**
* Replaces all available placeholders in a given string.
*
* @param world The world context.
* @param message The raw message with placeholders.
* @return The provided message with placeholders replaced with correct values for the world context.
*/
@NotNull
private String prepareMessage(final World world, final String message) {
Checker checker = harbor.getChecker();
@ -111,38 +140,22 @@ public class Messages implements Listener {
.replace("[more]", String.valueOf(checker.getNeeded(world))));
}
@NotNull
private BarColor getBarColor(final String enumString) {
BarColor barColor;
try {
barColor = BarColor.valueOf(enumString.toUpperCase().trim());
} catch (IllegalArgumentException e) {
barColor = BarColor.BLUE;
}
return barColor;
}
public void clearBar(@NotNull World world) {
BossBar bar = bossBars.get(world.getUID());
if (bar == null) {
return;
}
bar.removeAll();
}
/**
* Creates a new bossbar for the given world if one isn't already present.
*
* @param world The world in which to create the bossbar.
*/
private void registerBar(@NotNull World world) {
BossBar bar = bossBars.get(world.getUID());
bossBars.computeIfAbsent(world.getUID(), uuid -> Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID));
}
if (bar != null) {
return;
}
bossBars.put(world.getUID(), Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID));
System.out.println("registered bossbar for world " + world.getName());
/**
* Hides the bossbar for the given world if one is present.
*
* @param world The world in which to hide the bossbar.
*/
public void clearBar(@NotNull World world) {
Optional.ofNullable(bossBars.get(world.getUID())).ifPresent(BossBar::removeAll);
}
@EventHandler

View File

@ -15,6 +15,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
import xyz.nkomarn.harbor.Harbor;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@ -32,6 +33,7 @@ public class PlayerManager implements Listener {
/**
* Gets the last tracked cooldown time for a given player.
*
* @param player The player for which to return cooldown time.
* @return The player's last cooldown time.
*/
@ -39,12 +41,19 @@ public class PlayerManager implements Listener {
return cooldowns.getOrDefault(player.getUniqueId(), 0);
}
// TODO javadoc
/**
* Sets a player's cooldown to a specific, fixed value.
*
* @param player The player for which to set cooldown.
* @param cooldown The cooldown value.
*/
public void setCooldown(@NotNull Player player, long cooldown) {
cooldowns.put(player.getUniqueId(), cooldown);
}
// TODO javadocs
/**
* Resets every players' message cooldown.
*/
public void clearCooldowns() {
cooldowns.clear();
}
@ -60,9 +69,9 @@ public class PlayerManager implements Listener {
return false;
}
Essentials essentials = harbor.getEssentials();
if (essentials != null) {
User user = essentials.getUser(player);
Optional<Essentials> essentials = harbor.getEssentials();
if (essentials.isPresent()) {
User user = essentials.get().getUser(player);
if (user != null) {
return user.isAfk();
@ -86,6 +95,13 @@ public class PlayerManager implements Listener {
playerActivity.put(player.getUniqueId(), System.currentTimeMillis());
}
/**
* Registers Harbor's fallback listeners for AFK detection if Essentials is not present.
*/
public void registerFallbackListeners() {
harbor.getServer().getPluginManager().registerEvents(new AfkListeners(), harbor);
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
UUID uuid = event.getPlayer().getUniqueId();
@ -93,23 +109,26 @@ public class PlayerManager implements Listener {
playerActivity.removeLong(uuid);
}
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
updateActivity(event.getPlayer());
}
private final class AfkListeners implements Listener {
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
updateActivity(event.getPlayer());
}
@EventHandler(ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent event) {
updateActivity(event.getPlayer());
}
@EventHandler
public void onMove(PlayerMoveEvent event) {
updateActivity(event.getPlayer());
}
@EventHandler(ignoreCancelled = true)
public void onCommand(PlayerCommandPreprocessEvent event) {
updateActivity(event.getPlayer());
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
updateActivity((Player) event.getWhoClicked());
@EventHandler(ignoreCancelled = true)
public void onMove(PlayerMoveEvent event) {
updateActivity(event.getPlayer());
}
@EventHandler(ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
updateActivity((Player) event.getWhoClicked());
}
}
}

View File

@ -63,7 +63,7 @@ messages:
message: "&f&lEveryone is sleeping. Sweet dreams!"
color: GREEN
miscellaneous:
chat-prefix: "&8&l(&6&lHarbor&8&l)&7 "
chat-prefix: "&8&l(&6&lHarbor&8&l)&f "
unrecognized-command: "Unrecognized command."
# Spooky internal controls