Skip to content

Commit

Permalink
Fixed prompt, added arg aliases to /hc
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidemLIVE committed Jun 15, 2024
1 parent 166a0c8 commit e655dd5
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 114 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.greenwoodmc</groupId>
<artifactId>HelpCommand</artifactId>
<version>2.8.3</version>
<version>2.8.4</version>
<packaging>jar</packaging>

<name>HelpCommand</name>
Expand Down
78 changes: 76 additions & 2 deletions src/main/java/net/greenwoodmc/helpcommand/commands/hcCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class hcCommand implements CommandExecutor {
FileConfiguration config = JavaPlugin.getPlugin(HelpCommand.class).getConfig();
Expand All @@ -30,7 +38,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a
if (cmd.getName().equalsIgnoreCase("hc")) {
if (args.length >= 1) {
arg1 = args[0];
if (arg1.equalsIgnoreCase("reload")) {
if (arg1.equalsIgnoreCase("reload") || arg1.equalsIgnoreCase("rl")) {
ver = config.getString("reload");
plug.reloadConfig();
try {
Expand All @@ -47,12 +55,60 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a
}
}

if (arg1.equalsIgnoreCase("version")) {
if (arg1.equalsIgnoreCase("version") || arg1.equalsIgnoreCase("ver")) {
ver = plug.getDescription().getVersion();
if (isPlayer) {
sender.sendMessage("HelpCommand Version: " + ver);
try {
URL url = new URL("https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=102926");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

if (connection.getResponseCode() == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
response.append(line);
}

String spigotVersion = extractVersionFromResponse(response.toString());

if (!spigotVersion.equals(config.getString("version"))) {
sender.sendMessage(TextUtil.color("&dA new version of HelpCommand is available! download &eV" + spigotVersion + "&d here: &ahttps://www.spigotmc.org/resources/102926/"));
}
} else {
plug.getLogger().warning("Failed to retrieve plugin information from SpigotMC. HTTP response code: " + connection.getResponseCode());
}
} catch (IOException e) {
plug.getLogger().warning("Error checking Spigot version: " + e.getMessage());
}
} else {
plug.getLogger().info("HelpCommand Version: " + ver);
try {
URL url = new URL("https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=102926");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

if (connection.getResponseCode() == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
response.append(line);
}

String spigotVersion = extractVersionFromResponse(response.toString());

if (!spigotVersion.equals(config.getString("version"))) {
plug.getLogger().info(TextUtil.color("A new version of HelpCommand is available! download V" + spigotVersion + "here: https://www.spigotmc.org/resources/102926/"));
}
} else {
plug.getLogger().warning("Failed to retrieve plugin information from SpigotMC. HTTP response code: " + connection.getResponseCode());
}
} catch (IOException e) {
plug.getLogger().warning("Error checking Spigot version: " + e.getMessage());
}
}
}

Expand All @@ -77,4 +133,22 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a

return false;
}

private String extractVersionFromResponse(String jsonResponse) {
Plugin plugin = JavaPlugin.getPlugin(HelpCommand.class);

try {
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonResponse);
Object versionObject = jsonObject.get("current_version");
if (versionObject != null) {
return versionObject.toString();
} else {
plugin.getLogger().warning("Failed to extract version from JSON response. 'current_version' field not found.");
}
} catch (ParseException e) {
plugin.getLogger().warning("Error parsing JSON response: " + e.getMessage());
}
return "";
}
}
169 changes: 63 additions & 106 deletions src/main/java/net/greenwoodmc/helpcommand/commands/help.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,120 +23,77 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a
if (!(sender instanceof Player)) {
sender.sendMessage(config.getString("playersOnly"));
return true;
} else {
Player player = (Player) sender;
String ver;
String ver2;
String pageText = config.getString("pagePrompt.page");
Integer currPageNumber;
Integer nextPageNumber;
Integer previousPageNumber;
String colour1 = config.getString("pagePrompt.pageNumberColour");
String colour2 = config.getString("pagePrompt.pageTextColour");
ChatColor COLOUR1 = ChatColor.valueOf(colour1);
ChatColor COLOUR2 = ChatColor.valueOf(colour2);
List<Integer> enabledPages = config.getIntegerList("pagesEnabled");
Integer lastPage = enabledPages.get(enabledPages.size() - 1);
JavaPlugin plug = JavaPlugin.getPlugin(HelpCommand.class);
boolean papiInstalled = HelpCommand.isPapiInstalled(plug);
Boolean pagePromptEnabled = config.getBoolean("pagePrompt.enabled");
if (cmd.getName().equalsIgnoreCase("help")) {
if (config.getBoolean("helpcmd")) {
if (args.length >= 1 && !args[0].equals("1")) {
// For if there's 2 args
currPageNumber = Integer.valueOf(args[0]);
nextPageNumber = currPageNumber + 1;
previousPageNumber = currPageNumber - 1;
if (currPageNumber > 0 && currPageNumber < lastPage) {
// If between 1 and the last page number - 1
if (enabledPages.contains(currPageNumber)) {
ver = (String) config.getStringList("help." + currPageNumber).stream().collect(Collectors.joining("\n"));
if (papiInstalled) {
ver2 = PlaceholderAPI.setPlaceholders(player, ver);
} else {
ver2 = ver;
}
player.sendMessage(TextUtil.color(ver2));
if (pagePromptEnabled) {
if (enabledPages.contains(nextPageNumber)) {
String previousPage = "[<< " + previousPageNumber + "]";
String nextPage = "[" + nextPageNumber + " >>]";
String currentPage = pageText + " " + currPageNumber;
ComponentBuilder nextPageMSG = new ComponentBuilder(previousPage)
.color(COLOUR1)
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help " + previousPageNumber))
.append(" " + currentPage + " ")
.color(COLOUR2)
.append(nextPage)
.color(COLOUR1)
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help " + nextPageNumber));
player.spigot().sendMessage(nextPageMSG.create());
}
}
} else {
player.sendMessage(TextUtil.color(config.getString("pageNA")));
}
} else if (currPageNumber.equals(lastPage)) {
// If the last page number
if (enabledPages.contains(lastPage)) {
ver = (String) config.getStringList("help." + lastPage).stream().collect(Collectors.joining("\n"));
if (papiInstalled) {
ver2 = PlaceholderAPI.setPlaceholders(player, ver);
} else {
ver2 = ver;
}
player.sendMessage(TextUtil.color(ver2));
if (pagePromptEnabled) {
ComponentBuilder nextPageMSG = new ComponentBuilder("[<< " + previousPageNumber + "] ")
.color(COLOUR1)
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help " + previousPageNumber))
.append(pageText + " " + currPageNumber)
.color(COLOUR2);
player.spigot().sendMessage(nextPageMSG.create());
}
} else {
player.sendMessage(TextUtil.color(config.getString("pageNA")));
}
} else if (currPageNumber < 1 || currPageNumber > lastPage) {
// If smaller than 1 or larger than the last page number
player.sendMessage(TextUtil.color(config.getString("pageNA")));
}
} else {
// If no page number specified, send page 1
if (args.length > 0 && (enabledPages.contains(2) || args[0].equals("1"))) {
// If page 2 exists
ver = (String) config.getStringList("help.1").stream().collect(Collectors.joining("\n"));
if (papiInstalled) {
ver2 = PlaceholderAPI.setPlaceholders(player, ver);
} else {
ver2 = ver;
}
player.sendMessage(TextUtil.color(ver2));
if (pagePromptEnabled) {
ComponentBuilder nextPageMSG = new ComponentBuilder(pageText + " 1")
.color(COLOUR2)
.append(" [2 >>]")
}

Player player = (Player) sender;
String ver;
String ver2;
String pageText = config.getString("pagePrompt.page");
int currPageNumber;
String colour1 = config.getString("pagePrompt.pageNumberColour");
String colour2 = config.getString("pagePrompt.pageTextColour");
ChatColor COLOUR1 = ChatColor.valueOf(colour1);
ChatColor COLOUR2 = ChatColor.valueOf(colour2);
List<Integer> enabledPages = config.getIntegerList("pagesEnabled");
int lastPage = enabledPages.get(enabledPages.size() - 1);
JavaPlugin plug = JavaPlugin.getPlugin(HelpCommand.class);
boolean papiInstalled = HelpCommand.isPapiInstalled(plug);
boolean pagePromptEnabled = config.getBoolean("pagePrompt.enabled");

if (cmd.getName().equalsIgnoreCase("help")) {
if (config.getBoolean("helpcmd")) {
if (args.length > 0) {
try {
currPageNumber = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
player.sendMessage(TextUtil.color(config.getString("pageNA")));
return true;
}
} else {
currPageNumber = 1; // Default to page 1 if no arguments provided
}

if (currPageNumber >= 1 && currPageNumber <= lastPage) {
if (enabledPages.contains(currPageNumber)) {
ver = config.getStringList("help." + currPageNumber).stream().collect(Collectors.joining("\n"));
ver2 = papiInstalled ? PlaceholderAPI.setPlaceholders(player, ver) : ver;
player.sendMessage(TextUtil.color(ver2));

if (pagePromptEnabled && enabledPages.size() > 1) { // Only show page prompt if more than one page
ComponentBuilder pagePromptBuilder = new ComponentBuilder();

if (currPageNumber > 1) { // Show previous page arrow if not on the first page
int previousPageNumber = currPageNumber - 1;
pagePromptBuilder.append("[<< " + previousPageNumber + "] ")
.color(COLOUR1)
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help 2"));
player.spigot().sendMessage(nextPageMSG.create());
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help " + previousPageNumber));
}
} else {
// If page 2 doesn't exist
ver = (String) config.getStringList("help.1").stream().collect(Collectors.joining("\n"));
if (papiInstalled) {
ver2 = PlaceholderAPI.setPlaceholders(player, ver);
} else {
ver2 = ver;

// Add the current page number without a click event
pagePromptBuilder.append(pageText + " " + currPageNumber)
.color(COLOUR2);

if (currPageNumber < lastPage) { // Show next page arrow if not on the last page
int nextPageNumber = currPageNumber + 1;
pagePromptBuilder.append(" [" + nextPageNumber + " >>]")
.color(COLOUR1)
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/help " + nextPageNumber));
}
player.sendMessage(TextUtil.color(ver2));

player.spigot().sendMessage(pagePromptBuilder.create());
}
} else {
player.sendMessage(TextUtil.color(config.getString("pageNA")));
}
} else {
currPageNumber = Integer.valueOf(config.getString("disabled"));
player.sendMessage(TextUtil.color(String.valueOf(currPageNumber)));
player.sendMessage(TextUtil.color(config.getString("pageNA")));
}
} else {
int disabledPageNumber = Integer.parseInt(config.getString("disabled"));
player.sendMessage(TextUtil.color(String.valueOf(disabledPageNumber)));
}
}
return false;
return true;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Main Help Command
######################################

version: "2.8.3" # DO NOT CHANGE THIS VALUE
version: "2.8.4" # DO NOT CHANGE THIS VALUE
updateCheck: true # recommended to keep this value as true

help:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: HelpCommand
version: '2.8.3-PublicRelease'
version: '2.8.4-PublicRelease'
main: net.greenwoodmc.helpcommand.HelpCommand
api-version: 1.18
authors: [ VoidemLIVE ]
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion target/classes/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Main Help Command
######################################

version: "2.8.3" # DO NOT CHANGE THIS VALUE
version: "2.8.4" # DO NOT CHANGE THIS VALUE
updateCheck: true # recommended to keep this value as true

help:
Expand Down
Binary file not shown.
Binary file modified target/classes/net/greenwoodmc/helpcommand/commands/help.class
Binary file not shown.
2 changes: 1 addition & 1 deletion target/classes/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: HelpCommand
version: '2.8.3-PublicRelease'
version: '2.8.4-PublicRelease'
main: net.greenwoodmc.helpcommand.HelpCommand
api-version: 1.18
authors: [ VoidemLIVE ]
Expand Down
2 changes: 1 addition & 1 deletion target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
artifactId=HelpCommand
groupId=net.greenwoodmc
version=2.8.3
version=2.8.4
Binary file not shown.

0 comments on commit e655dd5

Please sign in to comment.