· 6 years ago · Aug 19, 2019, 05:28 PM
1package me.tylercreator.reportino;
2
3import me.tylercreator.reportino.Report.RemovePlayers;
4import me.tylercreator.reportino.Report.ReportAutoban;
5import org.bukkit.Bukkit;
6import org.bukkit.ChatColor;
7import org.bukkit.command.Command;
8import org.bukkit.command.CommandSender;
9import org.bukkit.configuration.file.FileConfiguration;
10import org.bukkit.entity.Player;
11import org.bukkit.event.Listener;
12import org.bukkit.plugin.java.JavaPlugin;
13
14import java.sql.Connection;
15import java.sql.DriverManager;
16import java.sql.SQLException;
17import java.sql.Statement;
18import java.util.ArrayList;
19
20public class Main extends JavaPlugin implements Listener {
21
22 public static ArrayList<Player> onlineplayers = new ArrayList<Player>();
23
24
25 public static final String RESET = "\u001B[0m"; //codes for reset System colors
26
27 public static final String RED = "\u001B[31m"; // red text .
28
29 public static final String YELLOW_BACKGROUND = "\u001B[43m"; //yellow background!
30
31 public static Main plugin;
32
33 private Connection connection;
34
35 private String host, database, username, password;
36
37 private int port;
38
39 @Override
40 public void onEnable() {
41
42 System.out.print(YELLOW_BACKGROUND + "The plugin has been enabled correctly." + RESET);
43
44 plugin = this;
45
46
47 FileConfiguration config = Main.plugin.getConfig();
48
49 getConfig().options().header(
50 "REPORTINO PLUGIN" +
51 "Plugin by TylerCreator" +
52 "Version= 1.0.0" +
53 "A simple plugin to make/send reports" +
54 "/----------------------------/"
55 );
56
57
58 getConfig().addDefault("autoBan", false); //autoban after X reports, this is risky.
59
60 getConfig().addDefault("BanForReports-min-value", 50); //the minimum reports needed for autoban feature
61
62 getConfig().addDefault("enableMySQL-db", true);
63
64 getConfig().addDefault("MySQL-hostname", "localhost");
65
66 getConfig().addDefault("MySQL-port", 3306);
67
68 getConfig().addDefault("MySQL-database", "root");
69
70 getConfig().addDefault("MySQL-username", "root");
71
72 getConfig().addDefault("MySQL-password", "admin");
73
74 getConfig().options().copyHeader();
75
76 getConfig().options().copyDefaults();
77
78 saveConfig();
79
80 reloadConfig();
81
82 host = config.getString("MySQL-hostname");
83
84 port = config.getInt("MySQL-port");
85
86 database = config.getString("MySQL-database");
87
88 username = config.getString("MySQL-username");
89
90 password = config.getString("MySQL-password");
91
92 if (config.getBoolean("enableMySQL-db")) {
93
94 try {
95 openConnection();
96 Statement statement = connection.createStatement();
97 } catch (ClassNotFoundException e) {
98 e.printStackTrace();
99 } catch (SQLException e) {
100 e.printStackTrace();
101 }
102
103
104 }
105
106 Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new ReportAutoban() {
107 }, 20, 60);
108
109 getServer().getPluginManager().registerEvents(new RemovePlayers(), this);
110
111
112 }
113
114 @Override
115 public void onDisable() {
116
117 System.out.print(YELLOW_BACKGROUND + "Plugin has been disabled correctly." + RESET);
118
119
120 }
121
122 public void openConnection() throws SQLException, ClassNotFoundException {
123
124 if (getConfig().getBoolean("enableMySQL-db")) {
125
126 if (connection != null && !connection.isClosed()) {
127 return;
128 }
129
130 synchronized (this) {
131 if (connection != null && !connection.isClosed()) {
132 return;
133 }
134 Class.forName("com.mysql.jdbc.Driver");
135
136 connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
137 }
138
139
140 }
141 }
142
143 private void createDatabase() {
144
145 if (getConfig().getBoolean("enableMySQL-db")) {
146
147 try {
148 Class.forName("org.sqlite.JDBC");
149
150 connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
151
152 connection.createStatement();
153
154 String database = "CREATE DATABASE IF NOT EXISTS reportino";
155
156 System.out.println("Database created succesfully");
157
158 } catch (ClassNotFoundException | SQLException e) {
159
160 e.printStackTrace();
161 }
162
163
164 }
165 }
166
167 private void createTable() {
168
169 if (getConfig().getBoolean("enableMySQL-db")) {
170
171 try {
172 String table = "CREATE TABLE IF NOT EXISTS data (" + "playername TINYTEXT( 16 ) NOT NULL," + "reason TINYTEXT( 120 ) " + ")";
173 Class.forName("org.sqlite.JDBC");
174 connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
175 connection.createStatement();
176 connection.createStatement().execute(table);
177 System.out.println("Tables created succesfully");
178
179
180 } catch (ClassNotFoundException | SQLException e) {
181 e.printStackTrace();
182
183 }
184
185
186 }
187 }
188
189
190 public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
191
192 if (cmd.getName().equalsIgnoreCase("report")) {
193
194 if (args.length == 0) {
195 sender.sendMessage(ChatColor.YELLOW + "[Report]: " + ChatColor.GRAY + "Use /report <name> <reason> to make a report!");
196 return true;
197
198 }
199
200 if (args.length >= 1) {
201
202 Player target = Bukkit.getPlayer(args[0]);
203
204 if (target == null) {
205
206 sender.sendMessage(ChatColor.RED + "The player you're reporting is not online!");
207
208 } else if (!args[1].isEmpty()) {
209
210 String report = "INSERT INTO data (player, reason) VALUES (" + target.getDisplayName() + "," + args[1] + ")";
211
212
213 sender.sendMessage(ChatColor.RED + "The report has been succesfully sent.");
214
215 } else {
216
217 sender.sendMessage(ChatColor.RED + "You haven't inserted a reason for the report.");
218
219 }
220
221
222 }
223
224 }
225
226
227 return true;
228 }
229
230
231}