1
0
mirror of https://github.com/nkomarn/harbor.git synced 2024-11-16 07:05:22 +01:00

🧹 Config warning + small cleanups

Now warn users if their config is too old for Harbor to recognize.
Clean up some formatting.
This commit is contained in:
Mykyta 2020-04-25 08:19:34 -07:00
parent 7b4c55e9a3
commit 247fb0bd10
No known key found for this signature in database
GPG Key ID: C147E30C19EA3570
8 changed files with 60 additions and 43 deletions

View File

@ -5,6 +5,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import xyz.nkomarn.Harbor.command.HarborCommand;
import xyz.nkomarn.Harbor.listener.AfkListener;
import xyz.nkomarn.Harbor.listener.BedListener;
import xyz.nkomarn.Harbor.listener.PlayerListener;
import xyz.nkomarn.Harbor.task.Checker;
import xyz.nkomarn.Harbor.util.Config;
import xyz.nkomarn.Harbor.util.Metrics;
@ -21,6 +22,7 @@ public class Harbor extends JavaPlugin {
getCommand("harbor").setExecutor(new HarborCommand());
getServer().getPluginManager().registerEvents(new BedListener(), this);
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getScheduler().runTaskTimerAsynchronously(this,
new Checker(), 0L, Config.getInteger("interval") * 20);
@ -29,6 +31,11 @@ public class Harbor extends JavaPlugin {
getLogger().info("Essentials not present- registering fallback AFK detection system.");
getServer().getPluginManager().registerEvents(new AfkListener(), this);
}
if (!Config.getString("version").equals("1.6.2")) {
getLogger().warning("Your Harbor configuration is outdated- please regenerate your " +
"config or Harbor may not work properly!");
}
}
public static Harbor getHarbor() {

View File

@ -31,20 +31,19 @@ public class HarborCommand implements TabExecutor {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
+ "This command requires you to be a player."));
return true;
}
Player player = (Player) sender;
World world = player.getWorld();
if (Checker.SKIPPING_WORLDS.contains(world)) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
+ "This world's time is already being accelerated."));
} else {
Checker.SKIPPING_WORLDS.add(world);
new AccelerateNightTask(world).runTaskTimer(Harbor.getHarbor(), 0L, 1);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
+ "Forcing night skip in your world."));
Player player = (Player) sender;
World world = player.getWorld();
if (Checker.SKIPPING_WORLDS.contains(world)) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
+ "This world's time is already being accelerated."));
} else {
Checker.SKIPPING_WORLDS.add(world);
new AccelerateNightTask(world).runTaskTimer(Harbor.getHarbor(), 0L, 1);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix
+ "Forcing night skip in your world."));
}
}
} else {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix

View File

@ -16,7 +16,7 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class BedListener implements Listener {
private Map<UUID, Long> cooldowns = new HashMap<>();
private static final Map<UUID, Long> COOLDOWNS = new HashMap<>();
@EventHandler(ignoreCancelled = true)
public void onBedEnter(final PlayerBedEnterEvent event) {
@ -33,7 +33,7 @@ public class BedListener implements Listener {
Config.getString("messages.chat.player-sleeping")
.replace("[player]", event.getPlayer().getName())
.replace("[displayname]", event.getPlayer().getDisplayName()));
cooldowns.put(playerUuid, System.currentTimeMillis());
COOLDOWNS.put(playerUuid, System.currentTimeMillis());
}, 1);
}
@ -51,12 +51,12 @@ public class BedListener implements Listener {
Config.getString("messages.chat.player-left-bed")
.replace("[player]", event.getPlayer().getName())
.replace("[displayname]", event.getPlayer().getDisplayName()));
cooldowns.put(playerUuid, System.currentTimeMillis());
COOLDOWNS.put(playerUuid, System.currentTimeMillis());
}, 1);
}
private long getCooldown(final UUID playerUuid) {
if (!cooldowns.containsKey(playerUuid)) return 0;
return cooldowns.get(playerUuid);
if (!COOLDOWNS.containsKey(playerUuid)) return 0;
return COOLDOWNS.get(playerUuid);
}
}

View File

@ -0,0 +1,19 @@
package xyz.nkomarn.Harbor.listener;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import xyz.nkomarn.Harbor.util.Config;
public class PlayerListener implements Listener {
@EventHandler
public void onPlayerJoin(final PlayerJoinEvent event) {
if (event.getPlayer().hasPermission("harbor.admin")) {
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', String.format(
"%sYour Harbor configuration is outdated- please regenerate it or Harbor may not work properly.",
Config.getString("messages.miscellaneous.prefix") // Use old prefix location
)));
}
}
}

View File

@ -7,21 +7,21 @@ import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class Afk {
private static HashMap<Player, Long> activity = new HashMap<>();
private static final HashMap<Player, Long> ACTIVITY = new HashMap<>();
public static boolean isAfk(Player player) {
if (!Config.getBoolean("afk-detection.enabled")) return false;
if (Harbor.getEssentials() != null) {
if (!Config.getBoolean("afk-detection.enabled")) {
return false;
} else if (Harbor.getEssentials() != null) {
return Harbor.getEssentials().getUser(player).isAfk();
} else {
if (!ACTIVITY.containsKey(player)) return false;
long minutes = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - ACTIVITY.get(player));
return minutes >= Config.getInteger("afk-detection.timeout");
}
if (!activity.containsKey(player)) return false;
long minutes = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - activity.get(player));
return minutes >= Config.getInteger("afk-detection.timeout");
}
public static void updateActivity(Player player) {
activity.put(player, System.currentTimeMillis());
public static void updateActivity(final Player player) {
ACTIVITY.put(player, System.currentTimeMillis());
}
}

View File

@ -8,8 +8,7 @@ import java.util.List;
public class Config {
/**
* Fetches a boolean from the configuration
* if location is not found, <code>false</code> is returned
*
* if location is not found, false is returned
* @param location Configuration location of the boolean
*/
public static boolean getBoolean(final String location) {
@ -18,8 +17,7 @@ public class Config {
/**
* Fetches a string from the configuration
* if location is not found, <code>empty string</code> is returned
*
* if location is not found, empty string is returned
* @param location Configuration location of the string
*/
public static String getString(final String location) {
@ -28,8 +26,7 @@ public class Config {
/**
* Fetches an integer from the configuration
* if location is not found, <code>0</code> is returned
*
* if location is not found, 0 is returned
* @param location Configuration location of the integer
*/
public static int getInteger(final String location) {
@ -38,8 +35,7 @@ public class Config {
/**
* Fetches a double from the configuration
* if location is not found, <code>0.0</code> is returned
*
* if location is not found, 0.0 is returned
* @param location Configuration location of the double
*/
public static double getDouble(final String location) {
@ -48,8 +44,7 @@ public class Config {
/**
* Fetches a list from the configuration
* if location is not found, <code>empty list</code> is returned
*
* if location is not found, empty list is returned
* @param location Configuration location of the list
*/
public static List<String> getList(final String location) {

View File

@ -25,7 +25,6 @@ import java.util.zip.GZIPOutputStream;
/**
* bStats collects some data for plugin authors.
* <p>
* Check out https://bStats.org/ to learn more about bStats!
*/
@SuppressWarnings({"WeakerAccess", "unused"})
@ -51,7 +50,7 @@ public class Metrics {
private static final String URL = "https://bStats.org/submitData/bukkit";
// Is bStats enabled on this server?
private boolean enabled;
private final boolean enabled;
// Should failed requests be logged?
private static boolean logFailedRequests;
@ -264,7 +263,6 @@ public class Metrics {
if (logFailedRequests) {
this.plugin.getLogger().log(Level.SEVERE, "Encountered unexpected exception ", e);
}
continue; // continue looping since we cannot do any other thing.
}
}
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
@ -361,5 +359,4 @@ public class Metrics {
gzip.close();
return outputStream.toByteArray();
}
}

View File

@ -68,5 +68,5 @@ messages:
# Spooky internal controls
version: 1.6.2
interval: 2
interval: 1
debug: false