Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.skdev.spawns;
- import java.util.Set;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- public class MultiSpawns extends JavaPlugin {
- private final MultiSpawns plugin = this;
- private enum SubCmd{ // Sub Command Aliases
- SET (new String[]{"set","assign"}),
- REMOVE (new String[]{"remove","rem","rm","delete","del"}),
- SPAWN (new String[]{"spawn","tp","tele","teleport"});
- private final String s[];
- SubCmd(String s[]){ this.s = s; }
- static SubCmd getCmd(String s){
- for (SubCmd sc : SubCmd.values()){
- for (String v : sc.s){
- if (v.equalsIgnoreCase(s)) return sc;
- }
- }
- return null;
- }
- }
- private static boolean isInt(String s){
- try{
- Integer.parseInt(s);
- return true;
- }catch(NumberFormatException e){
- return false;
- }
- }
- public void onEnable(){
- plugin.getCommand("ms").setExecutor(new CommandExecutor(){
- @Override
- public boolean onCommand(CommandSender sender, Command cmd,
- String label, String[] args) {
- SubCmd sc = SubCmd.getCmd(args[0]);
- if (sender instanceof Player){
- Player p = (Player) sender;
- switch (sc){
- case SET: // /ms set <[int] | [int string] | [string] | []>
- Location loc = p.getLocation();
- if (p.hasPermission("ms.set")){
- if (args.length >= 2 && args[1] != null && !(args[1] instanceof String)){
- if (isInt(args[1])){
- String node = "Spawn." + Integer.parseInt(args[1]);
- plugin.getConfig().set(node, null); // overwrite any existing
- plugin.getConfig().set(node + ".World", loc.getWorld().getName());
- plugin.getConfig().set(node + ".X", loc.getBlockX());
- plugin.getConfig().set(node + ".Y", loc.getBlockY());
- plugin.getConfig().set(node + ".Z", loc.getBlockZ());
- if (args[2] != null){ // int string
- plugin.getConfig().set(node + ".Name", args[2]);
- }
- //TODO: Message to player of successful assignment.
- }else{
- //TODO: Display Syntax error
- }
- }else{ // Assign unused number [string] | []
- Set<String> keys = plugin.getConfig().getKeys(false);
- String last = keys.toArray(new String[keys.size()])[keys.size()];
- if (isInt(last)){
- int i = Integer.parseInt(last) +1;
- String node = "Spawn." + i;
- plugin.getConfig().set(node, null); // overwrite any existing
- plugin.getConfig().set(node + ".World", loc.getWorld().getName());
- plugin.getConfig().set(node + ".X", loc.getBlockX());
- plugin.getConfig().set(node + ".Y", loc.getBlockY());
- plugin.getConfig().set(node + ".Z", loc.getBlockZ());
- if (args[1] != null){
- plugin.getConfig().set(node + ".Name", args[1]);
- }
- }else{
- // TODO: Display Parse error
- }
- }
- }
- break;
- case REMOVE: // /ms remove <[int] | [string]>
- if (p.hasPermission("ms.rm")){
- if (isInt(args[1])){ // Find by id
- int i = Integer.parseInt(args[1]);;
- plugin.getConfig().set("Spawn." + i , null);
- // TODO: Display confirmation message
- }else{ // Find by name
- for (String key : plugin.getConfig().getConfigurationSection("Spawn").getKeys(true)){
- if (key.toLowerCase().contains(args[1].toLowerCase())){
- String path = plugin.getConfig().getConfigurationSection(key).getParent().getCurrentPath();
- plugin.getConfig().set(path, null);
- }
- }
- }
- }
- break;
- case SPAWN: // /ms spawn <*|int>
- // ms.spawn.<*|int>
- try{
- int x,y,z;
- String world;
- if (isInt(args[1])){ // Find by id
- int i = Integer.parseInt(args[1]);
- String node = "Spawn." + i;
- x = plugin.getConfig().getInt(node + ".X");
- y = plugin.getConfig().getInt(node + ".Y");
- z = plugin.getConfig().getInt(node + ".Z");
- world = plugin.getConfig().getString(node + ".World");
- Location newLoc = new Location(Bukkit.getWorld(world), x, y, z);
- p.teleport(newLoc);
- }else{ // Find by name
- for (String key : plugin.getConfig().getConfigurationSection("Spawn").getKeys(true)){
- if (key.toLowerCase().contains(args[1].toLowerCase())){
- String path = plugin.getConfig().getConfigurationSection(key).getParent().getCurrentPath();
- x = plugin.getConfig().getInt(path + ".X");
- y = plugin.getConfig().getInt(path + ".Y");
- z = plugin.getConfig().getInt(path + ".Z");
- world = plugin.getConfig().getString(path + ".World");
- Location newLoc = new Location(Bukkit.getWorld(world), x, y, z);
- p.teleport(newLoc);
- break;
- }
- }
- }
- }catch(Exception e){
- // TODO: Display Config Error, unable to find warp point
- }
- break;
- default:
- }
- }else{ // Non-player
- switch (sc){
- case SET:
- break;
- case REMOVE:
- break;
- default:
- }
- }
- return false;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement