Skip to content

Commit e186efc

Browse files
committed
1.4 - /craft, /ec and VersionChecker.java added to notify if behind the GitHub Releases
1 parent 8ef0c85 commit e186efc

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.setloth</groupId>
88
<artifactId>ModificationMaster</artifactId>
9-
<version>1.3</version>
9+
<version>1.4</version>
1010
<packaging>jar</packaging>
1111

1212
<name>ModificationMaster</name>

src/main/java/me/setloth/modificationMaster/ModificationMaster.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package me.setloth.modificationMaster;
22

3+
import me.setloth.modificationMaster.commands.Craft;
4+
import me.setloth.modificationMaster.commands.EndChest;
35
import me.setloth.modificationMaster.commands.Sort;
46
import me.setloth.modificationMaster.listeners.BlockBreaking;
7+
import me.setloth.modificationMaster.util.VersionChecker;
58
import org.bukkit.plugin.Plugin;
69
import org.bukkit.plugin.java.JavaPlugin;
710

811
import java.util.Objects;
9-
import java.util.logging.Logger;
12+
import java.util.logging.Level;
1013

1114
@SuppressWarnings("unused")
1215
public final class ModificationMaster extends JavaPlugin {
@@ -19,11 +22,30 @@ public static Plugin instance() {
1922
@Override
2023
public void onEnable() {
2124
INSTANCE = this;
25+
long start = System.currentTimeMillis();
26+
27+
String ver = this.getPluginMeta().getVersion();
28+
String latestVer = VersionChecker.latestVersion();
29+
30+
log("Initializing plugin version "+ver);
31+
if (!ver.equals(latestVer)) {
32+
log("\n\nPlugin is outdated!\nYour Version: "+ver+"\nLatest Version: "+latestVer+"\n\nFetch" +
33+
" " +
34+
"updates at https://github.com/Setloth/ModificationMaster\n\n", Level.WARNING);
35+
}
36+
37+
2238
// Plugin startup logic
2339
log("Registering Events");
2440
getServer().getPluginManager().registerEvents(new BlockBreaking(), this);
41+
2542
log("Registering Commands");
2643
Objects.requireNonNull(getServer().getPluginCommand("sort")).setExecutor(new Sort());
44+
Objects.requireNonNull(getServer().getPluginCommand("sort")).setTabCompleter(new Sort());
45+
Objects.requireNonNull(getServer().getPluginCommand("endchest")).setExecutor(new EndChest());
46+
Objects.requireNonNull(getServer().getPluginCommand("craft")).setExecutor(new Craft());
47+
48+
log("Done! Took: "+(System.currentTimeMillis()-start)+" ms");
2749

2850
}
2951

@@ -33,7 +55,13 @@ public void onDisable() {
3355
log("Goodbye :(");
3456
}
3557

58+
59+
3660
public static void log(String msg) {
37-
Logger.getLogger("MM").info(msg);
61+
log(msg, Level.INFO);
62+
}
63+
64+
public static void log(String msg, Level level) {
65+
INSTANCE.getLogger().log(level, msg);
3866
}
3967
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.setloth.modificationMaster.commands;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
public class Craft implements CommandExecutor {
10+
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
11+
12+
if (commandSender instanceof Player p) {
13+
p.openWorkbench(null, true);
14+
}
15+
16+
return true;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.setloth.modificationMaster.commands;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
public class EndChest implements CommandExecutor {
10+
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
11+
12+
if (commandSender instanceof Player p) {
13+
p.openInventory(p.getEnderChest());
14+
}
15+
16+
return true;
17+
}
18+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package me.setloth.modificationMaster.util;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.HttpURLConnection;
6+
import java.net.URI;
7+
8+
import org.json.simple.JSONArray;
9+
import org.json.simple.JSONObject;
10+
import org.json.simple.parser.JSONParser;
11+
12+
public class VersionChecker {
13+
14+
public static String latestVersion() {
15+
String latestVersion = null;
16+
try {
17+
// URL to your GitHub repo tags API
18+
URI uri = new URI("https://api.github.com/repos/Setloth/ModificationMaster/tags");
19+
20+
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
21+
conn.setRequestMethod("GET");
22+
conn.setRequestProperty("Accept", "application/json");
23+
24+
// Check if the request was successful
25+
if (conn.getResponseCode() != 200) {
26+
throw new RuntimeException("Failed: HTTP error code: " + conn.getResponseCode());
27+
}
28+
29+
// Read the response from the API
30+
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
31+
StringBuilder jsonOutput = new StringBuilder();
32+
String line;
33+
while ((line = br.readLine()) != null) {
34+
jsonOutput.append(line);
35+
}
36+
conn.disconnect();
37+
38+
// Parse the JSON array to get the latest tag
39+
JSONParser parser = new JSONParser();
40+
Object obj = parser.parse(jsonOutput.toString());
41+
if (! (obj instanceof JSONArray json)) {
42+
throw new RuntimeException("Failed: JSON invalid "+jsonOutput);
43+
}
44+
if (!json.isEmpty()) {
45+
JSONObject jsonO = (JSONObject) json.getFirst();
46+
latestVersion = (String) jsonO.get("name");
47+
}
48+
} catch (Exception e) {
49+
throw new RuntimeException(e);
50+
}
51+
return latestVersion;
52+
}
53+
54+
}

src/main/resources/plugin.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
name: ModificationMaster
2-
version: 1.3
2+
version: 1.4
33
main: me.setloth.modificationMaster.ModificationMaster
44
api-version: '1.21'
55
authors: [ Setloth ]
66
commands:
77
sort:
88
usage: /sort [block]
99
description: Sorts your inventory or the target chest
10+
endchest:
11+
usage: /e[nd(er)]c[hest]
12+
aliases: [echest, enderchest, ec]
13+
description: Opens your ender chest on the go!
14+
craft:
15+
usage: /[craft(ing) | workbench]
16+
aliases: [crafting, workbench]
17+
description: Open a crafting table anywhere!

0 commit comments

Comments
 (0)