Skip to content

Commit bac04cd

Browse files
committed
deep clean the main, separate github checker
1 parent 0ed83f8 commit bac04cd

File tree

8 files changed

+103
-149
lines changed

8 files changed

+103
-149
lines changed

src/main/java/fr/autoplugins/Commands/CommandRunnerCommand.java renamed to src/main/java/fr/autoplugins/CommandExecutors/CommandExecutorAcmd.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fr.autoplugins.Commands;
1+
package fr.autoplugins.CommandExecutors;
22

33
import fr.autoplugins.Main;
44

@@ -10,11 +10,11 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

13-
public class CommandRunnerCommand implements CommandExecutor, TabCompleter {
13+
public class CommandExecutorAcmd implements CommandExecutor, TabCompleter {
1414

1515
private Main plugin;
1616

17-
public CommandRunnerCommand(Main plg) {
17+
public CommandExecutorAcmd(Main plg) {
1818
plugin = plg;
1919
}
2020

src/main/java/fr/autoplugins/Commands/CommandRunnerEditor.java renamed to src/main/java/fr/autoplugins/CommandExecutors/CommandExecutorAcmdEditor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fr.autoplugins.Commands;
1+
package fr.autoplugins.CommandExecutors;
22

33
import fr.autoplugins.Main;
44
import org.bukkit.command.Command;
@@ -10,11 +10,11 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

13-
public class CommandRunnerEditor implements CommandExecutor, TabCompleter {
13+
public class CommandExecutorAcmdEditor implements CommandExecutor, TabCompleter {
1414

1515
Main plugin;
1616

17-
public CommandRunnerEditor(Main plg) {
17+
public CommandExecutorAcmdEditor(Main plg) {
1818
plugin = plg;
1919
}
2020

src/main/java/fr/autoplugins/Commands/CommandRunnerHelp.java renamed to src/main/java/fr/autoplugins/CommandExecutors/CommandExecutorHelp.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
package fr.autoplugins.Commands;
1+
package fr.autoplugins.CommandExecutors;
22

33
import fr.autoplugins.Main;
44
import org.bukkit.ChatColor;
55
import org.bukkit.command.Command;
66
import org.bukkit.command.CommandExecutor;
77
import org.bukkit.command.CommandSender;
88
import org.bukkit.command.TabCompleter;
9+
import fr.autoplugins.Util.PluginVersionChecker;
910
import org.bukkit.entity.Player;
1011

1112
import java.util.ArrayList;
1213
import java.util.List;
1314

14-
public class CommandRunnerHelp implements CommandExecutor, TabCompleter {
15+
public class CommandExecutorHelp implements CommandExecutor, TabCompleter {
1516

1617

1718
Main plugin;
1819

19-
public CommandRunnerHelp(Main plg) {
20+
public CommandExecutorHelp(Main plg) {
2021

2122
plugin = plg;
2223
}
@@ -41,7 +42,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
4142

4243
String message =
4344
"§e-----------§aAutoCommands-Help§e--------------\n"
44-
+ "§6" + ChatColor.translateAlternateColorCodes('&', plugin.VerifyPluginVersion()) + "\n"
45+
+ "§6" + ChatColor.translateAlternateColorCodes('&', PluginVersionChecker.VerifyPluginVersion(plugin)) + "\n"
4546
+ "§6ACMD Version : " + plugin.getDescription().getVersion() + "\n"
4647
+ "§6 /acmd <params> §e-> §7The main command of the plugin\n"
4748
+ "§6 /acmd list [page] §e-> §7Displays the list of the AutoCommands\n"

src/main/java/fr/autoplugins/Commands/CommandRunnerReload.java renamed to src/main/java/fr/autoplugins/CommandExecutors/CommandExecutorReload.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
package fr.autoplugins.Commands;
1+
package fr.autoplugins.CommandExecutors;
22

33
import fr.autoplugins.Main;
4-
import org.bukkit.Bukkit;
54
import org.bukkit.command.Command;
65
import org.bukkit.command.CommandExecutor;
76
import org.bukkit.command.CommandSender;
87

98

10-
public class CommandRunnerReload implements CommandExecutor {
9+
public class CommandExecutorReload implements CommandExecutor {
1110

1211
Main plugin;
1312

14-
public CommandRunnerReload(Main plg) {
13+
public CommandExecutorReload(Main plg) {
1514
plugin = plg;
1615
}
1716

src/main/java/fr/autoplugins/Commands/CommandRunnerTime.java renamed to src/main/java/fr/autoplugins/CommandExecutors/CommandExecutorTime.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fr.autoplugins.Commands;
1+
package fr.autoplugins.CommandExecutors;
22

33
import fr.autoplugins.Main;
44
import org.bukkit.Bukkit;
@@ -8,14 +8,12 @@
88

99
import java.time.ZoneId;
1010
import java.util.Date;
11-
import java.time.LocalDateTime;
1211

13-
public class CommandRunnerTime implements CommandExecutor {
12+
public class CommandExecutorTime implements CommandExecutor {
1413

1514
Main plugin;
1615

17-
public CommandRunnerTime(Main plg) {
18-
16+
public CommandExecutorTime(Main plg) {
1917
plugin = plg;
2018
}
2119

src/main/java/fr/autoplugins/Commands/CommandRunnerConf.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/main/java/fr/autoplugins/Main.java

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
package fr.autoplugins;
2-
3-
import com.google.gson.JsonArray;
4-
import com.google.gson.JsonElement;
5-
import com.google.gson.JsonObject;
6-
import fr.autoplugins.Commands.*;
2+
import fr.autoplugins.CommandExecutors.*;
73
import fr.autoplugins.Metrics.Metrics;
84
import fr.autoplugins.Util.Utilities;
5+
import fr.autoplugins.Util.PluginVersionChecker;
96
import org.bukkit.ChatColor;
10-
117
import org.bukkit.plugin.java.JavaPlugin;
128
import org.bukkit.Bukkit;
139

14-
import java.net.HttpURLConnection;
15-
import java.net.URL;
16-
import java.io.BufferedReader;
17-
import java.io.InputStreamReader;
18-
import java.io.IOException;
19-
import java.util.logging.Level;
20-
21-
import com.google.gson.JsonParser;
22-
2310
import java.util.Objects;
2411

2512
public final class Main extends JavaPlugin {
@@ -30,26 +17,25 @@ public final class Main extends JavaPlugin {
3017
"&e&9/--\\&6\\__| ||__/&e| &8running on bukkit - paper",
3118
""};
3219

20+
boolean papiPresent = false;
21+
22+
private Utilities m_ut;
23+
24+
3325
private void printLogo() {
3426
for (String s : Logo)//print the logo
3527
Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', s));
3628
}
3729

38-
boolean papiPresent = false;
39-
4030
public boolean isPapiPresent() {
4131
return papiPresent;
4232
}
4333

44-
4534
public void addBstatsMetrics() {
4635
int pluginId = 21737;
4736
Metrics metrics = new Metrics(this, pluginId);
4837
}
4938

50-
51-
private Utilities m_ut;
52-
5339
public Utilities getUt() {
5440
return m_ut;
5541
}
@@ -58,69 +44,12 @@ public void init() {
5844
m_ut = new Utilities(this);
5945
}
6046

61-
62-
public String callGithubForTag() {
63-
StringBuilder response = new StringBuilder();
64-
try {
65-
// Make HTTP GET request
66-
URL url = new URL("https://api.github.com/repos/AutoPluginsDev/AutoCommands/tags");
67-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
68-
connection.setRequestMethod("GET");
69-
connection.setRequestProperty("Accept", "application/json");
70-
71-
// Check response code
72-
if (connection.getResponseCode() != 200) {
73-
throw new IOException("Failed to get response from GitHub API");
74-
}
75-
76-
// Read response
77-
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
78-
79-
String line;
80-
while ((line = reader.readLine()) != null) {
81-
response.append(line);
82-
}
83-
reader.close();
84-
} catch (IOException e) {
85-
getLogger().log(Level.WARNING, "Failed to check for a new version on spigot.", e);
86-
}
87-
return response.toString();
88-
}
89-
90-
public String VerifyPluginVersion() {
91-
String spigotResponse = "";
92-
String currentVersion = this.getDescription().getVersion();
93-
94-
String response = callGithubForTag();
95-
96-
// Parse JSON response
97-
JsonParser parser = new JsonParser();
98-
JsonElement jsonElement = parser.parse(response);
99-
if (jsonElement.isJsonArray()) {
100-
JsonArray jsonArray = jsonElement.getAsJsonArray();
101-
if (jsonArray.size() > 0) {
102-
JsonObject latestTag = jsonArray.get(0).getAsJsonObject();
103-
spigotResponse = latestTag.get("name").getAsString();
104-
}
105-
}
106-
107-
if (spigotResponse.equals("")) {
108-
return "&cFailed to check for a new version on spigot.";
109-
}
110-
111-
if (spigotResponse.equals(currentVersion)) {
112-
return "&aYou are running the latest version of AutoCommands " + currentVersion + " !";
113-
}
114-
115-
return "&eAutoCommands &a&l" + spigotResponse + " &eis available! &chttps://www.spigotmc.org/resources/acmd-%E2%8F%B0-%E2%8F%B3-autocommands-1-13-1-20-4.100090";
116-
}
117-
11847
@Override
11948
public void onEnable() {
12049
printLogo();
12150
// verify if the plugin is up to date and send a message to the admins
12251

123-
String broadcastMessage = ChatColor.translateAlternateColorCodes('&', getConfig().getString("Prefix") + VerifyPluginVersion());
52+
String broadcastMessage = ChatColor.translateAlternateColorCodes('&', getConfig().getString("Prefix") + PluginVersionChecker.VerifyPluginVersion(this));
12453
Bukkit.broadcast(broadcastMessage, "bukkit.broadcast.admin");
12554

12655
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
@@ -151,10 +80,10 @@ public void onDisable() {
15180
public boolean Load() {
15281
boolean verified = false;
15382

154-
Objects.requireNonNull(this.getCommand("acmdhelp")).setExecutor(new CommandRunnerHelp(this));
155-
Objects.requireNonNull(this.getCommand("acmd")).setExecutor(new CommandRunnerCommand(this));
156-
Objects.requireNonNull(this.getCommand("acmdreload")).setExecutor(new CommandRunnerReload(this));
157-
Objects.requireNonNull(this.getCommand("acmdTime")).setExecutor(new CommandRunnerTime(this));
83+
Objects.requireNonNull(this.getCommand("acmdhelp")).setExecutor(new CommandExecutorHelp(this));
84+
Objects.requireNonNull(this.getCommand("acmd")).setExecutor(new CommandExecutorAcmd(this));
85+
Objects.requireNonNull(this.getCommand("acmdreload")).setExecutor(new CommandExecutorReload(this));
86+
Objects.requireNonNull(this.getCommand("acmdTime")).setExecutor(new CommandExecutorTime(this));
15887

15988
Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', " &aLoaded"));
16089
return verified;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package fr.autoplugins.Util;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
import fr.autoplugins.Main;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStreamReader;
12+
import java.net.HttpURLConnection;
13+
import java.net.URL;
14+
import java.util.logging.Level;
15+
16+
public class PluginVersionChecker {
17+
private static String callGithubForTag(Main plugin) {
18+
StringBuilder response = new StringBuilder();
19+
try {
20+
// Make HTTP GET request
21+
URL url = new URL("https://api.github.com/repos/AutoPluginsDev/AutoCommands/tags");
22+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
23+
connection.setRequestMethod("GET");
24+
connection.setRequestProperty("Accept", "application/json");
25+
26+
// Check response code
27+
if (connection.getResponseCode() != 200) {
28+
throw new IOException("Failed to get response from GitHub API");
29+
}
30+
31+
// Read response
32+
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
33+
34+
String line;
35+
while ((line = reader.readLine()) != null) {
36+
response.append(line);
37+
}
38+
reader.close();
39+
} catch (IOException e) {
40+
plugin.getLogger().log(Level.WARNING, "Failed to check for a new version on spigot.", e);
41+
}
42+
return response.toString();
43+
}
44+
45+
public static String VerifyPluginVersion(Main plugin) {
46+
String spigotResponse = "";
47+
String currentVersion = plugin.getDescription().getVersion();
48+
49+
String response = callGithubForTag(plugin);
50+
51+
// Parse JSON response
52+
JsonParser parser = new JsonParser();
53+
JsonElement jsonElement = parser.parse(response);
54+
if (jsonElement.isJsonArray()) {
55+
JsonArray jsonArray = jsonElement.getAsJsonArray();
56+
if (jsonArray.size() > 0) {
57+
JsonObject latestTag = jsonArray.get(0).getAsJsonObject();
58+
spigotResponse = latestTag.get("name").getAsString();
59+
}
60+
}
61+
62+
if (spigotResponse.equals("")) {
63+
return "&cFailed to check for a new version on spigot.";
64+
}
65+
66+
if (spigotResponse.equals(currentVersion)) {
67+
return "&aYou are running the latest version of AutoCommands " + currentVersion + " !";
68+
}
69+
70+
return "&eAutoCommands &a&l" + spigotResponse + " &eis available! &chttps://www.spigotmc.org/resources/acmd-%E2%8F%B0-%E2%8F%B3-autocommands-1-13-1-20-4.100090";
71+
}
72+
73+
74+
}

0 commit comments

Comments
 (0)