-
Notifications
You must be signed in to change notification settings - Fork 4
/
PunishmentsX.java
195 lines (157 loc) · 6.78 KB
/
PunishmentsX.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package io.github.punishmentsx;
import io.github.punishmentsx.commands.BaseCommand;
import io.github.punishmentsx.commands.impl.*;
import io.github.punishmentsx.database.Database;
import io.github.punishmentsx.database.mongo.Mongo;
import io.github.punishmentsx.database.sequel.SQL;
import io.github.punishmentsx.database.redis.PunishRedisMessageListener;
import io.github.punishmentsx.database.redis.RedisPublisher;
import io.github.punishmentsx.database.redis.RedisSubscriber;
import io.github.punishmentsx.evasion.EvasionListener;
import io.github.punishmentsx.filter.Filter;
import io.github.punishmentsx.listeners.ChatListener;
import io.github.punishmentsx.listeners.JoinListener;
import io.github.punishmentsx.listeners.QuitListener;
import io.github.punishmentsx.profiles.ProfileManager;
import io.github.punishmentsx.punishments.PunishmentManager;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import redis.clients.jedis.Jedis;
import xyz.leuo.gooey.Gooey;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.logging.Level;
public class PunishmentsX extends JavaPlugin {
private CommandMap commandMap;
@Getter private RedisPublisher redisPublisher;
@Getter private RedisSubscriber redisSubscriber;
@Deprecated @Getter private Mongo mongo;
public Gooey gooey;
@Getter private ProfileManager profileManager;
@Getter private PunishmentManager punishmentManager;
@Getter private Filter filter;
private PunishRedisMessageListener punishRedisMessageListener;
@Getter private Database storage;
@Getter private YamlConfiguration messagesFile;
@Override
public void onEnable() {
this.saveDefaultConfig();
switch (getConfig().getString("DATABASE.USE").toLowerCase()) {
case "mongo":
mongo = new Mongo(this);
storage = mongo;
break;
case "mysql":
storage = new SQL(this, Database.Type.MySQL);
break;
case "sqlite":
storage = new SQL(this, Database.Type.SQLite);
break;
default:
getLogger().log(Level.SEVERE, "YOU MUST SELECT EITHER MONGO, MYSQL, OR SQLITE IN THE CONFIG!");
onDisable();
break;
}
//Creates & Loads messages file.
createMessages();
reloadMessages();
// Redis
if (getConfig().getBoolean("DATABASE.REDIS.ENABLED")) {
redisPublisher = new RedisPublisher(new Jedis(getConfig().getString("DATABASE.REDIS.HOST"), getConfig().getInt("DATABASE.REDIS.PORT")), this);
redisSubscriber = new RedisSubscriber(new Jedis(getConfig().getString("DATABASE.REDIS.HOST"), getConfig().getInt("DATABASE.REDIS.PORT")), this);
}
this.gooey = new Gooey(this);
// Managers
this.profileManager = new ProfileManager(this);
this.punishmentManager = new PunishmentManager(this);
if (getConfig().getBoolean("FILTER.ENABLED")) {
this.filter = new Filter(this);
}
// Registering Commandmap
try {
Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
commandMap = (CommandMap) commandMapField.get(Bukkit.getServer());
} catch (Exception e) {
e.printStackTrace();
}
// Listeners
new ChatListener(this);
new JoinListener(this);
new QuitListener(this);
// Commands
registerCommand(new ReloadCommand(this, "pxreload"));
registerCommand(new HistoryCommand(this, "history"));
registerCommand(new PunishCommands(this, "punishments"));
registerCommand(new PunishCommand(this, "punish"));
registerCommand(new TempPunishCommands(this, "temppunishments"));
registerCommand(new UnpunishCommands(this, "unpunishments"));
registerCommand(new CmdPunishCommand(this, "cmdpunish"));
if (getConfig().getBoolean("ANTI_EVASION.ENABLED")) {
if (storage instanceof Mongo) {
registerListener(new EvasionListener(this));
registerCommand(new WhyBannedCommand(this, "whybanned"));
registerCommand(new ExemptCommand(this, "exempt"));
registerCommand(new UnexemptCommand(this, "unexempt"));
} else {
getLogger().log(Level.WARNING, "PunishmentsX did not enable anti evasion because it requires MongoDB!");
getLogger().log(Level.WARNING, "Anti evasion will support MySQL and SQLite in a newer update!");
}
}
if (getConfig().getBoolean("DATABASE.REDIS.ENABLED")) {
this.punishRedisMessageListener = new PunishRedisMessageListener(this);
}
}
@Override
public void onDisable() {
profileManager.shutdown();
if (getConfig().getBoolean("DATABASE.REDIS.ENABLED")) {
punishRedisMessageListener.close();
}
storage.close();
}
private void createMessages() {
try {
File dataFolder = getDataFolder();
String file = dataFolder.toPath().toString() + "/messages.yml";
File messagesFile = new File(file);
if (!messagesFile.exists()) {
String[] files = file.split("/");
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(files[files.length - 1]);
File parentFile = messagesFile.getParentFile();
if (parentFile != null) parentFile.mkdirs();
if (inputStream != null) {
Files.copy(inputStream, messagesFile.toPath());
} else {
messagesFile.createNewFile();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void reloadMessages() {
final File dataFolder = getDataFolder();
final File file = new File(dataFolder.toPath() + "/messages.yml");
if (file.exists())
messagesFile = YamlConfiguration.loadConfiguration(file);
else messagesFile = new YamlConfiguration();
}
public void registerCommand(BaseCommand command) {
commandMap.register(command.getName(), command);
}
public void registerListener(Listener listener) {
getServer().getPluginManager().registerEvents(listener, this);
}
public void unregisterListener(Listener listener) {
HandlerList.unregisterAll(listener);
}
}