A square ring that gets bigger/smaller

Discussion in 'Plugin Development' started by khave, Sep 21, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    khave

    I'm a bit stuck on how to code a square ring of blocks starting at the players location and then going bit by bit outwards/indwards. I'm not the best at math, but I believe you'd need to use loops for this.
    If you do not get what I mean, then the MagicSpells plugin does the same kind of thing with it's spell 'FireNova'

    I don't know if I'm allowed to do this, but I found this video of the spell:


    I DO NOT OWN THE VIDEO.
    That's what I meant, but of course with any block.
     
  2. Offline

    Creeoer

    I don't suppose you can get all of the realtive blocks around the player, place fire, get the blocks realtive to those blocks, place fire . You could setup a for loop to do this 10 times, not too sure how to get the realtive blocks all around the player though
     
  3. Offline

    Regablith

    Sense I am not very into automation yet, I personally would make a repeating task for every X amount of seconds, and when it hits, a certain number, expand the square. The way I would do this, is by taking the player's location and constantly adding and subtracting numbers until I got a square. (This is an awful way, but this is how I would do it... xP)
     
  4. Offline

    khave


    Yea you could do that I guess. I'll try it. Still open for new ideas, too, though!


    That's pretty much the same as what Creeoer said.
     
  5. Offline

    blablubbabc

    Here are 2 methods how one could create a sqaure ring of given radius:
    Code:java
    1. private void createSquareRing1(Block center, int radius) {
    2. for (int x = -radius; x <= radius; x++) {
    3. for (int z = -radius; z <= radius; z++) {
    4. if (x == -radius || x == radius || z == -radius || z == radius) {
    5. Block block = center.getWorld().getBlockAt(center.getX() + x, center.getY(), center.getZ() + z);
    6. if (block.getType() == Material.AIR) {
    7. block.setType(Material.FIRE);
    8. }
    9. }
    10. }
    11. }
    12. }
    13.  
    14. private static final BlockFace[] DIRECTIONS = new BlockFace[] { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
    15.  
    16. private void createSquareRing2(Block center, int radius) {
    17. for (BlockFace direction : DIRECTIONS) {
    18. Block mid = center.getRelative(direction, radius);
    19. createOrthogonalLine(mid, direction, radius);
    20. }
    21. }
    22.  
    23. private void createOrthogonalLine(Block mid, BlockFace dir, int radius) {
    24. assert dir.getModY() == 0;
    25. int modX = dir.getModX();
    26. int modZ = dir.getModZ();
    27.  
    28. // orthogonal direction:
    29. if (modX == 1 || modX == -1) {
    30. assert modZ == 0;
    31. modX = 0;
    32. modZ = 1;
    33. } else {
    34. assert Math.abs(modZ) == 1;
    35. assert modX == 0;
    36. modX = 1;
    37. modZ = 0;
    38. }
    39.  
    40. for (int i = -radius; i <= radius; i++) {
    41. Block block = mid.getRelative(modX * i, 0, modZ * i);
    42. if (block.getType() == Material.AIR) {
    43. block.setType(Material.FIRE);
    44. }
    45. }
    46. }


    I am not completely sure which method is better.
    To achieve the shown effect one would have to call the method again but replacing the fire back to air and increasing the radius and doing the same in some kind of delayed/repeating task.
    Possibilities to think about:
    * only replacing fire blocks back to air if those were previously not already fire blocks (possible implementation: storing the replaced blocks, so only those get restored, OR when turning air to fire turn fire blocks to air at the same time, so that it basically toggles the blocks between air<->fire)
    * maybe don't actually set the fire but only send fake-block change packets to nearby players, so that the world isn't actually modified (would also solve the replacing of other fire blocks, which maybe are not meant to be replaced)
     
Thread Status:
Not open for further replies.

Share This Page