Advertisement
Guest User

Guardar / Cargar arenas.

a guest
Oct 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. // CREAR ARENA (GUARDARLA A UN ARCHIVO YAML Y VOLVERLO A LLAMAR DESPUÉS)
  2. private void createArena(String arenaName, Player player) {
  3.     WorldEditPlugin worldEdit = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"); // INICIALIZAMOS WORLD EDIT
  4.     if (worldEdit == null) { // SI NO SE ENCUENTRA
  5.         Log.Error("WorldEdit Not Load"); // PRINT
  6.     }else{ // SI SE ENCONTRÓ WORLD EDIT
  7.         Selection sel = worldEdit.getSelection(player); // RECOGEMOS
  8.         int X1 = sel.getMaximumPoint().getBlockX(); // LAS SELECCIONES
  9.         int Y1 = sel.getMaximumPoint().getBlockY(); // DESDE X1,Y1,Z1
  10.         int Z1 = sel.getMaximumPoint().getBlockZ(); // HASTA X2, Y2, Z2
  11.         int X2 = sel.getMinimumPoint().getBlockX();
  12.         int Y2 = sel.getMinimumPoint().getBlockY();
  13.         int Z2 = sel.getMinimumPoint().getBlockZ();
  14.         Location loc1 = new Location(sel.getWorld(),X1, Y1, Z1); // ASÍ ENCONTRAREMOS EL CUADRADO QUE HACEMOS CON WORLD EDIT
  15.         Location loc2 = new Location(sel.getWorld(),X2, Y2, Z2);
  16.         World world = sel.getWorld();
  17.         String name = arenaName;
  18.         Config saveFile = new Config(name, "NOMBREDETUPLUGIN", "Arenas"); // GUARDAMOS LA ARENA A UN ARCHIVO (VEÁSE ABAJO LA CLASE INHERENTE)
  19.         saveFile.delete();
  20.         saveFile.create();
  21.         for (int x = X2; x < X1; x++) { // formula para conseguir todos los bloques en ese cuadrado
  22.             for (int y = Y2; y < Y1; y++) {
  23.                 for (int z = Z2; z < Z1; z++) {
  24.                     Block block = world.getBlockAt(x, y, z); // recogemos cada bloque
  25.  
  26.                     saveFile.getConfig().set("BLOCKS." + x + "," + y + "," + z, // lo guardamos al
  27.                             block.getType().toString() + ":" + block.getData()); // archivo de configuracion previamente creado
  28.                 }
  29.             }
  30.         }
  31.         saveFile.getConfig().set("BLOCKS.WORLD", world.getName());
  32.         saveFile.getConfig().options().copyDefaults(true);
  33.         saveFile.save();
  34.     }
  35. }
  36.  
  37. // CARGAR UNA ARENA POR SEPARADO.
  38. private void loadArena(String arenaName) {
  39.     Config saveFile = new Config(arenaName, "NOMBREDETUPLUGIN", "Arenas"); // Conseguimos el archivo de configuración
  40.     ConfigurationSection privateSection = saveFile.getConfig().getConfigurationSection("BLOCKS"); // conseguimos las keys que habiamos creado antes
  41.     for (String location : privateSection.getKeys(false)) { // empezamos a agarrar todos los bloques (y su data) de la configuración
  42.         if (!location.equalsIgnoreCase("WORLD")){ // y los empezamos a colocar en donde estaban.
  43.             String[] cords = location.split(",");
  44.             int X = Integer.valueOf(cords[0]);
  45.             int Y = Integer.valueOf(cords[1]);
  46.             int Z = Integer.valueOf(cords[2]);
  47.             String[] Blockdata = saveFile.getConfig().getString("BLOCKS." + location).split(":");
  48.             String material = Blockdata[0];
  49.             World getter = Bukkit.getWorld(saveFile.getConfig().getString("BLOCKS.WORLD"));
  50.             byte data = Byte.valueOf(Blockdata[1]);
  51.             getter.getBlockAt(X, Y, Z).setType(Material.matchMaterial(material)); // aquí donde colocamos el material
  52.             getter.getBlockAt(X, Y, Z).setData(data); // aquí es donde colocamos la data del material     
  53.         }
  54.     }
  55. }
  56.  
  57. //Pequeña clase de configuración que hice, ustedes utilicen la suya o dejenla así, igual funciona.
  58. class Config(){
  59.     File fileDir;
  60.     File pFile;
  61.     FileConfiguration config;
  62.  
  63.     public Config(String fileName, String pluginName) {
  64.         this.FileDir = new File(Bukkit.getPluginManager().getPlugin(pluginName).getDataFolder(), "");
  65.         this.pFile = new File(Bukkit.getPluginManager().getPlugin(pluginName).getDataFolder(), fileName + ".yml");
  66.         this.config = YamlConfiguration.loadConfiguration(this.pFile);
  67.     }
  68.    
  69.     public Config(String fileName, String pluginName, String folder) {
  70.         try{
  71.             this.fileDir = new File(Bukkit.getPluginManager().getPlugin(pluginName).getDatafolder() + "\\" + folder, "");
  72.             this.pFile = new File(Bukkit.getPluginManager().getPlugin(pluginName).getDatafolder() + "\\" + folder, fileName + ".yml");
  73.             this.config = YamlConfiguration.loadConfiguration(this.pFile);  
  74.         }catch(YAMLException event){
  75.             event.printStackTrace();
  76.         }
  77.     }
  78.    
  79.     public FileConfiguration getConfig() {
  80.         return this.config;
  81.     }
  82.  
  83.     public void save() {
  84.         this.config.options().copyDefaults(true);
  85.         try {
  86.             this.config.save(this.pFile);
  87.         } catch (IOException var2) {
  88.             Log.Error(var2.getMessage());
  89.         }
  90.     }
  91.    
  92.     public void create() {
  93.         if(!this.fileDir.exists()) {
  94.             this.fileDir.mkdir();
  95.         }
  96.         if(!this.pFile.exists()) {
  97.             try {
  98.                 this.pFile.createNewFile();
  99.             } catch (Exception var2) {
  100.                 Log.Error(var2.getMessage());
  101.             }
  102.         }
  103.     }
  104.  
  105.     public void delete() {
  106.         this.pFile.delete();
  107.     }
  108.    
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement