From 5721589c24ef322c732f19fce86380d1a8ccdf9a Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:04:23 +0200 Subject: [PATCH 01/12] Make constants upper snake case --- .../forgemod/CommunityRadarMod.java | 12 ++++++------ .../event/ClientChatReceivedListener.java | 4 ++-- .../forgemod/radarlistmanager/RadarList.java | 4 ++-- .../radarlistmanager/RadarListManager.java | 16 ++++++++-------- .../communityradargg/forgemod/util/Utils.java | 6 +++--- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java index 9f0d2fe..e661302 100644 --- a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java +++ b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java @@ -41,8 +41,8 @@ @Mod(modid = CommunityRadarMod.MOD_ID) public class CommunityRadarMod { public static final String MOD_ID = "communityradar"; - private static final Logger logger = LogManager.getLogger(CommunityRadarMod.class); private static RadarListManager listManager; + private static final Logger LOGGER = LogManager.getLogger(CommunityRadarMod.class); private String version; private boolean onGrieferGames = false; @@ -54,7 +54,7 @@ public class CommunityRadarMod { @EventHandler @SuppressWarnings("unused") // called by the mod loader public void init(final FMLInitializationEvent event) { - logger.info("Loading the mod '" + MOD_ID + "'!"); + LOGGER.info("Loading the mod '{}'", MOD_ID); final ModContainer modContainer = Loader.instance().getIndexedModList().get(MOD_ID); version = modContainer == null ? "UNKNOWN" : modContainer.getVersion(); @@ -63,7 +63,7 @@ public void init(final FMLInitializationEvent event) { .getAbsolutePath(),"communityradar", "lists") .toFile(); if (!directoryPath.exists() && !directoryPath.mkdirs()) { - logger.error("Could not create directory: {}", directoryPath); + LOGGER.error("Could not create directory: {}", directoryPath); } listManager = new RadarListManager(directoryPath.getAbsolutePath() + "/"); @@ -72,7 +72,7 @@ public void init(final FMLInitializationEvent event) { listManager.loadPrivateLists(); registerEvents(); registerCommands(); - logger.info("Successfully loaded the mod '" + MOD_ID + "'!"); + LOGGER.info("Successfully loaded the mod '{}'", MOD_ID); } /** @@ -97,11 +97,11 @@ private void registerCommands() { */ private void registerPublicLists() { if (!listManager.registerPublicList("scammer", "&7[&cScammer&7]", "https://lists.community-radar.de/versions/v1/scammer.json")) { - logger.error("Could not register public list 'scammers'!"); + LOGGER.error("Could not register public list 'scammers'!"); } if (!listManager.registerPublicList("trusted", "&7[&aTrusted&7]", "https://lists.community-radar.de/versions/v1/trusted.json")) { - logger.error("Could not register public list 'verbvllert_trusted'!"); + LOGGER.error("Could not register public list 'verbvllert_trusted'!"); } } diff --git a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java index cbdb202..b3d1898 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java @@ -32,8 +32,8 @@ public class ClientChatReceivedListener { /** * A pattern matching private messages (in and out) and payments (in and out) as well as global and plot chat messages with the player name (nicked, bedrock and java) as only group. */ - private static final Pattern pattern = Pattern.compile("[A-Za-z\\-+]+\\s\\u2503\\s(~?!?\\w{1,16})"); private final CommunityRadarMod mod; + private static final Pattern PATTERN = Pattern.compile("[A-Za-z\\-+]+\\s\\u2503\\s(~?!?\\w{1,16})"); /** * Constructs the class {@link ClientChatReceivedListener}. @@ -56,7 +56,7 @@ public void onClientChatReceived(final ClientChatReceivedEvent event) { return; } - final Matcher matcher = pattern.matcher(event.message.getUnformattedText()); + final Matcher matcher = PATTERN.matcher(event.message.getUnformattedText()); if (!matcher.find()) { return; } diff --git a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java index 5607509..5bde1ac 100644 --- a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java +++ b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java @@ -38,7 +38,7 @@ * A class representing a radar list. */ public class RadarList { - private static final Logger logger = LogManager.getLogger(RadarList.class); + private static final Logger LOGGER = LogManager.getLogger(RadarList.class); @SerializedName("VERSION") @SuppressWarnings("unused") // needed in future private final int version = 1; @@ -191,7 +191,7 @@ private void loadPublicList() { .fromJson(reader, new TypeToken>() {}.getType()); players.forEach(this::loadRadarListEntry); } catch (final IOException | JsonIOException | JsonSyntaxException e) { - logger.error("Could not load public list", e); + LOGGER.error("Could not load public list", e); } } diff --git a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java index d532f9d..2b1f20c 100644 --- a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java +++ b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java @@ -49,8 +49,8 @@ * A class containing the methods to manage lists. */ public class RadarListManager { - private static final Logger logger = LogManager.getLogger(RadarListManager.class); - private static final Gson gson = new GsonBuilder() + private static final Logger LOGGER = LogManager.getLogger(RadarListManager.class); + private static final Gson GSON = new GsonBuilder() .setPrettyPrinting() .registerTypeAdapter(LocalDateTime.class, new GsonLocalDateTimeAdapter()) .registerTypeAdapter(Map.class, new GsonRadarListPlayerMapAdapter()) @@ -169,9 +169,9 @@ public void saveRadarList(final @NotNull RadarList list) { } try (final FileWriter writer = new FileWriter(directoryPath + list.getNamespace() + ".json")) { - writer.write(gson.toJson(list)); + writer.write(GSON.toJson(list)); } catch (final IOException e) { - logger.error("Could not save list", e); + LOGGER.error("Could not save list", e); } } @@ -273,13 +273,13 @@ public void loadPrivateLists() { */ private @NotNull Optional loadRadarListFromFile(final @NotNull String filePath) { try (final FileReader reader = new FileReader(filePath)) { - final RadarList list = gson.fromJson(reader, new TypeToken() {}.getType()); + final RadarList list = GSON.fromJson(reader, new TypeToken() {}.getType()); list.setUrl(filePath); if (list.validateList()) { return Optional.of(list); } } catch (final IOException | IllegalStateException | JsonIOException | JsonSyntaxException e) { - logger.error("Could not load list from file", e); + LOGGER.error("Could not load list from file", e); } return Optional.empty(); } @@ -298,7 +298,7 @@ public void loadPrivateLists() { .filter(string -> string.endsWith(".json")) .collect(Collectors.toSet()); } catch (final IOException e) { - logger.error("Could not get json urls", e); + LOGGER.error("Could not get json urls", e); } return new HashSet<>(); } @@ -309,7 +309,7 @@ public void loadPrivateLists() { * @return Returns the pre-configured {@link Gson} instance. */ public static @NotNull Gson getGson() { - return gson; + return GSON; } /** diff --git a/src/main/java/io/github/communityradargg/forgemod/util/Utils.java b/src/main/java/io/github/communityradargg/forgemod/util/Utils.java index 1872f0a..f1e5b4d 100644 --- a/src/main/java/io/github/communityradargg/forgemod/util/Utils.java +++ b/src/main/java/io/github/communityradargg/forgemod/util/Utils.java @@ -47,7 +47,7 @@ * A class with some util methods. */ public class Utils { - private static final Logger logger = LogManager.getLogger(Utils.class); + private static final Logger LOGGER = LogManager.getLogger(Utils.class); private static final String MOJANG_API_NAME_TO_UUID = "https://api.mojang.com/users/profiles/minecraft/"; private static final Pattern UUID_MOJANG_API_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); private static final DateTimeFormatter readableDateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"); @@ -107,7 +107,7 @@ public class Utils { connection.setRequestProperty("User-Agent", CommunityRadarMod.MOD_ID + "/" + mod.getVersion()); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { - logger.warn("Requesting data from '{}' resulted in following status code: {}", urlText, connection.getResponseCode()); + LOGGER.warn("Requesting data from '{}' resulted in following status code: {}", urlText, connection.getResponseCode()); return Optional.empty(); } @@ -127,7 +127,7 @@ public class Utils { if (connection != null) { connection.disconnect(); } - logger.error("Trying to request data from '{}' resulted in an exception", urlText, e); + LOGGER.error("Trying to request data from '{}' resulted in an exception", urlText, e); return Optional.empty(); } }); From f3473a8d25ee461223091dcc05793c21c8a2dab0 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:06:46 +0200 Subject: [PATCH 02/12] Remove not needed javadoc --- .../communityradargg/forgemod/CommunityRadarMod.java | 2 +- .../communityradargg/forgemod/command/RadarCommand.java | 8 +------- .../forgemod/event/ClientChatReceivedListener.java | 2 +- .../adapters/GsonLocalDateTimeAdapter.java | 2 -- .../adapters/GsonRadarListPlayerMapAdapter.java | 2 -- 5 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java index e661302..c9a5411 100644 --- a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java +++ b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java @@ -41,8 +41,8 @@ @Mod(modid = CommunityRadarMod.MOD_ID) public class CommunityRadarMod { public static final String MOD_ID = "communityradar"; - private static RadarListManager listManager; private static final Logger LOGGER = LogManager.getLogger(CommunityRadarMod.class); + private RadarListManager listManager; private String version; private boolean onGrieferGames = false; diff --git a/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java b/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java index f03fbaa..22a6acb 100644 --- a/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java +++ b/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java @@ -39,7 +39,7 @@ import java.util.UUID; /** - * The class containing all logic for the radar command. + * The class containing all logic for the main radar command. */ public class RadarCommand extends CommandBase { private final CommunityRadarMod mod; @@ -53,37 +53,31 @@ public RadarCommand(final CommunityRadarMod mod) { this.mod = mod; } - /** {@inheritDoc} */ @Override public @NotNull String getCommandName() { return "radar"; } - /** {@inheritDoc} */ @Override public String getCommandUsage(final @NotNull ICommandSender sender) { return "/radar"; } - /** {@inheritDoc} */ @Override public List getCommandAliases() { return Arrays.asList("communityradar", "scammer", "trustedmm", "mm"); } - /** {@inheritDoc} */ @Override public int getRequiredPermissionLevel() { return 0; } - /** {@inheritDoc} */ @Override public boolean canCommandSenderUseCommand(final ICommandSender sender) { return sender instanceof EntityPlayer; } - /** {@inheritDoc} */ @Override public void processCommand(final ICommandSender sender, final String[] args) { if (!(sender instanceof EntityPlayer)) { diff --git a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java index b3d1898..01559c5 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java @@ -32,8 +32,8 @@ public class ClientChatReceivedListener { /** * A pattern matching private messages (in and out) and payments (in and out) as well as global and plot chat messages with the player name (nicked, bedrock and java) as only group. */ - private final CommunityRadarMod mod; private static final Pattern PATTERN = Pattern.compile("[A-Za-z\\-+]+\\s\\u2503\\s(~?!?\\w{1,16})"); + private final CommunityRadarMod communityRadarMod; /** * Constructs the class {@link ClientChatReceivedListener}. diff --git a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonLocalDateTimeAdapter.java b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonLocalDateTimeAdapter.java index 9b11ea6..3e671ad 100644 --- a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonLocalDateTimeAdapter.java +++ b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonLocalDateTimeAdapter.java @@ -31,13 +31,11 @@ * A class with an adapter for serialization and deserialization of the class {@link LocalDateTime} for the GSON library. */ public class GsonLocalDateTimeAdapter implements JsonSerializer, JsonDeserializer { - /** {@inheritDoc} */ @Override public LocalDateTime deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { return LocalDateTime.parse(json.getAsString(), DateTimeFormatter.ISO_DATE_TIME); } - /** {@inheritDoc} */ @Override public JsonElement serialize(final LocalDateTime localDateTime, final Type typeOfSrc, final JsonSerializationContext context) { return new JsonPrimitive(localDateTime.format(DateTimeFormatter.ISO_DATE_TIME)); diff --git a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonRadarListPlayerMapAdapter.java b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonRadarListPlayerMapAdapter.java index 98d91dc..877d1f4 100644 --- a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonRadarListPlayerMapAdapter.java +++ b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/adapters/GsonRadarListPlayerMapAdapter.java @@ -33,7 +33,6 @@ * A class with an adapter for serialization and deserialization of following structure {@code Map} for the GSON library. */ public class GsonRadarListPlayerMapAdapter implements JsonSerializer>, JsonDeserializer> { - /** {@inheritDoc} */ @Override public Map deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final JsonArray playerMapJsonArray = json.getAsJsonArray(); @@ -46,7 +45,6 @@ public Map deserialize(final JsonElement json, final Type return playerMap; } - /** {@inheritDoc} */ @Override public JsonElement serialize(final Map playerMap, final Type typeOfSrc, final JsonSerializationContext context) { return context.serialize(playerMap.values()); From 41f51bff01cf014d8f1c5bc73927c514cdeff7ae Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:08:47 +0200 Subject: [PATCH 03/12] Remove suppress warnings unused from listeners and indirect called methods --- .../io/github/communityradargg/forgemod/CommunityRadarMod.java | 1 - .../forgemod/event/ClientChatReceivedListener.java | 1 - .../forgemod/event/ClientConnectionDisconnectListener.java | 2 -- .../communityradargg/forgemod/event/KeyInputListener.java | 1 - .../forgemod/event/PlayerNameFormatListener.java | 1 - 5 files changed, 6 deletions(-) diff --git a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java index c9a5411..b54ba87 100644 --- a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java +++ b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java @@ -52,7 +52,6 @@ public class CommunityRadarMod { * @param event The event. */ @EventHandler - @SuppressWarnings("unused") // called by the mod loader public void init(final FMLInitializationEvent event) { LOGGER.info("Loading the mod '{}'", MOD_ID); diff --git a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java index 01559c5..a7f4da1 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java @@ -50,7 +50,6 @@ public ClientChatReceivedListener(final @NotNull CommunityRadarMod mod) { * @param event The event. */ @SubscribeEvent - @SuppressWarnings("unused") // event listener public void onClientChatReceived(final ClientChatReceivedEvent event) { if (!mod.isOnGrieferGames()) { return; diff --git a/src/main/java/io/github/communityradargg/forgemod/event/ClientConnectionDisconnectListener.java b/src/main/java/io/github/communityradargg/forgemod/event/ClientConnectionDisconnectListener.java index 353e701..b89d2a3 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/ClientConnectionDisconnectListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/ClientConnectionDisconnectListener.java @@ -45,7 +45,6 @@ public ClientConnectionDisconnectListener(final @NotNull CommunityRadarMod commu * @param event The event. */ @SubscribeEvent - @SuppressWarnings("unused") // called by forge public void onFMLNetworkClientConnectedToServer(final FMLNetworkEvent.ClientConnectedToServerEvent event) { if (event.isLocal) { return; @@ -70,7 +69,6 @@ public void onFMLNetworkClientConnectedToServer(final FMLNetworkEvent.ClientConn * @param event The event. */ @SubscribeEvent - @SuppressWarnings("unused") // called by forge public void onFMLNetworkClientDisconnectionFromServer(final FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { communityRadarMod.setOnGrieferGames(false); } diff --git a/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java b/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java index b3b3a24..31d620d 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java @@ -43,7 +43,6 @@ public KeyInputListener(final @NotNull CommunityRadarMod communityRadarMod) { * @param event The event. */ @SubscribeEvent - @SuppressWarnings("unused") // called by mod loader on event public void onKeyInput(final InputEvent.KeyInputEvent event) { if (!communityRadarMod.isOnGrieferGames()) { return; diff --git a/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java b/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java index 1e30044..664038e 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java @@ -42,7 +42,6 @@ public PlayerNameFormatListener(final @NotNull CommunityRadarMod communityRadarM * @param event The event. */ @SubscribeEvent - @SuppressWarnings("unused") // called by forge event system public void onPlayerNameFormat(final PlayerEvent.NameFormat event) { if (!communityRadarMod.isOnGrieferGames()) { return; From d2c13b636f3df445dec88393de4c673aebbfcce2 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:13:33 +0200 Subject: [PATCH 04/12] Prepare for making the CommunityRadarMod#getListManager non-static --- .../forgemod/CommunityRadarMod.java | 2 +- .../forgemod/event/ClientChatReceivedListener.java | 14 +++++++------- .../forgemod/event/KeyInputListener.java | 2 +- .../forgemod/event/PlayerNameFormatListener.java | 2 +- .../forgemod/radarlistmanager/RadarList.java | 7 +++++-- .../radarlistmanager/RadarListManager.java | 10 +++++++--- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java index b54ba87..a9cb192 100644 --- a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java +++ b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java @@ -65,7 +65,7 @@ public void init(final FMLInitializationEvent event) { LOGGER.error("Could not create directory: {}", directoryPath); } - listManager = new RadarListManager(directoryPath.getAbsolutePath() + "/"); + listManager = new RadarListManager(this, directoryPath.getAbsolutePath() + "/"); registerPublicLists(); // Needs to be after loading public lists listManager.loadPrivateLists(); diff --git a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java index a7f4da1..bc48f59 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/ClientChatReceivedListener.java @@ -38,10 +38,10 @@ public class ClientChatReceivedListener { /** * Constructs the class {@link ClientChatReceivedListener}. * - * @param mod The mod main class instance. + * @param communityRadarMod The mod main class instance. */ - public ClientChatReceivedListener(final @NotNull CommunityRadarMod mod) { - this.mod = mod; + public ClientChatReceivedListener(final @NotNull CommunityRadarMod communityRadarMod) { + this.communityRadarMod = communityRadarMod; } /** @@ -51,7 +51,7 @@ public ClientChatReceivedListener(final @NotNull CommunityRadarMod mod) { */ @SubscribeEvent public void onClientChatReceived(final ClientChatReceivedEvent event) { - if (!mod.isOnGrieferGames()) { + if (!communityRadarMod.isOnGrieferGames()) { return; } @@ -66,9 +66,9 @@ public void onClientChatReceived(final ClientChatReceivedEvent event) { return; } - Utils.getUUID(mod, playerName).thenAccept(uuid -> { - if (uuid.isPresent() && CommunityRadarMod.getListManager().isInList(uuid.get())) { - event.message = new ChatComponentText(CommunityRadarMod.getListManager().getPrefix(uuid.get()).replace("&", "§")) + Utils.getUUID(communityRadarMod, playerName).thenAccept(uuid -> { + if (uuid.isPresent() && communityRadarMod.getListManager().isInList(uuid.get())) { + event.message = new ChatComponentText(communityRadarMod.getListManager().getPrefix(uuid.get()).replace("&", "§")) .appendText(" §r") .appendText(event.message.getFormattedText()); } diff --git a/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java b/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java index 31d620d..bd3253c 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/KeyInputListener.java @@ -53,6 +53,6 @@ public void onKeyInput(final InputEvent.KeyInputEvent event) { return; } - Utils.updatePrefixes(CommunityRadarMod.getListManager().getExistingPrefixes()); + Utils.updatePrefixes(communityRadarMod, communityRadarMod.getListManager().getExistingPrefixes()); } } diff --git a/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java b/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java index 664038e..1aab8c2 100644 --- a/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java +++ b/src/main/java/io/github/communityradargg/forgemod/event/PlayerNameFormatListener.java @@ -46,6 +46,6 @@ public void onPlayerNameFormat(final PlayerEvent.NameFormat event) { if (!communityRadarMod.isOnGrieferGames()) { return; } - Utils.updatePlayerNameTag(event.entityPlayer, CommunityRadarMod.getListManager().getExistingPrefixes()); + Utils.updatePlayerNameTag(communityRadarMod, event.entityPlayer, communityRadarMod.getListManager().getExistingPrefixes()); } } diff --git a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java index 5bde1ac..6685429 100644 --- a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java +++ b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarList.java @@ -39,6 +39,7 @@ */ public class RadarList { private static final Logger LOGGER = LogManager.getLogger(RadarList.class); + private transient final CommunityRadarMod communityRadarMod; @SerializedName("VERSION") @SuppressWarnings("unused") // needed in future private final int version = 1; @@ -55,12 +56,14 @@ public class RadarList { /** * Constructs a {@link RadarList}. * + * @param communityRadarMod The mod main class instance. * @param namespace The namespace for the list. * @param prefix The prefix for the list. * @param url The url for the list. * @param visibility The visibility of the list. */ - public RadarList(final @NotNull String namespace, final @NotNull String prefix, final @NotNull String url, final @NotNull RadarListVisibility visibility) { + public RadarList(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull String namespace, final @NotNull String prefix, final @NotNull String url, final @NotNull RadarListVisibility visibility) { + this.communityRadarMod = communityRadarMod; this.namespace = namespace; this.prefix = prefix; this.visibility = visibility; @@ -178,7 +181,7 @@ private void loadRadarListEntry(final @NotNull RadarListEntry radarListEntry) { */ public void saveList() { if (visibility == RadarListVisibility.PRIVATE) { - CommunityRadarMod.getListManager().saveRadarList(this); + communityRadarMod.getListManager().saveRadarList(this); } } diff --git a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java index 2b1f20c..233b216 100644 --- a/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java +++ b/src/main/java/io/github/communityradargg/forgemod/radarlistmanager/RadarListManager.java @@ -20,6 +20,7 @@ import com.google.gson.JsonIOException; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; +import io.github.communityradargg.forgemod.CommunityRadarMod; import io.github.communityradargg.forgemod.radarlistmanager.adapters.GsonLocalDateTimeAdapter; import io.github.communityradargg.forgemod.radarlistmanager.adapters.GsonRadarListPlayerMapAdapter; import org.apache.logging.log4j.LogManager; @@ -55,16 +56,19 @@ public class RadarListManager { .registerTypeAdapter(LocalDateTime.class, new GsonLocalDateTimeAdapter()) .registerTypeAdapter(Map.class, new GsonRadarListPlayerMapAdapter()) .create(); + private final CommunityRadarMod communityRadarMod; private final List lists; private final String directoryPath; /** * Constructs a {@link RadarListManager} * + * @param communityRadarMod The mod main class instance. * @param directoryPath The directory path of the list the manager manages. */ - public RadarListManager(final @NotNull String directoryPath) { + public RadarListManager(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull String directoryPath) { this.lists = new ArrayList<>(); + this.communityRadarMod = communityRadarMod; this.directoryPath = directoryPath; } @@ -189,7 +193,7 @@ public boolean registerPrivateList(final @NotNull String namespace, final @NotNu return false; } - lists.add(new RadarList(namespace, prefix, directoryPath + namespace + ".json", RadarListVisibility.PRIVATE)); + lists.add(new RadarList(communityRadarMod, namespace, prefix, directoryPath + namespace + ".json", RadarListVisibility.PRIVATE)); final Optional listOptional = getRadarList(namespace); if (!listOptional.isPresent()) { @@ -215,7 +219,7 @@ public boolean registerPublicList(final @NotNull String namespace, final @NotNul return false; } - lists.add(new RadarList(namespace, prefix, url, RadarListVisibility.PUBLIC)); + lists.add(new RadarList(communityRadarMod, namespace, prefix, url, RadarListVisibility.PUBLIC)); return true; } From ed4298c2a6148f4eeec106b6dd594ed9e687b9ae Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:14:59 +0200 Subject: [PATCH 05/12] Move subcommand to extra classes --- .../forgemod/command/CheckSubcommand.java | 143 +++++ .../forgemod/command/HelpSubcommand.java | 49 ++ .../forgemod/command/ListSubcommand.java | 217 ++++++++ .../forgemod/command/ListsSubcommand.java | 64 +++ .../forgemod/command/PlayerSubcommand.java | 180 +++++++ .../forgemod/command/RadarCommand.java | 501 +----------------- .../forgemod/command/Subcommand.java | 26 + .../communityradargg/forgemod/util/Utils.java | 22 +- 8 files changed, 718 insertions(+), 484 deletions(-) create mode 100644 src/main/java/io/github/communityradargg/forgemod/command/CheckSubcommand.java create mode 100644 src/main/java/io/github/communityradargg/forgemod/command/HelpSubcommand.java create mode 100644 src/main/java/io/github/communityradargg/forgemod/command/ListSubcommand.java create mode 100644 src/main/java/io/github/communityradargg/forgemod/command/ListsSubcommand.java create mode 100644 src/main/java/io/github/communityradargg/forgemod/command/PlayerSubcommand.java create mode 100644 src/main/java/io/github/communityradargg/forgemod/command/Subcommand.java diff --git a/src/main/java/io/github/communityradargg/forgemod/command/CheckSubcommand.java b/src/main/java/io/github/communityradargg/forgemod/command/CheckSubcommand.java new file mode 100644 index 0000000..16ad84a --- /dev/null +++ b/src/main/java/io/github/communityradargg/forgemod/command/CheckSubcommand.java @@ -0,0 +1,143 @@ +/* + * Copyright 2024 - present CommunityRadarGG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.communityradargg.forgemod.command; + +import io.github.communityradargg.forgemod.CommunityRadarMod; +import io.github.communityradargg.forgemod.radarlistmanager.RadarListEntry; +import io.github.communityradargg.forgemod.util.Messages; +import io.github.communityradargg.forgemod.util.RadarMessage; +import io.github.communityradargg.forgemod.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.NetworkPlayerInfo; +import net.minecraft.entity.player.EntityPlayer; +import org.jetbrains.annotations.NotNull; +import java.util.Optional; + +/** + * Holds the logic of the check subcommand. + */ +public class CheckSubcommand implements Subcommand { + private final CommunityRadarMod communityRadarMod; + private final EntityPlayer player; + private final String[] args; + + /** + * Constructs a {@link CheckSubcommand}. + * + * @param communityRadarMod The mod main class instance. + * @param player The player. + * @param args The args. + */ + public CheckSubcommand(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull EntityPlayer player, final @NotNull String[] args) { + this.communityRadarMod = communityRadarMod; + this.player = player; + this.args = args; + } + + @Override + public void run() { + if (args.length != 2) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + if (args[1].equalsIgnoreCase("*")) { + // check all argument + handleCheckAllSubcommand(player); + return; + } + handleCheckPlayerSubcommand(player, args); + } + + /** + * Handles the check - player subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handleCheckPlayerSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.INPUT_PROCESSING) + .build().toChatComponentText()); + Utils.getUUID(communityRadarMod, args[1]).thenAccept(checkPlayerOptional -> { + if (!checkPlayerOptional.isPresent()) { + // player uuid could not be fetched + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.FAILED) + .build().toChatComponentText()); + return; + } + + final Optional entryOptional = communityRadarMod.getListManager().getRadarListEntry(checkPlayerOptional.get()); + if (!entryOptional.isPresent()) { + // player uuid is on no list + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.FAILED) + .build().toChatComponentText()); + return; + } + + final RadarListEntry entry = entryOptional.get(); + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.FOUND + "\n" + Messages.Check.CHECK_ENTRY) + .replaceWithColorCodes("{prefix}", communityRadarMod.getListManager().getPrefix(entry.uuid())) + .replace("{name}", entry.name()) + .replace("{cause}", entry.cause()) + .replace("{entryCreationDate}", Utils.formatDateTime(entry.entryCreationDate())) + .replace("{entryUpdateDate}", Utils.formatDateTime(entry.entryUpdateDate())) + .build().toChatComponentText()); + }); + } + + /** + * Handles the check - all subcommand. + * + * @param player The player, which executed the subcommand. + */ + private void handleCheckAllSubcommand(final @NotNull EntityPlayer player) { + boolean anyPlayerFound = false; + for (final NetworkPlayerInfo networkPlayer : Minecraft.getMinecraft().getNetHandler().getPlayerInfoMap()) { + if (networkPlayer.getGameProfile().getId() == null) { + continue; + } + + final Optional listEntryOptional = communityRadarMod.getListManager() + .getRadarListEntry(networkPlayer.getGameProfile().getId()); + if (!listEntryOptional.isPresent()) { + // player uuid is on no list + continue; + } + + if (!anyPlayerFound) { + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.EVERYONE) + .build().toChatComponentText()); + anyPlayerFound = true; + } + + final RadarListEntry entry = listEntryOptional.get(); + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.CHECK_ENTRY) + .replaceWithColorCodes("{prefix}", communityRadarMod.getListManager().getPrefix(entry.uuid())) + .replace("{name}", entry.name()) + .replace("{cause}", entry.cause()) + .replace("{entryCreationDate}", Utils.formatDateTime(entry.entryCreationDate())) + .replace("{entryUpdateDate}", Utils.formatDateTime(entry.entryUpdateDate())) + .build().toChatComponentText()); + } + + if (!anyPlayerFound) { + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.NOT_FOUND) + .build().toChatComponentText()); + } + } +} diff --git a/src/main/java/io/github/communityradargg/forgemod/command/HelpSubcommand.java b/src/main/java/io/github/communityradargg/forgemod/command/HelpSubcommand.java new file mode 100644 index 0000000..d697516 --- /dev/null +++ b/src/main/java/io/github/communityradargg/forgemod/command/HelpSubcommand.java @@ -0,0 +1,49 @@ +/* + * Copyright 2024 - present CommunityRadarGG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.communityradargg.forgemod.command; + +import io.github.communityradargg.forgemod.CommunityRadarMod; +import io.github.communityradargg.forgemod.util.Messages; +import io.github.communityradargg.forgemod.util.RadarMessage; +import net.minecraft.entity.player.EntityPlayer; +import org.jetbrains.annotations.NotNull; + +/** + * Holds the logic of the help subcommand. + */ +public class HelpSubcommand implements Subcommand { + private final CommunityRadarMod communityRadarMod; + private final EntityPlayer player; + + /** + * Constructs a {@link HelpSubcommand}. + * + * @param communityRadarMod The mod main class instance. + * @param player The player. + */ + public HelpSubcommand(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull EntityPlayer player) { + this.communityRadarMod = communityRadarMod; + this.player = player; + } + + @Override + public void run() { + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.HELP) + .replace("{code_version}", communityRadarMod.getVersion()) + .excludePrefix() + .build().toChatComponentText()); + } +} diff --git a/src/main/java/io/github/communityradargg/forgemod/command/ListSubcommand.java b/src/main/java/io/github/communityradargg/forgemod/command/ListSubcommand.java new file mode 100644 index 0000000..da5cf47 --- /dev/null +++ b/src/main/java/io/github/communityradargg/forgemod/command/ListSubcommand.java @@ -0,0 +1,217 @@ +/* + * Copyright 2024 - present CommunityRadarGG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.communityradargg.forgemod.command; + +import io.github.communityradargg.forgemod.CommunityRadarMod; +import io.github.communityradargg.forgemod.radarlistmanager.RadarList; +import io.github.communityradargg.forgemod.radarlistmanager.RadarListManager; +import io.github.communityradargg.forgemod.util.Messages; +import io.github.communityradargg.forgemod.util.RadarMessage; +import io.github.communityradargg.forgemod.util.Utils; +import net.minecraft.entity.player.EntityPlayer; +import org.jetbrains.annotations.NotNull; +import java.util.Collections; +import java.util.Locale; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; + +/** + * Holds the logic of the list subcommand. + */ +public class ListSubcommand implements Subcommand { + private final CommunityRadarMod communityRadarMod; + private final EntityPlayer player; + private final String[] args; + + /** + * Constructs a {@link ListSubcommand}. + * + * @param communityRadarMod The mod main class instance. + * @param player The player. + * @param args The args. + */ + public ListSubcommand(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull EntityPlayer player, final @NotNull String[] args) { + this.communityRadarMod = communityRadarMod; + this.player = player; + this.args = args; + } + + @Override + public void run() { + if (args.length < 2) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + switch (args[1].toUpperCase(Locale.ENGLISH)) { + case "ADD": + handleListAddSubcommand(player, args); + break; + case "PREFIX": + handleListPrefixSubcommand(player, args); + break; + case "DELETE": + handleListDeleteSubcommand(player, args); + break; + case "SHOW": + handleListShowSubcommand(player, args); + break; + default: + new HelpSubcommand(communityRadarMod, player).run(); + break; + } + } + + /** + * Handles the list - add subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handleListAddSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + if (args.length != 4) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + if (communityRadarMod.getListManager().getRadarList(args[2]).isPresent()) { + // list already existing + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.CREATE_FAILED) + .build().toChatComponentText()); + return; + } + + if (!communityRadarMod.getListManager().registerPrivateList(args[2], args[3])) { + // list could not be registered + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.CREATE_FAILED) + .build().toChatComponentText()); + return; + } + + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.CREATE_SUCCESS) + .build().toChatComponentText()); + } + + /** + * Handles the list - delete subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handleListDeleteSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + if (args.length != 3) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + final RadarListManager listManager = communityRadarMod.getListManager(); + final Set oldPrefixes = listManager.getExistingPrefixes(); + final Set oldUuids = listManager.getRadarList(args[2]) + .map(radarList -> radarList.getPlayerMap().keySet()) + .orElse(Collections.emptySet()); + + if (!listManager.unregisterList(args[2])) { + // list is not existing, list is not private, file cannot be deleted + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.DELETE_FAILED) + .build().toChatComponentText()); + return; + } + + oldUuids.forEach(uuid -> Utils.updatePlayerByUuid(communityRadarMod, uuid, oldPrefixes)); + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.DELETE_SUCCESS) + .build().toChatComponentText()); + } + + /** + * Handles the list - show subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handleListShowSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + if (args.length != 3) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + final Optional listOptional = communityRadarMod.getListManager().getRadarList(args[2]); + if (!listOptional.isPresent()) { + // list is not existing + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.SHOW_FAILED) + .build().toChatComponentText()); + return; + } + + final RadarList list = listOptional.get(); + if (list.getPlayerMap().isEmpty()) { + // list is empty + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.SHOW_EMPTY) + .build().toChatComponentText()); + return; + } + + final StringBuilder players = new StringBuilder(); + list.getPlayerMap().values().forEach(value -> players.append(value.name()).append(", ")); + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.SHOW_SUCCESS) + .replace("{list}", list.getNamespace()) + .replaceWithColorCodes("{prefix}", listOptional.get().getPrefix()) + .replace("{players}", players.substring(0, players.length() - 2)) + .build().toChatComponentText()); + } + + /** + * Handles the list - prefix subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handleListPrefixSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + if (args.length != 4) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + final RadarListManager listManager = communityRadarMod.getListManager(); + final Optional listOptional = listManager.getRadarList(args[2]); + if (!listOptional.isPresent()) { + // list is not existing + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.PREFIX_FAILED) + .build().toChatComponentText()); + return; + } + + final RadarList list = listOptional.get(); + final Set oldPrefixes = listManager.getExistingPrefixes(); + list.setPrefix(args[3]); + list.saveList(); + list.getPlayerMap().keySet().forEach(uuid -> Utils.updatePlayerByUuid(communityRadarMod, uuid, oldPrefixes)); + + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.PREFIX_SUCCESS) + .replaceWithColorCodes("{prefix}", args[3]) + .build().toChatComponentText()); + } +} diff --git a/src/main/java/io/github/communityradargg/forgemod/command/ListsSubcommand.java b/src/main/java/io/github/communityradargg/forgemod/command/ListsSubcommand.java new file mode 100644 index 0000000..6ecb7a7 --- /dev/null +++ b/src/main/java/io/github/communityradargg/forgemod/command/ListsSubcommand.java @@ -0,0 +1,64 @@ +/* + * Copyright 2024 - present CommunityRadarGG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.communityradargg.forgemod.command; + +import io.github.communityradargg.forgemod.CommunityRadarMod; +import io.github.communityradargg.forgemod.radarlistmanager.RadarListVisibility; +import io.github.communityradargg.forgemod.util.Messages; +import io.github.communityradargg.forgemod.util.RadarMessage; +import net.minecraft.entity.player.EntityPlayer; +import org.jetbrains.annotations.NotNull; + +/** + * Holds the logic of the lists subcommand. + */ +public class ListsSubcommand implements Subcommand { + private final CommunityRadarMod communityRadarMod; + private final EntityPlayer player; + + /** + * Constructs a {@link ListsSubcommand}. + * + * @param communityRadarMod The mod main class instance. + * @param player The player. + */ + public ListsSubcommand(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull EntityPlayer player) { + this.communityRadarMod = communityRadarMod; + this.player = player; + } + + @Override + public void run() { + final StringBuilder listsText = new StringBuilder(); + for (final String namespace : communityRadarMod.getListManager().getNamespaces()) { + communityRadarMod.getListManager().getRadarList(namespace) + .ifPresent(radarList -> listsText.append("§e").append(namespace).append(" §7(§c") + .append(radarList.getRadarListVisibility() == RadarListVisibility.PRIVATE ? Messages.Lists.PRIVATE : Messages.Lists.PUBLIC) + .append("§7)").append(", ")); + } + + if (listsText.length() > 0) { + // players on the list + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Lists.FOUND) + .replace("{lists}", listsText.substring(0, listsText.length() - 2)) + .build().toChatComponentText()); + } else { + // list is empty + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Lists.EMPTY) + .build().toChatComponentText()); + } + } +} diff --git a/src/main/java/io/github/communityradargg/forgemod/command/PlayerSubcommand.java b/src/main/java/io/github/communityradargg/forgemod/command/PlayerSubcommand.java new file mode 100644 index 0000000..e621150 --- /dev/null +++ b/src/main/java/io/github/communityradargg/forgemod/command/PlayerSubcommand.java @@ -0,0 +1,180 @@ +/* + * Copyright 2024 - present CommunityRadarGG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.communityradargg.forgemod.command; + +import io.github.communityradargg.forgemod.CommunityRadarMod; +import io.github.communityradargg.forgemod.radarlistmanager.RadarList; +import io.github.communityradargg.forgemod.radarlistmanager.RadarListManager; +import io.github.communityradargg.forgemod.util.Messages; +import io.github.communityradargg.forgemod.util.RadarMessage; +import io.github.communityradargg.forgemod.util.Utils; +import net.minecraft.entity.player.EntityPlayer; +import org.jetbrains.annotations.NotNull; +import java.util.Locale; +import java.util.Optional; +import java.util.UUID; + +/** + * Holds the logic of the player subcommand. + */ +public class PlayerSubcommand implements Subcommand { + private final CommunityRadarMod communityRadarMod; + private final EntityPlayer player; + private final String[] args; + + /** + * Constructs a {@link PlayerSubcommand}. + * + * @param communityRadarMod The mod main class instance. + * @param player The player. + * @param args The args. + */ + public PlayerSubcommand(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull EntityPlayer player, final @NotNull String[] args) { + this.communityRadarMod = communityRadarMod; + this.player = player; + this.args = args; + } + + @Override + public void run() { + if (args.length < 2) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + switch (args[1].toUpperCase(Locale.ENGLISH)) { + case "ADD": + handlePlayerAddSubcommand(player, args); + break; + case "REMOVE": + handlePlayerRemoveSubcommand(player, args); + break; + default: + new HelpSubcommand(communityRadarMod, player).run(); + break; + } + } + + /** + * Handles the player - add subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handlePlayerAddSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + if (args.length < 5) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + final RadarListManager listManager = communityRadarMod.getListManager(); + final Optional listOptional = listManager.getRadarList(args[2]); + if (!listOptional.isPresent()) { + // list not existing + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_FAILED) + .build().toChatComponentText()); + return; + } + + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.INPUT_PROCESSING) + .build().toChatComponentText()); + Utils.getUUID(communityRadarMod, args[3]).thenAccept(uuidOptional -> { + if (!uuidOptional.isPresent()) { + // player uuid could not be fetched + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(args[3].startsWith("!") ? Messages.Player.NAME_INVALID_BEDROCK : Messages.Player.NAME_INVALID) + .build().toChatComponentText()); + return; + } + + final UUID uuid = uuidOptional.get(); + if (listOptional.get().isInList(uuid)) { + // player already on list + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_IN_LIST) + .build().toChatComponentText()); + return; + } + + final StringBuilder notes = new StringBuilder(); + for (int i = 4; i < args.length; i++) { + notes.append(args[i]).append(" "); + } + + if (!communityRadarMod.getListManager().addRadarListEntry(args[2], uuid, args[3], notes.substring(0, notes.length() - 1))) { + // list is not private + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_FAILED) + .build().toChatComponentText()); + return; + } + + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_SUCCESS) + .build().toChatComponentText()); + Utils.updatePlayerByUuid(communityRadarMod, uuid, listManager.getExistingPrefixes()); + }); + } + + /** + * Handles the player - remove subcommand. + * + * @param player The player, which executed the subcommand. + * @param args The arguments passed to the main command. + */ + private void handlePlayerRemoveSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { + if (args.length != 4) { + // missing arguments + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) + .build().toChatComponentText()); + return; + } + + final RadarListManager listManager = communityRadarMod.getListManager(); + final Optional listOptional = listManager.getRadarList(args[2]); + if (!listOptional.isPresent()) { + // list is not existing + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.REMOVE_FAILED) + .build().toChatComponentText()); + return; + } + + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.INPUT_PROCESSING) + .build().toChatComponentText()); + final RadarList list = listOptional.get(); + Utils.getUUID(communityRadarMod, args[3]).thenAccept(uuidOptional -> { + if (!uuidOptional.isPresent()) { + // player uuid could not be fetched + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(args[3].startsWith("!") ? Messages.Player.NAME_INVALID_BEDROCK : Messages.Player.NAME_INVALID) + .build().toChatComponentText()); + return; + } + + final UUID uuid = uuidOptional.get(); + if (!list.isInList(uuid)) { + // player uuid not on list + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.REMOVE_NOT_IN_LIST) + .build().toChatComponentText()); + return; + } + + list.getPlayerMap().remove(uuid); + Utils.updatePlayerByUuid(communityRadarMod, uuid, listManager.getExistingPrefixes()); + player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.REMOVE_SUCCESS) + .build().toChatComponentText()); + }); + } +} diff --git a/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java b/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java index 22a6acb..698ff72 100644 --- a/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java +++ b/src/main/java/io/github/communityradargg/forgemod/command/RadarCommand.java @@ -16,41 +16,30 @@ package io.github.communityradargg.forgemod.command; import io.github.communityradargg.forgemod.CommunityRadarMod; -import io.github.communityradargg.forgemod.radarlistmanager.RadarList; -import io.github.communityradargg.forgemod.radarlistmanager.RadarListManager; -import io.github.communityradargg.forgemod.radarlistmanager.RadarListVisibility; -import io.github.communityradargg.forgemod.radarlistmanager.RadarListEntry; import io.github.communityradargg.forgemod.util.Messages; import io.github.communityradargg.forgemod.util.RadarMessage; -import io.github.communityradargg.forgemod.util.Utils; -import net.minecraft.client.Minecraft; -import net.minecraft.client.network.NetworkPlayerInfo; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; import net.minecraft.entity.player.EntityPlayer; import org.jetbrains.annotations.NotNull; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; /** * The class containing all logic for the main radar command. */ public class RadarCommand extends CommandBase { - private final CommunityRadarMod mod; + private final CommunityRadarMod communityRadarMod; /** * Constructs a {@link RadarCommand}. * - * @param mod The mod main class instance. + * @param communityRadarMod The mod main class instance. */ - public RadarCommand(final CommunityRadarMod mod) { - this.mod = mod; + public RadarCommand(final CommunityRadarMod communityRadarMod) { + this.communityRadarMod = communityRadarMod; } @Override @@ -87,469 +76,31 @@ public void processCommand(final ICommandSender sender, final String[] args) { } final EntityPlayer player = (EntityPlayer) sender; + Subcommand subcommand = null; if (args.length == 0) { - handleHelpSubcommand(player); - return; - } - - switch (args[0].toUpperCase(Locale.ENGLISH)) { - case "CHECK": - handleCheckSubcommand(player, args); - break; - case "LIST": - handleListSubcommand(player, args); - break; - case "PLAYER": - handlePlayerSubcommand(player, args); - break; - case "LISTS": - handleListsSubcommand(player); - break; - default: - handleHelpSubcommand(player); - break; - } - } - - /** - * Handles the player subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handlePlayerSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length < 2) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - switch (args[1].toUpperCase(Locale.ENGLISH)) { - case "ADD": - handlePlayerAddSubcommand(player, args); - break; - case "REMOVE": - handlePlayerRemoveSubcommand(player, args); - break; - default: - handleHelpSubcommand(player); - break; - } - } - - /** - * Handles the player - add subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handlePlayerAddSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length < 5) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - final RadarListManager listManager = CommunityRadarMod.getListManager(); - final Optional listOptional = listManager.getRadarList(args[2]); - if (!listOptional.isPresent()) { - // list not existing - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_FAILED) - .build().toChatComponentText()); - return; - } - - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.INPUT_PROCESSING) - .build().toChatComponentText()); - Utils.getUUID(mod, args[3]).thenAccept(uuidOptional -> { - if (!uuidOptional.isPresent()) { - // player uuid could not be fetched - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(args[3].startsWith("!") ? Messages.Player.NAME_INVALID_BEDROCK : Messages.Player.NAME_INVALID) - .build().toChatComponentText()); - return; - } - - final UUID uuid = uuidOptional.get(); - if (listOptional.get().isInList(uuid)) { - // player already on list - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_IN_LIST) - .build().toChatComponentText()); - return; - } - - final StringBuilder notes = new StringBuilder(); - for (int i = 4; i < args.length; i++) { - notes.append(args[i]).append(" "); - } - - if (!CommunityRadarMod.getListManager().addRadarListEntry(args[2], uuid, args[3], notes.substring(0, notes.length() - 1))) { - // list is not private - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_FAILED) - .build().toChatComponentText()); - return; - } - - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.ADD_SUCCESS) - .build().toChatComponentText()); - Utils.updatePlayerByUuid(uuid, listManager.getExistingPrefixes()); - }); - } - - /** - * Handles the player - remove subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handlePlayerRemoveSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length != 4) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - final RadarListManager listManager = CommunityRadarMod.getListManager(); - final Optional listOptional = listManager.getRadarList(args[2]); - if (!listOptional.isPresent()) { - // list is not existing - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.REMOVE_FAILED) - .build().toChatComponentText()); - return; - } - - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.INPUT_PROCESSING) - .build().toChatComponentText()); - final RadarList list = listOptional.get(); - Utils.getUUID(mod, args[3]).thenAccept(uuidOptional -> { - if (!uuidOptional.isPresent()) { - // player uuid could not be fetched - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(args[3].startsWith("!") ? Messages.Player.NAME_INVALID_BEDROCK : Messages.Player.NAME_INVALID) - .build().toChatComponentText()); - return; + subcommand = new HelpSubcommand(communityRadarMod, player); + } + + if (subcommand == null) { + switch (args[0].toUpperCase(Locale.ENGLISH)) { + case "CHECK": + subcommand = new CheckSubcommand(communityRadarMod, player, args); + break; + case "LIST": + subcommand = new ListSubcommand(communityRadarMod, player, args); + break; + case "PLAYER": + subcommand = new PlayerSubcommand(communityRadarMod, player, args); + break; + case "LISTS": + subcommand = new ListsSubcommand(communityRadarMod, player); + break; + default: + subcommand = new HelpSubcommand(communityRadarMod, player); + break; } - - final UUID uuid = uuidOptional.get(); - if (!list.isInList(uuid)) { - // player uuid not on list - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.REMOVE_NOT_IN_LIST) - .build().toChatComponentText()); - return; - } - - list.getPlayerMap().remove(uuid); - Utils.updatePlayerByUuid(uuid, listManager.getExistingPrefixes()); - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Player.REMOVE_SUCCESS) - .build().toChatComponentText()); - }); - } - - /** - * Handles the list subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleListSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length < 2) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - switch (args[1].toUpperCase(Locale.ENGLISH)) { - case "ADD": - handleListAddSubcommand(player, args); - break; - case "PREFIX": - handleListPrefixSubcommand(player, args); - break; - case "DELETE": - handleListDeleteSubcommand(player, args); - break; - case "SHOW": - handleListShowSubcommand(player, args); - break; - default: - handleHelpSubcommand(player); - break; - } - } - - /** - * Handles the list - add subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleListAddSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length != 4) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - if (CommunityRadarMod.getListManager().getRadarList(args[2]).isPresent()) { - // list already existing - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.CREATE_FAILED) - .build().toChatComponentText()); - return; - } - - if (!CommunityRadarMod.getListManager().registerPrivateList(args[2], args[3])) { - // list could not be registered - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.CREATE_FAILED) - .build().toChatComponentText()); - return; } - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.CREATE_SUCCESS) - .build().toChatComponentText()); - } - - /** - * Handles the list - delete subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleListDeleteSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length != 3) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - final RadarListManager listManager = CommunityRadarMod.getListManager(); - final Set oldPrefixes = listManager.getExistingPrefixes(); - final Set oldUuids = listManager.getRadarList(args[2]) - .map(radarList -> radarList.getPlayerMap().keySet()) - .orElse(Collections.emptySet()); - - if (!listManager.unregisterList(args[2])) { - // list is not existing, list is not private, file cannot be deleted - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.DELETE_FAILED) - .build().toChatComponentText()); - return; - } - - oldUuids.forEach(uuid -> Utils.updatePlayerByUuid(uuid, oldPrefixes)); - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.DELETE_SUCCESS) - .build().toChatComponentText()); - } - - /** - * Handles the list - show subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleListShowSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length != 3) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - final Optional listOptional = CommunityRadarMod.getListManager().getRadarList(args[2]); - if (!listOptional.isPresent()) { - // list is not existing - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.SHOW_FAILED) - .build().toChatComponentText()); - return; - } - - final RadarList list = listOptional.get(); - if (list.getPlayerMap().isEmpty()) { - // list is empty - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.SHOW_EMPTY) - .build().toChatComponentText()); - return; - } - - final StringBuilder players = new StringBuilder(); - list.getPlayerMap().values().forEach(value -> players.append(value.name()).append(", ")); - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.SHOW_SUCCESS) - .replace("{list}", list.getNamespace()) - .replaceWithColorCodes("{prefix}", listOptional.get().getPrefix()) - .replace("{players}", players.substring(0, players.length() - 2)) - .build().toChatComponentText()); - } - - /** - * Handles the list - prefix subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleListPrefixSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length != 4) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - final RadarListManager listManager = CommunityRadarMod.getListManager(); - final Optional listOptional = listManager.getRadarList(args[2]); - if (!listOptional.isPresent()) { - // list is not existing - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.PREFIX_FAILED) - .build().toChatComponentText()); - return; - } - - final RadarList list = listOptional.get(); - final Set oldPrefixes = listManager.getExistingPrefixes(); - list.setPrefix(args[3]); - list.saveList(); - list.getPlayerMap().keySet().forEach(uuid -> Utils.updatePlayerByUuid(uuid, oldPrefixes)); - - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.List.PREFIX_SUCCESS) - .replaceWithColorCodes("{prefix}", args[3]) - .build().toChatComponentText()); - } - - /** - * Handles the check subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleCheckSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - if (args.length != 2) { - // missing arguments - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.MISSING_ARGS) - .build().toChatComponentText()); - return; - } - - if (args[1].equalsIgnoreCase("*")) { - // check all argument - handleCheckAllSubcommand(player); - return; - } - handleCheckPlayerSubcommand(player, args); - } - - /** - * Handles the check - player subcommand. - * - * @param player The player, which executed the subcommand. - * @param args The arguments passed to the main command. - */ - private void handleCheckPlayerSubcommand(final @NotNull EntityPlayer player, final @NotNull String[] args) { - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.INPUT_PROCESSING) - .build().toChatComponentText()); - Utils.getUUID(mod, args[1]).thenAccept(checkPlayerOptional -> { - if (!checkPlayerOptional.isPresent()) { - // player uuid could not be fetched - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.FAILED) - .build().toChatComponentText()); - return; - } - - final Optional entryOptional = CommunityRadarMod.getListManager().getRadarListEntry(checkPlayerOptional.get()); - if (!entryOptional.isPresent()) { - // player uuid is on no list - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.FAILED) - .build().toChatComponentText()); - return; - } - - final RadarListEntry entry = entryOptional.get(); - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.FOUND + "\n" + Messages.Check.CHECK_ENTRY) - .replaceWithColorCodes("{prefix}", CommunityRadarMod.getListManager().getPrefix(entry.uuid())) - .replace("{name}", entry.name()) - .replace("{cause}", entry.cause()) - .replace("{entryCreationDate}", Utils.formatDateTime(entry.entryCreationDate())) - .replace("{entryUpdateDate}", Utils.formatDateTime(entry.entryUpdateDate())) - .build().toChatComponentText()); - }); - } - - /** - * Handles the check - all subcommand. - * - * @param player The player, which executed the subcommand. - */ - private void handleCheckAllSubcommand(final @NotNull EntityPlayer player) { - boolean anyPlayerFound = false; - for (final NetworkPlayerInfo networkPlayer : Minecraft.getMinecraft().getNetHandler().getPlayerInfoMap()) { - if (networkPlayer.getGameProfile().getId() == null) { - continue; - } - - final Optional listEntryOptional = CommunityRadarMod.getListManager() - .getRadarListEntry(networkPlayer.getGameProfile().getId()); - if (!listEntryOptional.isPresent()) { - // player uuid is on no list - continue; - } - - if (!anyPlayerFound) { - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.EVERYONE) - .build().toChatComponentText()); - anyPlayerFound = true; - } - - final RadarListEntry entry = listEntryOptional.get(); - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.CHECK_ENTRY) - .replaceWithColorCodes("{prefix}", CommunityRadarMod.getListManager().getPrefix(entry.uuid())) - .replace("{name}", entry.name()) - .replace("{cause}", entry.cause()) - .replace("{entryCreationDate}", Utils.formatDateTime(entry.entryCreationDate())) - .replace("{entryUpdateDate}", Utils.formatDateTime(entry.entryUpdateDate())) - .build().toChatComponentText()); - } - - if (!anyPlayerFound) { - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Check.NOT_FOUND) - .build().toChatComponentText()); - } - } - - /** - * Handles the help subcommand. - * - * @param player The player, which executed the subcommand. - */ - private void handleHelpSubcommand(final @NotNull EntityPlayer player) { - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.HELP) - .replace("{code_version}", mod.getVersion()) - .excludePrefix() - .build().toChatComponentText()); - } - - /** - * Handles the lists subcommand. - * - * @param player The player, which executed the subcommand. - */ - private void handleListsSubcommand(final @NotNull EntityPlayer player) { - final StringBuilder listsText = new StringBuilder(); - for (final String namespace : CommunityRadarMod.getListManager().getNamespaces()) { - CommunityRadarMod.getListManager().getRadarList(namespace) - .ifPresent(radarList -> listsText.append("§e").append(namespace).append(" §7(§c") - .append(radarList.getRadarListVisibility() == RadarListVisibility.PRIVATE ? Messages.Lists.PRIVATE : Messages.Lists.PUBLIC) - .append("§7)").append(", ")); - } - - if (listsText.length() > 0) { - // players on the list - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Lists.FOUND) - .replace("{lists}", listsText.substring(0, listsText.length() - 2)) - .build().toChatComponentText()); - } else { - // list is empty - player.addChatComponentMessage(new RadarMessage.RadarMessageBuilder(Messages.Lists.EMPTY) - .build().toChatComponentText()); - } + subcommand.run(); } } diff --git a/src/main/java/io/github/communityradargg/forgemod/command/Subcommand.java b/src/main/java/io/github/communityradargg/forgemod/command/Subcommand.java new file mode 100644 index 0000000..87f46b6 --- /dev/null +++ b/src/main/java/io/github/communityradargg/forgemod/command/Subcommand.java @@ -0,0 +1,26 @@ +/* + * Copyright 2024 - present CommunityRadarGG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.communityradargg.forgemod.command; + +/** + * Represents a subcommand of a command. + */ +public interface Subcommand { + /** + * Runs the logic defined in the subcommand. + */ + void run(); +} diff --git a/src/main/java/io/github/communityradargg/forgemod/util/Utils.java b/src/main/java/io/github/communityradargg/forgemod/util/Utils.java index f1e5b4d..bbb4b05 100644 --- a/src/main/java/io/github/communityradargg/forgemod/util/Utils.java +++ b/src/main/java/io/github/communityradargg/forgemod/util/Utils.java @@ -197,22 +197,24 @@ public static boolean isGrieferGamesHostName(final @NotNull String hostName) { /** * Updates a player display name and name tag by its uuid. * + * @param communityRadarMod The mod main class instance. * @param uuid The uuid to update the corresponding player. */ - public static void updatePlayerByUuid(final @NotNull UUID uuid, final @NotNull Set oldPrefixes) { - getEntityPlayerByUuid(uuid).ifPresent(player -> updatePlayerNameTag(player, oldPrefixes)); - getNetworkPlayerInfoByUuid(uuid).ifPresent(networkPlayerInfo -> updatePlayerPrefix(networkPlayerInfo, oldPrefixes)); + public static void updatePlayerByUuid(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull UUID uuid, final @NotNull Set oldPrefixes) { + getEntityPlayerByUuid(uuid).ifPresent(player -> updatePlayerNameTag(communityRadarMod, player, oldPrefixes)); + getNetworkPlayerInfoByUuid(uuid).ifPresent(networkPlayerInfo -> updatePlayerPrefix(communityRadarMod, networkPlayerInfo, oldPrefixes)); } /** * Handles updating the name tag of a player entity. * + * @param communityRadarMod The mod main class instance. * @param player The player entity to update the name tag. * @param oldPrefixes The old prefixes that need to be removed before adding the new one. */ - public static void updatePlayerNameTag(final @NotNull EntityPlayer player, final @NotNull Set oldPrefixes) { + public static void updatePlayerNameTag(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull EntityPlayer player, final @NotNull Set oldPrefixes) { player.getPrefixes().removeIf(prefix -> oldPrefixes.stream().anyMatch(oldPrefix -> new ChatComponentText(oldPrefix.replace("&", "§") + " ").getUnformattedText().equals(prefix.getUnformattedText()))); - final String addonPrefix = CommunityRadarMod.getListManager() + final String addonPrefix = communityRadarMod.getListManager() .getPrefix(player.getGameProfile().getId()) .replace("&", "§"); @@ -224,20 +226,22 @@ public static void updatePlayerNameTag(final @NotNull EntityPlayer player, final /** * Handles updating the player prefixes in the display name. * + * @param communityRadarMod The mod main class instance. * @param oldPrefixes The old prefixes that need to be removed before adding the new one. */ - public static void updatePrefixes(final @NotNull Set oldPrefixes) { + public static void updatePrefixes(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull Set oldPrefixes) { Minecraft.getMinecraft().thePlayer.sendQueue.getPlayerInfoMap() - .forEach(player -> updatePlayerPrefix(player, oldPrefixes)); + .forEach(player -> updatePlayerPrefix(communityRadarMod, player, oldPrefixes)); } /** * Handles updating the player prefix in the display name of a single player. * + * @param communityRadarMod The mod main class instance. * @param player The player to update. * @param oldPrefixes The old prefixes that need to be removed before adding the new one. */ - private static void updatePlayerPrefix(final @NotNull NetworkPlayerInfo player, final @NotNull Set oldPrefixes) { + private static void updatePlayerPrefix(final @NotNull CommunityRadarMod communityRadarMod, final @NotNull NetworkPlayerInfo player, final @NotNull Set oldPrefixes) { if (player.getGameProfile() == null || player.getGameProfile().getId() == null || player.getDisplayName() == null) { return; } @@ -251,7 +255,7 @@ private static void updatePlayerPrefix(final @NotNull NetworkPlayerInfo player, newDisplayName = displayName.getSiblings().get(displayName.getSiblings().size() - 1); } - final String addonPrefix = CommunityRadarMod.getListManager() + final String addonPrefix = communityRadarMod.getListManager() .getPrefix(player.getGameProfile().getId()) .replace("&", "§"); if (!addonPrefix.isEmpty()) { From fa3d422e45b384145076a88de27d3de697840d9a Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:15:55 +0200 Subject: [PATCH 06/12] Make the method CommunityRadarMod#getListManager non-static --- .../io/github/communityradargg/forgemod/CommunityRadarMod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java index a9cb192..c879975 100644 --- a/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java +++ b/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java @@ -109,7 +109,7 @@ private void registerPublicLists() { * * @return Returns the radar list manager instance. */ - public static @NotNull RadarListManager getListManager() { + public @NotNull RadarListManager getListManager() { return listManager; } From 46ff064ab01d4a208cb2df737ab47ea162932054 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:21:15 +0200 Subject: [PATCH 07/12] Update GitHub actions build.yml workflow --- .github/workflows/build.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f930df2..ce58ccc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,7 +3,10 @@ name: Build on: workflow_dispatch: push: - branches: ["main"] + branches: + - "**" + tags-ignore: + - "**" jobs: build: From a87d349a064fadef41e65b6be3fda2d9df325ea9 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:21:53 +0200 Subject: [PATCH 08/12] Do not clean build in build.yml workflow --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ce58ccc..6a79276 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,7 @@ jobs: - name: Grant execute permission for gradlew run: chmod +x gradlew - name: Clean Build - run: ./gradlew clean build + run: ./gradlew build - name: Extract Version from Gradle run: | version=$(./gradlew properties -q | grep "^version:" | awk '{print $2}') From 3ab43e5037fff93a257a47a903f3e22b835e5375 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:23:14 +0200 Subject: [PATCH 09/12] Reorder some build.yml workflow tasks --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6a79276..eaf1d2c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,13 +15,13 @@ jobs: steps: - name: Checkout Repository uses: actions/checkout@v5 - - name: Validate Gradle Wrapper - uses: gradle/actions/wrapper-validation@v5 - name: Setup Java uses: actions/setup-java@v5 with: distribution: 'zulu' java-version: 21 + - name: Validate Gradle Wrapper + uses: gradle/actions/wrapper-validation@v5 - name: Setup Gradle uses: gradle/actions/setup-gradle@v5 - name: Grant execute permission for gradlew From 0522331824fb004af1b832a8058df9ba7e45d7e9 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:24:53 +0200 Subject: [PATCH 10/12] Change build.yml identation --- .github/workflows/build.yml | 70 ++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index eaf1d2c..98fcae6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,40 +1,40 @@ name: Build on: - workflow_dispatch: - push: - branches: - - "**" - tags-ignore: - - "**" + workflow_dispatch: + push: + branches: + - "**" + tags-ignore: + - "**" jobs: - build: - if: github.repository_owner == 'CommunityRadarGG' - runs-on: ubuntu-latest - steps: - - name: Checkout Repository - uses: actions/checkout@v5 - - name: Setup Java - uses: actions/setup-java@v5 - with: - distribution: 'zulu' - java-version: 21 - - name: Validate Gradle Wrapper - uses: gradle/actions/wrapper-validation@v5 - - name: Setup Gradle - uses: gradle/actions/setup-gradle@v5 - - name: Grant execute permission for gradlew - run: chmod +x gradlew - - name: Clean Build - run: ./gradlew build - - name: Extract Version from Gradle - run: | - version=$(./gradlew properties -q | grep "^version:" | awk '{print $2}') - echo "VERSION=$version" >> $GITHUB_ENV - echo $version - - name: Upload Artifact - uses: actions/upload-artifact@v4 - with: - name: communityradar-${{ env.VERSION }} - path: build/libs/*-${{ env.VERSION }}.jar + build: + if: github.repository_owner == 'CommunityRadarGG' + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v5 + - name: Setup Java + uses: actions/setup-java@v5 + with: + distribution: 'zulu' + java-version: 21 + - name: Validate Gradle Wrapper + uses: gradle/actions/wrapper-validation@v5 + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v5 + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: Clean Build + run: ./gradlew build + - name: Extract Version from Gradle + run: | + version=$(./gradlew properties -q | grep "^version:" | awk '{print $2}') + echo "VERSION=$version" >> $GITHUB_ENV + echo $version + - name: Upload Artifact + uses: actions/upload-artifact@v4 + with: + name: communityradar-${{ env.VERSION }} + path: build/libs/*-${{ env.VERSION }}.jar From af78aed279e897bb9de0ba4434e541be40461a9f Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:26:32 +0200 Subject: [PATCH 11/12] Only upload artifacts on release commits --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 98fcae6..09d1792 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,6 +34,7 @@ jobs: echo "VERSION=$version" >> $GITHUB_ENV echo $version - name: Upload Artifact + if: startsWith(github.event.head_commit.message || '', 'Release') uses: actions/upload-artifact@v4 with: name: communityradar-${{ env.VERSION }} From 08bd0453e1828c71d1092ebc5f6a66352ee76fc6 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:27:58 +0200 Subject: [PATCH 12/12] Fix gradlew build workflow step name build.yml after last changed --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 09d1792..c7b0c40 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,7 +26,7 @@ jobs: uses: gradle/actions/setup-gradle@v5 - name: Grant execute permission for gradlew run: chmod +x gradlew - - name: Clean Build + - name: Build run: ./gradlew build - name: Extract Version from Gradle run: |