Skip to content

Commit

Permalink
big refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmakila committed Dec 2, 2024
1 parent c2f2b95 commit b0c4366
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> {
private final ConfigManager<OperatorLevelConfig> configManager;
private final PacketSender packetSender = new PacketSender();
private final Platform<P> platform;
private LevelProvider<P> levelProvider;

public OperatorLevel(Platform<P> platform) {
this.platform = platform;

public OperatorLevel(Platform<?> platform) {
new TranslationManager(platform.logger());

this.configManager = ConfigManager.create(
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fi.fabianadrian.operatorlevel.common.level;

public interface LevelProvider<P> {
int level(P player);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fi.fabianadrian.operatorlevel.common.level;

import fi.fabianadrian.operatorlevel.common.OperatorLevel;
import org.slf4j.Logger;

public abstract class LevelProviderFactory<P> {
private final OperatorLevel<P> operatorLevel;
protected final Logger logger;

public LevelProviderFactory(OperatorLevel<P> operatorLevel) {
this.operatorLevel = operatorLevel;
this.logger = this.operatorLevel.logger();
}

public LevelProvider<P> levelProvider() {
if (!this.operatorLevel.config().luckPermsMeta()) {
return permissionLevelProvider();
}

LevelProvider<P> 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<P> permissionLevelProvider();

protected abstract LuckPermsLevelProvider<P> luckPermsLevelProvider();
}
Original file line number Diff line number Diff line change
@@ -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<P> implements LevelProvider<P> {
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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fi.fabianadrian.operatorlevel.common.level;

public abstract class PermissionLevelProvider<P> implements LevelProvider<P> {
@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);
}
Original file line number Diff line number Diff line change
@@ -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<P> {
private final Platform<P> platform;
private final OperatorLevel<P> operatorLevel;

public LuckPermsListener(Platform<P> platform, LuckPerms luckPerms, OperatorLevel<P> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,18 +9,13 @@
public interface Platform<P> {
Logger logger();

/**
* @return the plugin's data directory
*/
Path dataPath();

void updateOpLevel(P player);
LevelProviderFactory<P> 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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,71 +18,53 @@
import java.util.UUID;

public final class OperatorLevelPaper extends JavaPlugin implements Platform<Player> {
private OperatorLevel operatorLevel;
private PaperLuckPermsManager luckPermsManager;
private OperatorLevel<Player> 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<LuckPerms> 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<Player> 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
Expand All @@ -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);
}
}
Loading

0 comments on commit b0c4366

Please sign in to comment.