mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +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
@@ -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 {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private transient SimpleChat simpleChat;
|
||||
|
||||
private UUID uuid;
|
||||
|
||||
private ChatChannel selectedChannel;
|
||||
private List<ChatChannel> ignoredChannels = new ArrayList<>();
|
||||
private String selectedChannel = null;
|
||||
private List<String> ignoredChannels = new ArrayList<>();
|
||||
private List<UUID> ignoredUsers = new ArrayList<>();
|
||||
private boolean muted;
|
||||
private boolean shadowMuted;
|
||||
private UUID replyTarget = null;
|
||||
private boolean muted = false;
|
||||
private boolean shadowMuted = false;
|
||||
|
||||
private Audience audience;
|
||||
private transient UUID replyTarget = null;
|
||||
|
||||
public SimpleChatUser(SimpleChat simpleChat, UUID uuid) {
|
||||
this.simpleChat = simpleChat;
|
||||
public SimpleChatUser() {
|
||||
this.simpleChat = (SimpleChat)Bukkit.getPluginManager().getPlugin("SimpleChat");
|
||||
}
|
||||
|
||||
public SimpleChatUser(UUID uuid) {
|
||||
this.simpleChat = (SimpleChat)Bukkit.getPluginManager().getPlugin("SimpleChat");
|
||||
this.uuid = uuid;
|
||||
this.audience = simpleChat.getAudiences().player(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return audience;
|
||||
return simpleChat.getAudiences().player(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,25 +68,25 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return selectedChannel;
|
||||
return selectedChannel == null ? simpleChat.getDefaultChannel() : simpleChat.getChannel(selectedChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedChannel(ChatChannel chatChannel) {
|
||||
this.selectedChannel = chatChannel;
|
||||
this.selectedChannel = chatChannel.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoringChannel(ChatChannel chatChannel) {
|
||||
return ignoredChannels.contains(chatChannel);
|
||||
return ignoredChannels.contains(chatChannel.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoringChannel(ChatChannel chatChannel, boolean ignoring) {
|
||||
if (ignoring) {
|
||||
ignoredChannels.add(chatChannel);
|
||||
ignoredChannels.add(chatChannel.getName());
|
||||
} else {
|
||||
ignoredChannels.remove(chatChannel);
|
||||
ignoredChannels.remove(chatChannel.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user