How To Make Fence Border

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

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


    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


    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


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


  5. Offline


    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


    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


    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


    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


    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


    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

        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()));
            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){
            return l.add(0,1,0);
        public void setFence(HashSet<Location> locs){
            for(Location l: locs){
  11. Offline


    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


    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


    ... 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


    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


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

Share This Page