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

View File

@ -1,6 +1,5 @@
package dev.plex.command.impl; package dev.plex.command.impl;
import dev.plex.Plex;
import dev.plex.command.PlexCommand; import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
@ -10,16 +9,13 @@ import dev.plex.module.PlexModule;
import dev.plex.module.PlexModuleFile; import dev.plex.module.PlexModuleFile;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.BuildInfo; import dev.plex.util.BuildInfo;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import dev.plex.util.TimeUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -65,7 +61,7 @@ public class PlexCMD extends PlexCommand
} }
plugin.getServiceManager().endServices(); plugin.getServiceManager().endServices();
plugin.getServiceManager().startServices(); plugin.getServiceManager().startServices();
PlexLog.debug("Restarted services"); send(sender, "Restarted services.");
TimeUtils.TIMEZONE = plugin.config.getString("server.timezone"); TimeUtils.TIMEZONE = plugin.config.getString("server.timezone");
send(sender, "Set timezone to: " + TimeUtils.TIMEZONE); send(sender, "Set timezone to: " + TimeUtils.TIMEZONE);
send(sender, "Plex successfully reloaded."); send(sender, "Plex successfully reloaded.");
@ -92,17 +88,27 @@ public class PlexCMD extends PlexCommand
} }
if (args[1].equalsIgnoreCase("reload")) if (args[1].equalsIgnoreCase("reload"))
{ {
checkRank(sender, Rank.SENIOR_ADMIN, "plex.modules.reload"); checkRank(sender, Rank.EXECUTIVE, "plex.modules.reload");
plugin.getModuleManager().unloadModules(); plugin.getModuleManager().reloadModules();
plugin.getModuleManager().loadAllModules();
plugin.getModuleManager().loadModules();
plugin.getModuleManager().enableModules();
return mmString("<green>All modules reloaded!"); 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")) 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"); return messageComponent("noPermissionRank", "a developer");
} }
@ -110,8 +116,8 @@ public class PlexCMD extends PlexCommand
{ {
return mmString("<red>Plex is already up to date!"); return mmString("<red>Plex is already up to date!");
} }
plugin.getUpdateChecker().updateJar(sender); plugin.getUpdateChecker().updateJar(sender, "Plex", false);
return null; return mmString("<red>Alert: Restart the server for the new JAR file to be applied.");
} }
else 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.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import dev.plex.Plex;
import dev.plex.PlexBase; import dev.plex.PlexBase;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -14,6 +13,7 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -37,7 +37,7 @@ public class UpdateChecker implements PlexBase
* 0 = Up to date * 0 = Up to date
* > 0 = Number of commits behind * > 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 String branch = plugin.config.getString("update_branch");
private int distance = -4; 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 it's -4, it hasn't checked for updates yet
if (distance == -4) 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..."); PlexLog.debug("Never checked for updates, checking now...");
} }
else else
@ -101,7 +101,7 @@ public class UpdateChecker implements PlexBase
// If the request isn't asked to be cached, fetch it // If the request isn't asked to be cached, fetch it
if (!cached) 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."); PlexLog.debug("We have checked for updates before, but this request was not asked to be cached.");
} }
else 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(); 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 try
{ {
HttpResponse response = client.execute(get); HttpResponse response = client.execute(get);
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0);
String name = artifact.getString("fileName"); String jarFile = artifact.getString("fileName");
sender.sendMessage(PlexUtils.mmDeserialize("<green>Downloading latest Plex jar file: " + name)); 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(() -> CompletableFuture.runAsync(() ->
{ {
try try
{ {
FileUtils.copyURLToFile( FileUtils.copyURLToFile(
new URL(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/artifact/build/libs/" + name), new URL(url + "/lastSuccessfulBuild/artifact/build/libs/" + jarFile),
new File(Bukkit.getUpdateFolderFile(), name) 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) catch (IOException e)
{ {

View File

@ -176,3 +176,10 @@ sayMessage: "<blue>[Server: {0}] {1}"
consoleSayMessage: "<gray>[Console: {0}] <white>{1}" consoleSayMessage: "<gray>[Console: {0}] <white>{1}"
# 0 - The number attempted to be parsed # 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."