Advertisement
Guest User

Untitled

a guest
Mar 25th, 2021
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.04 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.Block;
  13. import org.bukkit.block.BlockState;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.entity.Snowball;
  17. import org.bukkit.event.EventHandler;
  18. import org.bukkit.event.EventPriority;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.BlockBreakEvent;
  21. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  22. import org.bukkit.event.entity.ProjectileHitEvent;
  23. import org.bukkit.event.player.PlayerInteractEvent;
  24. import org.bukkit.inventory.ItemStack;
  25. import org.bukkit.scheduler.BukkitRunnable;
  26. import org.bukkit.scheduler.BukkitTask;
  27. import org.bukkit.util.BlockIterator;
  28.  
  29. import me.wazup.partygames.Cuboid;
  30. import me.wazup.partygames.Enums.PlayCounter;
  31. import me.wazup.partygames.PartyGames;
  32. import me.wazup.partygames.minigames.MiniGame;
  33. import me.wazup.partygames.minigames.MiniGameMap;
  34.  
  35. public class Spleef extends MiniGame {
  36.  
  37. String prefix = ChatColor.DARK_GREEN + "[Spleef] " + ChatColor.GREEN;
  38.  
  39. String[] tutorialMessages;
  40. int roundDuration;
  41. ItemStack shovel, snowball;
  42. boolean giveSnowballs;
  43.  
  44. Addon plugin;
  45.  
  46. public Spleef(Addon plugin) {
  47. this.plugin = plugin;
  48. }
  49.  
  50. @Override
  51. public String getName() {
  52. return "Spleef";
  53. }
  54.  
  55. @Override
  56. public Material getRepresentingMaterial() {
  57. return Material.DIAMOND_SPADE;
  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 + "SetSpawn <Map>");
  72. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "SetFloor <Map>" + ChatColor.RED + " - Requires selection");
  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 SpleefMap(mapName, cuboid, null, null));
  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("setspawn")) {
  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. ((SpleefMap) maps.get(mapName.toLowerCase())).spawnpoint = p.getLocation();
  149.  
  150. FileConfiguration config = getConfiguration();
  151.  
  152. config.set("Maps." + mapName + ".Spawnpoint", PartyGames.plugin.getStringFromLocation(p.getLocation(), true));
  153.  
  154. saveConfiguration(config);
  155.  
  156. p.sendMessage(prefix + "Map spawnpoint has been set successfully!");
  157. return;
  158. }
  159.  
  160. if(subCommand.equals("setfloor")) {
  161. if(args.length == 1) {
  162. p.sendMessage(prefix + "You must specify a map name!");
  163. return;
  164. }
  165. String mapName = args[1];
  166.  
  167. if(!maps.containsKey(mapName.toLowerCase())) {
  168. p.sendMessage(prefix + "Could not find a map with that name!");
  169. return;
  170. }
  171.  
  172. Cuboid cuboid = PartyGames.api.getPlayerSelection(p);
  173.  
  174. if(cuboid == null) {
  175. p.sendMessage(prefix + "You must select the 2 corners of the floor first! Use '/pg wand' to receive the selection wand!");
  176. return;
  177. }
  178.  
  179. PartyGames.api.removePlayerSelection(p);
  180.  
  181. ((SpleefMap) maps.get(mapName.toLowerCase())).floorCuboid = cuboid;
  182.  
  183. FileConfiguration config = getConfiguration();
  184.  
  185. config.set("Maps." + mapName + ".Floor-Cuboid", cuboid.toString());
  186.  
  187. saveConfiguration(config);
  188.  
  189. p.sendMessage(prefix + "Floor has been set successfully!");
  190. return;
  191. }
  192.  
  193. if(subCommand.equals("list")) {
  194. if(maps.isEmpty()) {
  195. p.sendMessage(prefix + "No maps are loaded!");
  196. return;
  197. }
  198. p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Maps " + ChatColor.DARK_GREEN + " -------- ");
  199. for(MiniGameMap map: maps.values()) {
  200. p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + map.name + ChatColor.DARK_GREEN + " - Is Setup: " + ChatColor.GREEN + map.isSetup());
  201. }
  202. return;
  203. }
  204.  
  205. p.sendMessage(prefix + "Unknown command!");
  206. }
  207.  
  208. @Override
  209. public void load() {
  210. maps = new HashMap<String, MiniGameMap>();
  211.  
  212. FileConfiguration config = getConfiguration();
  213.  
  214. boolean save = false;
  215. if(!config.contains("Tutorial-Messages")) {
  216. config.set("Tutorial-Messages", Arrays.asList(new String[] {
  217. "&2 --------- &aSpleef &2--------- ",
  218. "- Use your shovel to break blocks below",
  219. "other players to eliminate them"
  220. }));
  221. save = true;
  222. }
  223.  
  224. if(!config.contains("Shovel")) {
  225. config.set("Shovel", "DIAMOND_SPADE : 1 : enchant:DIG_SPEED:4");
  226. save = true;
  227. }
  228.  
  229. if(!config.contains("Round-Duration")) {
  230. config.set("Round-Duration", 90);
  231. save = true;
  232. }
  233.  
  234. if(!config.contains("Give-Snowballs-On-Break")) {
  235. config.set("Give-Snowballs-On-Break", true);
  236. save = true;
  237. }
  238.  
  239. if(save) saveConfiguration(config);
  240.  
  241. List<String> temp = config.getStringList("Tutorial-Messages");
  242. tutorialMessages = new String[temp.size()];
  243. for(int i = 0; i < tutorialMessages.length; i++) tutorialMessages[i] = ChatColor.translateAlternateColorCodes('&', temp.get(i));
  244.  
  245. if(config.getConfigurationSection("Maps") != null && !config.getConfigurationSection("Maps").getKeys(false).isEmpty()) {
  246. for(String mapName: config.getConfigurationSection("Maps").getKeys(false)) {
  247. Cuboid cuboid = new Cuboid(config.getString("Maps." + mapName + ".Map-Cuboid"));
  248. Cuboid floorCuboid = config.contains("Maps." + mapName + ".Floor-Cuboid") ? new Cuboid(config.getString("Maps." + mapName + ".Floor-Cuboid")) : null;
  249. Location spawnpoint = config.contains("Maps." + mapName + ".Spawnpoint") ? PartyGames.plugin.getLocationFromString(config.getString("Maps." + mapName + ".Spawnpoint")) : null;
  250. maps.put(mapName.toLowerCase(), new SpleefMap(mapName, cuboid, floorCuboid, spawnpoint));
  251. }
  252. }
  253.  
  254. shovel = PartyGames.api.getItemStackFromString(config.getString("Shovel"));
  255. roundDuration = config.getInt("Round-Duration");
  256. snowball = new ItemStack(Material.SNOW_BALL);
  257. giveSnowballs = config.getBoolean("Give-Snowballs-On-Break");
  258. }
  259.  
  260. @Override
  261. public int getAliveCount(String mapName) {
  262. SpleefMap map = (SpleefMap) maps.get(mapName.toLowerCase());
  263. return map.alive.size();
  264. }
  265.  
  266. private class SpleefMap extends MiniGameMap implements Listener {
  267.  
  268. Cuboid mainCuboid;
  269. Cuboid floorCuboid;
  270. Location spawnpoint;
  271.  
  272. ArrayList<String> alive;
  273. String[] topPlayers;
  274.  
  275. HashMap<Location, BlockState> rollback;
  276.  
  277. BukkitTask task;
  278.  
  279. boolean allowBreaking;
  280.  
  281. public SpleefMap(String name, Cuboid mainCuboid, Cuboid floorCuboid, Location spawnpoint) {
  282. this.name = name;
  283. this.mainCuboid = mainCuboid;
  284. this.floorCuboid = floorCuboid;
  285. this.spawnpoint = spawnpoint;
  286.  
  287. alive = new ArrayList<String>();
  288. topPlayers = new String[3];
  289.  
  290. rollback = new HashMap<Location, BlockState>();
  291.  
  292. allowBreaking = false;
  293.  
  294. Bukkit.getPluginManager().registerEvents(this, plugin);
  295. }
  296.  
  297. public boolean isSetup() {
  298. return floorCuboid != null && spawnpoint != null;
  299. }
  300.  
  301. public void start() {
  302. for(int i = 0; i < topPlayers.length; i++) topPlayers[i] = null;
  303.  
  304. for(Player p: arena.getPlayers()) {
  305. p.getInventory().addItem(shovel);
  306. p.teleport(spawnpoint);
  307. alive.add(p.getName());
  308. }
  309.  
  310. for(Player p: arena.getSpectators()) p.teleport(spawnpoint);
  311.  
  312. startTask();
  313. }
  314.  
  315. public void cancel(boolean immediate) {
  316. allowBreaking = false;
  317.  
  318. alive.clear();
  319.  
  320. arena = null;
  321.  
  322. PartyGames.api.rollback(rollback, immediate);
  323.  
  324. mainCuboid.clearEntities();
  325.  
  326. if(task != null) {
  327. task.cancel();
  328. task = null;
  329. }
  330.  
  331. }
  332.  
  333. public void leave(Player p) {
  334. if(alive.contains(p.getName())) {
  335. alive.remove(p.getName());
  336. if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
  337. if(alive.size() == 1) {
  338. finish();
  339. }
  340. }
  341. }
  342.  
  343. private void startTask() {
  344. task = new BukkitRunnable() {
  345. int breakingTimer = 5;
  346. int timer = roundDuration;
  347. public void run() {
  348.  
  349. if(breakingTimer > 0) {
  350. breakingTimer--;
  351. if(breakingTimer == 4) arena.sendPlayCounterNotification(PlayCounter.READY);
  352. else if(breakingTimer == 2) arena.sendPlayCounterNotification(PlayCounter.SET);
  353. else if(breakingTimer == 0) {
  354. arena.sendPlayCounterNotification(PlayCounter.GO);
  355. allowBreaking = true;
  356. }
  357. } else {
  358.  
  359. for(Player p: arena.getPlayers()) {
  360. p.setLevel(timer);
  361. if(alive.contains(p.getName())) {
  362. if(p.getLocation().getY() < floorCuboid.y1) {
  363. eliminatePlayer(p);
  364. }
  365. }
  366. }
  367.  
  368. timer--;
  369. if(timer == 0) {
  370. finish();
  371. }
  372. }
  373.  
  374. }
  375. }.runTaskTimer(plugin, 40L, 20L);
  376. }
  377.  
  378. private void eliminatePlayer(Player p) {
  379. alive.remove(p.getName());
  380. p.sendMessage(PartyGames.api.getMessage("Player-Lose-Round"));
  381. p.getInventory().clear();
  382. String eliminationMessage = PartyGames.api.getMessage("Player-Lose-Round-Announce").replace("%player%", p.getName());
  383. for(Player x: arena.getAllPlayers()) x.sendMessage(eliminationMessage);
  384. if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
  385. PartyGames.api.MakeSpectator(p, true);
  386. if(alive.size() < 3) topPlayers[alive.size()] = p.getName(); //If size is 2, then this player is 3rd which is number 2 in []
  387. if(alive.size() == 1) {
  388. finish();
  389. }
  390. }
  391.  
  392. private void finish() {
  393. if(task != null) {
  394. task.cancel();
  395. task = null;
  396. }
  397.  
  398. for(int i = 0; i < alive.size(); i++) if(i < topPlayers.length) topPlayers[i] = alive.get(i);
  399.  
  400. PartyGames.api.sendRoundConclusion(arena, topPlayers);
  401.  
  402. String winnerName = topPlayers[0];
  403. final Player winner = Bukkit.getPlayer(winnerName);
  404.  
  405. task = new BukkitRunnable() {
  406. int timer = 5;
  407. boolean fireworks = false;
  408. public void run() {
  409. timer--;
  410.  
  411. fireworks = !fireworks;
  412. if(fireworks) {
  413. if(winner != null) PartyGames.plugin.fireWorkEffect(winner, true);
  414. }
  415.  
  416. if(timer == 0) {
  417. for(Player p: arena.getPlayers()) {
  418. PartyGames.api.MakeSpectator(p, false);
  419. }
  420. arena.finishCurrentMinigame(topPlayers);
  421. }
  422. }
  423. }.runTaskTimer(plugin, 0, 20L);
  424. }
  425.  
  426. @EventHandler(priority = EventPriority.HIGH)
  427. public void onBlockBreakEvent(BlockBreakEvent e) {
  428. Player p = e.getPlayer();
  429. if(allowBreaking && alive.contains(p.getName()) && floorCuboid.contains(e.getBlock().getLocation())) {
  430. e.setCancelled(false);
  431. rollback.put(e.getBlock().getLocation(), e.getBlock().getState());
  432. if(giveSnowballs) p.getInventory().addItem(snowball);
  433. }
  434. }
  435.  
  436. @EventHandler
  437. public void onProjectileHitEvent(ProjectileHitEvent e) {
  438. if(!allowBreaking || !(e.getEntity() instanceof Snowball)) return;
  439. BlockIterator bi = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 4);
  440. Block hit = null;
  441. while (bi.hasNext()) {
  442. hit = bi.next();
  443. if (!hit.getType().equals(Material.AIR)) {
  444. if(mainCuboid.contains(hit.getLocation()) && !rollback.containsKey(hit.getLocation())) {
  445. rollback.put(hit.getLocation(), hit.getState());
  446. hit.setType(Material.AIR);
  447. }
  448. break;
  449. }
  450. }
  451. }
  452.  
  453. public void handlePlayerInteractEvent(PlayerInteractEvent e) {
  454. Player p = e.getPlayer();
  455. if(allowBreaking && alive.contains(p.getName()) && p.getItemInHand() != null && p.getItemInHand().getType().equals(Material.SNOW_BALL)) {
  456. e.setCancelled(false);
  457. p.playSound(p.getLocation(), PartyGames.plugin.ITEM_PICKUP, 1, 1);
  458. }
  459. }
  460. public void handleEntityDamageByEntityEvent(EntityDamageByEntityEvent e) {}
  461.  
  462. }
  463.  
  464. }
  465.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement