MrLucasX

CmdBlock Manager

Jan 21st, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map.Entry;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.World;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11. import org.bukkit.configuration.file.YamlConfiguration;
  12. import org.bukkit.entity.Player;
  13.  
  14. public class CmdBlockManager {
  15.  
  16.     private HashMap<Location, CmdBlock> cmds = new HashMap<Location, CmdBlock>();
  17.    
  18.     public void clickedBlock(Player p, Location loc){
  19.         if(cmds.containsKey(loc) && !cmds.get(loc).isDeleted()){
  20.            
  21.             if(cmds.get(loc).isConsole()){
  22.                 Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmds.get(loc).getCmd().replace("%p%", p.getName()));
  23.             }else{
  24.                 p.performCommand(cmds.get(loc).getCmd());
  25.             }
  26.            
  27.         }
  28.     }
  29.    
  30.     public void addCmdBlock(String id, Location location, String cmd, boolean console){
  31.        
  32.         // Sicher gehen, dass die Location nicht doppelt benutzt wird
  33.        
  34.         if(cmds.containsKey(location)){
  35.             cmds.remove(location);
  36.         }
  37.        
  38.         // Sicher gehen, dass die ID nicht doppelt benutzt wird
  39.        
  40.         Location l = null;
  41.        
  42.         for(Location loc : cmds.keySet()){
  43.             if(cmds.get(loc).getId().equals(id)){
  44.                 l = loc;
  45.                 break;
  46.             }
  47.         }
  48.        
  49.         if(l != null){
  50.             cmds.remove(l);
  51.         }
  52.        
  53.         cmds.put(location, new CmdBlock(id, cmd, console, false));
  54.        
  55.     }
  56.    
  57.     public void removeCmdBlock(Location location){
  58.        
  59.         if(location == null)return;
  60.        
  61.         if(cmds.containsKey(location)){
  62.             cmds.get(location).setDeleted(true);
  63.         }
  64.        
  65.     }
  66.    
  67.     public void removeCmdBlock(String id){
  68.        
  69.         Location l = null;
  70.        
  71.         for(Location loc : cmds.keySet()){
  72.             if(cmds.get(loc).getId().equals(id)){
  73.                 l = loc;
  74.                 break;
  75.             }
  76.         }
  77.        
  78.         removeCmdBlock(l);
  79.        
  80.     }
  81.    
  82.     public CmdBlock getCmdBlock(String id){
  83.        
  84.         for(Location loc : cmds.keySet()){
  85.             if(cmds.get(loc).getId().equals(id) && !cmds.get(loc).isDeleted()){
  86.                 return cmds.get(loc);
  87.             }
  88.         }
  89.        
  90.         return null;
  91.        
  92.     }
  93.    
  94.     public CmdBlock getCmdBlock(Location l){
  95.        
  96.         if(cmds.containsKey(l) && !cmds.get(l).isDeleted()){
  97.             return cmds.get(l);
  98.         }
  99.        
  100.         return null;
  101.        
  102.     }
  103.    
  104.     public Location getLocation(String id){
  105.        
  106.         for(Location loc : cmds.keySet()){
  107.             if(cmds.get(loc).getId().equals(id) && !cmds.get(loc).isDeleted()){
  108.                 return loc;
  109.             }
  110.         }
  111.        
  112.         return null;
  113.        
  114.     }
  115.    
  116.     public List<String> listIds(){
  117.        
  118.         List<String> list = new ArrayList<String>();
  119.        
  120.         for(Location loc : cmds.keySet()){
  121.             if(!cmds.get(loc).isDeleted()){
  122.                 list.add(cmds.get(loc).getId());
  123.             }
  124.         }
  125.        
  126.         return list;
  127.        
  128.     }
  129.    
  130.    
  131.     public void load() {
  132.        
  133.         // Config erstellen
  134.        
  135.         FileConfiguration cfg = getConfigFileConfiguration();
  136.         cfg.options().copyDefaults(true);
  137.        
  138.         try {
  139.             cfg.save(getConfigFile());
  140.         } catch (IOException e) {
  141.             e.printStackTrace();
  142.         }
  143.        
  144.         // Inhalte der Config in die HashMap laden
  145.        
  146.         for(String path : cfg.getKeys(false)){
  147.            
  148.             try{
  149.                
  150.                 Location l;
  151.                 String cmd;
  152.                 boolean console;
  153.                 World w = Bukkit.getWorld(cfg.getString(path + ".world"));
  154.                 double x = cfg.getDouble(path + ".x");
  155.                 double y = cfg.getDouble(path + ".y");
  156.                 double z = cfg.getDouble(path + ".z");
  157.                
  158.                 l = new Location(w,x,y,z);
  159.                 cmd = cfg.getString(path + ".cmd");
  160.                 console = cfg.getBoolean(path + ".console");
  161.                 addCmdBlock(path,l,cmd, console);
  162.  
  163.             }catch(Exception e){
  164.  
  165.             }
  166.            
  167.         }
  168.        
  169.     }
  170.    
  171.     public void save() {
  172.        
  173.         FileConfiguration cfg = getConfigFileConfiguration();
  174.        
  175.         // Inhalte der HashMap in die Config laden
  176.        
  177.         for(Entry<Location, CmdBlock> e : this.cmds.entrySet()){
  178.            
  179.             if(!e.getValue().isDeleted()){
  180.                
  181.                 cfg.set(e.getValue().getId() + ".world", e.getKey().getWorld().getName());
  182.                 cfg.set(e.getValue().getId() + ".x", e.getKey().getX());
  183.                 cfg.set(e.getValue().getId() + ".y", e.getKey().getY());
  184.                 cfg.set(e.getValue().getId() + ".z", e.getKey().getZ());
  185.                 cfg.set(e.getValue().getId() + ".cmd", e.getValue().getCmd());
  186.                 cfg.set(e.getValue().getId() + ".console", e.getValue().isConsole());
  187.                
  188.             }else{
  189.                
  190.                 cfg.set(e.getValue().getId() + ".world", null);
  191.                 cfg.set(e.getValue().getId() + ".x", null);
  192.                 cfg.set(e.getValue().getId() + ".y", null);
  193.                 cfg.set(e.getValue().getId() + ".z", null);
  194.                 cfg.set(e.getValue().getId() + ".cmd", null);
  195.                 cfg.set(e.getValue().getId() + ".console", null);
  196.                 cfg.set(e.getValue().getId(), null);
  197.                
  198.             }
  199.            
  200.         }
  201.        
  202.         try {
  203.             cfg.save(getConfigFile());
  204.         } catch (IOException e1) {
  205.             // TODO Auto-generated catch block
  206.             e1.printStackTrace();
  207.         }
  208.        
  209.     }
  210.    
  211.     public static File getConfigFile() {
  212.         return new File("plugins/cmdblock", "commands.yml");
  213.     }
  214.    
  215.     public static FileConfiguration getConfigFileConfiguration() {
  216.         return YamlConfiguration.loadConfiguration(getConfigFile());
  217.     }
  218.    
  219.     public class CmdBlock{
  220.        
  221.         private String id;
  222.         private String cmd;
  223.         private boolean deleted;
  224.         private boolean console;
  225.        
  226.         public CmdBlock(String id, String cmd, boolean console, boolean deleted){
  227.             this.setId(id);
  228.             this.setCmd(cmd);
  229.             this.console = console;
  230.             this.setDeleted(deleted);
  231.         }
  232.  
  233.         public String getId() {
  234.             return id;
  235.         }
  236.  
  237.         public void setId(String id) {
  238.             this.id = id;
  239.         }
  240.  
  241.         public String getCmd() {
  242.             return cmd;
  243.         }
  244.  
  245.         public void setCmd(String cmd) {
  246.             this.cmd = cmd;
  247.         }
  248.  
  249.         public boolean isDeleted() {
  250.             return deleted;
  251.         }
  252.  
  253.         public void setDeleted(boolean deleted) {
  254.             this.deleted = deleted;
  255.         }
  256.  
  257.         public boolean isConsole() {
  258.             return console;
  259.         }
  260.  
  261.         public void setConsole(boolean console) {
  262.             this.console = console;
  263.         }      
  264.        
  265.     }
  266.    
  267. }
Add Comment
Please, Sign In to add comment