Last active
January 3, 2016 12:28
-
-
Save samuelhnrq/8462587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.samosaara; | |
import com.sk89q.worldedit.Vector; | |
import com.sk89q.worldedit.regions.CuboidRegion; | |
import io.github.samosaara.Exeptions.SpawnOutsideAreaException; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.Inventory; | |
/** | |
* | |
* @Author Jake | |
*/ | |
public class ArenaManager { | |
private static ArenaManager am = new ArenaManager(); | |
//Usefull for getting the ArenaManager, like so: ArenaManager.getManager() | |
public static ArenaManager getManager() { | |
return am; | |
} | |
//A method for getting one of the Arenas out of the list by name: | |
public Arena getArena(String name) { | |
for (Arena a: Arena.arenaObjects) { //For all of the arenas in the list of objects | |
if (a.getName().equals(name)) { //If the name of an arena object in the list is equal to the one in the parameter... | |
return a; //Return that object | |
} | |
} | |
return null; //No objects were found, return null | |
} | |
//A method for adding players | |
public Inventory addPlayers(Player player, String arenaName) { | |
Inventory old; | |
if (getArena(arenaName) != null) { //If the arena exsists | |
Arena arena = getArena(arenaName); //Create an arena for using in this method | |
if (!arena.isFull()) { //If the arena is not full | |
if (!arena.isInGame()) { | |
old= player.getInventory(); | |
//Every check is complete, arena is joinable | |
player.getInventory().clear(); //Clear the players inventory | |
player.setHealth(player.getMaxHealth()); //Heal the player | |
player.setFireTicks(0); //Heal the player even more ^ ^ ^ | |
//Teleport to the arena's join location | |
player.teleport(arena.getJoinLocation()); | |
//Add the player to the arena list | |
arena.getPlayers().add(player.getName()); //Add the players name to the arena | |
int playersLeft = arena.getMaxPlayers() - arena.getPlayers().size(); //How many players needed to start | |
//Send the arena's players a message | |
arena.sendMessage(ChatColor.BLUE + player.getName() + " has joined the arena! We only need " + playersLeft + " to start the game!"); | |
if (playersLeft == 0) { //IF there are 0 players needed to start the game | |
startArena(arenaName); //Start the arena, see the method way below :) | |
} | |
} else { //Specifiend arena is in game, send the player an error message | |
player.sendMessage(ChatColor.RED + "The arena you are looking for is currently full!"); | |
return null; | |
} | |
} else { //Specified arena is full, send the player an error message | |
player.sendMessage(ChatColor.RED + "The arena you are looking for is currently full!"); | |
return null; | |
} | |
} else { //The arena doesn't exsist, send the player an error message | |
player.sendMessage(ChatColor.RED + "The arena you are looking for could not be found!"); | |
return null; | |
} | |
return old; | |
} | |
public void removePlayer(Player player, String arenaName) { | |
removePlayer(player, arenaName, Bukkit.getOfflinePlayer("BOB").getPlayer().getInventory()); | |
} | |
//A method for removing players | |
public void removePlayer(Player player, String arenaName, Inventory old) { | |
if (getArena(arenaName) != null) { //If the arena exsists | |
Arena arena = getArena(arenaName); //Create an arena for using in this method | |
if (arena.getPlayers().contains(player.getName())) { //If the arena has the player already | |
//Every check is complete, arena is leaveable | |
player.getInventory().clear(); //Clear the players inventory | |
player.setHealth(player.getMaxHealth()); //Heal the player | |
player.setFireTicks(0); //Heal the player even more ^ ^ ^ | |
//Teleport to the arena's join location | |
player.teleport(arena.getEndLocation()); | |
//remove the player to the arena list | |
arena.getPlayers().remove(player.getName()); //Removes the players name to the arena | |
//Send the arena's players a message | |
arena.sendMessage(ChatColor.BLUE + player.getName() + " has left the Arena! There are " + arena.getPlayers().size() + "players currently left!"); | |
} else { //Specified arena doesn't have the player, send the player an error message | |
player.sendMessage(ChatColor.RED + "Your not in the arena your looking for!"); | |
} | |
} else { //The arena doesn't exsist, send the player an error message | |
player.sendMessage(ChatColor.RED + "The arena you are looking for could not be found!"); | |
} | |
} | |
//A method for starting an Arena: | |
public void startArena(String arenaName) { | |
if (getArena(arenaName) != null) { //If the arena exsists | |
Arena arena = getArena(arenaName); //Create an arena for using in this method | |
arena.sendMessage(ChatColor.GOLD + "The arena has BEGUN!"); | |
//Set ingame | |
arena.setInGame(true); | |
for (String s: arena.getPlayers()) {//Loop through every player in the arena | |
Bukkit.getPlayer(s).teleport(arena.getStartLocation()); //Teleports the player to the arena start location | |
//Do custom stuff here, like give weapons etc, but for the purpose of this tutorial, i'll do nothing | |
//Set inGa | |
} | |
} | |
} | |
//A method for ending an Arena: | |
public void endArena(String arenaName) { | |
if (getArena(arenaName) != null) { //If the arena exsists | |
Arena arena = getArena(arenaName); //Create an arena for using in this method | |
//Send them a message | |
arena.sendMessage(ChatColor.GOLD + "The arena has ended :("); | |
//Set ingame | |
arena.setInGame(false); | |
for (String s: arena.getPlayers()) {//Loop through every player in the arena | |
//Teleport them: | |
Player player = Bukkit.getPlayer(s); //Create a player by the name | |
player.teleport(arena.getEndLocation()); | |
player.getInventory().clear(); //Clear the players inventory | |
player.setHealth(player.getMaxHealth()); //Heal the player | |
player.setFireTicks(0); //Heal the player even more ^ ^ ^ | |
//Remove them all from the list | |
arena.getPlayers().remove(player.getName()); | |
} | |
} | |
} | |
//And our final method, loading each arena | |
//This will be resonsible for creating each arena from the config, and creating an object to represent it | |
//Call this method in your main class, onEnable | |
public void loadArenas() { | |
//I just create a quick Config Variable, obviously don't do this. | |
//Use your own config file | |
FileConfiguration fc = null //If you just use this code, it will erorr, its null. Read the notes above, USE YOUR OWN CONFIGURATION FILE | |
//Youll get an error here, FOR THE LOVE OF GAWD READ THE NOTES ABOVE!!! | |
for (String keys: fc.getConfigurationSection("arenas").getKeys(false)) { //For each arena name in the arena file | |
//Now lets get all of the values, and make an Arena object for each: | |
//Just to help me remember: Arena myArena = new Arena("My Arena", joinLocation, startLocation, endLocation, 17) | |
World world = Bukkit.getWorld("arenas." + keys + ".world"); | |
String shortcut = "arenas."+keys; | |
//Arena's name is keys | |
double pos1x = fc.getDouble(shortcut + ".regPos1.x"); | |
double pos1y = fc.getDouble(shortcut + ".regPos1.y"); | |
double pos1z = fc.getDouble(shortcut + ".regPos1.z"); | |
double pos2x = fc.getDouble(shortcut + ".regPos2.x"); | |
double pos2y = fc.getDouble(shortcut + ".regPos2.y"); | |
double pos2z = fc.getDouble(shortcut + ".regPos2.z"); | |
CuboidRegion region = new CuboidRegion(new Vector(pos1x, pos1y, pos1z), new Vector(pos2x, pos2y, pos2z)); | |
double joinX = fc.getDouble("arenas." + keys + ".joinX"); | |
double joinY = fc.getDouble("arenas." + keys + ".joinY"); | |
double joinZ = fc.getDouble("arenas." + keys + ".joinZ"); | |
Location joinLocation = new Location(world, joinX, joinY, joinZ); | |
double startX = fc.getDouble("arenas." + keys + ".startX"); | |
double startY = fc.getDouble("arenas." + keys + ".startY"); | |
double startZ = fc.getDouble("arenas." + keys + ".startZ"); | |
Location startLocation = new Location(world, startX, startY, startZ); | |
double endX = fc.getDouble("arenas." + keys + "endX"); | |
double endY = fc.getDouble("arenas." + keys + "endX"); | |
double endZ = fc.getDouble("arenas." + keys + "endX"); | |
Location endLocation = new Location(world, endX, endY, endZ); | |
int maxPlayers = fc.getInt("arenas." + keys + ".maxPlayers"); | |
//Now lets create an object to represent it: | |
try { | |
Arena arena = new Arena(region, keys, joinLocation, startLocation, endLocation, maxPlayers); | |
} catch (SpawnOutsideAreaException e) { | |
e.printStackTrace(); | |
} | |
} | |
fc = null; | |
} | |
//Our final method, create arena! | |
public void createArena(CuboidRegion region, String arenaName, Location joinLocation, Location startLocation, Location endLocation, int maxPlayers) { | |
//Now, lets create an arena object to represent it: | |
try { | |
Arena arena = new Arena(region, arenaName, joinLocation, startLocation, endLocation, maxPlayers); | |
} catch (SpawnOutsideAreaException e) { | |
e.printStackTrace(); | |
} | |
//Now here is where you would save it all to a file, again, im going to create a null FileConfiguration, USE YOUR OWN!!! | |
FileConfiguration fc = null; //USE YOUR OWN PUNK | |
fc.set("arenas." + arenaName, null); //Set its name | |
//Now sets the other values | |
String path = "arenas." + arenaName + "."; //Shortcut | |
//Sets the paths | |
fc.set(path + "joinX", joinLocation.getX()); | |
fc.set(path + "joinY", joinLocation.getY()); | |
fc.set(path + "joinZ", joinLocation.getZ()); | |
fc.set(path + "startX", startLocation.getX()); | |
fc.set(path + "startY", startLocation.getY()); | |
fc.set(path + "startZ", startLocation.getZ()); | |
fc.set(path + "endX", endLocation.getX()); | |
fc.set(path + "endY", endLocation.getY()); | |
fc.set(path + "endZ", endLocation.getZ()); | |
fc.set(path + "maxPlayers", maxPlayers); | |
fc.set(path + "regPos1", null); | |
fc.set(path + "regPos1.x", region.getPos1().getX()); | |
fc.set(path + "regPos1.y", region.getPos1().getY()); | |
fc.set(path + "regPos1.z", region.getPos1().getZ()); | |
fc.set(path + "regPos2", null); | |
fc.set(path + "regPos2.x", region.getPos2().getX()); | |
fc.set(path + "regPos2.y", region.getPos2().getY()); | |
fc.set(path + "regPos2.z", region.getPos2().getZ()); | |
//Now save it up down here | |
fc=null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment