Wouto1997

fillflowersv1

Jun 23rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  2. import com.sk89q.worldedit.bukkit.selections.Selection;
  3. import net.menoni.rd.Debugger;
  4. import net.menoni.rd.RuntimeDebugger;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.World;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.block.BlockFace;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.entity.Player;
  13.  
  14. import java.security.SecureRandom;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Random;
  18.  
  19. public class FillFlowers implements Debugger {
  20.  
  21.     private static List<Blocky> flowerList;
  22.     private static Random random;
  23.  
  24.     private static class Blocky {
  25.         private Material material;
  26.         private byte data;
  27.         private int height;
  28.         private int weight;
  29.  
  30.         public Blocky(Material material, int weight) {
  31.             this(material, (byte) 0, weight);
  32.         }
  33.        
  34.         public Blocky(Material material, int data, int weight) {
  35.             this(material, data, 1, weight);
  36.         }
  37.  
  38.         public Blocky(Material material, int data, int height, int weight) {
  39.             this.material = material;
  40.             this.height = height;
  41.             this.data = (byte) data;
  42.             this.weight = weight;
  43.         }
  44.  
  45.         public int getHeight() {
  46.             return height;
  47.         }
  48.  
  49.         public Material getMaterial() {
  50.             return material;
  51.         }
  52.  
  53.         public byte getData() {
  54.             return data;
  55.         }
  56.  
  57.         public int getWeight() {
  58.             return this.weight;
  59.         }
  60.  
  61.     }
  62.  
  63.     static {
  64.         random = new SecureRandom();
  65.         flowerList = new ArrayList<>();
  66.         flowerList.add(new Blocky(Material.YELLOW_FLOWER, 1));
  67.         flowerList.add(new Blocky(Material.LONG_GRASS, 1, 5));
  68.         flowerList.add(new Blocky(Material.LONG_GRASS, 2, 5));
  69.         flowerList.add(new Blocky(Material.RED_ROSE, 0, 1));
  70.         flowerList.add(new Blocky(Material.RED_ROSE, 1, 1));
  71.         flowerList.add(new Blocky(Material.RED_ROSE, 2, 1));
  72.         flowerList.add(new Blocky(Material.RED_ROSE, 3, 1));
  73.         flowerList.add(new Blocky(Material.RED_ROSE, 4, 1));
  74.         flowerList.add(new Blocky(Material.RED_ROSE, 5, 1));
  75.         flowerList.add(new Blocky(Material.RED_ROSE, 6, 1));
  76.         flowerList.add(new Blocky(Material.RED_ROSE, 7, 1));
  77.         flowerList.add(new Blocky(Material.RED_ROSE, 8, 1));
  78.         flowerList.add(new Blocky(Material.DOUBLE_PLANT, 0, 2, 1));
  79.         flowerList.add(new Blocky(Material.DOUBLE_PLANT, 1, 2, 1));
  80.         flowerList.add(new Blocky(Material.DOUBLE_PLANT, 4, 2, 1));
  81.         flowerList.add(new Blocky(Material.DOUBLE_PLANT, 5, 2, 1));
  82.     }
  83.  
  84.     @Override
  85.     public void debug(RuntimeDebugger runtimeDebugger, CommandSender commandSender) {
  86.         if (!(commandSender instanceof Player)) {
  87.             commandSender.sendMessage("Only players can run this script.");
  88.             return;
  89.         }
  90.         Player player = (Player) commandSender;
  91.         WorldEditPlugin worldEdit = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
  92.         if (worldEdit == null) {
  93.             player.sendMessage("Unable to indicate WorldEdit plugin presence...");
  94.             return;
  95.         }
  96.         Selection selection = worldEdit.getSelection(player);
  97.         if (selection == null) {
  98.             player.sendMessage("You need to make a selection with worldedit first");
  99.             return;
  100.         }
  101.         World world = selection.getWorld();
  102.         Location min = selection.getMinimumPoint();
  103.         Location max = selection.getMaximumPoint();
  104.         int placed = 0;
  105.         player.sendMessage("Starting flower placement...");
  106.         for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
  107.             for (int y = min.getBlockY(); y <= max.getBlockY(); y++) {
  108.                 for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
  109.                     Block block = world.getBlockAt(x, y, z);
  110.                     if (block == null) {
  111.                         continue;
  112.                     }
  113.                     if (block.getType() != Material.GRASS) {
  114.                         continue;
  115.                     }
  116.                     Block oneUp = block.getRelative(BlockFace.UP, 1);
  117.                     Block twoUp = block.getRelative(BlockFace.UP, 2);
  118.                     if (!((oneUp == null || oneUp.getType() == Material.AIR) && (twoUp == null || twoUp.getType() == Material.AIR))) {
  119.                         continue;
  120.                     }
  121.                     placeRandomFlower(oneUp);
  122.                     placed++;
  123.                 }
  124.             }
  125.         }
  126.         player.sendMessage("Placed " + placed + " flowers!");
  127.     }
  128.  
  129.     public Blocky getRandomFlower() {
  130.         Blocky res = null;
  131.         int totalWeight = 0;
  132.         for (Blocky blocky : flowerList) {
  133.             totalWeight += blocky.getWeight();
  134.         }
  135.         while (res == null) {
  136.             int index = random.nextInt(totalWeight);
  137.             for (Blocky blocky : flowerList) {
  138.                 index -= blocky.getWeight();
  139.                 if (index <= 0) {
  140.                     res = blocky;
  141.                     break;
  142.                 }
  143.             }
  144.         }
  145.         return res;
  146.     }
  147.  
  148.     public void placeRandomFlower(Block airBlock) {
  149.         if (random.nextBoolean()) {
  150.             return;
  151.         }
  152.         Blocky flower = getRandomFlower();
  153.         airBlock.setType(flower.getMaterial());
  154.         airBlock.setData(flower.getData());
  155.         if (flower.getHeight() > 1) {
  156.             Block up = airBlock.getRelative(BlockFace.UP);
  157.             up.setType(flower.getMaterial());
  158.             up.setData((byte) 8);
  159.         }
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment