Skip to content

Commit

Permalink
v2.11.0
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
BuildTools committed Feb 7, 2021
1 parent a169201 commit 822ad01
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 38 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<groupId>com.luffbox</groupId>
<artifactId>smoothsleep</artifactId>
<version>2.10.0</version>
<version>2.11.0</version>
<packaging>jar</packaging>

<name>SmoothSleep</name>

<description>Smooth sleeping experience for multiplayer Bukkit servers</description>
<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand All @@ -32,7 +32,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<version>3.2.3</version>
<configuration>
<relocations>
<relocation>
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/luffbox/smoothsleep/DataStore.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,28 +37,30 @@ 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");
if (papi != null && papi.isEnabled()) {
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");
}

Expand Down
13 changes: 1 addition & 12 deletions src/main/java/com/luffbox/smoothsleep/SmoothSleep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,24 +24,14 @@ 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;

private BukkitTask everyTickTask;

@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
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/com/luffbox/smoothsleep/lib/LoggablePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<Player> sleepers = wd.getSleepers();
sleepers.remove(pd.getPlayer());
if (sleepers.isEmpty()) {
wd.stopSleepTick();
} else {
SmoothSleep.logDebug("Bed leave: Other players still sleeping");
}
}
}).runTaskLater(pl, 0L);
}

}

0 comments on commit 822ad01

Please sign in to comment.