Skip to content
This repository was archived by the owner on Jun 10, 2022. It is now read-only.

Commit 02270ef

Browse files
committed
Initial commit.
1 parent e5bd3e8 commit 02270ef

File tree

8 files changed

+641
-1
lines changed

8 files changed

+641
-1
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
Nitrapi-Minecraft.iml
3+
target/
4+
classes/

Diff for: README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
# Nitrapi-Minecraft-Plugin
1+
<h1>Nitrapi-Minecraft</h1>
2+
This plugin enables the players with the minenitrapi.nitrapi permission <br>
3+
to use /nitrapi commands.<br>
4+
The documentation of the Nitrapi is located <href>on https://doc.nitrado.net</href><br>
5+
6+
<h2>Build the plugin</h2>
7+
To build the plugin all you need is to run "mvn compile assembly:single"
8+
9+
<h2>Authorize the Plugin</h2>
10+
Please visit <href>https://server.nitrado.net/deu/developer/index</href><br>
11+
Click the plus button to create a new application. <br>
12+
Insert name, website, email and a description(of your choice)<br>
13+
<b>The redirect URL has to be http://***YOUR_SERVER_IP***:8080/Callback</b><br><br>
14+
After saving, you can click your application to get the APP ID and the APP SECRET.<br>
15+
<b>You need the values in the next step</b><br>
16+
17+
<h2>Fill your config</h2>
18+
On first run, the plugin will create its config file.<br>
19+
Alternatively you can create the config and insert the values before you start, to save one restart.<br>
20+
The location has to be <b>plugins/Nitrapi-Minecraft/config.yml</b><br>
21+
<pre>
22+
APP_ID: Enter APP_ID here<br>
23+
APP_SECRET: Enter APP_SECRET here<br>
24+
IP: Enter IP here<br>
25+
PORT: 8080<br>
26+
SCOPES: user_info service service_order ssh_keys<br>
27+
CREDENTIALS_PATH: /ftproot/minecraftbukkit/plugins/Nitrapi-Minecraft/</pre>
28+
29+
<b>Insert APP ID, APP Secret and your Server IP in the config file of the plugin.</b><br>
30+
If you don't do this. the plugin will disable at start.<br>
31+
32+
<h2>First Auth</h2>
33+
To authenticate you have to start the server, join the server and <br>
34+
<b>execute /nitrapi auth</b>. Click the link and enter your Nitrado Account Credentials.<br>
35+
If you don't do this, the server will die.
36+
<h2>Have fun</h2>
37+
Current commands you can see if you execute just /nitrapi.

Diff for: pom.xml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>net.nitrado</groupId>
5+
<artifactId>Nitrapi-Minecraft</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0</version>
8+
<name>Nitrapi-Minecraft</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.google.oauth-client</groupId>
24+
<artifactId>google-oauth-client</artifactId>
25+
<version>1.22.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.google.oauth-client</groupId>
29+
<artifactId>google-oauth-client-jetty</artifactId>
30+
<version>1.22.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.google.oauth-client</groupId>
34+
<artifactId>google-oauth-client-java6</artifactId>
35+
<version>1.22.0</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.google.http-client</groupId>
39+
<artifactId>google-http-client-jackson2</artifactId>
40+
<version>1.11.0-beta</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.spigotmc</groupId>
44+
<artifactId>spigot-api</artifactId>
45+
<version>1.12-R0.1-SNAPSHOT</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>net.nitrado</groupId>
50+
<artifactId>nitrapi</artifactId>
51+
<version>1.0.0</version>
52+
</dependency>
53+
</dependencies>
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<!-- to build a single jar containing all dependencies run mvn compile assembly:single -->
58+
<artifactId>maven-assembly-plugin</artifactId>
59+
<configuration>
60+
<descriptorRefs>
61+
<descriptorRef>jar-with-dependencies</descriptorRef>
62+
</descriptorRefs>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>

Diff for: src/main/java/net/nitrado/MineNitrapi.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package net.nitrado;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.event.EventHandler;
5+
import org.bukkit.event.player.PlayerJoinEvent;
6+
import org.bukkit.plugin.java.JavaPlugin;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.entity.Player;
9+
10+
public class MineNitrapi extends JavaPlugin implements Listener {
11+
12+
public static MineNitrapi instance;
13+
14+
public void loadConfiguration() {
15+
this.getConfig().addDefault("APP_ID","Enter APP_ID here");
16+
this.getConfig().addDefault("APP_SECRET","Enter APP_SECRET here");
17+
this.getConfig().addDefault("IP","Enter IP here");
18+
this.getConfig().addDefault("PORT",8080);
19+
this.getConfig().addDefault("SCOPES","user_info service service_order ssh_keys");
20+
this.getConfig().addDefault("CREDENTIALS_PATH","/ftproot/minecraftbukkit/plugins/Nitrapi-Minecraft/");
21+
this.getConfig().options().copyDefaults(true);
22+
this.saveConfig();
23+
}
24+
25+
public String getAppId(){
26+
return getConfig().getString("APP_ID");
27+
}
28+
public String getAppSecret(){
29+
return getConfig().getString("APP_SECRET");
30+
}
31+
public String getIp(){
32+
return getConfig().getString("IP");
33+
}
34+
public String getScopes(){
35+
return getConfig().getString("SCOPES");
36+
}
37+
public String getCredentialsPath(){
38+
return getConfig().getString("CREDENTIALS_PATH");
39+
}
40+
public int getPort(){
41+
return getConfig().getInt("PORT");
42+
}
43+
44+
@Override
45+
public void onLoad(){
46+
instance = this;
47+
}
48+
49+
@Override
50+
public void onEnable(){
51+
loadConfiguration();
52+
/* Disable the plugin if the config is not valid */
53+
if (!NitrapiCommandExecutor.isConfigValid()){
54+
System.out.println("-------------------------------------------------------------------------");
55+
System.out.println("ERROR: PLEASE ENTER CONFIG VALUES in plugins/Nitrapi-Minecraft/config.yml");
56+
System.out.println(" ERROR: THE PLUGIN WILL NOW DISABLE ITSELF ");
57+
System.out.println("-------------------------------------------------------------------------");
58+
Bukkit.getPluginManager().disablePlugin(this);
59+
return;
60+
} else {
61+
try {
62+
Bukkit.getScheduler().runTaskAsynchronously(instance, () -> {
63+
try {
64+
System.out.println("Check if auth is already done");
65+
System.out.println(NitrapiAuth.ping());
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
}
69+
});
70+
} catch (Exception e) {
71+
e.printStackTrace();
72+
System.out.println("Please execute /nitrapi auth");
73+
}
74+
}
75+
/* register events and the nitrapi command */
76+
getServer().getPluginManager().registerEvents(this, this);
77+
this.getCommand("nitrapi").setExecutor(new NitrapiCommandExecutor(this));
78+
}
79+
80+
/* useless but polite */
81+
@Override
82+
public void onDisable(){
83+
this.getLogger().info("MineNitrapi says goodbye");
84+
}
85+
86+
/* Telling great news */
87+
@EventHandler
88+
public void onJoin(PlayerJoinEvent event) {
89+
Player p = event.getPlayer();
90+
p.sendMessage("§6MineNitrapi Admin Interface");
91+
p.sendMessage("§6start with /nitrapi auth");
92+
}
93+
94+
}

Diff for: src/main/java/net/nitrado/NitrapiAuth.java

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package net.nitrado;
2+
3+
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
4+
import com.google.api.client.auth.oauth2.BearerToken;
5+
import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
6+
import com.google.api.client.auth.oauth2.Credential;
7+
import com.google.api.client.http.*;
8+
import com.google.api.client.http.javanet.NetHttpTransport;
9+
import com.google.api.client.json.JsonFactory;
10+
import com.google.api.client.json.JsonObjectParser;
11+
import com.google.api.client.json.jackson2.JacksonFactory;
12+
import com.google.api.client.util.Key;
13+
import com.google.api.client.util.store.FileDataStoreFactory;
14+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
15+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
16+
import com.google.api.client.http.HttpRequestInitializer;
17+
import java.util.Arrays;
18+
19+
public class NitrapiAuth {
20+
21+
/* Global instance of the HTTP transport. */
22+
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
23+
/* Global instance of the JSON factory. */
24+
static final JsonFactory JSON_FACTORY = new JacksonFactory();
25+
26+
/*
27+
* Directory to store user credentials.
28+
* /ftproot/ is needed to see the file in your nitrado webinterface
29+
*/
30+
private static final java.io.File DATA_STORE_DIR =
31+
new java.io.File(System.getProperty("user.home"), MineNitrapi.instance.getCredentialsPath());
32+
33+
/* Global instance of the FileDataStoreFactory */
34+
private static FileDataStoreFactory DATA_STORE_FACTORY;
35+
36+
public static final String TOKEN_SERVER_URL = "https://oauth.nitrado.net/oauth/v2/token";
37+
public static final String AUTHORIZATION_SERVER_URL =
38+
"https://oauth.nitrado.net/oauth/v2/auth";
39+
40+
/* Authorizes MineNitrapi to access user's protected data. */
41+
public static Credential authorize() throws Exception {
42+
43+
// set up authorization code flow
44+
AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken
45+
.authorizationHeaderAccessMethod(),
46+
HTTP_TRANSPORT,
47+
JSON_FACTORY,
48+
new GenericUrl(TOKEN_SERVER_URL),
49+
new ClientParametersAuthentication(
50+
MineNitrapi.instance.getAppId(), MineNitrapi.instance.getAppSecret()),
51+
MineNitrapi.instance.getAppId(),
52+
AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(MineNitrapi.instance.getScopes()))
53+
.setDataStoreFactory(DATA_STORE_FACTORY).build();
54+
// authorize
55+
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(
56+
MineNitrapi.instance.getIp()).setPort(MineNitrapi.instance.getPort()).build();
57+
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
58+
}
59+
60+
/* URL for Nitrado API. */
61+
public static class NitradoUrl extends GenericUrl {
62+
63+
public NitradoUrl(String encodedUrl) {
64+
super(encodedUrl);
65+
}
66+
67+
@Key
68+
public String fields;
69+
}
70+
71+
public static HttpRequestFactory requestFactory() throws Exception{
72+
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
73+
final Credential credential = authorize();
74+
HttpRequestFactory requestFactory =
75+
HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
76+
@Override
77+
public void initialize(HttpRequest request){
78+
try {
79+
credential.initialize(request);
80+
} catch (Exception e){
81+
System.out.println(e.getMessage());
82+
}
83+
request.setParser(new JsonObjectParser(JSON_FACTORY));
84+
}
85+
});
86+
return requestFactory;
87+
}
88+
89+
public static boolean ping() throws Exception {
90+
try {
91+
try {
92+
NitradoUrl url = new NitradoUrl("https://api.nitrado.net/ping");
93+
url.fields = "";
94+
HttpRequest request = requestFactory().buildGetRequest(url);
95+
request.execute();
96+
return true;
97+
} catch (HttpResponseException e) {
98+
System.out.println(e.getMessage());
99+
return false;
100+
}
101+
} catch (Throwable t) {
102+
t.printStackTrace();
103+
}
104+
System.out.println("Api ping was not successful! Please do /nitrapi auth");
105+
return false;
106+
}
107+
108+
}

0 commit comments

Comments
 (0)