How To Make Fence Border

Discussion in 'Plugin Development' started by leimekiller, Sep 12, 2013.

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

    leimekiller

    Hello! I am making a protection plugin, it makes a worldguard region. But now, how can I get the outside of the region and create a fence border around it?
     
  2. Offline

    caseif

    Iterate the blocks bordering the region using the region's minimum and maximum values on the x and z axes. First iterate from the minimum x-value to the maximum x-value using the minimum and maximum z-values as the z-coord, the do the same for the other axis.
     
  3. Offline

    Double0negative

    That would be extremely slow depending on the size of the region
     
  4. Offline

    Aqua

  5. Offline

    metalhedd

    that doesn't make it the wrong way to do the job. optimization is left as an exercise for the person implementing the code. obviously if you have a large region you need to schedule the work across several ticks.
     
  6. Offline

    Ivan

    Is there any other way to do it? Java can handle some serious stuff :p.

    OT: I like AngryNerd 's idea, but if you want your fence wall to be only one high, you might want to use getHighestBlockAt (warning if your in a cave, fences wont be placed). You can also iterate over the y-axis, and placing a fence when you see the first air block after a solid block. This way you'll get a fence underground and aboveground.
     
  7. Offline

    Double0negative

    You could calculate the blocks that need to be changed in a thread, but, you dont want to change any blocks in a thread.
     
    TheGreenGamerHD likes this.
  8. Offline

    caseif

    Adding to what Double0negative said, changing the blocks asynchronously (i.e. in a separate thread) would throw ConcurrentModificationExceptions. Calculating the locations asynchronously and changing the blocks in the main thread would likely prove to be not much more efficient than just doing everything in the main thread. And as Ivan said, Java can typically execute quite a bit of code at once before there's a noticeable delay.
     
    CeramicTitan likes this.
  9. Offline

    metalhedd

    Or you could do what I suggested and just schedule it across multiple ticks! that would be the sane way to make sure it doesn't cause lag under any circumstances.
     
  10. Offline

    Double0negative

    metalhedd AngryNerd Ivan leimekiller

    After some test, a 100x100x256 region happens instantly (500x500x256 takes ~1 second), So I dont think using a thread or scheduler are needed. Heres the code

    Code:
        @Override
        public boolean onCommand(Player player, String[] args) {
            WorldEditPlugin we = GameManager.getInstance().getWorldEdit();
            Selection sel = we.getSelection(player);
            if (sel == null) {
                return false;
            }
            Location max = sel.getMaximumPoint();
            Location min = sel.getMinimumPoint();
         
            World w = max.getWorld();
         
            HashSet<Location> mark = new HashSet<Location>();
         
            for(int a = min.getBlockZ(); a < max.getBlockZ(); a++){
                mark.add(getYLocation(w,max.getBlockX(), max.getBlockY(), a));
                mark.add(getYLocation(w,min.getBlockX(), max.getBlockY(), a));
            }
            for(int a = min.getBlockX(); a < max.getBlockX(); a++){
                mark.add(getYLocation(w,a, max.getBlockY(), max.getBlockZ()));
                mark.add(getYLocation(w,a, max.getBlockY(), min.getBlockZ()));
            }
         
            setFence(mark);
            return true;
         
        }
     
        public Location getYLocation(World w, int x, int y, int z){
            Location l = new Location(w,x,y,z);
            while(l.getBlock().getTypeId() == 0){
                l.add(0,-1,0);
            }
            return l.add(0,1,0);
        }
     
        public void setFence(HashSet<Location> locs){
            for(Location l: locs){
                l.getBlock().setType(Material.FENCE);
            }
        }
    
     
  11. Offline

    metalhedd

    freezing your entire server for ~1 second could be pretty annoying. It's obviously up to you to decide if its 'good enough' but keep in mind every player on the server is going to feel that 1 second pause.
     
  12. Offline

    caseif

    That would definitely be a concern if this were an event which happened often. However, it's not everyday that someone creates a fence border for a region massive enough to cause such a delay. Coding it in a way which wouldn't cause the delay simply wouldn't be worth it.
     
  13. Offline

    metalhedd


    ... In your opinion.

    in mine, there's no good reason to lock up the main thread for an entire second if it can be avoided.
     
  14. Offline

    caseif

    Alright, point taken. I suppose it really is just a matter of preference, and whether one thinks the payoff is worth the extra code.
     
  15. Offline

    jayfella

    metalhedd likes this.
Thread Status:
Not open for further replies.

Share This Page