Advertisement
Guest User

Bukkit - MultipleSpawns Plugin example

a guest
Nov 6th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.70 KB | None | 0 0
  1. package net.skdev.spawns;
  2.  
  3. import java.util.Set;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandExecutor;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. public class MultiSpawns extends JavaPlugin {
  14.  
  15.     private final MultiSpawns plugin = this;
  16.    
  17.     private enum SubCmd{ // Sub Command Aliases
  18.        
  19.         SET     (new String[]{"set","assign"}),
  20.         REMOVE  (new String[]{"remove","rem","rm","delete","del"}),
  21.         SPAWN   (new String[]{"spawn","tp","tele","teleport"});
  22.        
  23.         private final String s[];
  24.        
  25.         SubCmd(String s[]){ this.s = s; }
  26.        
  27.         static SubCmd getCmd(String s){
  28.             for (SubCmd sc : SubCmd.values()){
  29.                 for (String v : sc.s){
  30.                     if (v.equalsIgnoreCase(s)) return sc;
  31.                 }
  32.             }
  33.             return null;
  34.         }
  35.        
  36.     }
  37.    
  38.     private static boolean isInt(String s){
  39.         try{
  40.             Integer.parseInt(s);
  41.             return true;
  42.         }catch(NumberFormatException e){
  43.             return false;
  44.         }
  45.     }
  46.    
  47.     public void onEnable(){
  48.        
  49.    
  50.        
  51.         plugin.getCommand("ms").setExecutor(new CommandExecutor(){
  52.  
  53.             @Override
  54.             public boolean onCommand(CommandSender sender, Command cmd,
  55.                     String label, String[] args) {
  56.                
  57.                 SubCmd sc = SubCmd.getCmd(args[0]);
  58.                
  59.                 if (sender instanceof Player){
  60.                     Player p = (Player) sender;
  61.                     switch (sc){
  62.                    
  63.                         case SET: // /ms set <[int] | [int string] | [string] | []>
  64.                             Location loc = p.getLocation();
  65.                             if (p.hasPermission("ms.set")){
  66.                                 if (args.length >= 2 && args[1] != null && !(args[1] instanceof String)){
  67.                                    
  68.                                     if (isInt(args[1])){
  69.                                         String node = "Spawn." + Integer.parseInt(args[1]);
  70.                                        
  71.                                         plugin.getConfig().set(node, null); // overwrite any existing
  72.                                        
  73.                                         plugin.getConfig().set(node + ".World", loc.getWorld().getName());
  74.                                         plugin.getConfig().set(node + ".X", loc.getBlockX());
  75.                                         plugin.getConfig().set(node + ".Y", loc.getBlockY());
  76.                                         plugin.getConfig().set(node + ".Z", loc.getBlockZ());
  77.                                         if (args[2] != null){ // int string
  78.                                             plugin.getConfig().set(node + ".Name", args[2]);
  79.                                         }
  80.                                        
  81.                                         //TODO: Message to player of successful assignment.
  82.                                     }else{
  83.                                        
  84.                                         //TODO: Display Syntax error
  85.                                        
  86.                                     }
  87.                                    
  88.                                 }else{ // Assign unused number [string] | []
  89.                                    
  90.                                     Set<String> keys = plugin.getConfig().getKeys(false);
  91.                                     String last = keys.toArray(new String[keys.size()])[keys.size()];
  92.                                     if (isInt(last)){
  93.                                         int i = Integer.parseInt(last) +1;
  94.                                         String node = "Spawn." + i;
  95.                                        
  96.                                         plugin.getConfig().set(node, null); // overwrite any existing
  97.                                        
  98.                                         plugin.getConfig().set(node + ".World", loc.getWorld().getName());
  99.                                         plugin.getConfig().set(node + ".X", loc.getBlockX());
  100.                                         plugin.getConfig().set(node + ".Y", loc.getBlockY());
  101.                                         plugin.getConfig().set(node + ".Z", loc.getBlockZ());
  102.                                         if (args[1] != null){
  103.                                             plugin.getConfig().set(node + ".Name", args[1]);
  104.                                         }
  105.                                     }else{
  106.                                         // TODO: Display Parse error
  107.                                     }
  108.                                    
  109.                                 }
  110.                             }
  111.                             break;
  112.                            
  113.                         case REMOVE: // /ms remove <[int] | [string]>
  114.                             if (p.hasPermission("ms.rm")){
  115.                                 if (isInt(args[1])){ // Find by id
  116.                                     int i = Integer.parseInt(args[1]);;
  117.                                     plugin.getConfig().set("Spawn." + i , null);
  118.                                    
  119.                                     // TODO: Display confirmation message
  120.                                 }else{ // Find by name
  121.                                    
  122.                                     for (String key : plugin.getConfig().getConfigurationSection("Spawn").getKeys(true)){
  123.                                         if (key.toLowerCase().contains(args[1].toLowerCase())){
  124.                                             String path = plugin.getConfig().getConfigurationSection(key).getParent().getCurrentPath();
  125.                                             plugin.getConfig().set(path, null);
  126.                                         }
  127.                                     }
  128.                                    
  129.                                 }
  130.                             }
  131.                             break;
  132.                            
  133.                         case SPAWN: // /ms spawn <*|int>
  134.                             // ms.spawn.<*|int>
  135.                             try{
  136.                                
  137.                                 int x,y,z;
  138.                                 String world;
  139.                                
  140.                                 if (isInt(args[1])){ // Find by id
  141.                                     int i = Integer.parseInt(args[1]);
  142.                                     String node = "Spawn." + i;
  143.                                     x = plugin.getConfig().getInt(node + ".X");
  144.                                     y = plugin.getConfig().getInt(node + ".Y");
  145.                                     z = plugin.getConfig().getInt(node + ".Z");
  146.                                     world = plugin.getConfig().getString(node + ".World");
  147.                                     Location newLoc = new Location(Bukkit.getWorld(world), x, y, z);
  148.                                     p.teleport(newLoc);
  149.                                    
  150.                                 }else{ // Find by name
  151.                                     for (String key : plugin.getConfig().getConfigurationSection("Spawn").getKeys(true)){
  152.                                         if (key.toLowerCase().contains(args[1].toLowerCase())){
  153.                                            
  154.                                             String path = plugin.getConfig().getConfigurationSection(key).getParent().getCurrentPath();
  155.                                             x = plugin.getConfig().getInt(path + ".X");
  156.                                             y = plugin.getConfig().getInt(path + ".Y");
  157.                                             z = plugin.getConfig().getInt(path + ".Z");
  158.                                             world = plugin.getConfig().getString(path + ".World");
  159.                                             Location newLoc = new Location(Bukkit.getWorld(world), x, y, z);
  160.                                             p.teleport(newLoc);
  161.                                            
  162.                                             break;
  163.                                         }
  164.                                     }
  165.                                 }
  166.                             }catch(Exception e){
  167.                                 // TODO: Display Config Error, unable to find warp point
  168.                             }
  169.                             break;
  170.                        
  171.                         default:
  172.                    
  173.                     }
  174.                    
  175.                 }else{ // Non-player
  176.                    
  177.                     switch (sc){
  178.                        
  179.                         case SET:
  180.                            
  181.                             break;
  182.                            
  183.                         case REMOVE:
  184.                            
  185.                             break;
  186.                        
  187.                         default:
  188.                    
  189.                     }
  190.                    
  191.                 }
  192.                
  193.                 return false;
  194.             }
  195.        
  196.         });
  197.        
  198.     }
  199.    
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement