forked from ChomeNS/chomens-bot-java
run it in an executor service
This commit is contained in:
parent
c2894c64d2
commit
69a64eae28
1 changed files with 153 additions and 155 deletions
|
@ -53,74 +53,20 @@ public class MailCommand extends Command {
|
|||
|
||||
// kinda messy ngl
|
||||
|
||||
switch (args[0]) {
|
||||
case "send" -> {
|
||||
int senderMailsSentTotal = 0;
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
bot.executorService().submit(() -> {
|
||||
switch (args[0]) {
|
||||
case "send" -> {
|
||||
int senderMailsSentTotal = 0;
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (mail.sentBy() == null) continue;
|
||||
if (mail.sentBy() == null) continue;
|
||||
|
||||
if (!mail.sentBy().equals(sender.profile().getName())) continue;
|
||||
senderMailsSentTotal++;
|
||||
}
|
||||
|
||||
if (senderMailsSentTotal > 256) return Component.text("You are sending too much mails!").color(NamedTextColor.RED);
|
||||
|
||||
bot.mail().send(
|
||||
new Mail(
|
||||
sender.profile().getName(),
|
||||
args[1],
|
||||
Instant.now().toEpochMilli(),
|
||||
bot.host() + ":" + bot.port(),
|
||||
String.join(" ", Arrays.copyOfRange(args, 2, args.length))
|
||||
)
|
||||
);
|
||||
|
||||
return Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()));
|
||||
}
|
||||
case "sendselecteditem" -> {
|
||||
int senderMailsSentTotal = 0;
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
senderMailsSentTotal++;
|
||||
}
|
||||
|
||||
if (senderMailsSentTotal > 256) return Component.text("You are sending too much mails!").color(NamedTextColor.RED);
|
||||
|
||||
final CompletableFuture<CompoundTag> future = bot.core().runTracked(
|
||||
"minecraft:data get entity " +
|
||||
UUIDUtilities.selector(sender.profile().getId()) +
|
||||
" SelectedItem.tag.message"
|
||||
);
|
||||
|
||||
if (future == null) return Component.text("There was an error while sending your mail").color(NamedTextColor.RED);
|
||||
|
||||
future.thenApply(tags -> {
|
||||
if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags;
|
||||
|
||||
final StringTag lastOutput = tags.get("LastOutput");
|
||||
|
||||
final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue());
|
||||
|
||||
final List<Component> children = output.children();
|
||||
|
||||
// too lazy to use translation,.,.,.
|
||||
final String parsed = ComponentUtilities.stringify(children.get(0));
|
||||
|
||||
if (parsed.startsWith("Found no elements matching ")) {
|
||||
context.sendOutput(Component.text("Player has no `message` NBT tag in the selected item").color(NamedTextColor.RED));
|
||||
return tags;
|
||||
if (!mail.sentBy().equals(sender.profile().getName())) continue;
|
||||
senderMailsSentTotal++;
|
||||
}
|
||||
|
||||
final String value = ComponentUtilities.stringify(((TranslatableComponent) children.get(0)).args().get(1));
|
||||
|
||||
if (!value.startsWith("\"") && !value.endsWith("\"") && !value.startsWith("'") && !value.endsWith("'")) {
|
||||
context.sendOutput(Component.text("`message` NBT is not a string").color(NamedTextColor.RED));
|
||||
return tags;
|
||||
}
|
||||
if (senderMailsSentTotal > 256) context.sendOutput(Component.text("You are sending too much mails!").color(NamedTextColor.RED));
|
||||
|
||||
bot.mail().send(
|
||||
new Mail(
|
||||
|
@ -128,104 +74,156 @@ public class MailCommand extends Command {
|
|||
args[1],
|
||||
Instant.now().toEpochMilli(),
|
||||
bot.host() + ":" + bot.port(),
|
||||
value.substring(1).substring(0, value.length() - 2)
|
||||
String.join(" ", Arrays.copyOfRange(args, 2, args.length))
|
||||
)
|
||||
);
|
||||
|
||||
context.sendOutput(
|
||||
Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
||||
context.sendOutput(Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor())));
|
||||
}
|
||||
case "sendselecteditem" -> {
|
||||
int senderMailsSentTotal = 0;
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
senderMailsSentTotal++;
|
||||
}
|
||||
|
||||
if (senderMailsSentTotal > 256) context.sendOutput(Component.text("You are sending too much mails!").color(NamedTextColor.RED));
|
||||
|
||||
final CompletableFuture<CompoundTag> future = bot.core().runTracked(
|
||||
"minecraft:data get entity " +
|
||||
UUIDUtilities.selector(sender.profile().getId()) +
|
||||
" SelectedItem.tag.message"
|
||||
);
|
||||
|
||||
return tags;
|
||||
});
|
||||
if (future == null) context.sendOutput(Component.text("There was an error while sending your mail").color(NamedTextColor.RED));
|
||||
|
||||
return null;
|
||||
future.thenApply(tags -> {
|
||||
if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags;
|
||||
|
||||
final StringTag lastOutput = tags.get("LastOutput");
|
||||
|
||||
final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue());
|
||||
|
||||
final List<Component> children = output.children();
|
||||
|
||||
// too lazy to use translation,.,.,.
|
||||
final String parsed = ComponentUtilities.stringify(children.get(0));
|
||||
|
||||
if (parsed.startsWith("Found no elements matching ")) {
|
||||
context.sendOutput(Component.text("Player has no `message` NBT tag in the selected item").color(NamedTextColor.RED));
|
||||
return tags;
|
||||
}
|
||||
|
||||
final String value = ComponentUtilities.stringify(((TranslatableComponent) children.get(0)).args().get(1));
|
||||
|
||||
if (!value.startsWith("\"") && !value.endsWith("\"") && !value.startsWith("'") && !value.endsWith("'")) {
|
||||
context.sendOutput(Component.text("`message` NBT is not a string").color(NamedTextColor.RED));
|
||||
return tags;
|
||||
}
|
||||
|
||||
bot.mail().send(
|
||||
new Mail(
|
||||
sender.profile().getName(),
|
||||
args[1],
|
||||
Instant.now().toEpochMilli(),
|
||||
bot.host() + ":" + bot.port(),
|
||||
value.substring(1).substring(0, value.length() - 2)
|
||||
)
|
||||
);
|
||||
|
||||
context.sendOutput(
|
||||
Component.text("Mail sent!").color(ColorUtilities.getColorByString(bot.config().colorPalette().defaultColor()))
|
||||
);
|
||||
|
||||
return tags;
|
||||
});
|
||||
}
|
||||
case "read" -> {
|
||||
// TODO: use less for loops?
|
||||
|
||||
int senderMailSize = 0;
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
senderMailSize++;
|
||||
}
|
||||
|
||||
if (senderMailSize == 0) context.sendOutput(Component.text("You have no new mails").color(NamedTextColor.RED));
|
||||
|
||||
final List<Component> mailsComponent = new ArrayList<>();
|
||||
|
||||
int i = 1;
|
||||
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");
|
||||
final String formattedTime = formatter.print(mail.timeSent());
|
||||
|
||||
mailsComponent.add(
|
||||
Component.translatable(
|
||||
"""
|
||||
%s %s Sent by: %s %s
|
||||
Contents:
|
||||
%s""",
|
||||
Component.text(i).color(ColorUtilities.getColorByString(bot.config().colorPalette().number())),
|
||||
Component.text("-").color(NamedTextColor.DARK_GRAY),
|
||||
|
||||
Component.text(mail.sentBy()).color(ColorUtilities.getColorByString(bot.config().colorPalette().username())),
|
||||
Component
|
||||
.text("[Hover here for more info]")
|
||||
.color(NamedTextColor.GREEN)
|
||||
.hoverEvent(
|
||||
HoverEvent.showText(
|
||||
Component.translatable(
|
||||
"""
|
||||
Time sent: %s
|
||||
Server: %s""",
|
||||
Component.text(formattedTime).color(ColorUtilities.getColorByString(bot.config().colorPalette().string())),
|
||||
Component.text(mail.server()).color(ColorUtilities.getColorByString(bot.config().colorPalette().string()))
|
||||
).color(NamedTextColor.GREEN)
|
||||
)
|
||||
),
|
||||
Component.text(mail.contents()).color(NamedTextColor.WHITE)
|
||||
).color(NamedTextColor.GREEN)
|
||||
);
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
final Component component = Component.empty()
|
||||
.append(Component.text("Mails ").color(NamedTextColor.GREEN))
|
||||
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.text(senderMailSize).color(NamedTextColor.GRAY))
|
||||
.append(Component.text(")").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.newline())
|
||||
.append(Component.join(JoinConfiguration.newlines(), mailsComponent));
|
||||
|
||||
if (context.inGame()) {
|
||||
bot.chat().tellraw(
|
||||
component,
|
||||
context.sender().profile().getId()
|
||||
);
|
||||
} else {
|
||||
context.sendOutput(component);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
default -> context.sendOutput(Component.text("Invalid argument").color(NamedTextColor.RED));
|
||||
}
|
||||
case "read" -> {
|
||||
// TODO: use less for loops?
|
||||
});
|
||||
|
||||
int senderMailSize = 0;
|
||||
for (JsonElement mailElement : MailPlugin.mails()) {
|
||||
final Mail mail = gson.fromJson(mailElement, Mail.class);
|
||||
|
||||
if (!mail.sentTo().equals(sender.profile().getName())) continue;
|
||||
senderMailSize++;
|
||||
}
|
||||
|
||||
if (senderMailSize == 0) return Component.text("You have no new mails").color(NamedTextColor.RED);
|
||||
|
||||
final List<Component> mailsComponent = new ArrayList<>();
|
||||
|
||||
int i = 1;
|
||||
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");
|
||||
final String formattedTime = formatter.print(mail.timeSent());
|
||||
|
||||
mailsComponent.add(
|
||||
Component.translatable(
|
||||
"""
|
||||
%s %s Sent by: %s %s
|
||||
Contents:
|
||||
%s""",
|
||||
Component.text(i).color(ColorUtilities.getColorByString(bot.config().colorPalette().number())),
|
||||
Component.text("-").color(NamedTextColor.DARK_GRAY),
|
||||
|
||||
Component.text(mail.sentBy()).color(ColorUtilities.getColorByString(bot.config().colorPalette().username())),
|
||||
Component
|
||||
.text("[Hover here for more info]")
|
||||
.color(NamedTextColor.GREEN)
|
||||
.hoverEvent(
|
||||
HoverEvent.showText(
|
||||
Component.translatable(
|
||||
"""
|
||||
Time sent: %s
|
||||
Server: %s""",
|
||||
Component.text(formattedTime).color(ColorUtilities.getColorByString(bot.config().colorPalette().string())),
|
||||
Component.text(mail.server()).color(ColorUtilities.getColorByString(bot.config().colorPalette().string()))
|
||||
).color(NamedTextColor.GREEN)
|
||||
)
|
||||
),
|
||||
Component.text(mail.contents()).color(NamedTextColor.WHITE)
|
||||
).color(NamedTextColor.GREEN)
|
||||
);
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
final Component component = Component.empty()
|
||||
.append(Component.text("Mails ").color(NamedTextColor.GREEN))
|
||||
.append(Component.text("(").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.text(senderMailSize).color(NamedTextColor.GRAY))
|
||||
.append(Component.text(")").color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.newline())
|
||||
.append(Component.join(JoinConfiguration.newlines(), mailsComponent));
|
||||
|
||||
if (context.inGame()) {
|
||||
bot.chat().tellraw(
|
||||
component,
|
||||
context.sender().profile().getId()
|
||||
);
|
||||
} else {
|
||||
context.sendOutput(component);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
default -> {
|
||||
return Component.text("Invalid argument").color(NamedTextColor.RED);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue