aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/MylesAndMore/tumble/api/Generator.java
blob: db8bacc070ae4491ee552a473eda67d86e06799b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.MylesAndMore.tumble.api;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * This class holds the methods that generate blocks in-game such as cylinders, cubiods, and clump logic.
 */
public class Generator {
    /**
     * Generates a layer (bascally just a cylinder) as best as it can w/ blocks
     * 
     * @return A list of Blocks containing all the blocks it just changed
     * 
     * @param center The center of the layer (Location)
     * @param radius The whole number radius of the circle
     * @param height The whole number height of the circle (1 for a flat layer)
     * @param material The Material to use for generation
     */
    public static List<Block> generateLayer(Location center, int radius, int height, Material material) {
        int Cx = center.getBlockX();
        int Cy = center.getBlockY();
        int Cz = center.getBlockZ();
        World world = center.getWorld();
        List<Block> blocks = new ArrayList<>();

        int rSq = radius * radius;

        for (int y = Cy; y < Cy + height; y++) {
            for (int x = Cx - radius; x <= Cx + radius; x++) {
                for (int z = Cz - radius; z <= Cz + radius; z++) {
                    if ((Cx - x) * (Cx - x) + (Cz - z) * (Cz - z) <= rSq) {
                        world.getBlockAt(x, y, z).setType(material);
                        blocks.add(world.getBlockAt(x, y, z));
                    }
                }
            }
        }
        return blocks;
    }

    /**
     * Generates a cubiod (literally just a ripoff fill command)
     * @param firstPos The first Location to fill (first three coords in a fill command)
     * @param secondPos The second Location to fill to (second three coords)
     * @param material The Material to fill
     */
    public static List<Block> generateCuboid(Location firstPos, Location secondPos, Material material) {
        World world = firstPos.getWorld();
        List<Block> blocks = new ArrayList<>();
        int fX = firstPos.getBlockX();
        int fY = firstPos.getBlockY();
        int fZ = firstPos.getBlockZ();
        int sX = secondPos.getBlockX();
        int sY = secondPos.getBlockY();
        int sZ = secondPos.getBlockZ();

        for (int x = fX; x <= sX; x++) {
            for (int y = fY; y <= sY; y++) {
                for (int z = fZ; z <= sZ; z++) {
                    world.getBlockAt(x, y, z).setType(material);
                    blocks.add(world.getBlockAt(x, y, z));
                }
            }
        }
        return blocks;
    }

    /**
     * Generates clumps in a pre-generated layer.
     * @param blockList A list of block Locations that this method is allowed to edit
     * @param materialList A list of Materials for the generator to randomly choose from.
     * Keep in mind that not all Materials may be used, the amount used depends on the size of the layer.
     * More Materials = more randomization
     */
    public static void generateClumps(List<Block> blockList, List<Material> materialList) {
        // Define random class
        Random random = new Random();
        // Define new blocks list so we can manipulate it
        List<Block> blocks = new ArrayList<>(blockList);
        // Define new shuffled Materials list
        List<Material> materials = new ArrayList<>(materialList);
        Collections.shuffle(materials);
        // This loop will run until there are no blocks left to change
        while (blocks.size() > 0) {
            // Get a random Material from the provided materials list
            Material randomMaterial = materials.get(random.nextInt(materials.size()));
            // Gets the first Block from the list, to modify
            Block aBlock = blocks.get(0);
            // Modifies the block
            aBlock.setType(randomMaterial);
            // Get the blocks around that and change it to that same material
            if (blocks.contains(aBlock.getRelative(BlockFace.NORTH))) {
                aBlock.getRelative(BlockFace.NORTH).setType(randomMaterial);
                blocks.remove(aBlock.getRelative(BlockFace.NORTH));
            }
            if (blocks.contains(aBlock.getRelative(BlockFace.SOUTH))) {
                aBlock.getRelative(BlockFace.SOUTH).setType(randomMaterial);
                blocks.remove(aBlock.getRelative(BlockFace.SOUTH));
            }
            if (blocks.contains(aBlock.getRelative(BlockFace.EAST))) {
                aBlock.getRelative(BlockFace.EAST).setType(randomMaterial);
                blocks.remove(aBlock.getRelative(BlockFace.EAST));
            }
            if (blocks.contains(aBlock.getRelative(BlockFace.WEST))) {
                aBlock.getRelative(BlockFace.WEST).setType(randomMaterial);
                blocks.remove(aBlock.getRelative(BlockFace.WEST));
            }
            blocks.remove(aBlock);
        }
    }
}