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 configManager; + private final PacketSender packetSender = new PacketSender(); + private final Platform

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 { - private OperatorLevel operatorLevel; - private PaperLuckPermsManager luckPermsManager; + private OperatorLevel operatorLevel; + private PaperLevelProviderFactory levelProviderFactory; @Override public void onEnable() { + this.operatorLevel = new OperatorLevel<>(this); + this.levelProviderFactory = new PaperLevelProviderFactory(this.operatorLevel); + PluginManager manager = getServer().getPluginManager(); if (manager.isPluginEnabled("LuckPerms")) { RegisteredServiceProvider provider = this.getServer().getServicesManager().getRegistration(LuckPerms.class); if (provider != null) { - this.luckPermsManager = new PaperLuckPermsManager(this, provider.getProvider()); + LuckPerms luckPerms = provider.getProvider(); + this.levelProviderFactory.createLuckPermsProvider(luckPerms); + this.operatorLevel.registerLuckPermsListener(luckPerms); } } - this.operatorLevel = new OperatorLevel(this); - this.operatorLevel.reload(); + this.operatorLevel.load(); - OperatorLevelCommand operatorLevelCommand = new OperatorLevelCommand(this); + OperatorLevelCommand operatorLevelCommand = new OperatorLevelCommand(this, this.operatorLevel); operatorLevelCommand.register(); - manager.registerEvents(new PlayerListener(this), this); + registerListeners(); new Metrics(this, 23464); } @Override - public void updateOpLevel(Player player) { - if (!this.operatorLevel.config().luckPermsMeta()) { - updateLevelPermission(player); - return; - } - - if (this.luckPermsManager == null) { - logger().warn("luckPermsMeta config option was enabled, but LuckPerms isn't enabled. Falling back to permission-based check."); - updateLevelPermission(player); - return; - } - - int level = this.luckPermsManager.level(player); - this.operatorLevel.sendPacket(player, level); + public LevelProviderFactory levelProviderFactory() { + return this.levelProviderFactory; } @Override - public void updateOpLevel(UUID uuid, int level) { - Player player = this.getServer().getPlayer(uuid); - if (player == null) { - return; - } - - if (!this.operatorLevel.config().luckPermsMeta()) { - updateLevelPermission(player); - return; - } + public void registerListeners() { + PluginManager manager = getServer().getPluginManager(); + manager.registerEvents(new PlayerListener(this.operatorLevel), this); + } - this.operatorLevel.sendPacket(player, level); + @Override + public void updateAll() { + getServer().getOnlinePlayers().forEach(this.operatorLevel::updateLevel); } - private void updateLevelPermission(Player player) { - int level = 0; - for (int i = 4; i > 0; i--) { - if (player.hasPermission("operatorlevel.level." + i)) { - level = i; - break; - } - } - this.operatorLevel.sendPacket(player, level); + @Override + public Player player(UUID uuid) { + return getServer().getPlayer(uuid); } @Override @@ -94,9 +77,4 @@ public Path dataPath() { //TODO Switch to this.getDataPath() when minimum version becomes 1.21 return this.getDataFolder().toPath(); } - - public void reload() { - this.operatorLevel.reload(); - this.getServer().getOnlinePlayers().forEach(this::updateOpLevel); - } } diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/command/OperatorLevelCommand.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/command/OperatorLevelCommand.java index 3473036..b3d444f 100644 --- a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/command/OperatorLevelCommand.java +++ b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/command/OperatorLevelCommand.java @@ -4,6 +4,7 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.tree.LiteralCommandNode; +import fi.fabianadrian.operatorlevel.common.OperatorLevel; import fi.fabianadrian.operatorlevel.paper.OperatorLevelPaper; import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.Commands; @@ -11,18 +12,19 @@ import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import static io.papermc.paper.command.brigadier.Commands.literal; @SuppressWarnings("UnstableApiUsage") public final class OperatorLevelCommand { - private final OperatorLevelPaper plugin; private final LifecycleEventManager manager; private final Component reloadMessage; + private final OperatorLevel operatorLevel; - public OperatorLevelCommand(OperatorLevelPaper plugin) { - this.plugin = plugin; + public OperatorLevelCommand(OperatorLevelPaper plugin, OperatorLevel operatorLevel) { + this.operatorLevel = operatorLevel; this.manager = plugin.getLifecycleManager(); this.reloadMessage = MiniMessage.miniMessage().deserialize( @@ -45,7 +47,7 @@ public void register() { } private int executeReload(CommandContext ctx) { - this.plugin.reload(); + this.operatorLevel.load(); ctx.getSource().getSender().sendMessage(this.reloadMessage); return Command.SINGLE_SUCCESS; } diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperLevelProviderFactory.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperLevelProviderFactory.java new file mode 100644 index 0000000..cbc2e1d --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperLevelProviderFactory.java @@ -0,0 +1,32 @@ +package fi.fabianadrian.operatorlevel.paper.level; + +import fi.fabianadrian.operatorlevel.common.OperatorLevel; +import fi.fabianadrian.operatorlevel.common.level.LevelProviderFactory; +import fi.fabianadrian.operatorlevel.common.level.LuckPermsLevelProvider; +import fi.fabianadrian.operatorlevel.common.level.PermissionLevelProvider; +import net.luckperms.api.LuckPerms; +import org.bukkit.entity.Player; + +public final class PaperLevelProviderFactory extends LevelProviderFactory { + private final PermissionLevelProvider permissionLevelProvider; + private LuckPermsLevelProvider luckPermsLevelProvider; + + public PaperLevelProviderFactory(OperatorLevel operatorLevel) { + super(operatorLevel); + this.permissionLevelProvider = new PaperPermissionLevelProvider(); + } + + @Override + protected PermissionLevelProvider permissionLevelProvider() { + return this.permissionLevelProvider; + } + + @Override + protected LuckPermsLevelProvider luckPermsLevelProvider() { + return this.luckPermsLevelProvider; + } + + public void createLuckPermsProvider(LuckPerms luckPerms) { + this.luckPermsLevelProvider = new PaperLuckPermsLevelProvider(luckPerms, this.logger); + } +} diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperLuckPermsLevelProvider.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperLuckPermsLevelProvider.java new file mode 100644 index 0000000..736f5a5 --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperLuckPermsLevelProvider.java @@ -0,0 +1,18 @@ +package fi.fabianadrian.operatorlevel.paper.level; + +import fi.fabianadrian.operatorlevel.common.level.LuckPermsLevelProvider; +import net.luckperms.api.LuckPerms; +import net.luckperms.api.model.user.User; +import org.bukkit.entity.Player; +import org.slf4j.Logger; + +public final class PaperLuckPermsLevelProvider extends LuckPermsLevelProvider { + public PaperLuckPermsLevelProvider(LuckPerms api, Logger logger) { + super(api, logger); + } + + @Override + protected User user(Player player) { + return super.api.getPlayerAdapter(Player.class).getUser(player); + } +} diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperPermissionLevelProvider.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperPermissionLevelProvider.java new file mode 100644 index 0000000..f2736c3 --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/level/PaperPermissionLevelProvider.java @@ -0,0 +1,11 @@ +package fi.fabianadrian.operatorlevel.paper.level; + +import fi.fabianadrian.operatorlevel.common.level.PermissionLevelProvider; +import org.bukkit.entity.Player; + +public final class PaperPermissionLevelProvider extends PermissionLevelProvider { + @Override + protected boolean hasPermission(Player player, String permission) { + return player.hasPermission(permission); + } +} diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/listener/PlayerListener.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/listener/PlayerListener.java index 44ed71e..7f2b1a5 100644 --- a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/listener/PlayerListener.java +++ b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/listener/PlayerListener.java @@ -1,19 +1,20 @@ package fi.fabianadrian.operatorlevel.paper.listener; -import fi.fabianadrian.operatorlevel.paper.OperatorLevelPaper; +import fi.fabianadrian.operatorlevel.common.OperatorLevel; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; public final class PlayerListener implements Listener { - private final OperatorLevelPaper plugin; + private final OperatorLevel operatorLevel; - public PlayerListener(OperatorLevelPaper plugin) { - this.plugin = plugin; + public PlayerListener(OperatorLevel operatorLevel) { + this.operatorLevel = operatorLevel; } @EventHandler public void onJoin(PlayerJoinEvent event) { - this.plugin.updateOpLevel(event.getPlayer()); + this.operatorLevel.updateLevel(event.getPlayer()); } } diff --git a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/luckperms/PaperLuckPermsManager.java b/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/luckperms/PaperLuckPermsManager.java deleted file mode 100644 index cccecbb..0000000 --- a/paper/src/main/java/fi/fabianadrian/operatorlevel/paper/luckperms/PaperLuckPermsManager.java +++ /dev/null @@ -1,21 +0,0 @@ -package fi.fabianadrian.operatorlevel.paper.luckperms; - -import fi.fabianadrian.operatorlevel.paper.OperatorLevelPaper; -import fi.fabianadrian.operatorlevel.common.luckperms.LuckPermsManager; -import net.luckperms.api.LuckPerms; -import net.luckperms.api.model.user.User; -import net.luckperms.api.platform.PlayerAdapter; -import org.bukkit.entity.Player; - -public final class PaperLuckPermsManager extends LuckPermsManager { - private final PlayerAdapter playerAdapter; - public PaperLuckPermsManager(OperatorLevelPaper plugin, LuckPerms luckPerms) { - super(plugin, luckPerms); - this.playerAdapter = luckPerms.getPlayerAdapter(Player.class); - } - - public int level(Player player) { - User user = this.playerAdapter.getUser(player); - return level(user); - } -}