Guest User

Untitled

a guest
Mar 25th, 2021
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.55 KB | None | 0 0
  1.  
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.Location;
  11. import org.bukkit.Material;
  12. import org.bukkit.block.Chest;
  13. import org.bukkit.configuration.file.FileConfiguration;
  14. import org.bukkit.entity.EntityType;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.event.EventHandler;
  17. import org.bukkit.event.EventPriority;
  18. import org.bukkit.event.Listener;
  19. import org.bukkit.event.block.Action;
  20. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  21. import org.bukkit.event.entity.PlayerDeathEvent;
  22. import org.bukkit.event.inventory.InventoryClickEvent;
  23. import org.bukkit.event.player.PlayerInteractEvent;
  24. import org.bukkit.event.player.PlayerMoveEvent;
  25. import org.bukkit.inventory.Inventory;
  26. import org.bukkit.inventory.ItemStack;
  27. import org.bukkit.scheduler.BukkitRunnable;
  28. import org.bukkit.scheduler.BukkitTask;
  29.  
  30. import me.wazup.partygames.Cuboid;
  31. import me.wazup.partygames.Enums.PlayCounter;
  32. import me.wazup.partygames.PartyGames;
  33. import me.wazup.partygames.minigames.MiniGame;
  34. import me.wazup.partygames.minigames.MiniGameMap;
  35.  
  36. public class SurvivalGames extends MiniGame {
  37.  
  38. String prefix = ChatColor.DARK_GREEN + "[Surival Games] " + ChatColor.GREEN;
  39.  
  40. String[] tutorialMessages;
  41. List<ItemStack> chestItems;
  42. int roundDuration;
  43.  
  44. Addon plugin;
  45.  
  46. public SurvivalGames(Addon plugin) {
  47. this.plugin = plugin;
  48. }
  49.  
  50. @Override
  51. public String getName() {
  52. return "Survival Games";
  53. }
  54.  
  55. @Override
  56. public Material getRepresentingMaterial() {
  57. return Material.CHEST;
  58. }
  59.  
  60. @Override
  61. public String[] getTutorialMessages() {
  62. return tutorialMessages;
  63. }
  64.  
  65. @Override
  66. public void handleSetupCommand(Player p, String[] args) {
  67. if(args.length == 0) {
  68. p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Commands " + ChatColor.DARK_GREEN + " -------- ");
  69. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "CreateMap <Name>" + ChatColor.RED + " - Requires selection");
  70. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "DeleteMap <Name>");
  71. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "AddSpawn <Map>");
  72. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "RemoveSpawn <Map>");
  73. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "List");
  74. return;
  75. }
  76.  
  77. String subCommand = args[0].toLowerCase();
  78.  
  79. if(subCommand.equals("createmap")) {
  80. if(args.length == 1) {
  81. p.sendMessage(prefix + "You must specify a map name!");
  82. return;
  83. }
  84. String mapName = args[1];
  85.  
  86. if(maps.containsKey(mapName.toLowerCase())) {
  87. p.sendMessage(prefix + "There is already a map with that name!");
  88. return;
  89. }
  90.  
  91. Cuboid cuboid = PartyGames.api.getPlayerSelection(p);
  92.  
  93. if(cuboid == null) {
  94. p.sendMessage(prefix + "You must select the 2 corners of the map first! Use '/pg wand' to receive the selection wand!");
  95. return;
  96. }
  97.  
  98. PartyGames.api.removePlayerSelection(p);
  99.  
  100. maps.put(mapName.toLowerCase(), new SurvivalGamesMap(mapName, cuboid, new ArrayList<Location>()));
  101.  
  102. FileConfiguration config = getConfiguration();
  103.  
  104. config.set("Maps." + mapName + ".Map-Cuboid", cuboid.toString());
  105.  
  106. saveConfiguration(config);
  107.  
  108. p.sendMessage(prefix + "Map has been created successfully!");
  109. return;
  110. }
  111.  
  112. if(subCommand.equals("deletemap")) {
  113. if(args.length == 1) {
  114. p.sendMessage(prefix + "You must specify a map name!");
  115. return;
  116. }
  117. String mapName = args[1];
  118.  
  119. if(!maps.containsKey(mapName.toLowerCase())) {
  120. p.sendMessage(prefix + "Could not find a map with that name!");
  121. return;
  122. }
  123.  
  124. maps.remove(mapName.toLowerCase());
  125.  
  126. FileConfiguration config = getConfiguration();
  127.  
  128. config.set("Maps." + mapName, null);
  129.  
  130. saveConfiguration(config);
  131.  
  132. p.sendMessage(prefix + "Map has been removed successfully!");
  133. return;
  134. }
  135.  
  136. if(subCommand.equals("addspawn")) {
  137. if(args.length == 1) {
  138. p.sendMessage(prefix + "You must specify a map name!");
  139. return;
  140. }
  141. String mapName = args[1];
  142.  
  143. if(!maps.containsKey(mapName.toLowerCase())) {
  144. p.sendMessage(prefix + "Could not find a map with that name!");
  145. return;
  146. }
  147.  
  148. SurvivalGamesMap map = (SurvivalGamesMap) maps.get(mapName.toLowerCase());
  149. map.spawnpoints.add(p.getLocation());
  150.  
  151. int spawnId = map.spawnpoints.size();
  152.  
  153. FileConfiguration config = getConfiguration();
  154.  
  155. config.set("Maps." + mapName + ".Spawnpoints." + spawnId, PartyGames.plugin.getStringFromLocation(p.getLocation(), true));
  156.  
  157. saveConfiguration(config);
  158.  
  159. p.sendMessage(prefix + "Map spawnpoint with id #" + spawnId + " has been set successfully!");
  160. return;
  161. }
  162.  
  163. if(subCommand.equals("removespawn")) {
  164. if(args.length == 1) {
  165. p.sendMessage(prefix + "You must specify a map name!");
  166. return;
  167. }
  168. String mapName = args[1];
  169.  
  170. if(!maps.containsKey(mapName.toLowerCase())) {
  171. p.sendMessage(prefix + "Could not find a map with that name!");
  172. return;
  173. }
  174.  
  175. SurvivalGamesMap map = (SurvivalGamesMap) maps.get(mapName.toLowerCase());
  176.  
  177. if(map.spawnpoints.isEmpty()) {
  178. p.sendMessage(prefix + "That map does not have any spawnpoints!");
  179. return;
  180. }
  181.  
  182. int spawnId = map.spawnpoints.size();
  183. Location lastSpawnpoint = map.spawnpoints.get(spawnId - 1);
  184. map.spawnpoints.remove(lastSpawnpoint);
  185.  
  186. FileConfiguration config = getConfiguration();
  187.  
  188. config.set("Maps." + mapName + ".Spawnpoints." + spawnId, null);
  189.  
  190. saveConfiguration(config);
  191.  
  192. p.sendMessage(prefix + "Map spawnpoint with id #" + spawnId + " has been removed successfully!");
  193. return;
  194. }
  195.  
  196. if(subCommand.equals("list")) {
  197. if(maps.isEmpty()) {
  198. p.sendMessage(prefix + "No maps are loaded!");
  199. return;
  200. }
  201. p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Maps " + ChatColor.DARK_GREEN + " -------- ");
  202. for(MiniGameMap map: maps.values()) {
  203. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + map.name + ChatColor.DARK_GREEN + " - Is Setup: " + ChatColor.GREEN + map.isSetup());
  204. }
  205. return;
  206. }
  207.  
  208. p.sendMessage(prefix + "Unknown command!");
  209. }
  210.  
  211. @Override
  212. public void load() {
  213. maps = new HashMap<String, MiniGameMap>();
  214.  
  215. FileConfiguration config = getConfiguration();
  216.  
  217. boolean save = false;
  218.  
  219. if(!config.contains("Tutorial-Messages")) {
  220. config.set("Tutorial-Messages", Arrays.asList(new String[] {
  221. "&2 --------- &aSurvival Games &2--------- ",
  222. "- Get to the middle and loot the chests",
  223. "to eliminate your enemies!"
  224. }));
  225. save = true;
  226. }
  227.  
  228. if(!config.contains("Round-Duration")) {
  229. config.set("Round-Duration", 120);
  230. save = true;
  231. }
  232.  
  233. if(!config.contains("Chest-Items")) {
  234. config.set("Chest-Items", Arrays.asList(new String[] {
  235. "STONE_SWORD : 1",
  236. "WOOD_SWORD : 1",
  237. "WOOD_SWORD : 1",
  238. "WOOD_SWORD : 1",
  239. "WOOD_SWORD : 1",
  240. "WOOD_SWORD : 1",
  241. "WOOD_SWORD : 1",
  242. "WOOD_SWORD : 1",
  243. "WOOD_SWORD : 1",
  244. "WOOD_SWORD : 1",
  245. "WOOD_SWORD : 1",
  246. "WOOD_SWORD : 1",
  247. "MUSHROOM_SOUP : 1",
  248. "BOW : 1 : enchant:ARROW_DAMAGE:1 : name:&bHave Fun",
  249. "LEATHER_HELMET : 1",
  250. "LEATHER_CHESTPLATE : 1",
  251. "LEATHER_LEGGINGS : 1",
  252. "IRON_HELMET : 1",
  253. "IRON_CHESTPLATE : 1",
  254. "IRON_LEGGINGS : 1",
  255. "ARROW : 20",
  256. "FISHING_ROD : 1",
  257. "GOLD_HELMET : 1",
  258. "GOLD_CHESTPLATE : 1",
  259. "BONE : 1",
  260. "GOLDEN_APPLE : 1",
  261. "ARROW : 20",
  262. "APPLE : 2",
  263. "APPLE : 2",
  264. "APPLE : 2"
  265. }));
  266. save = true;
  267. }
  268.  
  269. if(save) saveConfiguration(config);
  270.  
  271. List<String> temp = config.getStringList("Tutorial-Messages");
  272. tutorialMessages = new String[temp.size()];
  273. for(int i = 0; i < tutorialMessages.length; i++) tutorialMessages[i] = ChatColor.translateAlternateColorCodes('&', temp.get(i));
  274.  
  275. if(config.getConfigurationSection("Maps") != null && !config.getConfigurationSection("Maps").getKeys(false).isEmpty()) {
  276. for(String mapName: config.getConfigurationSection("Maps").getKeys(false)) {
  277. Cuboid cuboid = new Cuboid(config.getString("Maps." + mapName + ".Map-Cuboid"));
  278. List<Location> spawnpoints = new ArrayList<Location>();
  279.  
  280. if(config.getConfigurationSection("Maps." + mapName + ".Spawnpoints") != null && !config.getConfigurationSection("Maps." + mapName + ".Spawnpoints").getKeys(false).isEmpty()) {
  281. for(String locationId: config.getConfigurationSection("Maps." + mapName + ".Spawnpoints").getKeys(false)) {
  282. spawnpoints.add(PartyGames.plugin.getLocationFromString(config.getString("Maps." + mapName + ".Spawnpoints." + locationId)));
  283. }
  284. }
  285.  
  286. maps.put(mapName.toLowerCase(), new SurvivalGamesMap(mapName, cuboid, spawnpoints));
  287. }
  288. }
  289.  
  290. chestItems = new ArrayList<ItemStack>();
  291. for(String chestItem: config.getStringList("Chest-Items")) {
  292. try { chestItems.add(PartyGames.api.getItemStackFromString(chestItem));
  293. } catch (Exception e) { Bukkit.getConsoleSender().sendMessage(prefix + ChatColor.RED + "Failed to load an item in survival games. The item has the following text: " + chestItem); }
  294. }
  295.  
  296. roundDuration = config.getInt("Round-Duration");
  297. }
  298.  
  299. @Override
  300. public int getAliveCount(String mapName) {
  301. return ((SurvivalGamesMap) maps.get(mapName.toLowerCase())).alive.size();
  302. }
  303.  
  304. private class SurvivalGamesMap extends MiniGameMap implements Listener {
  305.  
  306. Cuboid mainCuboid;
  307.  
  308. List<Location> spawnpoints;
  309.  
  310. ArrayList<String> alive;
  311. String[] topPlayers;
  312.  
  313. BukkitTask task;
  314.  
  315. List<Location> openedChests;
  316.  
  317. boolean allowActivity;
  318.  
  319. public SurvivalGamesMap(String name, Cuboid mainCuboid, List<Location> spawnpoints) {
  320. this.name = name;
  321. this.mainCuboid = mainCuboid;
  322. this.spawnpoints = spawnpoints;
  323.  
  324. alive = new ArrayList<String>();
  325. topPlayers = new String[3];
  326.  
  327. allowActivity = false;
  328.  
  329. openedChests = new ArrayList<Location>();
  330.  
  331. Bukkit.getPluginManager().registerEvents(this, plugin);
  332. }
  333.  
  334. public boolean isSetup() {
  335. return !spawnpoints.isEmpty();
  336. }
  337.  
  338. public void start() {
  339. for(int i = 0; i < topPlayers.length; i++) topPlayers[i] = null;
  340.  
  341. int i = 0;
  342. for(Player p: arena.getPlayers()) {
  343. if(i >= spawnpoints.size()) i = 0;
  344. p.teleport(spawnpoints.get(i++));
  345. alive.add(p.getName());
  346. }
  347.  
  348. for(Player p: arena.getSpectators()) p.teleport(spawnpoints.get(0));
  349.  
  350. this.arena = arena;
  351.  
  352. startTask();
  353. }
  354.  
  355. public void cancel(boolean immediate) {
  356. arena = null;
  357. allowActivity = false;
  358.  
  359. alive.clear();
  360.  
  361. mainCuboid.clearEntities();
  362.  
  363. openedChests.clear();
  364.  
  365. if(task != null) {
  366. task.cancel();
  367. task = null;
  368. }
  369. }
  370.  
  371. public void leave(Player p) {
  372. if(alive.contains(p.getName())) {
  373. alive.remove(p.getName());
  374. if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
  375. if(alive.size() == 1) {
  376. finish();
  377. }
  378. }
  379. }
  380.  
  381. private void startTask() {
  382. task = new BukkitRunnable() {
  383. int activityTimer = 5;
  384. int timer = roundDuration;
  385. public void run() {
  386.  
  387. if(activityTimer > 0) {
  388. activityTimer--;
  389. if(activityTimer == 4) arena.sendPlayCounterNotification(PlayCounter.READY);
  390. else if(activityTimer == 2) arena.sendPlayCounterNotification(PlayCounter.SET);
  391. else if(activityTimer == 0) {
  392. arena.sendPlayCounterNotification(PlayCounter.GO);
  393. allowActivity = true;
  394. }
  395. } else {
  396. for(Player p: arena.getPlayers()) p.setLevel(timer);
  397.  
  398. timer--;
  399. if(timer == 0) {
  400. finish();
  401. }
  402. }
  403.  
  404. }
  405. }.runTaskTimer(plugin, 40L, 20L);
  406. }
  407.  
  408. private void eliminatePlayer(Player p) {
  409. alive.remove(p.getName());
  410. p.sendMessage(PartyGames.api.getMessage("Player-Lose-Round"));
  411. p.getInventory().clear();
  412. p.getInventory().setArmorContents(null);
  413. String eliminationMessage = PartyGames.api.getMessage("Player-Lose-Round-Announce").replace("%player%", p.getName());
  414. for(Player x: arena.getAllPlayers()) x.sendMessage(eliminationMessage);
  415. if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
  416. PartyGames.api.MakeSpectator(p, true);
  417. if(alive.size() < 3) topPlayers[alive.size()] = p.getName(); //If size is 2, then this player is 3rd which is number 2 in []
  418. if(alive.size() == 1) {
  419. finish();
  420. }
  421. }
  422.  
  423. private void finish() {
  424. if(task != null) {
  425. task.cancel();
  426. task = null;
  427. }
  428.  
  429. for(int i = 0; i < alive.size(); i++) if(i < topPlayers.length) topPlayers[i] = alive.get(i);
  430.  
  431. PartyGames.api.sendRoundConclusion(arena, topPlayers);
  432.  
  433. String winnerName = topPlayers[0];
  434. final Player winner = Bukkit.getPlayer(winnerName);
  435. if(winner != null) winner.setHealth(winner.getMaxHealth());
  436.  
  437. task = new BukkitRunnable() {
  438. int timer = 5;
  439. boolean fireworks = false;
  440. public void run() {
  441. timer--;
  442.  
  443. fireworks = !fireworks;
  444. if(fireworks) {
  445. if(winner != null) PartyGames.plugin.fireWorkEffect(winner, true);
  446. }
  447.  
  448. if(timer == 0) {
  449. for(Player p: arena.getPlayers()) {
  450. PartyGames.api.MakeSpectator(p, false);
  451. }
  452. arena.finishCurrentMinigame(topPlayers);
  453. }
  454. }
  455. }.runTaskTimer(plugin, 0, 20L);
  456. }
  457.  
  458. public void handleEntityDamageByEntityEvent(EntityDamageByEntityEvent e) {
  459. if(!e.getEntityType().equals(EntityType.PLAYER)) return;
  460. Player p = (Player) e.getEntity();
  461. if(alive.contains(p.getName()) && allowActivity && !e.getDamager().getType().equals(EntityType.FIREWORK)) {
  462. e.setCancelled(false);
  463. }
  464. }
  465.  
  466. @EventHandler(priority = EventPriority.HIGH)
  467. public void onPlayerDeath(PlayerDeathEvent e) {
  468. Player p = e.getEntity();
  469. if(alive.contains(p.getName()) && allowActivity) {
  470. eliminatePlayer(p);
  471. }
  472. }
  473.  
  474. public void handlePlayerInteractEvent(PlayerInteractEvent e) {
  475. Player p = e.getPlayer();
  476. if(alive.contains(p.getName()) && allowActivity) {
  477. e.setCancelled(false);
  478. if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
  479. if(e.getClickedBlock().getType().equals(Material.CHEST) && !openedChests.contains(e.getClickedBlock().getLocation())) {
  480. openedChests.add(e.getClickedBlock().getLocation());
  481. Inventory inv = ((Chest) e.getClickedBlock().getState()).getInventory();
  482. inv.clear();
  483.  
  484. int amount = random.nextInt(3) + 4;
  485. for(int i = 0; i < amount; i++){
  486. int slot = random.nextInt(27);
  487. for(int x = 0; x < 2; x++){
  488. if(inv.getItem(slot) != null) slot = random.nextInt(27);
  489. else break;
  490. }
  491. inv.setItem(slot, chestItems.get(random.nextInt(chestItems.size())));
  492. }
  493.  
  494. }
  495. }
  496. }
  497. }
  498.  
  499. @EventHandler
  500. public void onPlayerMoveEvent(PlayerMoveEvent e){
  501. Player p = e.getPlayer();
  502. if(!alive.contains(p.getName()) || allowActivity) return;
  503. if(e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockZ() != e.getTo().getBlockZ()) p.teleport(e.getFrom());
  504. }
  505.  
  506. @EventHandler(priority = EventPriority.HIGH)
  507. public void onInventoryClickEvent(InventoryClickEvent e){
  508. Player p = (Player) e.getWhoClicked();
  509. if(alive.contains(p.getName())) e.setCancelled(false);
  510. }
  511.  
  512. }
  513.  
  514. }
  515.  
Advertisement
Add Comment
Please, Sign In to add comment