Skip to content

Commit

Permalink
Clean up + more Javadocs
Browse files Browse the repository at this point in the history
We're getting closer to a final, stable. *bug-free* build of refactored Harbor.
  • Loading branch information
nkomarn committed Aug 20, 2020
1 parent 54c0628 commit c2a32f2
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 121 deletions.
15 changes: 8 additions & 7 deletions src/main/java/xyz/nkomarn/harbor/Harbor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import xyz.nkomarn.harbor.util.PlayerManager;

import java.util.Arrays;
import java.util.Optional;

public class Harbor extends JavaPlugin {

Expand All @@ -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,
Expand All @@ -43,10 +45,9 @@ public void onEnable() {

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")) {
Expand Down Expand Up @@ -90,8 +91,8 @@ public PlayerManager getPlayerManager() {
return playerManager;
}

@Nullable
public Essentials getEssentials() {
return essentials;
@NotNull
public Optional<Essentials> getEssentials() {
return Optional.ofNullable(essentials);
}
}
4 changes: 2 additions & 2 deletions src/main/java/xyz/nkomarn/harbor/command/HarborCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
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;
}

Expand All @@ -51,8 +51,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
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;
Expand Down
58 changes: 24 additions & 34 deletions src/main/java/xyz/nkomarn/harbor/listener/BedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
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;
Expand All @@ -30,57 +28,49 @@ public void onBedEnter(PlayerBedEnterEvent event) {
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);
}

@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;
}
}
13 changes: 5 additions & 8 deletions src/main/java/xyz/nkomarn/harbor/task/AccelerateNightTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
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 {

Expand Down Expand Up @@ -46,10 +44,8 @@ public void run() {
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) {
Expand All @@ -61,8 +57,9 @@ public void run() {
harbor.getPlayerManager().clearCooldowns();
harbor.getMessages().sendRandomChatMessage(world, "messages.chat.night-skipped");
cancel();
} else {
world.setTime(time + (int) timeRate);
return;
}

world.setTime(time + (int) timeRate);
}
}
18 changes: 5 additions & 13 deletions src/main/java/xyz/nkomarn/harbor/task/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,12 @@ private void checkWorld(@NotNull World world) {
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;
Expand Down Expand Up @@ -207,7 +199,7 @@ private List<Player> getExcluded(@NotNull World world) {
* @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) {
Expand Down
85 changes: 49 additions & 36 deletions src/main/java/xyz/nkomarn/harbor/util/Messages.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,17 +19,13 @@
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) {
Expand All @@ -46,6 +43,12 @@ public Messages(@NotNull Harbor harbor) {
}
}

/**
* 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;
Expand All @@ -57,6 +60,12 @@ public void sendWorldChatMessage(@NotNull World world, @NotNull String message)
}
}

/**
* 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;
Expand All @@ -68,6 +77,12 @@ public void sendActionBarMessage(@NotNull World world, @NotNull String message)
}
}

/**
* 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);

Expand All @@ -78,6 +93,14 @@ public void sendRandomChatMessage(@NotNull World world, @NotNull String listLoca
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;
Expand All @@ -86,7 +109,6 @@ public void sendBossBarMessage(@NotNull World world, @NotNull String message, @N
BossBar bar = bossBars.get(world.getUID());

if (bar == null) {
System.out.println("bar null");
return;
}

Expand All @@ -96,11 +118,18 @@ public void sendBossBarMessage(@NotNull World world, @NotNull String message, @N
}

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();
Expand All @@ -111,38 +140,22 @@ private String prepareMessage(final World world, final String message) {
.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;
/**
* 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) {
bossBars.computeIfAbsent(world.getUID(), uuid -> Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID));
}

/**
* 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) {
BossBar bar = bossBars.get(world.getUID());

if (bar == null) {
return;
}

bar.removeAll();
}

private void registerBar(@NotNull World world) {
BossBar bar = bossBars.get(world.getUID());

if (bar != null) {
return;
}

bossBars.put(world.getUID(), Bukkit.createBossBar("", BarColor.WHITE, BarStyle.SOLID));
System.out.println("registered bossbar for world " + world.getName());
Optional.ofNullable(bossBars.get(world.getUID())).ifPresent(BossBar::removeAll);
}

@EventHandler
Expand Down
Loading

0 comments on commit c2a32f2

Please sign in to comment.