Skip to content

Commit

Permalink
refactor(bukkit): Move permission package
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Sep 26, 2024
1 parent 180c11b commit 92f6adc
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 43 deletions.
8 changes: 0 additions & 8 deletions bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.hjug.refactorfirst.plugin</groupId>
<artifactId>refactor-first-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<showDetails>false</showDetails>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import io.wdsj.asw.bukkit.listener.*;
import io.wdsj.asw.bukkit.listener.packet.ASWBookPacketListener;
import io.wdsj.asw.bukkit.listener.packet.ASWChatPacketListener;
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool;
import io.wdsj.asw.bukkit.manage.punish.PlayerAltController;
import io.wdsj.asw.bukkit.manage.punish.PlayerShadowController;
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter;
import io.wdsj.asw.bukkit.method.*;
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool;
import io.wdsj.asw.bukkit.proxy.bungee.BungeeCordChannel;
import io.wdsj.asw.bukkit.proxy.bungee.BungeeReceiver;
import io.wdsj.asw.bukkit.proxy.velocity.VelocityChannel;
Expand Down
3 changes: 3 additions & 0 deletions bukkit/src/main/java/io/wdsj/asw/bukkit/ai/AIProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* Interface for AI processors (Maybe more in the future?)
*/
public interface AIProcessor {
/**
* Shared thread pool for AI processors
*/
ExecutorService THREAD_POOL = VirtualThreadUtils.newVirtualThreadPerTaskExecutorOrProvided(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("ASW AIProcessor Thread-%d").setDaemon(true).build()));
default void shutdown() {
THREAD_POOL.shutdownNow();
Expand Down
17 changes: 17 additions & 0 deletions bukkit/src/main/java/io/wdsj/asw/bukkit/ai/OllamaProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
import io.github.amithkoujalgi.ollama4j.core.utils.PromptBuilder;
import io.wdsj.asw.bukkit.setting.PluginSettings;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

import static io.wdsj.asw.bukkit.AdvancedSensitiveWords.LOGGER;
import static io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager;

/**
* Ollama AI processor
*/
public class OllamaProcessor implements AIProcessor {
public static boolean isOllamaInit = false;
private static PromptBuilder promptBuilder;
Expand All @@ -19,6 +23,13 @@ public class OllamaProcessor implements AIProcessor {
public OllamaProcessor() {
}

/**
* Initialize the Ollama service
* @param modelAddress Ollama server address
* @param name Model name to use
* @param timeOut Timeout in seconds
* @param debug Whether to enable debug logging
*/
public void initService(String modelAddress, String name, int timeOut, boolean debug) {
modelName = name;
api = new OllamaAPI(modelAddress);
Expand Down Expand Up @@ -51,6 +62,12 @@ public void shutdown() {
isOllamaInit = false;
}

/**
* Process the input message using OllamaProcessor
* @param inputMessage The message to process
* @return A future that will contain the returned message from Ollama server, the future inside may be null
*/
@NotNull
public static CompletableFuture<String> process(String inputMessage) {
if (!isOllamaInit) {
throw new IllegalStateException("OllamaProcessor is not initialized");
Expand Down
13 changes: 13 additions & 0 deletions bukkit/src/main/java/io/wdsj/asw/bukkit/ai/OpenAIProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
import static io.wdsj.asw.bukkit.AdvancedSensitiveWords.LOGGER;
import static io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager;

/**
* OpenAI Moderation Processor.
*/
public class OpenAIProcessor implements AIProcessor {
public static boolean isOpenAiInit = false;
private static OpenAiClient client;
public OpenAIProcessor() {
}

/**
* Initialize the OpenAI moderation service.
* @param apikey the openai key
* @param debug whether to enable debug logging
*/
public void initService(String apikey, boolean debug) {
@SuppressWarnings("rawtypes")
OpenAiClient.Builder builder = OpenAiClient.builder()
Expand All @@ -44,6 +52,11 @@ public void shutdown() {
isOpenAiInit = false;
}

/**
* Process the input message using OpenAI moderation.
* @param inputMessage the input message
* @return A future contains the moderation response
*/
public static CompletableFuture<ModerationResponse> process(String inputMessage) {
if (!isOpenAiInit) {
throw new IllegalStateException("OpenAI Moderation Processor is not initialized");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.github.houbb.heaven.util.util.OsUtil;
import io.wdsj.asw.bukkit.AdvancedSensitiveWords;
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum;
import io.wdsj.asw.bukkit.manage.punish.Punishment;
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter;
import io.wdsj.asw.bukkit.permission.PermissionsEnum;
import io.wdsj.asw.bukkit.setting.PluginMessages;
import io.wdsj.asw.bukkit.setting.PluginSettings;
import io.wdsj.asw.bukkit.util.cache.BookCache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.wdsj.asw.bukkit.command;

import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum;
import io.wdsj.asw.bukkit.permission.PermissionsEnum;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.wdsj.asw.bukkit.manage.notice;

import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum;
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool;
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter;
import io.wdsj.asw.bukkit.permission.PermissionsEnum;
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool;
import io.wdsj.asw.bukkit.setting.PluginMessages;
import io.wdsj.asw.bukkit.type.ModuleType;
import io.wdsj.asw.bukkit.util.SchedulingUtils;
import io.wdsj.asw.bukkit.util.message.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Collection;
import java.util.List;

import static io.wdsj.asw.bukkit.AdvancedSensitiveWords.getScheduler;

public class Notifier {
/**
* Notice the operators
Expand All @@ -23,7 +22,7 @@ public class Notifier {
* @param censoredList censored list
*/
public static void notice(Player violatedPlayer, ModuleType moduleType, String originalMessage, List<String> censoredList) {
getScheduler().runTask(() -> {
SchedulingUtils.runSyncIfNotOnMainThread(() -> {
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
String message = MessageUtils.retrieveMessage(PluginMessages.ADMIN_REMINDER).replace("%player%", violatedPlayer.getName()).replace("%type%", moduleType.toString()).replace("%message%", originalMessage).replace("%censored_list%", censoredList.toString()).replace("%violation%", String.valueOf(ViolationCounter.getViolationCount(violatedPlayer)));
for (Player player : players) {
Expand All @@ -35,7 +34,7 @@ public static void notice(Player violatedPlayer, ModuleType moduleType, String o
}

public static void normalNotice(String message) {
getScheduler().runTask(() -> {
SchedulingUtils.runSyncIfNotOnMainThread(() -> {
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
for (Player player : players) {
if (CachingPermTool.hasPermission(PermissionsEnum.NOTICE, player)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.wdsj.asw.bukkit.manage.permission;
package io.wdsj.asw.bukkit.permission;

/**
* Permission enums
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.wdsj.asw.bukkit.manage.permission.cache;
package io.wdsj.asw.bukkit.permission.cache;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum;
import io.wdsj.asw.bukkit.permission.PermissionsEnum;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package io.wdsj.asw.bukkit.listener
import io.wdsj.asw.bukkit.AdvancedSensitiveWords
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package io.wdsj.asw.bukkit.listener
import io.wdsj.asw.bukkit.AdvancedSensitiveWords
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import io.wdsj.asw.bukkit.AdvancedSensitiveWords.*
import io.wdsj.asw.bukkit.ai.OllamaProcessor
import io.wdsj.asw.bukkit.ai.OpenAIProcessor
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down Expand Up @@ -199,7 +199,8 @@ class ChatListener : Listener {


private fun shouldNotProcess(player: Player): Boolean {
if (isInitialized && !CachingPermTool.hasPermission(PermissionsEnum.BYPASS, player)) {
if (isInitialized && !CachingPermTool.hasPermission(
PermissionsEnum.BYPASS, player)) {
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
if (!AuthMeApi.getInstance().isAuthenticated(player)) return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import cc.baka9.catseedlogin.bukkit.CatSeedLoginAPI
import fr.xephi.authme.api.v3.AuthMeApi
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.*
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down Expand Up @@ -83,7 +83,8 @@ class CommandListener : Listener {
}

private fun shouldNotProcess(player: Player, message: String): Boolean {
if (isInitialized && !CachingPermTool.hasPermission(PermissionsEnum.BYPASS, player) && !Utils.isCommandAndWhiteListed(message)) {
if (isInitialized && !CachingPermTool.hasPermission(
PermissionsEnum.BYPASS, player) && !Utils.isCommandAndWhiteListed(message)) {
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
if (!AuthMeApi.getInstance().isAuthenticated(player)) return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package io.wdsj.asw.bukkit.listener

import io.wdsj.asw.bukkit.AdvancedSensitiveWords.messagesManager
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.setting.PluginMessages
import io.wdsj.asw.bukkit.setting.PluginSettings
import io.wdsj.asw.bukkit.update.Updater
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package io.wdsj.asw.bukkit.listener
import io.wdsj.asw.bukkit.AdvancedSensitiveWords
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package io.wdsj.asw.bukkit.listener

import io.wdsj.asw.bukkit.AdvancedSensitiveWords.*
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package io.wdsj.asw.bukkit.listener
import com.github.houbb.heaven.util.lang.StringUtil
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.*
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEd
import io.wdsj.asw.bukkit.AdvancedSensitiveWords
import io.wdsj.asw.bukkit.AdvancedSensitiveWords.settingsManager
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import io.wdsj.asw.bukkit.ai.OllamaProcessor
import io.wdsj.asw.bukkit.ai.OpenAIProcessor
import io.wdsj.asw.bukkit.listener.FakeMessageExecutor
import io.wdsj.asw.bukkit.manage.notice.Notifier
import io.wdsj.asw.bukkit.manage.permission.PermissionsEnum
import io.wdsj.asw.bukkit.manage.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.manage.punish.Punishment
import io.wdsj.asw.bukkit.manage.punish.ViolationCounter
import io.wdsj.asw.bukkit.permission.PermissionsEnum
import io.wdsj.asw.bukkit.permission.cache.CachingPermTool
import io.wdsj.asw.bukkit.proxy.bungee.BungeeSender
import io.wdsj.asw.bukkit.proxy.velocity.VelocitySender
import io.wdsj.asw.bukkit.setting.PluginMessages
Expand Down Expand Up @@ -292,7 +292,8 @@ class ASWChatPacketListener : PacketListenerAbstract(PacketListenerPriority.LOW)
}

private fun shouldNotProcess(player: Player, message: String): Boolean {
if (isInitialized && !CachingPermTool.hasPermission(PermissionsEnum.BYPASS, player) && !Utils.isCommandAndWhiteListed(message)) {
if (isInitialized && !CachingPermTool.hasPermission(
PermissionsEnum.BYPASS, player) && !Utils.isCommandAndWhiteListed(message)) {
if (isAuthMeAvailable && settingsManager.getProperty(PluginSettings.ENABLE_AUTHME_COMPATIBILITY)) {
if (!AuthMeApi.getInstance().isAuthenticated(player)) return true
}
Expand Down

0 comments on commit 92f6adc

Please sign in to comment.