Solved Generating a flat circle?

Discussion in 'Spigot Plugin Development' started by Eos, Oct 10, 2015.

1. Eos

How would I go about generating a flat circle I never really knew how to actually do that, what formula would I use as well?

#1
Last edited: Oct 10, 2015

Pi * r^2
?

3. BillyGalbreath

Code (Text):

public void cylinder(Location loc, Material mat, int r) {
int cx = loc.getBlockX();
int cy = loc.getBlockY();
int cz = loc.getBlockZ();
World w = loc.getWorld();
int rSquared = r * r;
for (int x = cx - r; x <= cx +r; x++) {
for (int z = cz - r; z <= cz +r; z++) {
if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rSquared) {
w.getBlockAt(x, cy, z).setType(mat);
}
}
}
}

Found this gem searching Google. They mention using fast block changes. I edited this to not be fast block changes. Might want to look into that if your circles are big.

• Like x 2
• Winner x 1
4. ItsMonkey

Whoever wrote that deserves a really big cookie.

5. foncused ModeratorPatron

I use this for generating all the locations needed for a circle based on the location of its center, its radius, and the number of locations on its circumference:
Code (Java):
public static List<Location> getCircle(Location center, double radius, int amount) {
List<Location> locations = new ArrayList<>();
World world = center.getWorld();
double increment = (2 * Math.PI) / amount;
for(int i = 0; i < amount; i++) {
double angle = i * increment;
double x = center.getX() + (radius * Math.cos(angle));
double z = center.getZ() + (radius * Math.sin(angle));
}
return locations;
}
I return all those locations in a list so that I can iterate through them for particle effects.
This code was not written by me, I found it somewhere else on Spigot or Bukkit. Pretty useful.

6. Eos

Worked perfectly now how would I spawn chests on the platform? If you don't mind me asking.

If you want the chest to be in the centre, before you generate the circle store the centre block, then set the block 1 block above the centre block to be a chest?

I hope I understood what you wanted correctly xD

• Useful x 1
8. Eos

I got this,
Code (Text):
Location center = new Location(w, cx + 0.5, cy, cz + 0.5);
Seems to work fine. I was hoping there would be a way better way of spawning multiple chests

9. Just_Roks

Whoever wrote that deserves 5 internets.

10. Eos

As of right now i'm trying to refill the chests within a radius, I have the chests stored in a arraylist, what would I do after I stored the locations?

Code (Text):
private List<Block> blocks = new ArrayList<Block>();

Loops through them, and get their inventory by casting through block state, and then fill them accordingly, if the chests have different items in them I would recommend storing them in a has map with the key being the block and the value being either a list of items to put in the chest or a string to get the items from a config that are to be added to the chest

12. Eos

What would I cast?
This is what I have so far.
Doesnt work..
Code (Text):
for ( Block blockloop : blocks )
{

Chest chest = (Chest)blockloop.getState();

Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Working");

}

#12
Last edited: Oct 11, 2015
13. Eos

Solutions:

Adding Items to chests from a arraylist

Code (Text):

List<Block> blocks = new ArrayList<Block>();
for ( Block blockloop : blocks )
{

if ( blockloop.getLocation().getBlock().getType().equals(Material.CHEST) )
{
if ( blockloop.getState() instanceof Chest )
{
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Yes");
Chest box = (Chest)blockloop.getState();
Inventory inv = box.getInventory();

}
}
}
Generating a 2D Circle
Code (Text):
public void cylinder(Location loc, Material mat, int r) {
int cx = loc.getBlockX();
int cy = loc.getBlockY();
int cz = loc.getBlockZ();
World w = loc.getWorld();
int rSquared = r * r;
for (int x = cx - r; x <= cx +r; x++) {
for (int z = cz - r; z <= cz +r; z++) {
if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rSquared) {
w.getBlockAt(x, cy, z).setType(mat);
}
}
}
}

• Winner x 1