Registered Tab and new cache

This commit is contained in:
ohAlee 2023-01-03 17:45:42 +01:00
parent fabf4be804
commit 609b7d8419

View File

@ -1,9 +1,9 @@
package it.ohalee.minecraftgpt; package it.ohalee.minecraftgpt;
import com.google.common.cache.*; import com.google.common.cache.*;
import it.ohalee.minecraftgpt.command.GPTCommand; import it.ohalee.minecraftgpt.command.ChatCommand;
import it.ohalee.minecraftgpt.handler.PlayerHandlers; import it.ohalee.minecraftgpt.handler.PlayerHandlers;
import lombok.Getter; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -11,8 +11,8 @@ import java.util.concurrent.TimeUnit;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
@Getter public static Cache<Player, StringBuilder> CACHE;
private Cache<Player, StringBuilder> cache; public static Cache<Player, Type> USER_TYPE;
@Override @Override
public void onEnable() { public void onEnable() {
@ -20,16 +20,24 @@ public class Main extends JavaPlugin {
OpenAI.init(getConfig().getString("API_KEY")); OpenAI.init(getConfig().getString("API_KEY"));
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.getCause() == RemovalCause.EXPIRED && notification.getKey() != null) { if (notification.getKey() == null) {
return;
}
USER_TYPE.invalidate(notification.getKey());
if (notification.getCause() == RemovalCause.EXPIRED) {
notification.getKey().sendMessage(getConfig().getString("command.toggle.disabled").replace("&", "§")); notification.getKey().sendMessage(getConfig().getString("command.toggle.disabled").replace("&", "§"));
} }
}).build(); }).build();
USER_TYPE = CacheBuilder.newBuilder().build();
getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this); getServer().getPluginManager().registerEvents(new PlayerHandlers(this), this);
getCommand("chatgpt").setExecutor(new GPTCommand(this)); ChatCommand command = new ChatCommand(this);
PluginCommand chatgpt = getCommand("chatgpt");
chatgpt.setExecutor(command);
chatgpt.setTabCompleter(command);
getLogger().info("Plugin enabled!"); getLogger().info("Plugin enabled!");
} }