· 4 years ago · Jul 03, 2021, 09:36 PM
1package me.mine.cscmd;
2
3import org.bukkit.Bukkit;
4import org.bukkit.command.Command;
5import org.bukkit.command.CommandSender;
6import org.bukkit.entity.Player;
7import org.bukkit.plugin.java.JavaPlugin;
8
9import java.sql.Connection;
10import java.sql.DriverManager;
11import java.sql.ResultSet;
12import java.sql.Statement;
13
14public final class CsCMD extends JavaPlugin {
15 @Override
16 public void onEnable() {
17
18 String if_config = this.getConfig().getString("config_version");
19
20 if(if_config == null) {
21
22 this.getConfig().set("config_version", "1.0");
23 this.getConfig().set("prefix", "&6&lServer &8&l>> &f");
24 this.getConfig().set("error_message", "An internal error has ocured!");
25 this.getConfig().set("command_is_not_complete_message", "Error! Command is not complete! Use /cscmd help");
26 this.getConfig().set("succes_mess", "Command successfully send!");
27 this.getConfig().set("db_host", "localhost");
28 this.getConfig().set("db_user", "user");
29 this.getConfig().set("db_database", "database");
30 this.getConfig().set("db_password", "secret password");
31 this.getConfig().set("server", "Enter specific server name (like survival, lobby)");
32 this.getConfig().set("admin_perm", "cscmd.admin");
33 this.getConfig().set("perm_mess", "You are not allowed to do this!");
34 this.getConfig().set("help_mess", "Usage: /cscmd <server> <command>");
35 this.getConfig().set("reload_mess", "Plugin successfully reloaded!");
36 this.getConfig().set("refresh_time", 1000);
37
38 this.saveConfig();
39 }
40
41 String db_host = this.getConfig().getString("db_host");
42 String db_user = this.getConfig().getString("db_user");
43 String db_database = this.getConfig().getString("db_database");
44 String db_password = this.getConfig().getString("db_password");
45 String error_mess = this.getConfig().getString("error_message").replace("&", "§");
46 String prefix = this.getConfig().getString("prefix").replace("&", "§");
47 String server = this.getConfig().getString("server");
48 Integer refresh = this.getConfig().getInt("refresh_time");
49
50
51 try {
52 String myDriver = "com.mysql.jdbc.Driver";
53 String myUrl = "jdbc:mysql://" + db_host + "/" + db_database + "";
54 Class.forName(myDriver);
55 Connection conn = DriverManager.getConnection(myUrl, "" + db_user + "", "" + db_password + "");
56 Statement st = conn.createStatement();
57
58
59 try {
60 st.executeUpdate(
61 "CREATE TABLE IF NOT EXISTS `cscmd` (" +
62 " `id` int(50) NOT NULL AUTO_INCREMENT," +
63 " `server` varchar(255) DEFAULT NULL," +
64 " `command` varchar(255) DEFAULT NULL," +
65 " PRIMARY KEY (`id`)" +
66 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
67 );
68
69 } catch (Exception ex) {
70 System.out.print(prefix + error_mess + ex);
71 }
72
73 System.out.println("CsCMD > Loaded successfully");
74
75 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
76 public void run() {
77 try {
78
79 String query = "SELECT * FROM cscmd WHERE server='" + server + "'";
80
81 ResultSet rs = st.executeQuery(query);
82
83 while (rs.next()) {
84 String cscmd = rs.getString("command");
85 Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), cscmd);
86 System.out.print("Command " + cscmd + " successfully executed!");
87 }
88 query = "DELETE FROM cscmd WHERE server='" + server + "'";
89 st.executeUpdate(query);
90
91
92 } catch (Exception ex) {
93 System.out.print(error_mess + ex);
94 }
95 }
96 }, 0, refresh);
97 }
98 catch (Exception e){
99 System.out.print("Error: " + e);
100 }
101 }
102
103 @Override
104 public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
105 if (command.getName().equals("cscmd")){
106
107 String db_host = this.getConfig().getString("db_host");
108 String db_user = this.getConfig().getString("db_user");
109 String db_database = this.getConfig().getString("db_database");
110 String db_password = this.getConfig().getString("db_password");
111 String error_mess = this.getConfig().getString("error_message").replace("&", "§");
112 String args_not_set_mess = this.getConfig().getString("command_is_not_complete_message").replace("&", "§");
113 String prefix = this.getConfig().getString("prefix").replace("&", "§");
114 String success_mess = this.getConfig().getString("succes_mess").replace("&", "§");
115 String admin_perm = this.getConfig().getString("admin_perm");
116 String perm_mess = this.getConfig().getString("perm_mess");
117 String help = this.getConfig().getString("help_mess");
118
119
120 if (sender instanceof Player) {
121 Player player = (Player) sender;
122 if (player.hasPermission(admin_perm)) {
123 if (args.length > 0) {
124 if (args[0] != "info" || args[0] != "help") {
125 if (args.length > 1) {
126 String arg_dva = "";
127 for (int i = 1; i < args.length; i++) {
128 arg_dva = arg_dva + args[i] + " ";
129 }
130 if (player instanceof Player) {
131 player.sendMessage(prefix + success_mess);
132 }
133 try {
134 String myDriver = "com.mysql.jdbc.Driver";
135 String myUrl = "jdbc:mysql://" + db_host + "/" + db_database + "";
136 Class.forName(myDriver);
137 Connection conn = DriverManager.getConnection(myUrl, "" + db_user + "", "" + db_password + "");
138
139 Statement st = conn.createStatement();
140
141 st.executeUpdate("INSERT INTO cscmd (server, command) VALUES ('" + args[0] + "', '" + arg_dva + "')");
142
143 st.close();
144 } catch (Exception ex) {
145 System.out.print(prefix + error_mess + ex);
146 }
147 } else {
148 if (player instanceof Player) {
149 player.sendMessage(prefix + args_not_set_mess);
150 }
151 }
152 } else if (args[0] == "info" || args[0] == "help") {
153 player.sendMessage(prefix + help);
154 }
155 } else {
156 if (player instanceof Player) {
157 player.sendMessage(prefix + help);
158 }
159 }
160 }
161 else{
162 player.sendMessage(prefix + perm_mess);
163 }
164 }
165 else{
166 if (args.length > 0) {
167 if (args[0] != "info" || args[0] != "help") {
168 if (args.length > 1) {
169 String arg_dva = "";
170 for (int i = 1; i < args.length; i++) {
171 arg_dva = arg_dva + args[i] + " ";
172 }
173 try {
174 String myDriver = "com.mysql.jdbc.Driver";
175 String myUrl = "jdbc:mysql://" + db_host + "/" + db_database + "";
176 Class.forName(myDriver);
177 Connection conn = DriverManager.getConnection(myUrl, "" + db_user + "", "" + db_password + "");
178 Statement st = conn.createStatement();
179 st.executeUpdate("INSERT INTO cscmd (server, command) VALUES ('" + args[0] + "', '" + arg_dva + "')");
180 st.close();
181 } catch (Exception ex) {
182 System.out.print(prefix + error_mess + ex);
183 }
184 }
185 }
186 }
187 }
188 }
189 return false;
190 }
191}
192