From 20b2c7891baca46fcc8d6fa2fb8d0fb4d4b9dc9a Mon Sep 17 00:00:00 2001 From: Etzelia Date: Wed, 12 Sep 2018 11:01:20 -0500 Subject: [PATCH] Initial commit for Gitea --- .gitignore | 4 + pom.xml | 137 ++++++++++++++++++ .../commandlistener/CommandListener.java | 59 ++++++++ .../CommandListenerThread.java | 68 +++++++++ .../commandlistener/command/CommandMain.java | 87 +++++++++++ src/main/resources/config.yml | 15 ++ src/main/resources/plugin.yml | 14 ++ 7 files changed, 384 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/xyz/etztech/commandlistener/CommandListener.java create mode 100644 src/main/java/xyz/etztech/commandlistener/CommandListenerThread.java create mode 100644 src/main/java/xyz/etztech/commandlistener/command/CommandMain.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d13e3f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +*.iml +target/ +dependency-reduced-pom.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e53661a --- /dev/null +++ b/pom.xml @@ -0,0 +1,137 @@ + + 4.0.0 + xyz.etztech + CommandListener + + 1.4 + jar + + + + CommandListener + A plugin that listens on a designated socket for command input. + http://www.etztech.xyz + + + + Zlib License + http://opensource.org/licenses/Zlib + Copyright (c) 2017 EtzTech + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + + + + + + EtzTech + http://www.etztech.xyz + + + + + + EtzTech + xyz.etztech.commandlistener.CommandListener + UTF-8 + + + + + org.spigotmc + spigot-api + 1.13.1-R0.1-SNAPSHOT + + + commons-lang + commons-lang + 2.6 + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + mvn-repo + https://mvnrepository.com/artifact/ + + + jcenter + http://jcenter.bintray.com + + + jitpack.io + https://jitpack.io + + + + + src/main/java + clean install + + + src/main/resources + + true + + plugin.yml + config.yml + + + + src/main/resources + + false + + **/*.java + plugin.yml + + + + + + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + lib/ + xyz.etztech.commandlistener.CommandListener + + + + + + + \ No newline at end of file diff --git a/src/main/java/xyz/etztech/commandlistener/CommandListener.java b/src/main/java/xyz/etztech/commandlistener/CommandListener.java new file mode 100644 index 0000000..e4391f7 --- /dev/null +++ b/src/main/java/xyz/etztech/commandlistener/CommandListener.java @@ -0,0 +1,59 @@ +package xyz.etztech.commandlistener; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.java.JavaPlugin; +import xyz.etztech.commandlistener.command.CommandMain; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +public class CommandListener extends JavaPlugin { + + private static CommandListener instance; + + public static FileConfiguration config; + private Logger log = Logger.getLogger( "Minecraft" ); + private List hookCommands = new ArrayList<>(); + + + + @Override + public void onEnable() { + + instance = this; + saveDefaultConfig(); + loadConfig(); + + if (isEnabled()) { + CommandMain cmdMain = new CommandMain(this); + this.getCommand("commandlistener").setExecutor(cmdMain); + cmdMain.startThread(); + Bukkit.getConsoleSender().sendMessage("Command Listener has started successfully."); + } + } + + @Override + public void onDisable() { + + } + + public void loadConfig() { + config = Bukkit.getPluginManager().getPlugin("CommandListener").getConfig(); + } + + @Override + public void reloadConfig() { + super.reloadConfig(); + loadConfig(); + } + + + + public void log(String message) { + log.info( "[CommandListener]: " + message ); + } + +} + diff --git a/src/main/java/xyz/etztech/commandlistener/CommandListenerThread.java b/src/main/java/xyz/etztech/commandlistener/CommandListenerThread.java new file mode 100644 index 0000000..368d517 --- /dev/null +++ b/src/main/java/xyz/etztech/commandlistener/CommandListenerThread.java @@ -0,0 +1,68 @@ +package xyz.etztech.commandlistener; + +import org.bukkit.Bukkit; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CommandListenerThread extends Thread { + private boolean running = true; + + private CommandListener plugin; + + public CommandListenerThread(CommandListener plugin) {this.plugin = plugin;} + + public boolean getRunning() { + return running; + } + + public void setRunning(boolean running) { + this.running = running; + } + + + public void run() { + List commands = this.plugin.getConfig().getStringList("commands"); + try { + ServerSocket cmdSock = new ServerSocket(this.plugin.getConfig().getInt("port"), 0, InetAddress.getByName(null)); + while (getRunning()) { + Socket data = cmdSock.accept(); + BufferedReader myInput = new BufferedReader(new InputStreamReader(data.getInputStream())); + + String buf = myInput.readLine(); + + if (buf != null) { + String[] input = buf.split(" "); + String base = input[0]; + ArrayList args = new ArrayList<>(); + if (input.length > 1) { + args = new ArrayList<>(Arrays.asList(buf.split(" "))); + args.remove(0); + } + if (this.plugin.getConfig().getBoolean("verbose")) { + Bukkit.getConsoleSender().sendMessage("Received input '" + buf + "'"); + Bukkit.getConsoleSender().sendMessage("Base Command '" + base + "'"); + Bukkit.getConsoleSender().sendMessage("Arguments '" + args.toString() + "'"); + } + if (commands.contains(base)) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), buf); + } else if (this.plugin.getConfig().getBoolean("verbose")){ + Bukkit.getConsoleSender().sendMessage("Input not recognized, ignoring."); + } + } + } + Bukkit.getConsoleSender().sendMessage("Closing CommandListener on Port " + this.plugin.getConfig().get("port")); + cmdSock.close(); + } catch (Exception ex) { + Bukkit.getConsoleSender().sendMessage("Error: " + ex.getMessage()); + } + } + + +} diff --git a/src/main/java/xyz/etztech/commandlistener/command/CommandMain.java b/src/main/java/xyz/etztech/commandlistener/command/CommandMain.java new file mode 100644 index 0000000..fea45bf --- /dev/null +++ b/src/main/java/xyz/etztech/commandlistener/command/CommandMain.java @@ -0,0 +1,87 @@ +package xyz.etztech.commandlistener.command; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import xyz.etztech.commandlistener.CommandListener; +import xyz.etztech.commandlistener.CommandListenerThread; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; + +public class CommandMain implements CommandExecutor { + + CommandListener plugin; + + private CommandListenerThread cmdThread; + + public CommandMain(CommandListener plugin) { + this.plugin = plugin; + this.cmdThread = new CommandListenerThread(plugin); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String base, String[] args) { + if (sender.hasPermission("commandlistener.use")) { + if (args.length == 0) { + String version = Bukkit.getPluginManager().getPlugin("CommandListener").getDescription().getVersion(); + sender.sendMessage(ChatColor.GOLD + "----- CommandListener v" + version + " -----"); + sender.sendMessage(ChatColor.YELLOW + "Developed by EtzTech for 24CarrotCraft"); + } else if (args.length == 1) { + switch (args[0]) { + case "help": + sender.sendMessage(ChatColor.GOLD + "----- CommandListener Commands -----"); + sender.sendMessage(ChatColor.YELLOW + "/cl reload - Reload the config"); + sender.sendMessage(ChatColor.YELLOW + "/cl port - Display the port CommandListener is on"); + break; + case "port": + sender.sendMessage("CommandListener is listening on port " + plugin.getConfig().getInt("port")); + break; + case "reload": + try { + stopThread(); + Thread.sleep(1000); + this.plugin.reloadConfig(); + Thread.sleep(1000); + startThread(); + sender.sendMessage("CommandListener reloaded."); + } catch (Exception ex) { + sender.sendMessage("CommandListener reload was interrupted."); + } + break; + } + } else { + return false; + } + } else { + sender.sendMessage(ChatColor.RED + "You do not have permission to use CommandListener."); + return false; + } + + return true; + } + + public void startThread() { + Bukkit.getConsoleSender().sendMessage("Starting Command Listener on Port " + plugin.getConfig().get("port")); + cmdThread.setRunning(true); + Thread th = new Thread(cmdThread); + th.start(); + } + + public void stopThread() { + cmdThread.setRunning(false); + try { + Socket trashSock = new Socket("127.0.0.1", plugin.getConfig().getInt("port")); + DataOutputStream trashStream = new DataOutputStream(trashSock.getOutputStream()); + trashStream.writeUTF("trash"); + trashStream.flush(); + trashStream.close(); + } catch (IOException ex) { + Bukkit.getConsoleSender().sendMessage("Error: " + ex.getMessage()); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..35055a7 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,15 @@ +# The port to listen for commands on +port: 8888 + +# A list of commands that can be used with CommandListner +# Any command sent to the above port that is not in this list will be discarded +commands: +- commandlistener +- cl + +hooks: + carrotchat: false + +# Whether to print extra to the console +# Mostly useful for debugging +verbose: false diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..33bf724 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,14 @@ +name: ${name} +version: ${version} +description: ${description} +author: ${author} +website: ${url} +main: ${mainClass} +softdepend: [CarrotChat] +commands: + commandlistener: + aliases: [cl] +permissions: + commandlistener.use: + description: Allows use of CommandListener commands + default: op \ No newline at end of file