Solved Generating a flat circle?

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

  1. Eos

    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 Eos, Oct 10, 2015
    Last edited: Oct 10, 2015
  2. 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.

    https://bukkit.org/threads/generating-a-circle-without-lag.100573/
     
    • Like Like x 2
    • Winner Winner x 1
  3. Whoever wrote that deserves a really big cookie.
     
  4. foncused

    Moderator Patron

    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));
            locations.add(new Location(world, x, center.getY(), z));
        }
        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. :)
     
  5. Eos

    Eos

    Worked perfectly now how would I spawn chests on the platform? If you don't mind me asking.
     
  6. 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 Useful x 1
  7. Eos

    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
     
  8. Whoever wrote that deserves 5 internets.
     
  9. Eos

    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>();
     
     
  10. 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
     
  11. Eos

    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();

                    chest.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE));

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

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

    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();

                        inv.addItem(new ItemStack(Material.DIAMOND_SWORD));

                    }
                }
            }
    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 Winner x 1