Small code improvements
This commit is contained in:
parent
27ef6d5b2d
commit
19432ca0fe
@ -13,28 +13,30 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class Main extends JavaPlugin {
|
public class Main extends JavaPlugin {
|
||||||
|
|
||||||
public static Cache<Player, StringBuilder> CACHE;
|
public static Cache<Player, StringBuilder> CACHE;
|
||||||
public static Cache<Player, Type> USER_TYPE;
|
public static Cache<Player, Type> USER_TYPE = CacheBuilder.newBuilder().build();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
saveDefaultConfig();
|
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()
|
CACHE = CacheBuilder.newBuilder()
|
||||||
.expireAfterWrite(30, TimeUnit.MINUTES)
|
.expireAfterWrite(30, TimeUnit.MINUTES)
|
||||||
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
|
.removalListener((RemovalListener<Player, StringBuilder>) notification -> {
|
||||||
if (notification.getKey() == null) {
|
if (notification.getKey() == null) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
USER_TYPE.invalidate(notification.getKey());
|
USER_TYPE.invalidate(notification.getKey());
|
||||||
if (notification.getCause() == RemovalCause.EXPIRED) {
|
if (notification.getCause() == RemovalCause.EXPIRED) {
|
||||||
notification.getKey().sendMessage(Messages.format(getConfig().getString("command.toggle.disabled")));
|
notification.getKey().sendMessage(Messages.format(getConfig().getString("command.toggle.disabled")));
|
||||||
}
|
}
|
||||||
}).build();
|
}).build();
|
||||||
USER_TYPE = CacheBuilder.newBuilder().build();
|
|
||||||
|
|
||||||
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
|
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
|
||||||
|
|
||||||
ChatCommand command = new ChatCommand(this);
|
ChatCommand command = new ChatCommand(this);
|
||||||
PluginCommand chatgpt = getCommand("chatgpt");
|
PluginCommand chatgpt = getCommand("chatgpt");
|
||||||
chatgpt.setExecutor(command);
|
chatgpt.setExecutor(command);
|
||||||
|
@ -11,8 +11,8 @@ public class OpenAI {
|
|||||||
|
|
||||||
private static OpenAiService service;
|
private static OpenAiService service;
|
||||||
|
|
||||||
public static void init(String key) {
|
public static CompletableFuture<Void> init(String key) {
|
||||||
service = new OpenAiService(key, 0);
|
return CompletableFuture.runAsync(() -> service = new OpenAiService(key, 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) {
|
public static CompletableFuture<String> getResponse(ConfigurationSection section, StringBuilder cached, String message) {
|
||||||
|
@ -37,7 +37,7 @@ public class PlayerHandlers implements Listener {
|
|||||||
return;
|
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 SINGLE -> Collections.singletonList(player);
|
||||||
case FULL, BROADCAST -> e.getRecipients();
|
case FULL, BROADCAST -> e.getRecipients();
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user