· 4 years ago · Jun 12, 2021, 09:46 PM
1package com.atlasplugins.factions;
2
3import java.io.File;
4import java.nio.charset.Charset;
5import java.sql.Connection;
6import java.sql.SQLException;
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.Map.Entry;
12
13import org.bukkit.Bukkit;
14import org.bukkit.entity.EntityType;
15import org.bukkit.plugin.PluginManager;
16import org.bukkit.plugin.RegisteredServiceProvider;
17import org.bukkit.plugin.java.JavaPlugin;
18import org.bukkit.scheduler.BukkitRunnable;
19
20import com.atlasplugins.factions.atlasfactions.atlasmysql.AtlasStorage;
21import com.atlasplugins.factions.atlasfactions.atlasmysql.MySQL;
22import com.atlasplugins.factions.atlasfactions.commands.ChatFaction;
23import com.atlasplugins.factions.atlasfactions.commands.ChatFactionAlly;
24import com.atlasplugins.factions.atlasfactions.commands.CommandF;
25import com.atlasplugins.factions.atlasfactions.commands.CommandRank;
26import com.atlasplugins.factions.atlasfactions.commands.SlimeChunkCommand;
27import com.atlasplugins.factions.atlasfactions.factionremover.FactionRemoverTask;
28import com.atlasplugins.factions.atlasfactions.fbatalha.BatalhasManager;
29import com.atlasplugins.factions.atlasfactions.fbatalha.listeners.BatalhaListener;
30import com.atlasplugins.factions.atlasfactions.fgeradores.FGeradoresCore;
31import com.atlasplugins.factions.atlasfactions.hooks.Cash;
32import com.atlasplugins.factions.atlasfactions.hooks.DefaultCash;
33import com.atlasplugins.factions.atlasfactions.hooks.PlaceHolder;
34import com.atlasplugins.factions.atlasfactions.hooks.SpawnersAPI;
35import com.atlasplugins.factions.atlasfactions.hooks.UnknownCash;
36import com.atlasplugins.factions.atlasfactions.listeners.AsyncMinecraftChatCreation;
37import com.atlasplugins.factions.atlasfactions.listeners.AtlasFactionMenu;
38import com.atlasplugins.factions.atlasfactions.listeners.AtlasMembrosManagerEvent;
39import com.atlasplugins.factions.atlasfactions.listeners.AtlasPVPManager;
40import com.atlasplugins.factions.atlasfactions.listeners.AtlasPermissionManager;
41import com.atlasplugins.factions.atlasfactions.listeners.AtlasPoderManager;
42import com.atlasplugins.factions.atlasfactions.listeners.AtlasRegionUpdater;
43import com.atlasplugins.factions.atlasfactions.listeners.AtlasResetKDRManager;
44import com.atlasplugins.factions.atlasfactions.listeners.AtlasSobAtaque;
45import com.atlasplugins.factions.atlasfactions.listeners.AtlasUserSetupEvent;
46import com.atlasplugins.factions.atlasfactions.listeners.FMap;
47import com.atlasplugins.factions.atlasfactions.listeners.Fixes;
48import com.atlasplugins.factions.atlasfactions.listeners.GeneralEventManager;
49import com.atlasplugins.factions.atlasfactions.listeners.LegendChatCreation;
50import com.atlasplugins.factions.atlasfactions.listeners.RelacaoEvent;
51import com.atlasplugins.factions.atlasfactions.listeners.TimeOutConfirmation;
52import com.atlasplugins.factions.atlasfactions.models.database.AtlasUser;
53import com.atlasplugins.factions.atlasfactions.models.database.CheckMapa;
54import com.atlasplugins.factions.atlasfactions.models.database.Creation;
55import com.atlasplugins.factions.atlasfactions.models.database.Faction;
56import com.atlasplugins.factions.atlasfactions.models.database.Terreno;
57import com.atlasplugins.factions.atlasfactions.rankingposition.DeathListener;
58import com.atlasplugins.factions.atlasfactions.rankingposition.SQL;
59import com.atlasplugins.factions.atlasfactions.runnables.CoinsRanking;
60import com.atlasplugins.factions.atlasfactions.runnables.GeneralRanking;
61import com.atlasplugins.factions.atlasfactions.runnables.KDRRanking;
62import com.atlasplugins.factions.atlasfactions.runnables.OnlineFactions;
63import com.atlasplugins.factions.atlasfactions.runnables.PoderRanking;
64import com.atlasplugins.factions.atlasfactions.runnables.PowerRecover;
65import com.atlasplugins.factions.atlasfactions.runnables.SpawnersRanking;
66import com.atlasplugins.factions.atlasfactions.scoreboard.ScoreBoard;
67import com.atlasplugins.factions.atlasfactions.scoreboard.ScoreUpdater;
68import com.atlasplugins.factions.atlasfactions.scoreboard.mcMMOScoreBoard;
69import com.atlasplugins.factions.atlasfactions.setspawn.CommandFSetSpawn;
70import com.atlasplugins.factions.atlasfactions.setspawn.SetSpawnListener;
71import com.google.common.io.Resources;
72import com.sk89q.worldedit.bukkit.WorldEditPlugin;
73
74import net.milkbowl.vault.chat.Chat;
75import net.milkbowl.vault.economy.Economy;
76
77public class Main extends JavaPlugin {
78
79 private static final PluginManager manager = Bukkit.getPluginManager();
80
81 public static Connection con;
82 public static String tabela;
83 public static Cash cash;
84 public static Faction firstFaction;
85 public static WorldEditPlugin worldEditPlugin = null;
86 public static MySQL connection;
87 public static Map<String, Creation> creation_players = new HashMap<String, Creation>();
88 public static Map<EntityType, Integer> creatures_registreds = new HashMap<EntityType, Integer>();
89 public static Map<String, Faction> cachedFactions = new HashMap<String, Faction>();
90 public static Map<String, AtlasUser> cachedUsers = new HashMap<String, AtlasUser>();
91 public static List<String> onlineFactions = new ArrayList<String>();
92 public static Map<String, Terreno> claims = new HashMap<String, Terreno>();
93 private static Economy econ = null;
94 public static ArrayList<String> flying = new ArrayList<String>();
95 public static Main instance;
96 private static SpawnersAPI spawnersAPI;
97 private static Chat chat = null;
98
99 public void onEnable() {
100 Bukkit.getConsoleSender().sendMessage("§a[AtlasFactions] Inicializando");
101 if (manager.getPlugin("PlaceholderAPI") == null) {
102 Bukkit.getConsoleSender().sendMessage("§c[AtlasFactions] Erro ao registrar o PlaceHolderAPI.");
103 Bukkit.getConsoleSender().sendMessage(
104 "§c[AtlasFactions] Fa§a download em: https://www.spigotmc.org/resources/placeholderapi.6245/");
105 super.onDisable();
106 }
107 if (!setupEconomy()) {
108 Bukkit.getConsoleSender().sendMessage("§c[AtlasFactions] Erro ao registrar o Vault.");
109 Bukkit.getConsoleSender()
110 .sendMessage("§c[AtlasFactions] Fa§a download em: https://dev.bukkit.org/projects/vault");
111 getServer().getPluginManager().disablePlugin(this);
112 return;
113 }
114 saveDefaultConfig();
115 try {
116 File file = new File(getDataFolder() + File.separator, "config.yml");
117 String allText = Resources.toString(file.toURI().toURL(), Charset.forName("UTF-8"));
118 getConfig().loadFromString(allText);
119 } catch (Exception e) {
120 e.printStackTrace();
121 }
122 (Main.instance = this).saveDefaultConfig();
123 manager.registerEvents(new SetSpawnListener(this), this);
124 manager.registerEvents(new AtlasUserSetupEvent(), this);
125 manager.registerEvents(new CommandFSetSpawn(), this);
126 manager.registerEvents(new AtlasFactionMenu(), this);
127 manager.registerEvents(new AtlasRegionUpdater(), this);
128 manager.registerEvents(new FMap(), this);
129 manager.registerEvents(new RelacaoEvent(), this);
130 manager.registerEvents(new AtlasMembrosManagerEvent(), this);
131 manager.registerEvents(new AtlasPoderManager(), this);
132 manager.registerEvents(new AtlasResetKDRManager(), this);
133 manager.registerEvents(new AtlasPermissionManager(), this);
134 manager.registerEvents(new AtlasPVPManager(), this);
135 manager.registerEvents(new GeneralEventManager(), this);
136 manager.registerEvents(new TimeOutConfirmation(), this);
137 manager.registerEvents(new Fixes(), this);
138 manager.registerEvents(new mcMMOScoreBoard(), this);
139 manager.registerEvents(new SlimeChunkCommand(), this);
140 manager.registerEvents(new AtlasSobAtaque(), this);
141 manager.registerEvents(new BatalhaListener(), this);
142 manager.registerEvents(new ScoreBoard(), this);
143 manager.registerEvents(new DeathListener(), this);
144 getCommand("f").setExecutor(new CommandF());
145 getCommand("c").setExecutor(new ChatFaction());
146 getCommand("ranking").setExecutor(new CommandRank());
147 getCommand("a").setExecutor(new ChatFactionAlly());
148 getCommand("slimechunk").setExecutor(new SlimeChunkCommand());
149 BatalhasManager.loadArenas();
150 BatalhasManager.setFilasActive(Main.getInstance().getConfig().getBoolean("FilasDeBatalha"));
151 connection = new MySQL(this, this.getConfig().getString("Mysql.Host").replace(":3306", ""),
152 this.getConfig().getString("Mysql.Usuario"), this.getConfig().getString("Mysql.Senha"),
153 this.getConfig().getString("Mysql.Database"), (this.getConfig().isSet("Mysql.Port") ? this.getConfig().getInt("Mysql.Port") : 3306));
154 connection.addTableStatement(
155 "CREATE TABLE IF NOT EXISTS `factions` ( `id` INT NOT NULL AUTO_INCREMENT , `faction` VARCHAR(50) NOT NULL , `json` TEXT NOT NULL , PRIMARY KEY (`id`));", "factions");
156 connection.addTableStatement(
157 "CREATE TABLE IF NOT EXISTS `fgeradores` ( `id` INT NOT NULL AUTO_INCREMENT , `faccao` VARCHAR(50) NOT NULL , `json` TEXT NOT NULL , PRIMARY KEY (`id`));", "fgeradores");
158 connection.addTableStatement(
159 "CREATE TABLE IF NOT EXISTS `atlasuser` ( `id` INT NOT NULL AUTO_INCREMENT , `jogador` VARCHAR(50) NOT NULL , `json` TEXT NOT NULL , PRIMARY KEY (`id`));", "atlasuser");
160 connection.openConnection();
161 SQL.setup();
162 tabela = "rank";
163 new FGeradoresCore(this);
164 new CheckMapa();
165 PowerRecover.run();
166 loadScoreTask();
167 // createFactions();
168 loadDependencies();
169 for (Faction f : AtlasStorage.getAllFactions(false)) {
170 if(f != null) {
171 if (f.getClaim_save().size() > 0) {
172 for (Entry<String, Terreno> s : f.getClaim_save().entrySet()) {
173 String a = s.getKey();
174 Terreno t = s.getValue();
175 claims.put(a, t);
176 }
177 new BukkitRunnable() {
178
179 @Override
180 public void run() {
181 for (Entry<String, Terreno> s : claims.entrySet()) {
182 String a = s.getKey();
183 Terreno t = s.getValue();
184 if (t.isSobataque()) {
185 f.setFactionSobAtaque(true);
186 } else if (t.isTemporario()) {
187 new BukkitRunnable() {
188 @Override
189 public void run() {
190 if (Main.cachedFactions.containsKey(f.getFactionName())) {
191 Main.claims.remove(a);
192 f.getTerras().remove(a);
193 } else {
194 Main.claims.remove(a);
195 this.cancel();
196 }
197 }
198 }.runTaskLaterAsynchronously(Main.getPlugin(Main.class), 20L * 60 * 30);
199 }
200 }
201 }
202 }.runTaskLater(Main.getPlugin(Main.class), 20L);
203 }
204 if(f.getMaxMembers() == 0) {
205 f.setMaxMembers(15);
206 f.save(false);
207 }
208 cachedFactions.put(f.getFactionName(), f);
209 }
210 }
211 for (AtlasUser a : AtlasStorage.getAllUsers(false)) {
212 cachedUsers.put(a.getUser(), a);
213 }
214 for (String s : Main.getInstance().getConfig().getConfigurationSection("Geradores").getKeys(false)) {
215 int price = Main.getInstance().getConfig().getInt("Geradores." + s);
216 if (EntityType.valueOf(s.toUpperCase()) != null) {
217 creatures_registreds.put(EntityType.valueOf(s), price);
218 } else {
219 Bukkit.getConsoleSender()
220 .sendMessage("§c[AtlasFactions] Erro: Criatura nao existente no jogo cadastrada: " + s);
221 }
222 }
223 new FactionRemoverTask();
224 new GeneralRanking();
225 new SpawnersRanking();
226 new PoderRanking();
227 new KDRRanking();
228 new CoinsRanking();
229 new OnlineFactions();
230 Bukkit.getConsoleSender().sendMessage("§b[AtlasFactions] Plugin inicializado com sucesso.");
231
232 }
233
234 public void onDisable() {
235 try {
236 if (con != null) {
237 con.close();
238 }
239 } catch (SQLException e) {
240 e.printStackTrace();
241 }
242 for (String fac : Main.cachedFactions.keySet()) {
243 Main.cachedFactions.get(fac).save(false);
244 }
245 for (String user : Main.cachedUsers.keySet()) {
246 Main.cachedUsers.get(user).save(false);
247 }
248 if(connection != null)connection.closeConnection();
249 }
250
251 public static Map<String, Terreno> getClaims() {
252 return claims;
253 }
254
255 public static ArrayList<String> getFlying() {
256 return flying;
257 }
258
259 public static Chat getChatPreferences() {
260 return chat;
261 }
262
263 public static Map<String,Faction> getCachedFactions(){
264 return cachedFactions;
265 }
266
267 private void loadDependencies() {
268 new BukkitRunnable() {
269
270 @Override
271 public void run() {
272 new PlaceHolder().register();
273 hookPluginChat();
274 tryHookCashPlugin();
275 hookMcMMO();
276 hookWorldEdit();
277 }
278 }.runTask(this);
279 }
280
281 private void hookPluginChat() {
282 if (manager.isPluginEnabled("Legendchat")) {
283 manager.registerEvents(new LegendChatCreation(), this);
284 Bukkit.getConsoleSender().sendMessage("§a[LegendChat] LegendChat encontrado, finalizando hook...");
285 } else {
286 manager.registerEvents(new AsyncMinecraftChatCreation(), this);
287 Bukkit.getConsoleSender()
288 .sendMessage("§a[Chat Assincrono] Inicializando plugin de chat custom como hook de chat...");
289 }
290 }
291
292 private void hookMcMMO() {
293 if (!manager.isPluginEnabled("mcMMO")) {
294 new BukkitRunnable() {
295
296 @Override
297 public void run() {
298 Bukkit.getConsoleSender().sendMessage("§c[AtlasFactions] Erro mcMMO nao encontrado.");
299 Bukkit.getConsoleSender().sendMessage("§c[AtlasFactions] Servidor sendo desabilitado...");
300 onDisable();
301 }
302 }.runTask(this);
303 }
304 }
305
306 private void tryHookCashPlugin() {
307 if(manager.isPluginEnabled("Cash")) {
308 cash = new UnknownCash();
309 } else {
310 cash = new DefaultCash();
311 }
312 }
313
314 public static void loadRunnable() {
315 new BukkitRunnable() {
316
317 @Override
318 public void run() {
319 // TODO Auto-generated method stub
320 loadFactionRank();
321 }
322 }.runTaskTimer(Main.getPlugin(Main.class), 20L, 20L * 60 * 30);
323 }
324
325 public static void loadFactionRank() {
326 new BukkitRunnable() {
327
328 @Override
329 public void run() {
330 HashMap<Faction, Double> top = new HashMap<Faction, Double>();
331 for (Entry<String, Faction> s : cachedFactions.entrySet()) {
332 Faction f = s.getValue();
333 top.put(f, f.getValorFaccao());
334 }
335 int ranking = 1;
336 for (Entry<Faction, Double> a : com.atlasplugins.factions.atlasfactions.apis.Utils.entriesSortedByValues(top)) {
337 if (ranking == 1) {
338 firstFaction = a.getKey();
339 }
340 return;
341 }
342 }
343 }.runTask(Main.getPlugin(Main.class));
344 }
345
346 private boolean setupEconomy() {
347 if (getServer().getPluginManager().getPlugin("Vault") == null) {
348 return false;
349 }
350 RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
351 if (rsp == null) {
352 return false;
353 }
354 econ = rsp.getProvider();
355 return econ != null;
356 }
357
358 private void loadScoreTask() {
359 ScoreBoard.enabled = Main.getInstance().getConfig().getBoolean("ScoreBoardEnabled");
360 ScoreBoard.disabledScoreBoardWorlds = Main.getInstance().getConfig().getStringList("DisabledWorldsScoreBoard");
361 Bukkit.getOnlinePlayers().forEach(p -> {
362 if(!cachedUsers.containsKey(p.getName())) {
363 p.kickPlayer("§cPor favor relogue.");
364 }
365 });
366 new ScoreUpdater().runTaskTimerAsynchronously(this, 40L, 100L);
367 }
368
369 private boolean hookWorldEdit() {
370 if (worldEditPlugin == null) {
371 worldEditPlugin = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
372 }
373 return true;
374 }
375
376 public static Main getInstance() {
377 return instance;
378 }
379
380 public static Cash getCashAPI() {
381 return cash;
382 }
383
384 public static Economy getEconomy() {
385 return econ;
386 }
387
388 public static SpawnersAPI getSpawnersAPI() {
389 return spawnersAPI;
390 }
391
392}
393