mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Add JSON user service
This commit is contained in:
1 parent
06470f46da
commit
fc8b11c83c
5 files changed
+184
-18
No files matched your search
@@ -6,6 +6,7 @@ import net.draycia.simplechat.listeners.VoteListener;
|
|||||||
import net.draycia.simplechat.listeners.chat.*;
|
import net.draycia.simplechat.listeners.chat.*;
|
||||||
import net.draycia.simplechat.managers.*;
|
import net.draycia.simplechat.managers.*;
|
||||||
import net.draycia.simplechat.storage.UserService;
|
import net.draycia.simplechat.storage.UserService;
|
||||||
|
import net.draycia.simplechat.storage.impl.JSONUserService;
|
||||||
import net.draycia.simplechat.util.ItemStackUtils;
|
import net.draycia.simplechat.util.ItemStackUtils;
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
import net.milkbowl.vault.permission.Permission;
|
import net.milkbowl.vault.permission.Permission;
|
||||||
@@ -54,7 +55,7 @@ public final class SimpleChat extends JavaPlugin {
|
|||||||
commandManager = new CommandManager(this);
|
commandManager = new CommandManager(this);
|
||||||
channelManager = new ChannelManager(this);
|
channelManager = new ChannelManager(this);
|
||||||
|
|
||||||
// TODO: initialize UserService
|
userService = new JSONUserService(this); // TODO: SQLUserService
|
||||||
|
|
||||||
// Setup listeners
|
// Setup listeners
|
||||||
setupListeners();
|
setupListeners();
|
||||||
@@ -62,7 +63,7 @@ public final class SimpleChat extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
// TODO: save user data
|
userService.cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupListeners() {
|
private void setupListeners() {
|
||||||
|
|||||||
@@ -9,4 +9,6 @@ public abstract class UserService {
|
|||||||
public abstract ChatUser wrap(OfflinePlayer player);
|
public abstract ChatUser wrap(OfflinePlayer player);
|
||||||
public abstract ChatUser wrap(UUID uuid);
|
public abstract ChatUser wrap(UUID uuid);
|
||||||
|
|
||||||
|
public abstract void cleanUp();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package net.draycia.simplechat.storage.impl;
|
||||||
|
|
||||||
|
import com.google.common.cache.*;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
import net.draycia.simplechat.SimpleChat;
|
||||||
|
import net.draycia.simplechat.channels.ChatChannel;
|
||||||
|
import net.draycia.simplechat.storage.ChatUser;
|
||||||
|
import net.draycia.simplechat.storage.UserService;
|
||||||
|
import net.draycia.simplechat.util.ChatChannelAdapter;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class JSONUserService extends UserService {
|
||||||
|
|
||||||
|
private final SimpleChat simpleChat;
|
||||||
|
|
||||||
|
private final LoadingCache<UUID, SimpleChatUser> userCache = CacheBuilder.newBuilder()
|
||||||
|
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||||
|
.removalListener(this::saveUser)
|
||||||
|
.build(CacheLoader.from(this::loadUser));
|
||||||
|
|
||||||
|
private Gson gson;
|
||||||
|
|
||||||
|
private Type userType = new TypeToken<SimpleChatUser>() {}.getType();
|
||||||
|
|
||||||
|
public JSONUserService(SimpleChat simpleChat) {
|
||||||
|
this.simpleChat =simpleChat;
|
||||||
|
|
||||||
|
Bukkit.getScheduler().scheduleSyncRepeatingTask(simpleChat, userCache::cleanUp, 0L, 20 * 60 * 10);
|
||||||
|
|
||||||
|
GsonBuilder builder = new GsonBuilder().setPrettyPrinting();
|
||||||
|
builder.registerTypeAdapter(new TypeToken<ChatChannel>() {}.getType(), new ChatChannelAdapter(simpleChat));
|
||||||
|
gson = builder.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatUser wrap(OfflinePlayer player) {
|
||||||
|
return wrap(player.getUniqueId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatUser wrap(UUID uuid) {
|
||||||
|
try {
|
||||||
|
return userCache.get(uuid);
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanUp() {
|
||||||
|
userCache.invalidateAll();
|
||||||
|
userCache.cleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
private SimpleChatUser loadUser(UUID uuid) {
|
||||||
|
File userFile = new File(simpleChat.getDataFolder(), "users/" + uuid.toString() + ".json");
|
||||||
|
ensureFileExists(userFile);
|
||||||
|
|
||||||
|
try (JsonReader reader = gson.newJsonReader(new FileReader(userFile))) {
|
||||||
|
SimpleChatUser user = gson.fromJson(reader, userType);
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SimpleChatUser(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveUser(RemovalNotification<UUID, SimpleChatUser> notification) {
|
||||||
|
File userFile = new File(simpleChat.getDataFolder(), "users/" + notification.getKey().toString() + ".json");
|
||||||
|
ensureFileExists(userFile);
|
||||||
|
|
||||||
|
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(userFile))) {
|
||||||
|
gson.toJson(notification.getValue(), userType, writer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureFileExists(File file) {
|
||||||
|
if (!file.getParentFile().exists()) {
|
||||||
|
file.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
|
try {
|
||||||
|
file.createNewFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -20,27 +20,30 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||||
|
|
||||||
private SimpleChat simpleChat;
|
private transient SimpleChat simpleChat;
|
||||||
|
|
||||||
private UUID uuid;
|
private UUID uuid;
|
||||||
|
|
||||||
private ChatChannel selectedChannel;
|
private String selectedChannel = null;
|
||||||
private List<ChatChannel> ignoredChannels = new ArrayList<>();
|
private List<String> ignoredChannels = new ArrayList<>();
|
||||||
private List<UUID> ignoredUsers = new ArrayList<>();
|
private List<UUID> ignoredUsers = new ArrayList<>();
|
||||||
private boolean muted;
|
private boolean muted = false;
|
||||||
private boolean shadowMuted;
|
private boolean shadowMuted = false;
|
||||||
private UUID replyTarget = null;
|
|
||||||
|
|
||||||
private Audience audience;
|
private transient UUID replyTarget = null;
|
||||||
|
|
||||||
public SimpleChatUser(SimpleChat simpleChat, UUID uuid) {
|
public SimpleChatUser() {
|
||||||
this.simpleChat = simpleChat;
|
this.simpleChat = (SimpleChat)Bukkit.getPluginManager().getPlugin("SimpleChat");
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleChatUser(UUID uuid) {
|
||||||
|
this.simpleChat = (SimpleChat)Bukkit.getPluginManager().getPlugin("SimpleChat");
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
this.audience = simpleChat.getAudiences().player(uuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Audience audience() {
|
public Audience audience() {
|
||||||
return audience;
|
return simpleChat.getAudiences().player(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -65,25 +68,25 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChatChannel getSelectedChannel() {
|
public ChatChannel getSelectedChannel() {
|
||||||
return selectedChannel;
|
return selectedChannel == null ? simpleChat.getDefaultChannel() : simpleChat.getChannel(selectedChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setSelectedChannel(ChatChannel chatChannel) {
|
public void setSelectedChannel(ChatChannel chatChannel) {
|
||||||
this.selectedChannel = chatChannel;
|
this.selectedChannel = chatChannel.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean ignoringChannel(ChatChannel chatChannel) {
|
public boolean ignoringChannel(ChatChannel chatChannel) {
|
||||||
return ignoredChannels.contains(chatChannel);
|
return ignoredChannels.contains(chatChannel.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setIgnoringChannel(ChatChannel chatChannel, boolean ignoring) {
|
public void setIgnoringChannel(ChatChannel chatChannel, boolean ignoring) {
|
||||||
if (ignoring) {
|
if (ignoring) {
|
||||||
ignoredChannels.add(chatChannel);
|
ignoredChannels.add(chatChannel.getName());
|
||||||
} else {
|
} else {
|
||||||
ignoredChannels.remove(chatChannel);
|
ignoredChannels.remove(chatChannel.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package net.draycia.simplechat.util;
|
||||||
|
|
||||||
|
import com.google.gson.TypeAdapter;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
import net.draycia.simplechat.SimpleChat;
|
||||||
|
import net.draycia.simplechat.channels.ChatChannel;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ChatChannelAdapter extends TypeAdapter<ChatChannel> {
|
||||||
|
|
||||||
|
private SimpleChat simpleChat;
|
||||||
|
|
||||||
|
public ChatChannelAdapter(SimpleChat simpleChat) {
|
||||||
|
this.simpleChat = simpleChat;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter out, ChatChannel value) throws IOException {
|
||||||
|
if (value != null) {
|
||||||
|
out.beginObject();
|
||||||
|
out.name("name").value(value.getName());
|
||||||
|
out.endObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatChannel read(JsonReader in) throws IOException {
|
||||||
|
ChatChannel channel;
|
||||||
|
|
||||||
|
in.beginObject();
|
||||||
|
|
||||||
|
if (in.hasNext()) {
|
||||||
|
String key = in.nextName();
|
||||||
|
String value = in.nextString();
|
||||||
|
|
||||||
|
channel = simpleChat.getChannel(value);
|
||||||
|
} else {
|
||||||
|
channel = simpleChat.getDefaultChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
in.endObject();
|
||||||
|
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user