Small code improvements

This commit is contained in:
ohAlee 2023-03-08 16:58:57 +01:00
parent 27ef6d5b2d
commit 19432ca0fe
3 changed files with 11 additions and 9 deletions

View File

@ -13,28 +13,30 @@ import java.util.concurrent.TimeUnit;
public class Main extends JavaPlugin {
public static Cache<Player, StringBuilder> CACHE;
public static Cache<Player, Type> USER_TYPE;
public static Cache<Player, Type> USER_TYPE = CacheBuilder.newBuilder().build();
@Override
public void onEnable() {
saveDefaultConfig();
OpenAI.init(getConfig().getString("API_KEY"));
OpenAI.init(getConfig().getString("API_KEY")).exceptionallyAsync(throwable -> {
getLogger().severe("Error while initializing OpenAI service! Is your API key valid?");
throwable.printStackTrace();
return null;
});
CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.MINUTES)
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
if (notification.getKey() == null) {
return;
}
if (notification.getKey() == null) return;
USER_TYPE.invalidate(notification.getKey());
if (notification.getCause() == RemovalCause.EXPIRED) {
notification.getKey().sendMessage(Messages.format(getConfig().getString("command.toggle.disabled")));
}
}).build();
USER_TYPE = CacheBuilder.newBuilder().build();
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
ChatCommand command = new ChatCommand(this);
PluginCommand chatgpt = getCommand("chatgpt");
chatgpt.setExecutor(command);

View File

@ -11,8 +11,8 @@ public class OpenAI {
private static OpenAiService service;
public static void init(String key) {
service = new OpenAiService(key, 0);
public static CompletableFuture<Void> init(String key) {
return CompletableFuture.runAsync(() -> service = new OpenAiService(key, 5));
}
public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) {

View File

@ -37,7 +37,7 @@ public class PlayerHandlers implements Listener {
return;
}
Collection<Player> recipients = switch (Main.USER_TYPE.asMap().getOrDefault(player, hasFull ? Type.FULL : Type.SINGLE) ) {
Collection<Player> recipients = switch (Main.USER_TYPE.asMap().getOrDefault(player, hasFull ? Type.FULL : Type.SINGLE)) {
case SINGLE -> Collections.singletonList(player);
case FULL, BROADCAST -> e.getRecipients();
};