Advertisement
Guest User

Untitled

a guest
Jan 27th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.39 KB | None | 0 0
  1. public class Main extends JavaPlugin {
  2.  
  3. private static Main instance;
  4.  
  5. private Thread thread;
  6.  
  7.  
  8. public String prefix = "§3QSG §8» §7";
  9. public String trackerMeta;
  10. public GameStateManager gameManager;
  11. public ChestManager chestManager;
  12. public Map normalMap;
  13. public Map dmMap;
  14. public String table;
  15. public boolean edit;
  16. public java.util.Map<UUID, Integer> ranking;
  17. public boolean teams = false;
  18. public boolean qsg = false;
  19. public int minplayers = 2;
  20. public int maxplayers = 12;
  21.  
  22. @Override
  23. public void onEnable() {
  24.  
  25.  
  26.  
  27.  
  28. File file = new File(Main.getInstance().getDataFolder(),"config.yml");
  29. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  30.  
  31. if(!file.exists()){
  32. cfg.set("qsg", true);
  33. cfg.set("teams", true);
  34. cfg.set("minplayers", 2);
  35. cfg.set("maxplayers", 12);
  36. cfg.set("motd.lobby", "&aLobby");
  37. cfg.set("motd.lobbyfull", "&6Lobby");
  38. cfg.set("motd.warmup", "&9Warmup");
  39. cfg.set("motd.shield", "&bShield");
  40. cfg.set("motd.ingame", "&3Ingame");
  41. cfg.set("motd.deathmatch", "&cDeathMatch");
  42. cfg.set("motd.end", "&7End");
  43.  
  44. for(World w : Bukkit.getWorlds()){
  45. String wo = w.toString();
  46. Bukkit.createWorld(new WorldCreator(wo).type(WorldType.NORMAL));
  47. System.out.println(wo+" geladen!");
  48. }
  49.  
  50.  
  51. try {
  52. cfg.save(file);
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57.  
  58. teams = cfg.getBoolean("teams");
  59. minplayers = cfg.getInt("minplayers");
  60. maxplayers = cfg.getInt("maxplayers");
  61. qsg = cfg.getBoolean("qsg");
  62.  
  63. this.gameManager = new GameStateManager();
  64.  
  65. this.edit = false;
  66. this.table = "sgstats";
  67.  
  68. this.getCommand("createmap").setExecutor(new CreateMap());
  69. this.getCommand("setlobby").setExecutor(new SetLobby());
  70. this.getCommand("addspawn").setExecutor(new AddSpawn());
  71. this.getCommand("deletespawn").setExecutor(new DeleteSpawn());
  72. this.getCommand("setspectator").setExecutor(new SetSpectator());
  73. this.getCommand("forcemap").setExecutor(new Forcemap());
  74. this.getCommand("start").setExecutor(new Start());
  75. this.getCommand("edit").setExecutor(new Edit());
  76. this.getCommand("stats").setExecutor(new Stats());
  77. this.getCommand("sethead").setExecutor(new SetHead());
  78. this.getCommand("setsign").setExecutor(new SetSign());
  79. this.getCommand("removerank").setExecutor(new RemoveRank());
  80.  
  81. this.loadMap();
  82. this.loadChestManager();
  83. this.loadBlocks();
  84.  
  85. this.startMinutesThread();
  86.  
  87. this.trackerMeta = "§6Tracker";
  88. this.ranking = new HashMap<>();
  89. this.loadMySQL();
  90. this.updateRanks();
  91.  
  92.  
  93. PluginManager pm = Bukkit.getPluginManager();
  94. pm.registerEvents(new PlayerJoin(), this);
  95. pm.registerEvents(new AsyncPlayerChat(), this);
  96. pm.registerEvents(new PlayerQuit(), this);
  97. pm.registerEvents(new PlayerInteract(), this);
  98. pm.registerEvents(new PlayerLogin(), this);
  99. pm.registerEvents(new ServerListPing(), this);
  100. pm.registerEvents(new BlockPlace(), this);
  101. pm.registerEvents(new EntityExplode(), this);
  102. pm.registerEvents(new InventoryClick(), this);
  103. pm.registerEvents(new PlayerMove(), this);
  104. pm.registerEvents(new BlockBreak(), this);
  105. pm.registerEvents(new EntityDamage(), this);
  106. pm.registerEvents(new EntityDamageByEntity(), this);
  107. pm.registerEvents(new PlayerDropItem(), this);
  108. pm.registerEvents(new BlockBurn(), this);
  109. pm.registerEvents(new BlockIgnite(), this);
  110. pm.registerEvents(new CreatureSpawn(), this);
  111. pm.registerEvents(new PlayerDeath(), this);
  112. pm.registerEvents(new PlayerRespawn(), this);
  113. pm.registerEvents(new FoodLevelChange(), this);
  114. pm.registerEvents(new LeavesDecay(), this);
  115. pm.registerEvents(new PlayerInteractEntity(), this);
  116. pm.registerEvents(new PlayerAchievementAwarded(), this);
  117. pm.registerEvents(new PlayerBedEnter(), this);
  118. pm.registerEvents(new PlayerExpChange(), this);
  119. pm.registerEvents(new PlayerLevelChange(), this);
  120. pm.registerEvents(new PlayerPickUpItem(), this);
  121. pm.registerEvents(new WeatherChange(), this);
  122. pm.registerEvents(new InventoryClose(), this);
  123. pm.registerEvents(new ItemEnchant(), this);
  124. pm.registerEvents(new PlayerToggleSprint(), this);
  125.  
  126. }
  127.  
  128.  
  129.  
  130. @Override
  131. public void onDisable() {
  132.  
  133. this.thread.interrupt();
  134. if (this.normalMap != null) {
  135. Bukkit.unloadWorld(this.normalMap.getWorld(), false);
  136. }
  137. if (this.dmMap != null) {
  138. Bukkit.unloadWorld(this.dmMap.getWorld(), false);
  139. }
  140. if(GameStateShield.getThread() != null){
  141. GameStateShield.getThread().interrupt();
  142. }
  143. MySQL.getInstance().closeConnection();
  144.  
  145. }
  146.  
  147. @Override
  148. public void onLoad() {
  149. instance= this;
  150.  
  151.  
  152. }
  153.  
  154. @SuppressWarnings("deprecation")
  155. private void loadChestManager() {
  156. File file = new File(this.getDataFolder(), "items.yml");
  157. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  158.  
  159. List<String> defaults = new ArrayList<>();
  160. // id:damage,min-max,prop. (Wahrscheinlichkeit)
  161.  
  162. // Waffen
  163. defaults.add("267:0,1-1,2");
  164. defaults.add("268:0,1-1,20");
  165. defaults.add("272:0,1-1,15");
  166. defaults.add("261:0,1-1,30");
  167. defaults.add("262:0,10-1,50");
  168. defaults.add("279:0,1-1,28");
  169. defaults.add("271:0,1-1,23");
  170. defaults.add("346:0,1-1,13");
  171. // Rüstung
  172. defaults.add("298,1-1,13");
  173. defaults.add("299,1-1,17");
  174. defaults.add("300,1-1,15");
  175. defaults.add("301,1-1,19");
  176. defaults.add("302,1-1,14");
  177. defaults.add("303,1-1,34");
  178. defaults.add("304,1-1,18");
  179. defaults.add("305,1-1,14");
  180. defaults.add("306,1-1,17");
  181. defaults.add("307,1-1,16");
  182. defaults.add("308,1-1,15");
  183. defaults.add("309,1-1,11");
  184. defaults.add("314,1-1,19");
  185. defaults.add("315,1-1,16");
  186. defaults.add("316,1-1,15");
  187. defaults.add("317,1-1,21");
  188.  
  189. // Essen
  190. defaults.add("260,10-1,31");
  191. defaults.add("297,7-1,35");
  192. defaults.add("320,5-1,28");
  193. defaults.add("322,5-1,13");
  194. defaults.add("363,7-1,16");
  195. defaults.add("349:3,1-1,1");
  196. defaults.add("393,7-1,19");
  197.  
  198. // TODO More Defaults
  199.  
  200. cfg.addDefault("items", defaults);
  201. cfg.options().copyDefaults(true);
  202. try {
  203. cfg.save(file);
  204. } catch (IOException e) {
  205. e.printStackTrace();
  206. }
  207. List<ChestItem> chestItems = new ArrayList<ChestItem>();
  208. List<String> items = cfg.getStringList("items");
  209. int i = 1;
  210. for (String item : items) {
  211. try {
  212. String[] split = item.split(",");
  213. String[] itemStack = split[0].split(":");
  214. String[] count = split[1].split("-");
  215. ItemStack stack = null;
  216. int id = Integer.parseInt(itemStack[0]);
  217.  
  218. if (itemStack.length == 1) {
  219. stack = new ItemStack(id);
  220. } else {
  221. short damage = Short.parseShort(itemStack[1]);
  222. stack = new ItemStack(id, 0, damage);
  223. }
  224. int min = Integer.parseInt(count[0]);
  225. int max = Integer.parseInt(count[1]);
  226. int probability = Integer.parseInt(split[2]);
  227. chestItems.add(new ChestItem(stack, probability, min, max));
  228.  
  229. } catch (Exception ex) {
  230. this.getLogger().log(Level.WARNING, "Das Item an der Stelle " + i + " benutzt einen falschen Syntax!");
  231. ex.printStackTrace();
  232. continue;
  233. } finally {
  234. i++;
  235. }
  236. }
  237. //for(ChestItem item : this.getSpecialItems()){
  238. // chestItems.add(item);
  239. // }
  240. this.chestManager = new ChestManager("§8Truhe", chestItems, 5, 2, 27);
  241.  
  242. }
  243. public List<ChestItem> getSpecialItems(){
  244. List<ChestItem> items = new ArrayList<>();
  245. items.add(new ChestItem(this.getItem(Material.COMPASS, this.trackerMeta), 2, 1, 1));
  246. return items;
  247. }
  248. public ItemStack getItem(Material mat,String name){
  249. ItemStack stack = new ItemStack(mat);
  250. ItemMeta meta = stack.getItemMeta();
  251. meta.setDisplayName(name);
  252. stack.setItemMeta(meta);
  253. return stack;
  254. }
  255.  
  256. public static Main getInstance() {
  257. return instance;
  258. }
  259. private void loadMySQL(){
  260. File file = new File(this.getDataFolder(), "MySQL.yml");
  261. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  262.  
  263. cfg.addDefault("MySQL.host", "host");
  264. cfg.addDefault("MySQL.database", "datenbank");
  265. cfg.addDefault("MySQL.user", "username");
  266. cfg.addDefault("MySQL.password", "passwort");
  267. cfg.addDefault("MySQL.port", 3306);
  268.  
  269. cfg.options().copyDefaults(true);
  270. try {
  271. cfg.save(file);
  272. } catch (IOException e) {
  273. e.printStackTrace();
  274. }
  275.  
  276. String host = cfg.getString("MySQL.host");
  277. String database = cfg.getString("MySQL.database");
  278. String user = cfg.getString("MySQL.user");
  279. String password = cfg.getString("MySQL.password");
  280. int port = cfg.getInt("MySQL.port");
  281.  
  282. new MySQL(this, host, database, user, password, port);
  283. MySQL.getInstance().connect();
  284.  
  285. MySQL.getInstance().update("CREATE TABLE IF NOT EXISTS "+this.table+" (id INT(16) AUTO_INCREMENT PRIMARY KEY, uuid VARCHAR(50), kills INT(16), deaths INT(16), points INT, minutes INT,games INT, wins INT, achievements VARCHAR(500), name VARCHAR(50))");
  286.  
  287.  
  288. }
  289.  
  290. private void startMinutesThread() {
  291. this.thread = new Thread(new Runnable() {
  292.  
  293. @Override
  294. public void run() {
  295. try {
  296. Thread.sleep(1000 * 60);
  297.  
  298. } catch (InterruptedException e) {}
  299. for(Player all : PlayerUtility.getLivingPlayers()){
  300. MySQL.getInstance().update("UPDATE " + Main.getInstance().table + " SET minutes = minutes+1 WHERE uuid = '"+all.getUniqueId().toString()+"'");
  301.  
  302. }
  303. }
  304. });
  305. this.thread.start();
  306. }
  307. private void updateRanks(){
  308.  
  309. MySQL.getInstance().query("SELECT uuid ,points ,wins FROM " +this.table + " ORDER BY points DESC",t->{
  310. try {
  311. File file = new File(Main.getInstance().getDataFolder(),"locs.yml");
  312. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  313.  
  314. int rank = 0;
  315. java.util.Map<Integer, UUID> top = new HashMap<Integer,UUID>();
  316. while (t.next()) {
  317. rank++;
  318. this.ranking.put(UUID.fromString(t.getString("uuid")), rank);
  319. top.put(rank, UUID.fromString(t.getString("uuid")));
  320. }
  321.  
  322. if(cfg.isConfigurationSection("head")){
  323. for(String s : cfg.getConfigurationSection("head").getKeys(false)){
  324. int i = Integer.parseInt(s);
  325. if(top.containsKey(i)){
  326.  
  327. UUID uuid = top.get(i);
  328. Location loc = FileHelper.getLocation(file,"head."+s);
  329. if(loc.getBlock().getState() instanceof Skull){
  330. Skull skull = (Skull) loc.getBlock().getState();
  331.  
  332. // if(!skull.getOwner().equals(Bukkit.getOfflinePlayer(uuid).getName())){
  333.  
  334. skull.setOwner(Bukkit.getOfflinePlayer(uuid).getName());
  335. skull.update();
  336. // }
  337. }
  338. }}
  339. }
  340.  
  341. if(cfg.isConfigurationSection("sign")){
  342. for(String s : cfg.getConfigurationSection("sign").getKeys(false)){
  343. int i = Integer.parseInt(s);
  344. if(top.containsKey(i)){
  345. UUID uuid = top.get(i);
  346. Location loc = FileHelper.getLocation(file, "sign." + s);
  347. if(loc.getBlock().getState() instanceof Sign){
  348. t.absolute(rank);
  349. Sign sign = (Sign) loc.getBlock().getState();
  350. sign.setLine(0, "#"+s);
  351. sign.setLine(1, Bukkit.getOfflinePlayer(uuid).getName());
  352. sign.setLine(2, t.getInt("points") + " Punkte");
  353. sign.setLine(3, t.getInt("wins") + " Siege");
  354.  
  355. sign.update();
  356. }
  357. }
  358. }
  359. }
  360. t.close();
  361. } catch (Exception ex) {
  362. ex.printStackTrace();
  363. }
  364.  
  365. });
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376. }
  377. private void loadMap() {
  378. this.normalMap = this.getRandomMap("normal");
  379. this.dmMap = this.getRandomMap("deathmatch");
  380.  
  381. if (this.normalMap != null) {
  382. this.getLogger().log(Level.INFO, "Als normale Map wird " + this.normalMap.getName() + " gespielt");
  383. }
  384. if (this.dmMap != null) {
  385. this.getLogger().log(Level.INFO, "Als Deathmatch Map wird " + this.dmMap.getName() + " gespielt");
  386. }
  387. }
  388. private void loadBlocks(){
  389. File file = new File(this.getDataFolder(),"items.yml");
  390. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  391.  
  392. List<String> build = new ArrayList<String>();
  393. build.add("92");
  394.  
  395. cfg.addDefault("build", build);
  396.  
  397. List<String> breaks = new ArrayList<String>();
  398.  
  399.  
  400. breaks.add("18");
  401. cfg.addDefault("break", breaks);
  402.  
  403. cfg.options().copyDefaults(true);
  404.  
  405. try {
  406. cfg.save(file);
  407. } catch (IOException e) {
  408. e.printStackTrace();
  409. }
  410.  
  411. }
  412. public List<Integer> getBuildBlocks(){
  413. File file = new File(this.getDataFolder(),"items.yml");
  414. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  415.  
  416. List<Integer> ints = new ArrayList<Integer>();
  417. int i = 1;
  418. for(String s : cfg.getStringList("build")){
  419. try{
  420. ints.add(Integer.parseInt(s));
  421. }catch(NumberFormatException ex){
  422. this.getLogger().log(Level.WARNING, "Build ID "+ i + " hat nicht den richtigen Syntax!");
  423. }finally{
  424. i++;
  425. }
  426. }
  427. return ints;
  428. }
  429. public List<Integer> getBreakBlocks(){
  430. File file = new File(this.getDataFolder(),"items.yml");
  431. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  432.  
  433. List<Integer> ints = new ArrayList<Integer>();
  434. int i = 1;
  435. for(String s : cfg.getStringList("break")){
  436. try{
  437. ints.add(Integer.parseInt(s));
  438. }catch(NumberFormatException ex){
  439. this.getLogger().log(Level.WARNING, "Break ID "+ i + " hat nicht den richtigen Syntax!");
  440. }finally{
  441. i++;
  442. }
  443. }
  444. return ints;
  445. }
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455. private Map getRandomMap(String type) {
  456.  
  457. File file = new File(this.getDataFolder(), "maps.yml");
  458. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  459.  
  460. List<String> mapNames = new ArrayList<String>();
  461. if (cfg.isConfigurationSection("")) {
  462. for (String mapName : cfg.getConfigurationSection("").getKeys(false)) {
  463. if (cfg.getString(mapName + ".type").equals(type)) {
  464. mapNames.add(mapName);
  465. }
  466. }
  467. }
  468. if (!mapNames.isEmpty()) {
  469. String mapName = mapNames.get(new Random().nextInt(mapNames.size()));
  470. return this.getByName(mapName);
  471.  
  472. }
  473. this.getLogger().log(Level.WARNING, "Es ist keine Map vorhanden");
  474. return null;
  475.  
  476. }
  477.  
  478. public Map getByName(String mapName) {
  479. File file = new File(this.getDataFolder(), "maps.yml");
  480. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  481. final String world = cfg.getString(mapName + ".world");
  482.  
  483. List<Location> locs = new ArrayList<>();
  484. if (cfg.isConfigurationSection(mapName + ".spawns")) {
  485. for (String location : cfg.getConfigurationSection(mapName + ".spawns").getKeys(false)) {
  486. locs.add(FileHelper.getLocation(file, mapName + ".spawns." + location));
  487. }
  488. }
  489. Location spectator = null;
  490. if (cfg.contains(mapName + ".spectator")) {
  491. spectator = FileHelper.getLocation(file, mapName + ".spectator");
  492. }
  493. for (World w : Bukkit.getWorlds()) {
  494. if (w.getName().equals(world)) {
  495. this.getLogger().log(Level.INFO, "Welt wurde geladen");
  496. w.setAutoSave(false);
  497. w.setGameRuleValue("doDaylightCycle", "false");
  498. w.setStorm(false);
  499. w.setTime(2000);
  500. w.setThundering(false);
  501. return new Map(mapName, world, spectator, locs);
  502. }
  503. }
  504. new Thread(new Runnable() {
  505.  
  506. @Override
  507. public void run() {
  508. Bukkit.getScheduler().runTask(Main.getInstance(), new Runnable() {
  509.  
  510. @Override
  511. public void run() {
  512. World w = Bukkit.getWorld(world);
  513. w.setAutoSave(false);
  514. w.setGameRuleValue("doDaylightCycle", "false");
  515. w.setStorm(false);
  516. w.setTime(2000);
  517. w.setThundering(false);
  518. Main.getInstance().getLogger().log(Level.INFO, "Welt wurde geladen");
  519.  
  520. }
  521. });
  522. }
  523. });
  524. return new Map(mapName, world, spectator, locs);
  525.  
  526. }
  527.  
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement