From 822ad01f189445fa760490d6392741e6ab56c558 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 6 Feb 2021 23:20:38 -0500 Subject: [PATCH] v2.11.0 - Merged a pull request for Purpur AFK support if other plugins don't manage it - Added enums to represent server types rather than multiple booleans - Delayed processing leave bed event because player is still considered sleeping during event - Moved server type and nms version detection to LoggablePlugin superclass. - Setting resource ID now automatically checks for updates - Now compiling against Java 11 as recommended by the server --- pom.xml | 6 +-- .../com/luffbox/smoothsleep/DataStore.java | 27 +++++++----- .../com/luffbox/smoothsleep/SmoothSleep.java | 13 +----- .../smoothsleep/lib/LoggablePlugin.java | 44 ++++++++++++++++--- .../listeners/PlayerListeners.java | 26 ++++++++--- 5 files changed, 78 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index 4c53c33..10e2d1e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,14 +6,14 @@ com.luffbox smoothsleep - 2.10.0 + 2.11.0 jar SmoothSleep Smooth sleeping experience for multiplayer Bukkit servers - 1.8 + 11 UTF-8 @@ -32,7 +32,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.1.0 + 3.2.3 diff --git a/src/main/java/com/luffbox/smoothsleep/DataStore.java b/src/main/java/com/luffbox/smoothsleep/DataStore.java index b5e61fd..7efd093 100644 --- a/src/main/java/com/luffbox/smoothsleep/DataStore.java +++ b/src/main/java/com/luffbox/smoothsleep/DataStore.java @@ -1,6 +1,7 @@ package com.luffbox.smoothsleep; import com.luffbox.smoothsleep.lib.ConfigHelper; +import com.luffbox.smoothsleep.lib.LoggablePlugin; import com.luffbox.smoothsleep.lib.Purgeable; import com.luffbox.smoothsleep.lib.actionbar.ActionBarHelper; import com.luffbox.smoothsleep.lib.actionbar.NmsActionBarHelper; @@ -36,7 +37,7 @@ public DataStore(SmoothSleep plugin) { Plugin cmi = pl.getServer().getPluginManager().getPlugin("CMI"); if (ess != null && ess.isEnabled()) { userHelper = new EssUserHelper(pl); } else if (cmi != null && cmi.isEnabled()) { userHelper = new CmiUserHelper(pl); } - else if (SmoothSleep.isPurpurServer) { userHelper = new PurpurUserHelper(); } + else if (SmoothSleep.serverType == LoggablePlugin.ServerType.PURPUR) { userHelper = new PurpurUserHelper(); } else { userHelper = new DefUserHelper(); } Plugin papi = pl.getServer().getPluginManager().getPlugin("PlaceholderAPI"); @@ -44,20 +45,22 @@ public DataStore(SmoothSleep plugin) { placeholders = new PlaceholderAPIHelper(pl); } else { placeholders = new DefPlaceholderHelper(pl); } - try { // See if the Paper method exists - Player.class.getMethod("sendActionBar", String.class); - SmoothSleep.logDebug("Detected Paper, using Player#sendActionBar() to send action bar"); - actionBarHelper = new PaperActionHelper(); - } catch (Exception e1) { - try { // See if the Spigot method exists - Player.class.getMethod("spigot"); - SmoothSleep.logDebug("Detected Spigot, using Player#spigot()#sendMessage() to send action bar"); - actionBarHelper = new SpigotActionBarHelper(); - } catch (Exception e2) { // Resort to NMS + switch (SmoothSleep.serverType) { + case BUKKIT: SmoothSleep.logDebug("Not using Paper or Spigot, resorting to NMS method to send action bar"); actionBarHelper = new NmsActionBarHelper(); - } + break; + case SPIGOT: + SmoothSleep.logDebug("Detected Spigot, using Player#spigot()#sendMessage() to send action bar"); + actionBarHelper = new SpigotActionBarHelper(); + break; + case PAPER: + case PURPUR: + SmoothSleep.logDebug("Detected Paper, using Player#sendActionBar() to send action bar"); + actionBarHelper = new PaperActionHelper(); + break; } + SmoothSleep.logDebug("DataStore initialized"); } diff --git a/src/main/java/com/luffbox/smoothsleep/SmoothSleep.java b/src/main/java/com/luffbox/smoothsleep/SmoothSleep.java index e35f663..9389dd9 100644 --- a/src/main/java/com/luffbox/smoothsleep/SmoothSleep.java +++ b/src/main/java/com/luffbox/smoothsleep/SmoothSleep.java @@ -7,7 +7,6 @@ import com.luffbox.smoothsleep.listeners.PlayerListeners; import com.luffbox.smoothsleep.tasks.EveryTickTask; import org.bstats.bukkit.Metrics; -import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.scheduler.BukkitTask; @@ -25,11 +24,6 @@ public final class SmoothSleep extends LoggablePlugin { TICKS_PER_HOUR = 72000L, TICKS_PER_MIN = 1200L; - public static String nmsver; - public static boolean isPaperServer; - public static boolean isPurpurServer; - public static boolean hasUpdate = false; - public DataStore data; public static Metrics metrics; @@ -37,12 +31,7 @@ public final class SmoothSleep extends LoggablePlugin { @Override public void onEnable() { - resourceId = "32043"; - nmsver = Bukkit.getServer().getClass().getPackage().getName(); - nmsver = nmsver.substring(nmsver.lastIndexOf(".") + 1); - isPaperServer = classExists("com.destroystokyo.paper.PaperConfig"); - isPurpurServer = classExists("net.pl3x.purpur.PurpurConfig"); - hasUpdate = checkUpdate(); + setResourceId("32043"); metrics = new Metrics(this, STAT_ID); data = new DataStore(this); // init() after assign so data variable isn't null diff --git a/src/main/java/com/luffbox/smoothsleep/lib/LoggablePlugin.java b/src/main/java/com/luffbox/smoothsleep/lib/LoggablePlugin.java index f1efc8a..4155a45 100644 --- a/src/main/java/com/luffbox/smoothsleep/lib/LoggablePlugin.java +++ b/src/main/java/com/luffbox/smoothsleep/lib/LoggablePlugin.java @@ -9,11 +9,6 @@ public class LoggablePlugin extends JavaPlugin { - private static LoggablePlugin inst; - protected String resourceId = null; - - public LoggablePlugin() { inst = this; } - public enum LogLevel { INFO("info"), DEBUG("debug"), @@ -25,6 +20,43 @@ public enum LogLevel { public String getLevelName() { return levelName; } } + public enum ServerType { // More 'specific' server at the bottom + BUKKIT(null), + SPIGOT("org.bukkit.entity.Player$Spigot"), + PAPER("com.destroystokyo.paper.PaperConfig"), + PURPUR("net.pl3x.purpur.PurpurConfig"); + + private final String classPath; + ServerType(String cp) { classPath = cp; } + public String getClassPath() { return classPath; } + } + + private static LoggablePlugin inst; + private String resourceId = null; + + public static String nmsver; + public static ServerType serverType = ServerType.BUKKIT; + public static boolean hasUpdate = false; + + public LoggablePlugin() { + inst = this; + nmsver = Bukkit.getServer().getClass().getPackage().getName(); + nmsver = nmsver.substring(nmsver.lastIndexOf(".") + 1); + for (ServerType st : ServerType.values()) { + logDebug("Checking if server is type: " + st.name()); + if (st.getClassPath() != null && classExists(st.getClassPath())) { + logDebug("Server contains " + st.name() + " specific class: " + st.getClassPath()); + serverType = st; + } + } + logDebug("Server type likely to be: " + serverType.name()); + } + + protected void setResourceId(String resId) { + this.resourceId = resId; + hasUpdate = checkUpdate(); + } + protected boolean checkUpdate() { if (resourceId == null) { logDebug("No resource ID specified, skipping update check."); return false; } try { @@ -42,7 +74,7 @@ protected boolean checkUpdate() { return false; } - public static boolean classExists(String className) { + protected boolean classExists(String className) { try { Class.forName(className); return true; diff --git a/src/main/java/com/luffbox/smoothsleep/listeners/PlayerListeners.java b/src/main/java/com/luffbox/smoothsleep/listeners/PlayerListeners.java index 595fdbc..875aec0 100644 --- a/src/main/java/com/luffbox/smoothsleep/listeners/PlayerListeners.java +++ b/src/main/java/com/luffbox/smoothsleep/listeners/PlayerListeners.java @@ -6,9 +6,13 @@ import com.luffbox.smoothsleep.lib.ConfigHelper; import com.luffbox.smoothsleep.tasks.UpdateNotifyTask; import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.*; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.Set; public class PlayerListeners implements Listener { @@ -64,14 +68,26 @@ public void enterBed(PlayerBedEnterEvent e) { @EventHandler(ignoreCancelled = true) public void leaveBed(PlayerBedLeaveEvent e) { if (!pl.isEnabled()) { return; } - World w = e.getPlayer().getWorld(); + final World w = e.getPlayer().getWorld(); if (!pl.data.worldEnabled(w)) { return; } - WorldData wd = pl.data.getWorldData(w); + final WorldData wd = pl.data.getWorldData(w); if (wd == null) { SmoothSleep.logWarning("An error occurred while handing PlayerBedLeaveEvent. Missing WorldData."); return; } - PlayerData pd = pl.data.getPlayerData(e.getPlayer()); + final PlayerData pd = pl.data.getPlayerData(e.getPlayer()); if (pd == null) { SmoothSleep.logWarning("An error occurred while handling PlayerBedLeaveEvent. Missing PlayerData."); return; } - pd.wake(); - if (wd.getSleepers().isEmpty()) { wd.stopSleepTick(); } + + (new BukkitRunnable() { + @Override + public void run() { + pd.wake(); + Set sleepers = wd.getSleepers(); + sleepers.remove(pd.getPlayer()); + if (sleepers.isEmpty()) { + wd.stopSleepTick(); + } else { + SmoothSleep.logDebug("Bed leave: Other players still sleeping"); + } + } + }).runTaskLater(pl, 0L); } }