Advertisement
Guest User

Spleef

a guest
Oct 10th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. SPLEEF CLASS
  2.  
  3. package me.kahjiit;
  4.  
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandSender;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. public class Spleef extends JavaPlugin {
  12.  
  13. public Arena arena = new Arena(this);
  14. public String tag = ("[" + ChatColor.AQUA + "Spleef" + ChatColor.WHITE + "] ");
  15.  
  16. public void onEnable() {
  17. getServer().getPluginManager().registerEvents(this.arena, this);
  18. getConfig().options().copyDefaults(true);
  19. saveConfig();
  20. }
  21.  
  22. public void onDisable() {
  23. saveConfig();
  24. }
  25.  
  26. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  27. if(command.getName().equalsIgnoreCase("spleef")) {
  28. Player player = (Player) sender;
  29. if(args.length == 0) {
  30. player.sendMessage(tag + ChatColor.DARK_AQUA + "= Spleef Commands =");
  31. player.sendMessage(tag + ChatColor.AQUA + "/Spleef join" + ChatColor.DARK_AQUA + ": " + ChatColor.GRAY + "Joins the game");
  32. player.sendMessage(tag + ChatColor.AQUA + "/Spleef leave" + ChatColor.DARK_AQUA + ": " + ChatColor.GRAY + "Leaves the game");
  33. player.sendMessage(tag + ChatColor.AQUA + "/Spleef version" + ChatColor.DARK_AQUA + ": " + ChatColor.GRAY + "See the version");
  34. if(player.hasPermission("spleef.admin")) {
  35. player.sendMessage(tag + ChatColor.AQUA + "/Spleef setlobby" + ChatColor.DARK_AQUA + ": " + ChatColor.GRAY + "Sets the lobby");
  36. player.sendMessage(tag + ChatColor.AQUA + "/Spleef setspawn" + ChatColor.DARK_AQUA + ": " + ChatColor.GRAY + "Sets the arena spawn");
  37. }
  38. }
  39. if(args.length == 1) {
  40. if(args[0].equalsIgnoreCase("join")) {
  41. arena.addPlayer(player);
  42. if(arena.players.size() == 1) {
  43. player.sendMessage(tag + ChatColor.AQUA + "You have joined the Spleef queue! Waiting for more players...");
  44. }
  45. else {
  46. player.sendMessage(tag + ChatColor.AQUA + "You have joined the Spleef queue!");
  47. arena.start();
  48. }
  49. }
  50. if(args[0].equalsIgnoreCase("leave")) {
  51. arena.removePlayer(player);
  52. player.sendMessage(tag + ChatColor.AQUA + "You have left the Spleef queue, thanks for playing!");
  53. }
  54. if(args[0].equalsIgnoreCase("version")) {
  55. arena.setLobby(player);
  56. player.sendMessage(tag + ChatColor.AQUA + "Running " + ChatColor.DARK_AQUA + "Spleef v1.0 " + ChatColor.AQUA + "by Kahjiit");
  57. }
  58. if(args[0].equalsIgnoreCase("setlobby")) {
  59. if(player.hasPermission("spleef.admin")) {
  60. arena.setLobby(player);
  61. player.sendMessage(tag + ChatColor.AQUA + "You have successfully set the lobby!");
  62. }
  63. else {
  64. player.sendMessage(ChatColor.RED + "You don't have permission.");
  65. }
  66. }
  67. if(args[0].equalsIgnoreCase("setspawn")) {
  68. if(player.hasPermission("spleef.admin")) {
  69. arena.setSpawn(player);
  70. player.sendMessage(tag + ChatColor.AQUA + "You have successfully set the spawn!");
  71. }
  72. else {
  73. player.sendMessage(ChatColor.RED + "You don't have permission.");
  74. }
  75. }
  76. if(args[0].equalsIgnoreCase("start")) {
  77. arena.start();
  78. }
  79. }
  80. if(args.length > 1) {
  81. player.sendMessage(tag + ChatColor.AQUA + "Too many arguments!");
  82. player.sendMessage(tag + ChatColor.AQUA + "Use /spleef instead.");
  83. }
  84. }
  85. return true;
  86. }
  87. }
  88.  
  89. ARENA CLASS
  90.  
  91. package me.kahjiit;
  92.  
  93. import java.util.ArrayList;
  94. import java.util.List;
  95. import java.util.UUID;
  96. import org.bukkit.Bukkit;
  97. import org.bukkit.ChatColor;
  98. import org.bukkit.Location;
  99. import org.bukkit.Material;
  100. import org.bukkit.entity.Player;
  101. import org.bukkit.event.EventHandler;
  102. import org.bukkit.event.Listener;
  103. import org.bukkit.event.entity.EntityDamageEvent;
  104. import org.bukkit.inventory.ItemStack;
  105.  
  106. public class Arena implements Listener {
  107.  
  108. Spleef plugin;
  109.  
  110. public Arena(Spleef plugin) {
  111. this.plugin = plugin;
  112. }
  113.  
  114. public List<UUID> players = new ArrayList<UUID>();
  115. private int countdown;
  116. private int game;
  117.  
  118. public void addPlayer(Player player) {
  119. if(!this.players.contains(player.getUniqueId())) {
  120. this.players.add(player.getUniqueId());
  121. }
  122. }
  123.  
  124. public void removePlayer(Player player) {
  125. if(this.players.contains(player.getUniqueId())) {
  126. this.players.remove(player.getUniqueId());
  127. }
  128. }
  129.  
  130. public void cycleMessage(String message) {
  131. for(UUID uuid : this.players) {
  132. if(Bukkit.getPlayer(uuid) != null) {
  133. Bukkit.getPlayer(uuid).sendMessage(message);
  134. }
  135. }
  136. }
  137.  
  138. public void setGameInventories() {
  139. for(UUID uuid : this.players) {
  140. if(Bukkit.getPlayer(uuid) != null) {
  141. Bukkit.getPlayer(uuid).getInventory().clear();
  142. Player player = Bukkit.getPlayer(uuid);
  143. if(Bukkit.getPlayer(uuid).hasPermission("spleef.perks")) {
  144. ItemStack diamond_shovel = new ItemStack(Material.DIAMOND_SPADE);
  145. player.getInventory().addItem(new ItemStack[] { diamond_shovel });
  146. }
  147. else {
  148. ItemStack iron_shovel = new ItemStack(Material.IRON_SPADE);
  149. player.getInventory().addItem(new ItemStack[] { iron_shovel });
  150. }
  151. }
  152. }
  153. }
  154.  
  155. public void setLobby(Player player) {
  156. if(!this.plugin.getConfig().contains("Spleef.Lobby")) {
  157. this.plugin.getConfig().addDefault("Spleef.Lobby" + ".X", player.getLocation().getX());
  158. this.plugin.getConfig().addDefault("Spleef.Lobby" + ".Y", player.getLocation().getY());
  159. this.plugin.getConfig().addDefault("Spleef.Lobby" + ".Z", player.getLocation().getZ());
  160. this.plugin.saveConfig();
  161. }
  162. else {
  163. this.plugin.getConfig().set("Spleef.Lobby.X", Double.valueOf(player.getLocation().getX()));
  164. this.plugin.getConfig().set("Spleef.Lobby.Y", Double.valueOf(player.getLocation().getY()));
  165. this.plugin.getConfig().set("Spleef.Lobby.Z", Double.valueOf(player.getLocation().getZ()));
  166. this.plugin.saveConfig();
  167. }
  168. }
  169.  
  170. public void setSpawn(Player player) {
  171. if(!this.plugin.getConfig().contains("Spleef.Spawn")) {
  172. this.plugin.getConfig().addDefault("Spleef.Spawn" + ".X", player.getLocation().getX());
  173. this.plugin.getConfig().addDefault("Spleef.Spawn" + ".Y", player.getLocation().getY());
  174. this.plugin.getConfig().addDefault("Spleef.Spawn" + ".Z", player.getLocation().getZ());
  175. this.plugin.saveConfig();
  176. }
  177. else {
  178. this.plugin.getConfig().set("Spleef.Spawn.X", Double.valueOf(player.getLocation().getX()));
  179. this.plugin.getConfig().set("Spleef.Spawn.Y", Double.valueOf(player.getLocation().getY()));
  180. this.plugin.getConfig().set("Spleef.Spawn.Z", Double.valueOf(player.getLocation().getZ()));
  181. this.plugin.saveConfig();
  182. }
  183. }
  184.  
  185. public void teleportLobby() {
  186. for(UUID uuid : this.players) {
  187. if(Bukkit.getPlayer(uuid) != null) {
  188. Player victim = Bukkit.getPlayer(uuid);
  189. if(this.plugin.getConfig().contains("Spleef.Lobby")) {
  190. double x = this.plugin.getConfig().getDouble("Spleef.Lobby.X");
  191. double y = this.plugin.getConfig().getDouble("Spleef.Lobby.Y");
  192. double z = this.plugin.getConfig().getDouble("Spleef.Lobby.Z");
  193. Location lobby = new Location(victim.getWorld(), x, y ,z);
  194. victim.teleport(lobby);
  195. }
  196. else {
  197. victim.sendMessage(plugin.tag + ChatColor.AQUA + "Couldn't teleport as there is no lobby set!");
  198. }
  199. }
  200. }
  201. }
  202.  
  203. public void teleportSpawn() {
  204. for(UUID uuid : this.players) {
  205. if(Bukkit.getPlayer(uuid) != null) {
  206. Player player = Bukkit.getPlayer(uuid);
  207. if(this.plugin.getConfig().contains("Spleef.Spawn")) {
  208. double x = this.plugin.getConfig().getDouble("Spleef.Spawn.X");
  209. double y = this.plugin.getConfig().getDouble("Spleef.Spawn.Y");
  210. double z = this.plugin.getConfig().getDouble("Spleef.Spawn.Z");
  211. Location spawn = new Location(player.getWorld(), x, y ,z);
  212. player.teleport(spawn);
  213. }
  214. else {
  215. player.sendMessage(plugin.tag + ChatColor.AQUA + "Couldn't teleport as there is no spawn set!");
  216. }
  217. }
  218. }
  219. }
  220.  
  221. public void start() {
  222. if(this.players.size() > 1) {
  223. this.countdown = plugin.getConfig().getInt("Spleef.Countdown");
  224. this.game = Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
  225. public void run() {
  226. if (Arena.this.countdown > 0) {
  227. if(Arena.this.countdown == 5 || Arena.this.countdown == 10) {
  228. Bukkit.broadcastMessage(plugin.tag + ChatColor.AQUA + "The game will be starting in " + Arena.this.countdown + " seconds!");
  229. }
  230. Arena.this.countdown --;
  231. }
  232. else {
  233. Bukkit.getScheduler().cancelTask(Arena.this.game);
  234. if(Arena.this.players.size() < 2) {
  235. Bukkit.broadcastMessage(plugin.tag + ChatColor.AQUA + "The game has been cancelled! Waiting for more players...");
  236. return;
  237. }
  238. Arena.this.setGameInventories();
  239. Arena.this.teleportSpawn();
  240. Arena.this.countdown += plugin.getConfig().getInt("Spleef.Countdown");
  241. }
  242. }
  243. }, 0, 20);
  244. }
  245. }
  246.  
  247. @EventHandler
  248. public void onDamage(EntityDamageEvent event) {
  249. if(event.getCause().equals(EntityDamageEvent.DamageCause.LAVA)) {
  250. Player player = (Player) event.getEntity();
  251. if(this.players.contains(player.getUniqueId())) {
  252. this.teleportLobby();
  253. player.getInventory().clear();
  254. this.cycleMessage(plugin.tag + ChatColor.DARK_AQUA + player.getName() + ChatColor.AQUA + " has been defeated!");
  255. player.sendMessage(plugin.tag + ChatColor.AQUA + "You have been defeated! Type /spleef join again to join the next game.");
  256. if(this.players.size() == 1) {
  257. Player winner = Bukkit.getPlayer(this.players.get(0));
  258. Bukkit.broadcastMessage(plugin.tag + ChatColor.DARK_AQUA + winner.getName() + ChatColor.AQUA + " has won the game!");
  259. }
  260. }
  261. }
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement