Skip to content

Commit

Permalink
Merge pull request #2 from gnmyt/features/plugins-description
Browse files Browse the repository at this point in the history
🔌 Added the plugin feature & added a description
  • Loading branch information
gnmyt authored Aug 1, 2021
2 parents 701a56f + 5400213 commit 4830fe5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
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>de.gnmyt</groupId>
<artifactId>MCDash</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.gnmyt.mcdash.panel.routes.plugin;

import de.gnmyt.mcdash.api.handler.DefaultHandler;
import de.gnmyt.mcdash.api.http.ContentType;
import de.gnmyt.mcdash.api.http.Request;
import de.gnmyt.mcdash.api.http.ResponseController;
import de.gnmyt.mcdash.api.json.ArrayBuilder;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

public class PluginListRoute extends DefaultHandler {

@Override
public String path() {
return "list";
}

/**
* Gets all plugins of the server
* @param request The request object from the HttpExchange
* @param response The response controller from the HttpExchange
*/
@Override
public void get(Request request, ResponseController response) throws Exception {

ArrayBuilder builder = new ArrayBuilder();

for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
builder.addNode()
.add("name", plugin.getName())
.add("author", plugin.getDescription().getAuthors().size() == 0 ? null : plugin.getDescription().getAuthors().get(0))
.add("description", plugin.getDescription().getDescription())
.add("path", plugin.getClass().getProtectionDomain().getCodeSource().getLocation().getFile())
.add("enabled", plugin.isEnabled())
.register();
}

response.type(ContentType.JSON).text(builder.toJSON());
}

}
92 changes: 92 additions & 0 deletions src/main/java/de/gnmyt/mcdash/panel/routes/plugin/PluginRoute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package de.gnmyt.mcdash.panel.routes.plugin;

import de.gnmyt.mcdash.api.handler.DefaultHandler;
import de.gnmyt.mcdash.api.http.Request;
import de.gnmyt.mcdash.api.http.ResponseController;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

public class PluginRoute extends DefaultHandler {

/**
* Gets a plugin by name
* @param request The request object from the HttpExchange
* @param response The response controller from the HttpExchange
*/
@Override
public void get(Request request, ResponseController response) throws Exception {
if (!isStringInQuery("name")) return;

String pluginName = getStringFromQuery("name");

Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);

if (plugin == null) {
response.code(404).message("Plugin not found");
return;
}

String filePath = plugin.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
String author = plugin.getDescription().getAuthors().size() == 0 ? null : plugin.getDescription().getAuthors().get(0);

response.json("name=\""+plugin.getName()+"\"", "author=\""+author+"\"",
"description=\""+plugin.getDescription().getDescription()+"\"", "path=\""+filePath+"\"", "enabled="+plugin.isEnabled());
}

/**
* Enables a plugin
* @param request The request object from the HttpExchange
* @param response The response controller from the HttpExchange
*/
@Override
public void post(Request request, ResponseController response) throws Exception {
if (!isStringInBody("name")) return;

String pluginName = getStringFromBody("name");

Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);

if (plugin == null) {
response.code(404).message("Plugin not found");
return;
}

runSync(() -> {
try {
Bukkit.getPluginManager().enablePlugin(plugin);
response.message("Plugin successfully enabled");
} catch (Exception e) {
response.code(500).message("Could not enable the plugin");
}
});
}

/**
* Disables a plugin
* @param request The request object from the HttpExchange
* @param response The response controller from the HttpExchange
*/
@Override
public void delete(Request request, ResponseController response) throws Exception {
if (!isStringInBody("name")) return;

String pluginName = getStringFromBody("name");

Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);

if (plugin == null) {
response.code(404).message("Plugin not found");
return;
}

runSync(() -> {
try {
Bukkit.getPluginManager().disablePlugin(plugin);
response.message("Plugin successfully disabled");
} catch (Exception e) {
response.code(500).message("Could not disable the plugin");
}
});
}

}
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: MinecraftDashboard
main: de.gnmyt.mcdash.MinecraftDashboard
version: 1.0-SNAPSHOT
description: This is the official plugin (wrapper) for MCDash, a free and open-source minecraft dashboard.
author: GNMYT

0 comments on commit 4830fe5

Please sign in to comment.