Advertisement
Guest User

Does not work

a guest
Aug 2nd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.98 KB | None | 0 0
  1. /*
  2.  * Cleanroom Generator
  3.  * Copyright (C) 2011-2012 nvx
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU Affero General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU Affero General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Affero General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19.  
  20. import org.bukkit.Location;
  21. import org.bukkit.Material;
  22. import org.bukkit.World;
  23. import org.bukkit.generator.ChunkGenerator;
  24.  
  25. import java.util.Arrays;
  26. import java.util.Random;
  27. import java.util.logging.Logger;
  28.  
  29. import static java.lang.System.arraycopy;
  30.  
  31. public class CleanroomChunkGenerator extends ChunkGenerator
  32. {
  33.     private Logger log = Logger.getLogger("Minecraft");
  34.  
  35.     private Material[] materials;
  36.  
  37.     public CleanroomChunkGenerator()
  38.     {
  39.         this("16,stone,32,dirt,1,grass_block");
  40.     }
  41.  
  42.     public CleanroomChunkGenerator(String id)
  43.     {
  44.         if (id != null)
  45.         {
  46.             try
  47.             {
  48.                 int y = 0;
  49.  
  50.                 materials = new Material[128]; // Default to 128, will be resized later if required
  51.                 materials[y++] = Material.BEDROCK;
  52.  
  53.                 if (id.length() > 0)
  54.                 {
  55.                     String tokens[] = id.split("[,]");
  56.  
  57.                     if ((tokens.length % 2) != 0)
  58.                     {
  59.                         throw new Exception();
  60.                     }
  61.  
  62.                     for (int i = 0; i < tokens.length; i += 2)
  63.                     {
  64.                         int height = Integer.parseInt(tokens[i]);
  65.                         if (height <= 0)
  66.                         {
  67.                             log.warning("[CleanroomGenerator] Invalid height '" + tokens[i] + "'. Using 64 instead.");
  68.                             height = 64;
  69.                         }
  70.  
  71.                         String materialTokens[] = tokens[i + 1].split("[:]", 2);
  72.  
  73.                         if (materialTokens.length == 2)
  74.                         {
  75.                             log.warning("[CleanroomGenerator] Data values are no longer supported in 1.13. Defaulting to the base material for " + materialTokens[0]);
  76.                         }
  77.  
  78.                         Material mat = Material.matchMaterial(materialTokens[0].toUpperCase());
  79.                         if (mat == null)
  80.                         {
  81.                             log.warning("[CleanroomGenerator] Invalid Block ID '" + materialTokens[0] + "'. Defaulting to stone. (Integer IDs were removed in 1.13)");
  82.                             mat = Material.STONE;
  83.                         }
  84.  
  85.                         if (!mat.isBlock())
  86.                         {
  87.                             log.warning("[CleanroomGenerator] Error, '" + materialTokens[0] + "' is not a block. Defaulting to stone.");
  88.                             mat = Material.STONE;
  89.                         }
  90.  
  91.                         if (y + height > materials.length)
  92.                         {
  93.                             Material[] newMaterials = new Material[Math.max(y + height, materials.length * 2)];
  94.  
  95.                             arraycopy(materials, 0, newMaterials, 0, y);
  96.                             materials = newMaterials;
  97.                         }
  98.  
  99.                         Arrays.fill(materials, y, y + height, mat);
  100.                         y += height;
  101.                     }
  102.                 }
  103.  
  104.                 // Trim to size
  105.                 if (materials.length > y)
  106.                 {
  107.                     Material[] newMaterials = new Material[y];
  108.                     arraycopy(materials, 0, newMaterials, 0, y);
  109.                     materials = newMaterials;
  110.                 }
  111.             }
  112.             catch (Exception e)
  113.             {
  114.                 log.severe("[CleanroomGenerator] Error parsing CleanroomGenerator ID '" + id + "'. using defaults '64,1': " + e.toString());
  115.                 e.printStackTrace();
  116.  
  117.                 materials = new Material[65];
  118.                 materials[0] = Material.BEDROCK;
  119.                 Arrays.fill(materials, 1, 65, Material.STONE);
  120.             }
  121.         }
  122.         else
  123.         {
  124.             materials = new Material[65];
  125.             materials[0] = Material.BEDROCK;
  126.             Arrays.fill(materials, 1, 65, Material.STONE);
  127.         }
  128.     }
  129.  
  130.     @Override
  131.     public ChunkData generateChunkData(World world, Random random, int x, int z, BiomeGrid biome)
  132.     {
  133.         int maxHeight = world.getMaxHeight();
  134.         if (materials.length > maxHeight)
  135.         {
  136.             log.warning("[CleanroomGenerator] Error, chunk height " + materials.length + " is greater than the world max height (" + maxHeight + "). Trimming to world max height.");
  137.             Material[] newMaterials = new Material[maxHeight];
  138.             arraycopy(materials, 0, newMaterials, 0, maxHeight);
  139.             materials = newMaterials;
  140.         }
  141.  
  142.         ChunkData result = createChunkData(world);
  143.  
  144.         for (int y = 0; y < materials.length; y++)
  145.         {
  146.  
  147.             result.setRegion(0, y, 0, 15, y, 15, materials[y]);
  148.         }
  149.  
  150.         return result;
  151.     }
  152.  
  153.     @Override
  154.     public Location getFixedSpawnLocation(World world, Random random)
  155.     {
  156.         if (!world.isChunkLoaded(0, 0))
  157.         {
  158.             world.loadChunk(0, 0);
  159.         }
  160.  
  161.         if ((world.getHighestBlockYAt(0, 0) <= 0) && (world.getBlockAt(0, 0, 0).getType() == Material.AIR)) // SPACE!
  162.         {
  163.             return new Location(world, 0, 64, 0); // Lets allow people to drop a little before hitting the void then shall we?
  164.         }
  165.  
  166.         return new Location(world, 0, world.getHighestBlockYAt(0, 0), 0);
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement