Add customizable messages to notes

Also allows updating modules
This commit is contained in:
Telesphoreo 2022-04-21 21:26:51 -05:00
parent 6e4e634f59
commit ea834da575
5 changed files with 80 additions and 39 deletions

View File

@ -10,7 +10,6 @@ import dev.plex.rank.enums.Rank;
import dev.plex.storage.StorageType;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
@ -21,6 +20,8 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -38,8 +39,12 @@ public class NotesCMD extends PlexCommand
return usage();
}
Player player = getNonNullPlayer(args[0]);
PlexPlayer plexPlayer = getPlexPlayer(player);
OfflinePlayer player = Bukkit.getPlayer(args[0]);
if (!player.hasPlayedBefore())
{
return messageComponent("playerNotFound");
}
PlexPlayer plexPlayer = getPlexPlayer(player.getPlayer());
switch (args[1].toLowerCase())
{
@ -51,7 +56,7 @@ public class NotesCMD extends PlexCommand
{
if (notes.size() == 0)
{
send(sender, mmString("<red>This player has no notes!"));
send(sender, messageComponent("noNotes"));
return;
}
readNotes(sender, plexPlayer, notes);
@ -62,7 +67,7 @@ public class NotesCMD extends PlexCommand
List<Note> notes = plexPlayer.getNotes();
if (notes.size() == 0)
{
return mmString("<red>This player has no notes!");
return messageComponent("noNotes");
}
readNotes(sender, plexPlayer, notes);
}
@ -87,7 +92,7 @@ public class NotesCMD extends PlexCommand
{
DataUtils.update(plexPlayer);
}
return Component.text("Note added.").color(NamedTextColor.GREEN);
return messageComponent("noteAdded");
}
}
case "remove":
@ -103,7 +108,7 @@ public class NotesCMD extends PlexCommand
}
catch (NumberFormatException ignored)
{
return Component.text("Invalid number: " + args[2]).color(NamedTextColor.RED);
return messageComponent("unableToParseNumber", args[2]);
}
if (plugin.getStorageType() != StorageType.MONGODB)
{
@ -115,13 +120,13 @@ public class NotesCMD extends PlexCommand
if (note.getId() == id)
{
plugin.getSqlNotes().deleteNote(id, plexPlayer.getUuid()).whenComplete((notes1, ex1) ->
send(sender, Component.text("Removed note with ID: " + id).color(NamedTextColor.GREEN)));
send(sender, messageComponent("removedNote", id)));
deleted = true;
}
}
if (!deleted)
{
send(sender, mmString("<red>A note with this ID could not be found"));
send(sender, messageComponent("noteNotFound"));
}
plexPlayer.getNotes().removeIf(note -> note.getId() == id);
});
@ -130,9 +135,9 @@ public class NotesCMD extends PlexCommand
{
if (plexPlayer.getNotes().removeIf(note -> note.getId() == id))
{
return Component.text("Removed note with ID: " + id).color(NamedTextColor.GREEN);
return messageComponent("removedNote", id);
}
return mmString("<red>A note with this ID could not be found");
return messageComponent("noteNotFound");
}
return null;
}
@ -147,7 +152,7 @@ public class NotesCMD extends PlexCommand
plugin.getSqlNotes().deleteNote(note.getId(), plexPlayer.getUuid());
}
plexPlayer.getNotes().clear();
send(sender, Component.text("Cleared " + notes.size() + " note(s).").color(NamedTextColor.GREEN));
send(sender, messageComponent("clearedNotes", notes.size()));
});
}
else
@ -155,7 +160,7 @@ public class NotesCMD extends PlexCommand
int count = plexPlayer.getNotes().size();
plexPlayer.getNotes().clear();
DataUtils.update(plexPlayer);
return Component.text("Cleared " + count + " note(s).").color(NamedTextColor.GREEN);
return messageComponent("clearedNotes", count);
}
return null;
}

View File

@ -1,6 +1,5 @@
package dev.plex.command.impl;
import dev.plex.Plex;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
@ -10,16 +9,13 @@ import dev.plex.module.PlexModule;
import dev.plex.module.PlexModuleFile;
import dev.plex.rank.enums.Rank;
import dev.plex.util.BuildInfo;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import dev.plex.util.TimeUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -65,7 +61,7 @@ public class PlexCMD extends PlexCommand
}
plugin.getServiceManager().endServices();
plugin.getServiceManager().startServices();
PlexLog.debug("Restarted services");
send(sender, "Restarted services.");
TimeUtils.TIMEZONE = plugin.config.getString("server.timezone");
send(sender, "Set timezone to: " + TimeUtils.TIMEZONE);
send(sender, "Plex successfully reloaded.");
@ -92,17 +88,27 @@ public class PlexCMD extends PlexCommand
}
if (args[1].equalsIgnoreCase("reload"))
{
checkRank(sender, Rank.SENIOR_ADMIN, "plex.modules.reload");
plugin.getModuleManager().unloadModules();
plugin.getModuleManager().loadAllModules();
plugin.getModuleManager().loadModules();
plugin.getModuleManager().enableModules();
checkRank(sender, Rank.EXECUTIVE, "plex.modules.reload");
plugin.getModuleManager().reloadModules();
return mmString("<green>All modules reloaded!");
}
else if (args[1].equalsIgnoreCase("update"))
{
if (sender instanceof Player && !PlexUtils.DEVELOPERS.contains(playerSender.getUniqueId().toString()))
{
return messageComponent("noPermissionRank", "a developer");
}
for (PlexModule module : plugin.getModuleManager().getModules())
{
plugin.getUpdateChecker().updateJar(sender, module.getPlexModuleFile().getName(), true);
}
plugin.getModuleManager().reloadModules();
return mmString("<green>All modules updated and reloaded!");
}
}
else if (args[0].equalsIgnoreCase("update"))
{
if (sender instanceof Player player && !PlexUtils.DEVELOPERS.contains(player.getUniqueId().toString()))
if (sender instanceof Player && !PlexUtils.DEVELOPERS.contains(playerSender.getUniqueId().toString()))
{
return messageComponent("noPermissionRank", "a developer");
}
@ -110,8 +116,8 @@ public class PlexCMD extends PlexCommand
{
return mmString("<red>Plex is already up to date!");
}
plugin.getUpdateChecker().updateJar(sender);
return null;
plugin.getUpdateChecker().updateJar(sender, "Plex", false);
return mmString("<red>Alert: Restart the server for the new JAR file to be applied.");
}
else
{

View File

@ -141,4 +141,12 @@ public class ModuleManager
}
});
}
public void reloadModules()
{
unloadModules();
loadAllModules();
loadModules();
enableModules();
}
}

View File

@ -4,7 +4,6 @@ import com.google.common.base.Charsets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import dev.plex.Plex;
import dev.plex.PlexBase;
import java.io.BufferedReader;
import java.io.File;
@ -14,6 +13,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@ -37,7 +37,7 @@ public class UpdateChecker implements PlexBase
* 0 = Up to date
* > 0 = Number of commits behind
*/
private final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
private final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/";
private String branch = plugin.config.getString("update_branch");
private int distance = -4;
@ -93,7 +93,7 @@ public class UpdateChecker implements PlexBase
// If it's -4, it hasn't checked for updates yet
if (distance == -4)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", branch, Plex.build.head);
distance = fetchDistanceFromGitHub("plexusorg/Plex", branch, BuildInfo.getHead());
PlexLog.debug("Never checked for updates, checking now...");
}
else
@ -101,7 +101,7 @@ public class UpdateChecker implements PlexBase
// If the request isn't asked to be cached, fetch it
if (!cached)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", branch, Plex.build.head);
distance = fetchDistanceFromGitHub("plexusorg/Plex", branch, BuildInfo.getHead());
PlexLog.debug("We have checked for updates before, but this request was not asked to be cached.");
}
else
@ -149,26 +149,41 @@ public class UpdateChecker implements PlexBase
}
}
public void updateJar(CommandSender sender)
public void updateJar(CommandSender sender, String name, boolean module)
{
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/api/json");
AtomicReference<String> url = new AtomicReference<>(DOWNLOAD_PAGE + name);
if (!module)
{
url.set(url.get() + "/job/" + branch);
}
PlexLog.debug(url.toString());
HttpGet get = new HttpGet(url + "/lastSuccessfulBuild/api/json");
try
{
HttpResponse response = client.execute(get);
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0);
String name = artifact.getString("fileName");
sender.sendMessage(PlexUtils.mmDeserialize("<green>Downloading latest Plex jar file: " + name));
String jarFile = artifact.getString("fileName");
sender.sendMessage(PlexUtils.mmDeserialize("<green>Downloading latest JAR file: " + jarFile));
File copyTo;
if (!module)
{
copyTo = new File(Bukkit.getUpdateFolderFile(), jarFile);
}
else
{
copyTo = new File(plugin.getModulesFolder().getPath(), jarFile);
}
CompletableFuture.runAsync(() ->
{
try
{
FileUtils.copyURLToFile(
new URL(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/artifact/build/libs/" + name),
new File(Bukkit.getUpdateFolderFile(), name)
new URL(url + "/lastSuccessfulBuild/artifact/build/libs/" + jarFile),
copyTo
);
sender.sendMessage(PlexUtils.mmDeserialize("<green>Saved new jar. Please restart your server."));
sender.sendMessage(PlexUtils.mmDeserialize("<green>New JAR file downloaded successfully."));
}
catch (IOException e)
{

View File

@ -175,4 +175,11 @@ sayMessage: "<blue>[Server: {0}] {1}"
# 1 - The message being said
consoleSayMessage: "<gray>[Console: {0}] <white>{1}"
# 0 - The number attempted to be parsed
unableToParseNumber: "<red>Unable to parse {0} as a number!"
unableToParseNumber: "<red>Unable to parse {0} as a number!"
noNotes: "<red>This player has no notes!"
noteAdded: "<green>Note added."
noteNotFound: "<red>A note with this ID could not be found."
# 0 - The ID of the note removed
removedNote: "<green>Removed note with ID: {0}"
# 0 - The number of notes cleared
clearedNotes: "<green>Cleared {0} notes."