完成基本功能
This commit is contained in:
commit
4dc0f661e9
27 changed files with 1206 additions and 0 deletions
|
@ -0,0 +1,46 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class BanTask extends BukkitRunnable {
|
||||
private final String username;
|
||||
private final CommandSender sender;
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public BanTask(ClientPlugin plugin, CommandSender sender, String username) {
|
||||
this.sender = sender;
|
||||
this.plugin = plugin;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String uuid;
|
||||
sender.sendMessage("Querying UUID of player " + username + " from Mojang server...");
|
||||
try {
|
||||
uuid = PlayerUUID.FromUsername(username);
|
||||
sender.sendMessage("UUID of player " + username + " is " + uuid + ".");
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().equals("Player does not exist!")) {
|
||||
sender.sendMessage("Player " + username + " does not exist!");
|
||||
} else {
|
||||
sender.sendMessage("Cannot connect to Mojang server!");
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
PlayerUUID.Ban(plugin.ServerAddress, uuid);
|
||||
sender.sendMessage("Banned player " + username + " successfully!");
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().equals("Record exists!")) {
|
||||
sender.sendMessage("Player " + username + " is already in the UnionBan list!");
|
||||
} else if (e.getMessage().equals("Not logged in!")) {
|
||||
sender.sendMessage("You have not logged in!");
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
sender.sendMessage("Failed to ban player " + username + "!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.net.CookieHandler;
|
||||
import java.net.CookieManager;
|
||||
import java.net.CookiePolicy;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ClientPlugin extends JavaPlugin {
|
||||
public String ServerAddress = "https://api.unionban.icu";
|
||||
public BukkitTask login = null;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
|
||||
getServer().getPluginManager().registerEvents(new PlayerLoginListener(this), this);
|
||||
getServer().getPluginManager().registerEvents(new WhitelistListener(this), this);
|
||||
CommandsExecutor executor = new CommandsExecutor(this);
|
||||
Objects.requireNonNull(this.getCommand("unionban")).setExecutor(executor);
|
||||
Objects.requireNonNull(this.getCommand("unionban")).setTabCompleter(executor);
|
||||
saveDefaultConfig();
|
||||
new LoginTask(this, Bukkit.getConsoleSender()).runTaskAsynchronously(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
HandlerList.unregisterAll(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandsExecutor implements TabExecutor {
|
||||
private final ClientPlugin plugin;
|
||||
private final List<String> SubCommands = new ArrayList<>();
|
||||
|
||||
public CommandsExecutor(ClientPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
SubCommands.add("help");
|
||||
SubCommands.add("status");
|
||||
SubCommands.add("login");
|
||||
SubCommands.add("logout");
|
||||
SubCommands.add("ban");
|
||||
SubCommands.add("pardon");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (sender.hasPermission("unionban.account")) {
|
||||
if (args.length == 3 && args[0].equals("login")) {
|
||||
if (plugin.login != null) {
|
||||
plugin.login.cancel();
|
||||
plugin.login = null;
|
||||
}
|
||||
plugin.getConfig().set("username", args[1]);
|
||||
plugin.getConfig().set("password", args[2]);
|
||||
plugin.getConfig().set("api_key", "");
|
||||
plugin.saveConfig();
|
||||
new LoginTask(plugin, sender).runTaskAsynchronously(plugin);
|
||||
return true;
|
||||
}
|
||||
if (args.length == 1 && args[0].equals("logout")) {
|
||||
sender.sendMessage("Logging out...");
|
||||
if (plugin.login != null) {
|
||||
plugin.login.cancel();
|
||||
plugin.login = null;
|
||||
}
|
||||
plugin.getConfig().set("username", "");
|
||||
plugin.getConfig().set("password", "");
|
||||
plugin.getConfig().set("api_key", "");
|
||||
plugin.saveConfig();
|
||||
new LogoutTask(plugin, sender).runTaskAsynchronously(plugin);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (sender.hasPermission("unionban.ban")) {
|
||||
if (args.length == 2 && args[0].equals("ban")) {
|
||||
new BanTask(plugin, sender, args[1]).runTaskAsynchronously(plugin);
|
||||
return true;
|
||||
}
|
||||
if (args.length == 2 && args[0].equals("pardon")) {
|
||||
new PardonTask(plugin, sender, args[1]).runTaskAsynchronously(plugin);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (sender.hasPermission("unionban.info")) {
|
||||
if (args.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (args.length == 1 && args[0].equals("help")) {
|
||||
return false;
|
||||
}
|
||||
if (args.length == 1 && args[0].equals("status")) {
|
||||
new GetServerStatusTask(plugin, sender).runTaskAsynchronously(plugin);
|
||||
return true;
|
||||
}
|
||||
sender.sendMessage("Unknown command. Type \"/unionban help\" for help.");
|
||||
return true;
|
||||
}
|
||||
sender.sendMessage("You do not have permission to execute this command!");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(cmd);
|
||||
for (String arg : args) {
|
||||
builder.append(" ");
|
||||
builder.append(arg);
|
||||
}
|
||||
plugin.getLogger().warning(sender.getName() + " tried to execute command: " + builder);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
|
||||
List<String> result = new ArrayList<>();
|
||||
if (args.length == 1) {
|
||||
result.addAll(SubCommands);
|
||||
} else if (args.length == 2 && (args[0].equals("ban") || args[0].equals("pardon"))) {
|
||||
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
|
||||
String name = player.getName();
|
||||
if (name != null) {
|
||||
result.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
|
||||
class ServerStatus {
|
||||
public String version;
|
||||
}
|
||||
|
||||
public class GetServerStatusTask extends BukkitRunnable {
|
||||
private final ClientPlugin plugin;
|
||||
private final CommandSender sender;
|
||||
|
||||
public GetServerStatusTask(ClientPlugin plugin, CommandSender sender) {
|
||||
this.plugin = plugin;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public static String GetVersion(String ServerAddress) throws Exception {
|
||||
URL url = new URL(ServerAddress);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("GET");
|
||||
con.setDoOutput(true);
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
if (responseCode != 200) {
|
||||
con.disconnect();
|
||||
throw new Exception("HTTP Response Code: " + responseCode);
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String inputLine;
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
builder.append(inputLine);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
con.disconnect();
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
ServerStatus status = objectMapper.readValue(builder.toString(), ServerStatus.class);
|
||||
|
||||
return status.version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
sender.sendMessage("Querying server status...");
|
||||
try {
|
||||
String version = GetVersion(plugin.ServerAddress);
|
||||
sender.sendMessage("UnionBan Server is ON! Version: " + version);
|
||||
String username = LoginTask.GetLoginStatus(plugin.ServerAddress).username;
|
||||
sender.sendMessage("Logged in as " + plugin.getConfig().getString("username") + ".");
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().equals("Not logged in!")) {
|
||||
sender.sendMessage("You have not logged in!");
|
||||
} else {
|
||||
sender.sendMessage("Failed to connect to UnionBan server.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class KickPlayerTask extends BukkitRunnable {
|
||||
private final String username;
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public KickPlayerTask(ClientPlugin plugin, String username) {
|
||||
this.username = username;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Objects.requireNonNull(Bukkit.getPlayer(username)).kickPlayer("You are in the UnionBan list!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,216 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
|
||||
class LoginStatus {
|
||||
public String username;
|
||||
public String api_key;
|
||||
}
|
||||
|
||||
class Expire {
|
||||
public int expire;
|
||||
}
|
||||
|
||||
class UserInfo {
|
||||
public String account;
|
||||
public String password;
|
||||
}
|
||||
|
||||
public class LoginTask extends BukkitRunnable {
|
||||
private final ClientPlugin plugin;
|
||||
private final CommandSender sender;
|
||||
|
||||
public LoginTask(ClientPlugin plugin, CommandSender sender) {
|
||||
this.plugin = plugin;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
private static int LoginViaKey(String ServerAddress, String api_key) throws Exception {
|
||||
LoginStatus key = new LoginStatus();
|
||||
key.api_key = api_key;
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = mapper.writeValueAsString(key);
|
||||
|
||||
String Address = ServerAddress + "/session/";
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("PUT");
|
||||
con.setDoOutput(true);
|
||||
|
||||
con.connect();
|
||||
|
||||
DataOutputStream stream = new DataOutputStream(con.getOutputStream());
|
||||
stream.writeBytes(json);
|
||||
stream.flush();
|
||||
stream.close();
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
|
||||
if (responseCode != 200) {
|
||||
con.disconnect();
|
||||
throw new Exception("Login failed!");
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String inputLine;
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
builder.append(inputLine);
|
||||
}
|
||||
reader.close();
|
||||
con.disconnect();
|
||||
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
Expire expire = mapper.readValue(builder.toString(), Expire.class);
|
||||
|
||||
return expire.expire;
|
||||
}
|
||||
|
||||
private static int LoginViaUsername(String ServerAddress, String username, String password) throws Exception {
|
||||
UserInfo info = new UserInfo();
|
||||
info.account = username;
|
||||
info.password = password;
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = mapper.writeValueAsString(info);
|
||||
|
||||
String Address = ServerAddress + "/session/";
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("POST");
|
||||
con.setDoOutput(true);
|
||||
|
||||
con.connect();
|
||||
|
||||
DataOutputStream stream = new DataOutputStream(con.getOutputStream());
|
||||
stream.writeBytes(json);
|
||||
stream.flush();
|
||||
stream.close();
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
|
||||
if (responseCode != 200) {
|
||||
con.disconnect();
|
||||
throw new Exception("Login failed!");
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String inputLine;
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
builder.append(inputLine);
|
||||
}
|
||||
reader.close();
|
||||
con.disconnect();
|
||||
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
Expire expire = mapper.readValue(builder.toString(), Expire.class);
|
||||
|
||||
return expire.expire;
|
||||
}
|
||||
|
||||
public static LoginStatus GetLoginStatus(String ServerAddress) throws Exception {
|
||||
URL url = new URL(ServerAddress + "/user/");
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("GET");
|
||||
con.setDoOutput(true);
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
if (responseCode != 200) {
|
||||
con.disconnect();
|
||||
throw new Exception("Not logged in!");
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String inputLine;
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
builder.append(inputLine);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
con.disconnect();
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
return mapper.readValue(builder.toString(), LoginStatus.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
String username = config.getString("username");
|
||||
String password = config.getString("password");
|
||||
String api_key = config.getString("api_key");
|
||||
|
||||
sender.sendMessage("Logging in via api_key...");
|
||||
|
||||
if (api_key != null && !api_key.isEmpty()) {
|
||||
try {
|
||||
int expire = LoginViaKey(plugin.ServerAddress, api_key);
|
||||
sender.sendMessage("Login success. The session will expires after " + expire + " seconds.");
|
||||
plugin.login = new LoginTask(plugin, Bukkit.getConsoleSender()).runTaskLaterAsynchronously(plugin, expire * 20);
|
||||
sender.sendMessage("Automated login scheduled.");
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
if (!e.getMessage().equals("Login failed!")) {
|
||||
sender.sendMessage("Failed to connect to UnionBan server.");
|
||||
sender.sendMessage("Failed to login!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sender.sendMessage("Cannot login via api_key. Trying username and password...");
|
||||
|
||||
if (username != null && username.isEmpty()) {
|
||||
sender.sendMessage("Username is empty.");
|
||||
sender.sendMessage("Failed to login!");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int expire = LoginViaUsername(plugin.ServerAddress, username, password);
|
||||
sender.sendMessage("Login success. Updating api_key...");
|
||||
api_key = GetLoginStatus(plugin.ServerAddress).api_key;
|
||||
sender.sendMessage("Got new api_key: " + api_key + ". Saving to file...");
|
||||
config.set("api_key", api_key);
|
||||
plugin.saveConfig();
|
||||
sender.sendMessage("The session will expires after " + expire + " seconds.");
|
||||
plugin.login = new LoginTask(plugin, Bukkit.getConsoleSender()).runTaskLaterAsynchronously(plugin, expire * 20);
|
||||
sender.sendMessage("Automated login scheduled.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (e.getMessage().equals("Login failed!")) {
|
||||
sender.sendMessage("Cannot login via username and password!");
|
||||
} else {
|
||||
sender.sendMessage("Failed to connect to UnionBan server.");
|
||||
}
|
||||
sender.sendMessage("Failed to login!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class LogoutTask extends BukkitRunnable {
|
||||
private final ClientPlugin plugin;
|
||||
private final CommandSender sender;
|
||||
|
||||
public LogoutTask(ClientPlugin plugin, CommandSender sender) {
|
||||
this.plugin = plugin;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
private static void Logout(String ServerAddress) throws Exception {
|
||||
String Address = ServerAddress + "/session/";
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("DELETE");
|
||||
con.setDoOutput(true);
|
||||
|
||||
con.connect();
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
|
||||
if (responseCode != 204) {
|
||||
con.disconnect();
|
||||
throw new Exception("You have not logged in!");
|
||||
}
|
||||
|
||||
con.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Logout(plugin.ServerAddress);
|
||||
sender.sendMessage("Logged out successfully.");
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().equals("You have not logged in!")) {
|
||||
sender.sendMessage("You have not logged in!");
|
||||
} else {
|
||||
sender.sendMessage("Cannot connect to UnionBan server!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class PardonTask extends BukkitRunnable {
|
||||
private final String username;
|
||||
private final CommandSender sender;
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public PardonTask(ClientPlugin plugin, CommandSender sender, String username) {
|
||||
this.sender = sender;
|
||||
this.plugin = plugin;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String uuid;
|
||||
sender.sendMessage("Querying UUID of player " + username + " from Mojang server...");
|
||||
try {
|
||||
uuid = PlayerUUID.FromUsername(username);
|
||||
sender.sendMessage("UUID of player " + username + " is " + uuid + ".");
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().equals("Player does not exist!")) {
|
||||
sender.sendMessage("Player " + username + " does not exist!");
|
||||
} else {
|
||||
sender.sendMessage("Cannot connect to Mojang server!");
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
PlayerUUID.Pardon(plugin.ServerAddress, uuid);
|
||||
sender.sendMessage("Removed player " + username + " from UnionBan list successfully!");
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage().equals("Record does not exist!")) {
|
||||
sender.sendMessage("Player " + username + " is not in the UnionBan list!");
|
||||
} else if (e.getMessage().equals("Not logged in!")) {
|
||||
sender.sendMessage("You have not logged in!");
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
sender.sendMessage("Failed to remove player " + username + " from UnionBan list!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
public final class PlayerLoginListener implements Listener {
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public PlayerLoginListener(ClientPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onLogin(PlayerLoginEvent event) {
|
||||
Bukkit.getLogger().info("Player " + event.getPlayer().getName() + " is logging in!");
|
||||
new PlayerLoginTask(plugin, event.getPlayer().getName()).runTaskAsynchronously(plugin);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class PlayerLoginTask extends BukkitRunnable {
|
||||
private final String username;
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public PlayerLoginTask(ClientPlugin plugin, String username) {
|
||||
this.plugin = plugin;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String uuid = PlayerUUID.FromUsername(username);
|
||||
plugin.getLogger().info("UUID of " + username + ": " + uuid);
|
||||
boolean isBanned = PlayerUUID.IsBan(plugin.ServerAddress, uuid);
|
||||
plugin.getLogger().info("Player " + username + " is banned: " + isBanned);
|
||||
if (isBanned) {
|
||||
new KickPlayerTask(plugin, username).runTask(plugin);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Bukkit.getLogger().warning("Failed to get information about player " + username + "!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
|
||||
class MojangUserInfo {
|
||||
public String id;
|
||||
}
|
||||
|
||||
public class PlayerUUID {
|
||||
public static boolean IsBan(String ServerAddress, String uuid) throws Exception {
|
||||
String Address = ServerAddress + "/record/uuid/" + uuid;
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("GET");
|
||||
con.setDoOutput(true);
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
con.disconnect();
|
||||
|
||||
if (responseCode == 200) {
|
||||
return true;
|
||||
} else if (responseCode == 404) {
|
||||
return false;
|
||||
} else {
|
||||
throw new Exception("Failed to connect to UnionBan server!");
|
||||
}
|
||||
}
|
||||
|
||||
public static String FromUsername(String username) throws Exception {
|
||||
String Address = "https://api.mojang.com/users/profiles/minecraft/" + username;
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("GET");
|
||||
con.setDoOutput(true);
|
||||
|
||||
int responseCode = con.getResponseCode();
|
||||
if (responseCode == 204) {
|
||||
throw new Exception("Player does not exist!");
|
||||
} else if (responseCode != 200) {
|
||||
con.disconnect();
|
||||
throw new Exception("Failed to connect to Mojang API!");
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String inputLine;
|
||||
while ((inputLine = reader.readLine()) != null) {
|
||||
builder.append(inputLine);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
con.disconnect();
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
MojangUserInfo mojangUserInfo = objectMapper.readValue(builder.toString(), MojangUserInfo.class);
|
||||
|
||||
return mojangUserInfo.id.substring(0, 8) + "-" + mojangUserInfo.id.substring(8, 12) + "-" + mojangUserInfo.id.substring(12, 16) + "-" + mojangUserInfo.id.substring(16, 20) + "-" + mojangUserInfo.id.substring(20, 32);
|
||||
}
|
||||
|
||||
public static void Ban(String ServerAddress, String uuid) throws Exception {
|
||||
String Address = ServerAddress + "/record/uuid/" + uuid;
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("PUT");
|
||||
con.setDoOutput(true);
|
||||
|
||||
con.connect();
|
||||
int responseCode = con.getResponseCode();
|
||||
con.disconnect();
|
||||
|
||||
if (responseCode == 409) {
|
||||
throw new Exception("Record exists!");
|
||||
} else if (responseCode == 403) {
|
||||
throw new Exception("Not logged in!");
|
||||
} else if (responseCode != 200) {
|
||||
throw new Exception("Ban failed!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void Pardon(String ServerAddress, String uuid) throws Exception {
|
||||
String Address = ServerAddress + "/record/uuid/" + uuid;
|
||||
URL url = new URL(Address);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setReadTimeout(5000);
|
||||
con.setConnectTimeout(5000);
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestMethod("DELETE");
|
||||
con.setDoOutput(true);
|
||||
|
||||
con.connect();
|
||||
int responseCode = con.getResponseCode();
|
||||
con.disconnect();
|
||||
|
||||
if (responseCode == 404) {
|
||||
throw new Exception("Record does not exist!");
|
||||
} else if (responseCode == 403) {
|
||||
throw new Exception("Not logged in!");
|
||||
} else if (responseCode != 204) {
|
||||
throw new Exception("Pardon failed!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
|
||||
public class WhitelistListener implements Listener {
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public WhitelistListener(ClientPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
void onWhitelist(String prefix, String command) {
|
||||
if (command.startsWith(prefix) && command.length() > prefix.length() + 2 && command.charAt(prefix.length() + 1) != ' ') {
|
||||
new WhitelistTask(plugin, command.substring(prefix.length() + 1)).runTask(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void PlayerAction(PlayerCommandPreprocessEvent event) {
|
||||
String prefix = "/whitelist add";
|
||||
onWhitelist(prefix, event.getMessage());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void ServerAction(ServerCommandEvent event) {
|
||||
String prefix = "whitelist add";
|
||||
onWhitelist(prefix, event.getCommand());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package cn.cnklp.studio.UnionBanClientSpigot;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class WhitelistTask extends BukkitRunnable {
|
||||
private final String username;
|
||||
private final ClientPlugin plugin;
|
||||
|
||||
public WhitelistTask(ClientPlugin plugin, String username) {
|
||||
this.username = username;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String uuid = PlayerUUID.FromUsername(username);
|
||||
plugin.getLogger().info("UUID of " + username + ": " + uuid);
|
||||
boolean isBanned = PlayerUUID.IsBan(plugin.ServerAddress, uuid);
|
||||
plugin.getLogger().info("Player " + username + " is banned: " + isBanned);
|
||||
if (isBanned) {
|
||||
Bukkit.broadcastMessage(ChatColor.RED + "[UnionBan] Player " + username + " is in the UnionBan list!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Bukkit.getLogger().warning("Failed to get information about player " + username + "!");
|
||||
}
|
||||
}
|
||||
}
|
4
src/main/resources/config.yml
Normal file
4
src/main/resources/config.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
# Do NOT change this file manually!
|
||||
username: ""
|
||||
password: ""
|
||||
api_key: ""
|
24
src/main/resources/plugin.yml
Normal file
24
src/main/resources/plugin.yml
Normal file
|
@ -0,0 +1,24 @@
|
|||
name: UnionBan-Client
|
||||
version: "1.0"
|
||||
api-version: "1.15"
|
||||
main: cn.cnklp.studio.UnionBanClientSpigot.ClientPlugin
|
||||
commands:
|
||||
unionban:
|
||||
description: UnionBan-Client
|
||||
usage: |
|
||||
/unionban help - Show this help.
|
||||
/unionban status - Show status of UnionBan plugin and server.
|
||||
/unionban login <username> <password> - Login to UnionBan server.
|
||||
/unionban logout - Logout from UnionBan server.
|
||||
/unionban ban <player> - Add <player> to UnionBan server(requires login).
|
||||
/unionban pardon <player> - Remove <player> from UnionBan server(requires login).
|
||||
permissions:
|
||||
unionban.info:
|
||||
description: Allow user to get help and status of UnionBan plugin and server.
|
||||
default: true
|
||||
unionban.account:
|
||||
description: Allow user to login or logout.
|
||||
default: op
|
||||
unionban.ban:
|
||||
description: Allow user to add or remove player from UnionBan list.
|
||||
default: op
|
Loading…
Add table
Add a link
Reference in a new issue