3
0
Fork 0

Custom command support, added global status

This commit is contained in:
thomas 2021-08-09 02:26:23 +02:00
parent e8804233e1
commit 3f7f86ef67
12 changed files with 366 additions and 25 deletions

View file

@ -57,7 +57,7 @@
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>me.clip</groupId>
@ -73,7 +73,7 @@
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>1.1.1</version>
<version>2.4.7</version>
</dependency>
<dependency>
<groupId>com.bergerkiller.bukkit</groupId>
@ -90,7 +90,7 @@
<dependency>
<groupId>com.github.cryptomorin</groupId>
<artifactId>XSeries</artifactId>
<version>8.1.0</version>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>

View file

@ -4,6 +4,7 @@ import nl.iobyte.menuapi.MenuAPI;
import nl.iobyte.themepark.api.ThemeParkAPI;
import nl.iobyte.themepark.commands.ThemeParkCommand;
import nl.iobyte.themepark.listeners.*;
import nl.iobyte.themepark.utils.UpdateManager;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
@ -22,6 +23,7 @@ public class ThemePark extends JavaPlugin {
loadCommands();
loadListeners();
loadTrainCarts();
loadUpdateManager();
}
private void loadCommands() {
@ -47,13 +49,17 @@ public class ThemePark extends JavaPlugin {
Class<?> clazz = Class.forName("com.bergerkiller.bukkit.tc.signactions.SignAction");
Class<?> sign = Class.forName("nl.iobyte.themepark.traincarts.RideCountSign");
Method method = clazz.getMethod("register", clazz);
method.invoke(clazz, sign.newInstance());
method.invoke(clazz, sign.getDeclaredConstructor().newInstance());
}
} catch (Exception e) {
System.out.println("["+getName()+"] Unable to hook into TrainCarts");
}
}
private void loadUpdateManager() {
//TODO new UpdateManager(this, 48648);
}
public void onDisable() {
disabling = true;
api.disable();

View file

@ -7,6 +7,7 @@ import org.bukkit.inventory.ItemStack;
public enum Status {
GLOBAL(0, "&1", "#3498db", "Global", "STAINED_CLAY:3", false),
CONSTRUCTION(1, "&7", "#95a5a6", "Under Contruction", "STAINED_CLAY:1",false),
OPEN(2, "&a", "#4cd137", "Open", "STAINED_CLAY:5", true),
CLOSED(3, "&4", "#e84118", "Closed", "STAINED_CLAY:14", false),

View file

@ -2,6 +2,9 @@ package nl.iobyte.themepark.api.config.enums;
public enum StorageKey {
//General Settings
CMD(StorageLocation.SETTINGS, "cmd"),
//Database Settings
MYSQL_ENABLED(StorageLocation.SETTINGS, "mysql.enabled"),
MYSQL_URL(StorageLocation.SETTINGS, "mysql.url"),
@ -12,7 +15,7 @@ public enum StorageKey {
MYSQL_PASSWORD(StorageLocation.SETTINGS, "mysql.password"),
//Menu Settings
MENU_ENABLED_MAIN(StorageLocation.MENU, "main.menu.enabled"),
MENU_ENABLED_MAIN(StorageLocation.MENU, "menu.main.enabled"),
MENU_SIZE_MAIN(StorageLocation.MENU, "menu.main.size"),
MENU_TITLE_MAIN(StorageLocation.MENU, "menu.main.title"),
MENU_TITLE_STATUS(StorageLocation.MENU, "menu.status.title"),

View file

@ -113,7 +113,7 @@ public class StatusDataLoader implements IDataLoader {
return;
ConfigurationSection section = config.getSection("attractions");
if(section.getKeys(false).isEmpty())
if(section == null || section.getKeys(false).isEmpty())
return;
//Load Attractions

View file

@ -4,6 +4,7 @@ import nl.iobyte.commandapi.CommandFactory;
import nl.iobyte.commandapi.interfaces.ICommandExecutor;
import nl.iobyte.commandapi.middlewares.PermissionMiddleware;
import nl.iobyte.themepark.ThemePark;
import nl.iobyte.themepark.api.config.enums.StorageKey;
import nl.iobyte.themepark.commands.executors.ConsoleExecutor;
import nl.iobyte.themepark.commands.executors.PlayerExecutor;
import nl.iobyte.themepark.commands.subcommands.HelpCommand;
@ -13,16 +14,21 @@ import nl.iobyte.themepark.commands.subcommands.attraction.AttractionCommands;
import nl.iobyte.themepark.commands.subcommands.region.RegionCommands;
import nl.iobyte.themepark.commands.subcommands.ridecount.RideCountCommands;
import nl.iobyte.themepark.commands.subcommands.status.StatusCommands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
public class ThemeParkCommand {
//Load command data
public ThemeParkCommand() {
CommandFactory factory = new CommandFactory("themepark");
String cmd = ThemePark.getInstance().getAPI().getConfigurationManager().getString(StorageKey.CMD);
CommandFactory factory = new CommandFactory(cmd);
factory.addSubCommand(new HelpCommand(factory))
.addSubCommand(new MenuCommand());
@ -45,14 +51,24 @@ public class ThemeParkCommand {
factory.addMiddleware(new PermissionMiddleware());
//Register command
ThemePark.getInstance().getCommand(factory.getName()).setExecutor((sender, command, s, args) -> {
try {
Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true);
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
commandMap.register(cmd, new Command(cmd) {
public boolean execute(CommandSender sender, String label, String[] args) {
ICommandExecutor executor = new ConsoleExecutor(sender);
if(sender instanceof Player)
executor = new PlayerExecutor((Player) sender);
factory.onCommand(executor, args);
return true;
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}

View file

@ -132,6 +132,9 @@ public class PlayerListener implements Listener {
return;
Inventory inventory = e.getClickedInventory();
if(inventory == null)
return;
if(inventory.getType() != InventoryType.PLAYER)
return;

View file

@ -0,0 +1,312 @@
package nl.iobyte.themepark.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.function.BiConsumer;
/**
* Update class for SBDevelopment
* @author Stijn [SBDeveloper]
* @since 05-03-2020
* @version 2.0 [26-12-2020] - This class supports the v2 Update API
*
* <p>&copy; Stijn Bannink [stijnbannink23@gmail.com] - All rights reserved.</p>
*/
public class UpdateManager {
private static final String SPIGOT_API = "https://api.spigotmc.org/legacy/update.php?resource=%d";
private static final String SPIGOT_DOWNLOAD = "http://api.spiget.org/v2/resources/%s/download";
private static final String SBDPLUGINS_API = "https://updates.sbdplugins.nl/api/v2/plugins/%d";
private static final String SBDPLUGINS_DOWNLOAD = "https://updates.sbdplugins.nl/api/v2/download/%d";
private final Plugin plugin;
private final Version currentVersion;
private final int resourceID;
private final CheckType type;
private final String license;
private BiConsumer<VersionResponse, Version> versionResponse;
private BiConsumer<DownloadResponse, String> downloadResponse;
/**
* Construct a new UpdateManager for Spigot
*
* @param plugin The javaplugin (Main class)
* @param resourceID The resourceID on spigot/sbdplugins
*/
public UpdateManager(@NotNull Plugin plugin, int resourceID) {
this.plugin = plugin;
this.currentVersion = new Version(plugin.getDescription().getVersion());
this.resourceID = resourceID;
this.type = CheckType.SPIGOT;
this.license = null;
}
/**
* Construct a new UpdateManager for SBDPlugins
*
* @param plugin The javaplugin (Main class)
* @param resourceID The resourceID on spigot/sbdplugins
* @param license The license for the download
*/
public UpdateManager(@NotNull Plugin plugin, int resourceID, String license) {
this.plugin = plugin;
this.currentVersion = new Version(plugin.getDescription().getVersion());
this.resourceID = resourceID;
this.type = CheckType.SBDPLUGINS;
this.license = license;
}
/**
* Handle the response given by check();
* @param versionResponse The response
* @return The updatemanager
*/
public UpdateManager handleResponse(BiConsumer<VersionResponse, Version> versionResponse) {
this.versionResponse = versionResponse;
return this;
}
public UpdateManager handleDownloadResponse(BiConsumer<DownloadResponse, String> downloadResponse) {
this.downloadResponse = downloadResponse;
return this;
}
/**
* Check for a new version
*/
public void check() {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
BufferedReader in = null;
if (type == CheckType.SPIGOT) {
HttpsURLConnection con = (HttpsURLConnection) new URL(String.format(SPIGOT_API, this.resourceID)).openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else if (type == CheckType.SBDPLUGINS) {
HttpsURLConnection con = (HttpsURLConnection) new URL(String.format(SBDPLUGINS_API, this.resourceID)).openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
if (in == null) return;
String version;
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JsonParser parser = new JsonParser();
if (type == CheckType.SPIGOT) {
JsonArray array = parser.parse(response.toString()).getAsJsonArray();
version = array.get(0).getAsJsonObject().get("name").getAsString();
} else {
JsonObject object = parser.parse(response.toString()).getAsJsonObject();
version = object.get("version").getAsString();
}
if (version == null) return;
Version onlineVersion = new Version(version);
VersionResponse verRes = this.currentVersion.check(onlineVersion);
Bukkit.getScheduler().runTask(this.plugin, () -> this.versionResponse.accept(verRes, onlineVersion));
} catch (IOException | NullPointerException e) {
e.printStackTrace();
Bukkit.getScheduler().runTask(this.plugin, () -> this.versionResponse.accept(VersionResponse.UNAVAILABLE, null));
}
});
}
public void runUpdate() {
File pluginFile = getPluginFile(); // /plugins/XXX.jar
if (pluginFile == null) {
this.downloadResponse.accept(DownloadResponse.ERROR, null);
Bukkit.getLogger().info("Pluginfile is null");
return;
}
File updateFolder = Bukkit.getUpdateFolderFile();
if (!updateFolder.exists()) {
if (!updateFolder.mkdirs()) {
this.downloadResponse.accept(DownloadResponse.ERROR, null);
Bukkit.getLogger().info("Updatefolder doesn't exists, and can't be made");
return;
}
}
final File updateFile = new File(updateFolder, pluginFile.getName());
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
ReadableByteChannel channel;
try {
//https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
int response;
InputStream stream;
if (type == CheckType.SBDPLUGINS) {
HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection();
String urlParameters = "license=" + license + "&port=" + Bukkit.getPort();
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(postData);
wr.close();
response = connection.getResponseCode();
stream = connection.getInputStream();
} else {
HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
response = connection.getResponseCode();
stream = connection.getInputStream();
}
if (response != 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String inputLine;
StringBuilder responsestr = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
responsestr.append(inputLine);
}
in.close();
throw new RuntimeException("Download returned status #" + response, new Throwable(responsestr.toString()));
}
channel = Channels.newChannel(stream);
} catch (IOException e) {
Bukkit.getScheduler().runTask(this.plugin, () -> this.downloadResponse.accept(DownloadResponse.ERROR, null));
e.printStackTrace();
return;
}
FileChannel fileChannel = null;
try {
FileOutputStream fosForDownloadedFile = new FileOutputStream(updateFile);
fileChannel = fosForDownloadedFile.getChannel();
fileChannel.transferFrom(channel, 0, Long.MAX_VALUE);
} catch (IOException e) {
Bukkit.getScheduler().runTask(this.plugin, () -> this.downloadResponse.accept(DownloadResponse.ERROR, null));
e.printStackTrace();
return;
} finally {
if (channel != null) {
try {
channel.close();
} catch (IOException ioe) {
System.out.println("Error while closing response body channel");
}
}
if (fileChannel != null) {
try {
fileChannel.close();
} catch (IOException ioe) {
System.out.println("Error while closing file channel for downloaded file");
}
}
}
Bukkit.getScheduler().runTask(this.plugin, () -> this.downloadResponse.accept(DownloadResponse.DONE, updateFile.getPath()));
});
}
@Nullable
private File getPluginFile() {
if (!(this.plugin instanceof JavaPlugin)) { return null; }
try {
Method method = JavaPlugin.class.getDeclaredMethod("getFile");
method.setAccessible(true);
return (File) method.invoke(this.plugin);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Could not get plugin file", e);
}
}
private enum CheckType {
SPIGOT, SBDPLUGINS
}
public enum VersionResponse {
LATEST, //Latest version
FOUND_NEW, //Newer available
THIS_NEWER, //Local version is newer?
UNAVAILABLE //Error
}
public enum DownloadResponse {
DONE, ERROR, UNAVAILABLE
}
public static class Version {
private final String version;
public final String get() {
return this.version;
}
private Version(String version) {
if(version == null)
throw new IllegalArgumentException("Version can not be null");
if(!version.matches("[0-9]+(\\.[0-9]+)*"))
throw new IllegalArgumentException("Invalid version format");
this.version = version;
}
private VersionResponse check(@NotNull Version that) {
String[] thisParts = this.get().split("\\.");
String[] thatParts = that.get().split("\\.");
int length = Math.max(thisParts.length, thatParts.length);
for (int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ? Integer.parseInt(thisParts[i]) : 0;
int thatPart = i < thatParts.length ? Integer.parseInt(thatParts[i]) : 0;
if(thisPart < thatPart)
return VersionResponse.FOUND_NEW;
if(thisPart > thatPart)
return VersionResponse.THIS_NEWER;
}
return VersionResponse.LATEST;
}
}
}

View file

@ -1,4 +1,4 @@
version: 1.0
version: 1.1
menu:
item:

View file

@ -1,10 +1,7 @@
name: ThemePark
version: 3.0.2
version: 3.0.3
author: IOByte
website: 'https://www.iobyte.nl'
main: nl.iobyte.themepark.ThemePark
softdepend: [PlaceholderAPI,Train_Carts,Multiverse-Core,MultiWorld]
api-version: 1.13
commands:
themepark:
aliases: [tp]

View file

@ -1,4 +1,6 @@
version: 1.1
version: 1.2
cmd: "themepark"
mysql:
enabled: false

View file

@ -21,12 +21,14 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: nl.iobyte:commandapi:1.2" level="project" />
<orderEntry type="library" name="Maven: nl.iobyte:menuapi:1.0" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:5.0.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:2.0.0-alpha1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: me.clip:placeholderapi:2.10.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:19.0.0" level="project" />
<orderEntry type="library" name="Maven: com.dumptruckman.minecraft:JsonConfiguration:1.1" level="project" />
<orderEntry type="library" name="Maven: net.minidev:json-smart:1.1.1" level="project" />
<orderEntry type="library" name="Maven: net.minidev:json-smart:2.4.7" level="project" />
<orderEntry type="library" name="Maven: net.minidev:accessors-smart:2.4.7" level="project" />
<orderEntry type="library" name="Maven: org.ow2.asm:asm:9.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.bergerkiller.bukkit:TrainCarts:1.16.5-v1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.github.qveshn:LightAPI-fork:3.4.2-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: me.m56738:SmoothCoastersAPI:1.1" level="project" />
@ -60,7 +62,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:adventure-platform-viaversion:4.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.bergerkiller.bukkit:BKCommonLib:1.16.5-v1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.bergerkiller.mountiplex:Mountiplex:2.22" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm:7.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.objenesis:objenesis:3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.javassist:javassist:3.27.0-GA" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-project:2.0.6" level="project" />
@ -75,7 +76,7 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-artifact:2.0.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-container-default:1.0-alpha-9-stable-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: classworlds:classworlds:1.1-alpha-2" level="project" />
<orderEntry type="library" name="Maven: com.github.cryptomorin:XSeries:8.1.0" level="project" />
<orderEntry type="library" name="Maven: com.github.cryptomorin:XSeries:8.2.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldguard:worldguard-legacy:6.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit:worldedit-bukkit:6.1.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit:worldedit-core:6.1.4-SNAPSHOT" level="project" />