diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/OperatorLevel.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/OperatorLevel.java index 7621bdc..b583135 100644 --- a/common/src/main/java/fi/fabianadrian/operatorlevel/common/OperatorLevel.java +++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/OperatorLevel.java @@ -2,18 +2,26 @@ import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.event.PacketListenerPriority; -import com.github.retrooper.packetevents.protocol.player.User; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityStatus; import fi.fabianadrian.operatorlevel.common.config.ConfigManager; import fi.fabianadrian.operatorlevel.common.config.OperatorLevelConfig; +import fi.fabianadrian.operatorlevel.common.level.LevelProvider; +import fi.fabianadrian.operatorlevel.common.listener.LuckPermsListener; import fi.fabianadrian.operatorlevel.common.locale.TranslationManager; import fi.fabianadrian.operatorlevel.common.packet.PacketListener; +import fi.fabianadrian.operatorlevel.common.packet.PacketSender; import fi.fabianadrian.operatorlevel.common.platform.Platform; +import net.luckperms.api.LuckPerms; +import org.slf4j.Logger; -public final class OperatorLevel { +public final class OperatorLevel
{
private final ConfigManager platform;
+ private LevelProvider levelProvider;
+
+ public OperatorLevel(Platform platform) {
+ this.platform = platform;
- public OperatorLevel(Platform> platform) {
new TranslationManager(platform.logger());
this.configManager = ConfigManager.create(
@@ -30,17 +38,22 @@ public OperatorLevelConfig config() {
return this.configManager.config();
}
- public void reload() {
+ public void load() {
this.configManager.load();
+ this.levelProvider = this.platform.levelProviderFactory().levelProvider();
+ this.platform.updateAll();
+ }
+
+ public Logger logger() {
+ return this.platform.logger();
}
- public void sendPacket(Object player, int level) {
- User user = PacketEvents.getAPI().getPlayerManager().getUser(player);
- if (user == null) {
- return;
- }
+ public void updateLevel(P player) {
+ int level = this.levelProvider.level(player);
+ this.packetSender.sendPacket(player, level);
+ }
- WrapperPlayServerEntityStatus packet = new WrapperPlayServerEntityStatus(user.getEntityId(), 24 + level);
- user.sendPacketSilently(packet);
+ public void registerLuckPermsListener(LuckPerms luckPerms) {
+ new LuckPermsListener<>(this.platform, luckPerms, this);
}
}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LevelProvider.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LevelProvider.java
new file mode 100644
index 0000000..7d946fc
--- /dev/null
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LevelProvider.java
@@ -0,0 +1,5 @@
+package fi.fabianadrian.operatorlevel.common.level;
+
+public interface LevelProvider {
+ int level(P player);
+}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LevelProviderFactory.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LevelProviderFactory.java
new file mode 100644
index 0000000..2caeb6d
--- /dev/null
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LevelProviderFactory.java
@@ -0,0 +1,32 @@
+package fi.fabianadrian.operatorlevel.common.level;
+
+import fi.fabianadrian.operatorlevel.common.OperatorLevel;
+import org.slf4j.Logger;
+
+public abstract class LevelProviderFactory {
+ private final OperatorLevel operatorLevel;
+ protected final Logger logger;
+
+ public LevelProviderFactory(OperatorLevel operatorLevel) {
+ this.operatorLevel = operatorLevel;
+ this.logger = this.operatorLevel.logger();
+ }
+
+ public LevelProvider levelProvider() {
+ if (!this.operatorLevel.config().luckPermsMeta()) {
+ return permissionLevelProvider();
+ }
+
+ LevelProvider luckPermsProvider = luckPermsLevelProvider();
+ if (luckPermsProvider == null) {
+ this.logger.warn("luckPermsMeta config option was enabled, but LuckPerms isn't enabled. Falling back to a permission-based check.");
+ return permissionLevelProvider();
+ }
+
+ return luckPermsProvider;
+ }
+
+ protected abstract PermissionLevelProvider permissionLevelProvider();
+
+ protected abstract LuckPermsLevelProvider luckPermsLevelProvider();
+}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/luckperms/LuckPermsManager.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LuckPermsLevelProvider.java
similarity index 50%
rename from common/src/main/java/fi/fabianadrian/operatorlevel/common/luckperms/LuckPermsManager.java
rename to common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LuckPermsLevelProvider.java
index e8c017b..4036e30 100644
--- a/common/src/main/java/fi/fabianadrian/operatorlevel/common/luckperms/LuckPermsManager.java
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/LuckPermsLevelProvider.java
@@ -1,22 +1,22 @@
-package fi.fabianadrian.operatorlevel.common.luckperms;
+package fi.fabianadrian.operatorlevel.common.level;
-import fi.fabianadrian.operatorlevel.common.platform.Platform;
import net.luckperms.api.LuckPerms;
-import net.luckperms.api.event.EventBus;
-import net.luckperms.api.event.user.UserDataRecalculateEvent;
import net.luckperms.api.model.user.User;
+import org.slf4j.Logger;
-public abstract class LuckPermsManager {
- private final Platform> platform;
+public abstract class LuckPermsLevelProvider implements LevelProvider {
+ protected final LuckPerms api;
+ private final Logger logger;
- public LuckPermsManager(Platform> platform, LuckPerms luckPerms) {
- this.platform = platform;
-
- EventBus eventBus = luckPerms.getEventBus();
- eventBus.subscribe(platform, UserDataRecalculateEvent.class, this::onUserDataRecalculate);
+ public LuckPermsLevelProvider(LuckPerms api, Logger logger) {
+ this.api = api;
+ this.logger = logger;
}
- protected int level(User user) {
+ @Override
+ public int level(P player) {
+ User user = user(player);
+
String unparsed = user.getCachedData().getMetaData().getMetaValue("operatorlevel");
if (unparsed == null) {
return 0;
@@ -26,7 +26,7 @@ protected int level(User user) {
try {
level = Integer.parseInt(unparsed);
} catch (NumberFormatException e) {
- this.platform.logger().warn(
+ this.logger.warn(
"Operator level must be a number between 0 and 4 but {} has a meta value of \"{}\"! Please check your LuckPerms configuration.",
user.getUsername(),
unparsed
@@ -36,7 +36,7 @@ protected int level(User user) {
// Make sure that the level is always between 0 and 4
if (level < 0 || level > 4) {
- this.platform.logger().warn(
+ this.logger.warn(
"Operator level must be between 0 and 4 but {} has a level of {}! Please check your LuckPerms configuration.",
user.getUsername(),
level
@@ -47,8 +47,5 @@ protected int level(User user) {
return (byte) level;
}
- private void onUserDataRecalculate(UserDataRecalculateEvent event) {
- User user = event.getUser();
- this.platform.updateOpLevel(user.getUniqueId(), level(user));
- }
+ protected abstract User user(P player);
}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/PermissionLevelProvider.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/PermissionLevelProvider.java
new file mode 100644
index 0000000..162e6c6
--- /dev/null
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/level/PermissionLevelProvider.java
@@ -0,0 +1,17 @@
+package fi.fabianadrian.operatorlevel.common.level;
+
+public abstract class PermissionLevelProvider implements LevelProvider {
+ @Override
+ public int level(P player) {
+ int level = 0;
+ for (int i = 4; i > 0; i--) {
+ if (hasPermission(player, "operatorlevel.level." + i)) {
+ level = i;
+ break;
+ }
+ }
+ return level;
+ }
+
+ protected abstract boolean hasPermission(P player, String permission);
+}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/listener/LuckPermsListener.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/listener/LuckPermsListener.java
new file mode 100644
index 0000000..0543026
--- /dev/null
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/listener/LuckPermsListener.java
@@ -0,0 +1,29 @@
+package fi.fabianadrian.operatorlevel.common.listener;
+
+import fi.fabianadrian.operatorlevel.common.OperatorLevel;
+import fi.fabianadrian.operatorlevel.common.platform.Platform;
+import net.luckperms.api.LuckPerms;
+import net.luckperms.api.event.EventBus;
+import net.luckperms.api.event.user.UserDataRecalculateEvent;
+
+public final class LuckPermsListener {
+ private final Platform platform;
+ private final OperatorLevel operatorLevel;
+
+ public LuckPermsListener(Platform platform, LuckPerms luckPerms, OperatorLevel operatorLevel) {
+ this.platform = platform;
+ this.operatorLevel = operatorLevel;
+
+ EventBus eventBus = luckPerms.getEventBus();
+ eventBus.subscribe(platform, UserDataRecalculateEvent.class, this::onUserDataRecalculate);
+ }
+
+ private void onUserDataRecalculate(UserDataRecalculateEvent event) {
+ P player = this.platform.player(event.getUser().getUniqueId());
+ if (player == null) {
+ return;
+ }
+
+ this.operatorLevel.updateLevel(player);
+ }
+}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/packet/PacketSender.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/packet/PacketSender.java
new file mode 100644
index 0000000..1ab61b5
--- /dev/null
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/packet/PacketSender.java
@@ -0,0 +1,17 @@
+package fi.fabianadrian.operatorlevel.common.packet;
+
+import com.github.retrooper.packetevents.PacketEvents;
+import com.github.retrooper.packetevents.protocol.player.User;
+import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityStatus;
+
+public final class PacketSender {
+ public void sendPacket(Object player, int level) {
+ User user = PacketEvents.getAPI().getPlayerManager().getUser(player);
+ if (user == null) {
+ return;
+ }
+
+ WrapperPlayServerEntityStatus packet = new WrapperPlayServerEntityStatus(user.getEntityId(), 24 + level);
+ user.sendPacketSilently(packet);
+ }
+}
diff --git a/common/src/main/java/fi/fabianadrian/operatorlevel/common/platform/Platform.java b/common/src/main/java/fi/fabianadrian/operatorlevel/common/platform/Platform.java
index 3d76e34..9019fcb 100644
--- a/common/src/main/java/fi/fabianadrian/operatorlevel/common/platform/Platform.java
+++ b/common/src/main/java/fi/fabianadrian/operatorlevel/common/platform/Platform.java
@@ -1,5 +1,6 @@
package fi.fabianadrian.operatorlevel.common.platform;
+import fi.fabianadrian.operatorlevel.common.level.LevelProviderFactory;
import org.slf4j.Logger;
import java.nio.file.Path;
@@ -8,18 +9,13 @@
public interface Platform {
Logger logger();
- /**
- * @return the plugin's data directory
- */
Path dataPath();
- void updateOpLevel(P player);
+ LevelProviderFactory levelProviderFactory();
- /**
- * This is only called from LuckPermsManager
- *
- * @param uuid UUID of the player
- * @param level OP level that will be sent to the player
- */
- void updateOpLevel(UUID uuid, int level);
+ void registerListeners();
+
+ void updateAll();
+
+ P player(UUID uuid);
}
diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/OperatorLevelPaper.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/OperatorLevelPaper.java
index fad35ad..88a70b4 100644
--- a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/OperatorLevelPaper.java
+++ b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/OperatorLevelPaper.java
@@ -1,10 +1,11 @@
package fi.fabianadrian.operatorlevel.paper;
import fi.fabianadrian.operatorlevel.common.OperatorLevel;
+import fi.fabianadrian.operatorlevel.common.level.LevelProviderFactory;
import fi.fabianadrian.operatorlevel.common.platform.Platform;
import fi.fabianadrian.operatorlevel.paper.command.OperatorLevelCommand;
+import fi.fabianadrian.operatorlevel.paper.level.PaperLevelProviderFactory;
import fi.fabianadrian.operatorlevel.paper.listener.PlayerListener;
-import fi.fabianadrian.operatorlevel.paper.luckperms.PaperLuckPermsManager;
import net.luckperms.api.LuckPerms;
import org.bstats.bukkit.Metrics;
import org.bukkit.entity.Player;
@@ -17,71 +18,53 @@
import java.util.UUID;
public final class OperatorLevelPaper extends JavaPlugin implements Platform