mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Add LuckPerms hook and fix PAPI parsing
This commit is contained in:
1 parent
dceb47e160
commit
6286811cb8
5 files changed
+112
-7
No files matched your search
+46
@@ -0,0 +1,46 @@
|
||||
package net.draycia.simplechat.managers.luckperms;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.managers.luckperms.contexts.CanSeeChannelCalculator;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.context.ContextCalculator;
|
||||
import net.luckperms.api.context.ContextManager;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LuckPermsHookManager {
|
||||
|
||||
private final SimpleChat simpleChat;
|
||||
private final ContextManager contextManager;
|
||||
private final List<ContextCalculator<Player>> registeredCalculators = new ArrayList<>();
|
||||
|
||||
public LuckPermsHookManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
LuckPerms luckPerms = this.simpleChat.getServer().getServicesManager().load(LuckPerms.class);
|
||||
|
||||
if (luckPerms == null) {
|
||||
throw new IllegalStateException("LuckPerms API not loaded.");
|
||||
}
|
||||
|
||||
this.contextManager = luckPerms.getContextManager();
|
||||
|
||||
register(() -> new CanSeeChannelCalculator(simpleChat));
|
||||
}
|
||||
|
||||
private void register(Supplier<ContextCalculator<Player>> calculatorSupplier) {
|
||||
ContextCalculator<Player> calculator = calculatorSupplier.get();
|
||||
|
||||
this.contextManager.registerCalculator(calculator);
|
||||
this.registeredCalculators.add(calculator);
|
||||
}
|
||||
|
||||
public void unregisterContexts() {
|
||||
this.registeredCalculators.forEach(this.contextManager::unregisterCalculator);
|
||||
this.registeredCalculators.clear();
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package net.draycia.simplechat.managers.luckperms.contexts;
|
||||
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.luckperms.api.context.ContextCalculator;
|
||||
import net.luckperms.api.context.ContextConsumer;
|
||||
import net.luckperms.api.context.ImmutableContextSet;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class CanSeeChannelCalculator implements ContextCalculator<Player> {
|
||||
|
||||
private static final String KEY = "can-see-channel";
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
|
||||
public CanSeeChannelCalculator(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(Player player, ContextConsumer consumer) {
|
||||
for (ChatChannel channel : simpleChat.getChannelManager().getRegistry().values()) {
|
||||
boolean canUse = channel.canPlayerUse(simpleChat.getUserService().wrap(player));
|
||||
|
||||
consumer.accept(KEY + ":" + channel.getKey(), canUse ? "true" : "false");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public net.luckperms.api.context.ContextSet estimatePotentialContexts() {
|
||||
ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
|
||||
|
||||
for (ChatChannel channel : simpleChat.getChannelManager().getRegistry().values()) {
|
||||
builder.add(KEY + ":" + channel.getKey(), "true");
|
||||
builder.add(KEY + ":" + channel.getKey(), "false");
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user