forked from ChomeNS/chomens-bot-java
add persistent data and *seen and make stuff persistent in some cummands
This commit is contained in:
parent
b3d36ad9db
commit
c102214a4b
12 changed files with 332 additions and 37 deletions
|
@ -1,10 +1,13 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.commands;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.Command;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.TrustLevel;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.FilteredPlayer;
|
||||
import land.chipmunk.chayapak.chomens_bot.plugins.FilterPlugin;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.JoinConfiguration;
|
||||
|
@ -73,6 +76,8 @@ public class FilterCommand implements Command {
|
|||
args = Arrays.copyOfRange(_args, 2, _args.length);
|
||||
}
|
||||
|
||||
final Gson gson = new Gson();
|
||||
|
||||
switch (args[0]) {
|
||||
case "add" -> {
|
||||
final String player = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
|
||||
|
@ -105,7 +110,9 @@ public class FilterCommand implements Command {
|
|||
final List<Component> filtersComponents = new ArrayList<>();
|
||||
|
||||
int index = 0;
|
||||
for (FilteredPlayer player : bot.filter().filteredPlayers()) {
|
||||
for (JsonElement playerElement : FilterPlugin.filteredPlayers()) {
|
||||
final FilteredPlayer player = gson.fromJson(playerElement, FilteredPlayer.class);
|
||||
|
||||
filtersComponents.add(
|
||||
Component.translatable(
|
||||
"%s › %s",
|
||||
|
@ -120,7 +127,7 @@ public class FilterCommand implements Command {
|
|||
return Component.empty()
|
||||
.append(Component.text("Filtered players ").color(NamedTextColor.GREEN))
|
||||
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.text(bot.filter().filteredPlayers().size()).color(NamedTextColor.GRAY))
|
||||
.append(Component.text(FilterPlugin.filteredPlayers().size()).color(NamedTextColor.GRAY))
|
||||
.append(Component.text(")").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.newline())
|
||||
.append(
|
||||
|
|
|
@ -2,6 +2,8 @@ package land.chipmunk.chayapak.chomens_bot.commands;
|
|||
|
||||
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.Command;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
|
||||
|
@ -11,6 +13,7 @@ import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
|||
import land.chipmunk.chayapak.chomens_bot.plugins.MailPlugin;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.PersistentDataUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.UUIDUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.JoinConfiguration;
|
||||
|
@ -18,10 +21,10 @@ import net.kyori.adventure.text.TranslatableComponent;
|
|||
import net.kyori.adventure.text.event.HoverEvent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -59,12 +62,16 @@ public class MailCommand implements Command {
|
|||
|
||||
final MutablePlayerListEntry sender = context.sender();
|
||||
|
||||
final Gson gson = new Gson();
|
||||
|
||||
// kinda messy ngl
|
||||
|
||||
switch (args[0]) {
|
||||
case "send" -> {
|
||||
int senderMailsSentTotal = 0;
|
||||
for (Mail mail : MailPlugin.mails()) {
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (mail.sentBy() == null) continue;
|
||||
|
||||
if (!mail.sentBy().equals(sender.profile().getName())) continue;
|
||||
|
@ -77,7 +84,7 @@ public class MailCommand implements Command {
|
|||
new Mail(
|
||||
sender.profile().getName(),
|
||||
args[1],
|
||||
DateTime.now(),
|
||||
Instant.now().toEpochMilli(),
|
||||
bot.host() + ":" + bot.port(),
|
||||
String.join(" ", Arrays.copyOfRange(args, 2, args.length))
|
||||
)
|
||||
|
@ -87,7 +94,9 @@ public class MailCommand implements Command {
|
|||
}
|
||||
case "sendselecteditem" -> {
|
||||
int senderMailsSentTotal = 0;
|
||||
for (Mail mail : MailPlugin.mails()) {
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
senderMailsSentTotal++;
|
||||
}
|
||||
|
@ -130,7 +139,7 @@ public class MailCommand implements Command {
|
|||
new Mail(
|
||||
sender.profile().getName(),
|
||||
args[1],
|
||||
DateTime.now(),
|
||||
Instant.now().toEpochMilli(),
|
||||
bot.host() + ":" + bot.port(),
|
||||
value.substring(1).substring(0, value.length() - 2)
|
||||
)
|
||||
|
@ -149,7 +158,9 @@ public class MailCommand implements Command {
|
|||
// TODO: use less for loops?
|
||||
|
||||
int senderMailSize = 0;
|
||||
for (Mail mail : MailPlugin.mails()) {
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
senderMailSize++;
|
||||
}
|
||||
|
@ -159,7 +170,9 @@ public class MailCommand implements Command {
|
|||
final List<Component> mailsComponent = new ArrayList<>();
|
||||
|
||||
int i = 1;
|
||||
for (Mail mail : MailPlugin.mails()) {
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
|
||||
final DateTimeFormatter formatter = DateTimeFormat.forPattern("MMMM d, YYYY, hh:mm:ss a Z");
|
||||
|
@ -213,8 +226,13 @@ public class MailCommand implements Command {
|
|||
context.sendOutput(component);
|
||||
}
|
||||
|
||||
// thanks https://www.baeldung.com/java-concurrentmodificationexception#3-using-removeif:~:text=3.3.%20Using%20removeIf()
|
||||
MailPlugin.mails().removeIf(mail -> mail.sentTo().equals(sender.profile().getName()));
|
||||
for (JsonElement mailElement : MailPlugin.mails().deepCopy()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (mail.sentTo().equals(sender.profile().getName())) MailPlugin.mails().remove(mailElement);
|
||||
}
|
||||
|
||||
PersistentDataUtilities.put("mails", MailPlugin.mails());
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.commands;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.Command;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.TrustLevel;
|
||||
import land.chipmunk.chayapak.chomens_bot.plugins.PlayersPlugin;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SeenCommand implements Command {
|
||||
public String name() { return "seen"; }
|
||||
|
||||
public String description() {
|
||||
return "Shows the last seen of a player";
|
||||
}
|
||||
|
||||
public List<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("<{player}>");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public List<String> alias() {
|
||||
final List<String> aliases = new ArrayList<>();
|
||||
aliases.add("lastseen");
|
||||
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public TrustLevel trustLevel() {
|
||||
return TrustLevel.PUBLIC;
|
||||
}
|
||||
|
||||
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
|
||||
final Bot bot = context.bot();
|
||||
|
||||
final String player = String.join(" ", args);
|
||||
|
||||
for (Bot eachBot : bot.bots()) {
|
||||
if (eachBot.players().getEntry(player) != null) return Component.empty()
|
||||
.append(Component.text(player))
|
||||
.append(Component.text(" is currently online on "))
|
||||
.append(Component.text(eachBot.host() + ":" + eachBot.port()))
|
||||
.color(NamedTextColor.RED);
|
||||
}
|
||||
|
||||
final JsonElement playerElement = PlayersPlugin.playersObject().get(player);
|
||||
if (playerElement == null) return Component.translatable(
|
||||
"%s was never seen",
|
||||
Component.text(player)
|
||||
).color(NamedTextColor.RED);
|
||||
|
||||
final JsonObject lastSeen = playerElement.getAsJsonObject().get("lastSeen").getAsJsonObject();
|
||||
|
||||
final DateTime dateTime = new DateTime(lastSeen.get("time").getAsLong(), DateTimeZone.UTC);
|
||||
|
||||
final DateTimeFormatter formatter = DateTimeFormat.forPattern("EEEE, MMMM d, YYYY, hh:mm:ss a z");
|
||||
final String formattedTime = formatter.print(dateTime);
|
||||
|
||||
final String server = lastSeen.get("server").getAsString();
|
||||
|
||||
return Component.translatable(
|
||||
"%s was last seen at %s on %s",
|
||||
Component.text(player).color(ColorUtilities.getColorByString(bot.config().colorPalette().username())),
|
||||
Component.text(formattedTime).color(ColorUtilities.getColorByString(bot.config().colorPalette().string())),
|
||||
Component.text(server).color(ColorUtilities.getColorByString(bot.config().colorPalette().string()))
|
||||
).color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()));
|
||||
}
|
||||
}
|
|
@ -6,9 +6,9 @@ import org.joda.time.DateTime;
|
|||
|
||||
@AllArgsConstructor
|
||||
public class Mail {
|
||||
@Getter private final String sentBy;
|
||||
@Getter private final String sentTo;
|
||||
@Getter private final DateTime timeSent;
|
||||
@Getter private final String server;
|
||||
@Getter private final String contents;
|
||||
@Getter private String sentBy;
|
||||
@Getter private String sentTo;
|
||||
@Getter private long timeSent;
|
||||
@Getter private String server;
|
||||
@Getter private String contents;
|
||||
}
|
||||
|
|
|
@ -66,6 +66,7 @@ public class CommandHandlerPlugin {
|
|||
registerCommand(new FilterCommand());
|
||||
registerCommand(new UptimeCommand());
|
||||
registerCommand(new MailCommand());
|
||||
registerCommand(new SeenCommand());
|
||||
}
|
||||
|
||||
public void registerCommand (Command command) {
|
||||
|
|
|
@ -1,23 +1,33 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.FilteredPlayer;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.PersistentDataUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.UUIDUtilities;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class FilterPlugin extends PlayersPlugin.Listener {
|
||||
private final Bot bot;
|
||||
|
||||
@Getter private final List<FilteredPlayer> filteredPlayers = new ArrayList<>();
|
||||
@Getter private static JsonArray filteredPlayers = new JsonArray();
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
static {
|
||||
if (PersistentDataUtilities.jsonObject.has("filters")) {
|
||||
filteredPlayers = PersistentDataUtilities.jsonObject.get("filters").getAsJsonArray();
|
||||
}
|
||||
}
|
||||
|
||||
public FilterPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
@ -43,7 +53,9 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
|||
// mess
|
||||
// also regex and ignorecase codes from greplog plugin
|
||||
FilteredPlayer filteredPlayer = null;
|
||||
for (FilteredPlayer _filteredPlayer : filteredPlayers) {
|
||||
for (JsonElement filteredPlayerElement : filteredPlayers) {
|
||||
final FilteredPlayer _filteredPlayer = gson.fromJson(filteredPlayerElement, FilteredPlayer.class);
|
||||
|
||||
if (_filteredPlayer.regex) {
|
||||
Pattern pattern = null;
|
||||
if (_filteredPlayer.ignoreCase) {
|
||||
|
@ -128,7 +140,9 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
|||
}
|
||||
|
||||
public void add (String playerName, boolean regex, boolean ignoreCase) {
|
||||
filteredPlayers.add(new FilteredPlayer(playerName, regex, ignoreCase));
|
||||
filteredPlayers.add(gson.fromJson(gson.toJson(new FilteredPlayer(playerName, regex, ignoreCase)), JsonElement.class));
|
||||
|
||||
PersistentDataUtilities.put("filters", filteredPlayers);
|
||||
|
||||
final MutablePlayerListEntry target = bot.players().getEntry(playerName); // fix not working for regex and ignorecase
|
||||
|
||||
|
@ -139,10 +153,18 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
|||
}
|
||||
|
||||
public FilteredPlayer remove (int index) {
|
||||
return filteredPlayers.remove(index);
|
||||
final JsonElement element = filteredPlayers.remove(index);
|
||||
|
||||
PersistentDataUtilities.put("filters", filteredPlayers);
|
||||
|
||||
return gson.fromJson(element, FilteredPlayer.class);
|
||||
}
|
||||
|
||||
public void clear () {
|
||||
filteredPlayers.clear();
|
||||
for (int i = 0; i <= filteredPlayers.size(); i++) {
|
||||
filteredPlayers.remove(i);
|
||||
}
|
||||
|
||||
PersistentDataUtilities.put("filters", filteredPlayers);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package land.chipmunk.chayapak.chomens_bot.plugins;
|
|||
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.FileLoggerUtilities;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
|
@ -75,7 +75,7 @@ public class GrepLogPlugin {
|
|||
).color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
||||
);
|
||||
|
||||
final File[] fileList = LoggerUtilities.logDir.listFiles();
|
||||
final File[] fileList = FileLoggerUtilities.logDir.listFiles();
|
||||
|
||||
Arrays.sort(fileList, Comparator.comparing(File::getName)); // VERY IMPORTANT
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ package land.chipmunk.chayapak.chomens_bot.plugins;
|
|||
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.FileLoggerUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
@ -87,7 +87,7 @@ public class LoggerPlugin extends ChatPlugin.Listener {
|
|||
_message
|
||||
);
|
||||
|
||||
LoggerUtilities.log(
|
||||
FileLoggerUtilities.log(
|
||||
formattedMessage.replaceAll( // use replaceAll for regexes, use replace for normal string
|
||||
"\u001B\\[[;\\d]*[ -/]*[@-~]",
|
||||
""
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.Mail;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.PersistentDataUtilities;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
@ -14,8 +18,15 @@ import java.util.List;
|
|||
public class MailPlugin extends PlayersPlugin.Listener {
|
||||
private final Bot bot;
|
||||
|
||||
// TODO: make this persistent
|
||||
@Getter private static final List<Mail> mails = new ArrayList<>();
|
||||
@Getter private static JsonArray mails = new JsonArray();
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
static {
|
||||
if (PersistentDataUtilities.jsonObject.has("mails")) {
|
||||
mails = PersistentDataUtilities.jsonObject.get("mails").getAsJsonArray();
|
||||
}
|
||||
}
|
||||
|
||||
public MailPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
@ -29,10 +40,16 @@ public class MailPlugin extends PlayersPlugin.Listener {
|
|||
|
||||
final List<String> sendTos = new ArrayList<>(); // confusing name,.,.
|
||||
|
||||
for (Mail mail : mails) sendTos.add(mail.sentTo());
|
||||
for (JsonElement mailElement : mails) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
sendTos.add(mail.sentTo());
|
||||
}
|
||||
|
||||
boolean shouldSend = false;
|
||||
for (Mail mail : mails) {
|
||||
for (JsonElement mailElement : mails) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (mail.sentTo().equals(name)) {
|
||||
shouldSend = true;
|
||||
break;
|
||||
|
@ -54,6 +71,8 @@ public class MailPlugin extends PlayersPlugin.Listener {
|
|||
}
|
||||
|
||||
public void send (Mail mail) {
|
||||
mails.add(mail);
|
||||
mails.add(gson.fromJson(gson.toJson(mail), JsonElement.class)); // is this the best way?
|
||||
|
||||
PersistentDataUtilities.put("mails", mails);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,14 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPl
|
|||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.google.gson.JsonObject;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.PersistentDataUtilities;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
@ -24,8 +27,17 @@ public class PlayersPlugin extends Bot.Listener {
|
|||
|
||||
private final List<Listener> listeners = new ArrayList<>();
|
||||
|
||||
@Getter private static JsonObject playersObject = new JsonObject();
|
||||
|
||||
static {
|
||||
if (PersistentDataUtilities.jsonObject.has("players")) {
|
||||
playersObject = PersistentDataUtilities.jsonObject.get("players").getAsJsonObject();
|
||||
}
|
||||
}
|
||||
|
||||
public PlayersPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.addListener(this);
|
||||
}
|
||||
|
||||
|
@ -115,8 +127,20 @@ public class PlayersPlugin extends Bot.Listener {
|
|||
|
||||
list.add(target);
|
||||
|
||||
if (duplicate == null) for (Listener listener : listeners) { listener.playerJoined(target); }
|
||||
else for (Listener listener : listeners) { listener.playerUnVanished(target); }
|
||||
if (duplicate == null) {
|
||||
for (Listener listener : listeners) { listener.playerJoined(target); }
|
||||
|
||||
// should this be here?
|
||||
if (playersObject.has(target.profile().getName())) return;
|
||||
|
||||
final JsonObject object = new JsonObject();
|
||||
object.addProperty("uuid", target.profile().getIdAsString());
|
||||
object.add("lastSeen", new JsonObject());
|
||||
|
||||
playersObject.add(target.profile().getName(), object);
|
||||
|
||||
PersistentDataUtilities.put("players", playersObject);
|
||||
} else for (Listener listener : listeners) { listener.playerUnVanished(target); }
|
||||
}
|
||||
|
||||
private void updateGamemode (PlayerListEntry newEntry) {
|
||||
|
@ -172,6 +196,21 @@ public class PlayersPlugin extends Bot.Listener {
|
|||
|
||||
for (Listener listener : listeners) { listener.playerLeft(target); }
|
||||
|
||||
// should this be here?
|
||||
if (!playersObject.has(target.profile().getName())) return packet;
|
||||
|
||||
final JsonObject player = playersObject.get(target.profile().getName()).getAsJsonObject();
|
||||
|
||||
if (player.has("lastSeen")) player.remove("lastSeen");
|
||||
|
||||
final JsonObject object = new JsonObject();
|
||||
object.addProperty("time", Instant.now().toEpochMilli());
|
||||
object.addProperty("server", bot.host() + ":" + bot.port());
|
||||
|
||||
player.add("lastSeen", object);
|
||||
|
||||
PersistentDataUtilities.put("players", playersObject);
|
||||
|
||||
return packet;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -10,11 +10,10 @@ import java.time.LocalDateTime;
|
|||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
// totallynotskidded™ from HBot
|
||||
public class LoggerUtilities {
|
||||
public class FileLoggerUtilities {
|
||||
public static File logDir = new File("logs");
|
||||
public static File logFile = new File(logDir, "log.txt");
|
||||
public static OutputStreamWriter logWriter;
|
|
@ -0,0 +1,109 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class PersistentDataUtilities {
|
||||
public static File file = new File("persistent.json");
|
||||
|
||||
private static FileWriter writer;
|
||||
|
||||
public static JsonObject jsonObject = new JsonObject();
|
||||
|
||||
static {
|
||||
init();
|
||||
}
|
||||
|
||||
private static void init () {
|
||||
try {
|
||||
if (!file.exists()) file.createNewFile();
|
||||
|
||||
// loads the persistent data from the last session
|
||||
else {
|
||||
final InputStream opt = new FileInputStream(file);
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
|
||||
|
||||
final Gson gson = new Gson();
|
||||
|
||||
jsonObject = gson.fromJson(reader, JsonObject.class);
|
||||
}
|
||||
|
||||
writer = new FileWriter(file, false);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void put (String property, JsonElement value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.add(property, value);
|
||||
|
||||
try {
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
writer.write(jsonObject.toString());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void put (String property, String value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
try {
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
writer.write(jsonObject.toString());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void put (String property, boolean value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
try {
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
writer.write(jsonObject.toString());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void put (String property, int value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
try {
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
writer.write(jsonObject.toString());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void put (String property, char value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
try {
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
writer.write(jsonObject.toString());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue