Solved Create Region (Without WorldGuard)

Discussion in 'Spigot Plugin Development' started by Dorian349, Jul 4, 2018.

  1. Hey everyone,

    I want to create region (without worldguard). I want to make a region with an item with pos1 and pos2 to create event like regionenter or regionleave.
    Thanks for your help :)!

    Dorian349,
     
  2. You just save the two farthest locations in a cuboid.
     
    • Like Like x 1
  3. And what is a cubeoid :p?

    Finally i found it. Just, its possible to check if a player enter a cubeoid?
     
    #3 Dorian349, Jul 4, 2018
    Last edited: Jul 4, 2018
  4. Listen for player move event.
    Then check if the to location is inside the cuboid and the from location is outside.
     
    • Informative Informative x 1
  5. if you are not willing to make your own plugin,
    you'll find that WorldGuard is your only option.

    Mind if i ask what you have against WorldGuard?
     
    • Informative Informative x 1
  6. Yep, but if nothing else exist to prevent lag.
    I want to create my own minigame plugin :p and i don’t want other plugin on the server or api :).
     
  7. Easiest way would be to create a config containing the two corner positions,
    from this, make a cuboid.
    PlayerMoveEvent create an entry for if the player enters/exits the cuboid.
    Feel free to DM if you need more help :)
     
    • Like Like x 1
    • Like Like x 1
  8. Thanks! I will ask if i have problem ;).
    ty for your perfect cubeoid tuto! :)
    Thread resolved.
     
  9. Hi, don't use PlayerMoveEvent Event, use a scheduler, cuz moveEvent will be cast each time he moven, each time visionOffset change...

    It's lagy, but a scheduler will cast code each x sec, and in code make if player pos x y z is between cornerPos1X to cornerPos2X cornerPos1Y to cornerPos2Y etc...
     
    • Like Like x 1
    • Informative Informative x 1
    • Optimistic Optimistic x 1
  10. Wow, i was looking for that! Ty so much :D
     
  11. While this sounds good, and is said in good intention.. This is actually bad advice for this use-case.

    Instead of using a repeating task that will run regardless if a player moves or not, stick with the handy dandy PlayerMoveEvent. To avoid the useless calculations that will cause lag (as he pointed out) you will need to simply check the block coordinates to see if the player has really moved to another block.

    Code (Text):
    public class RegionsAreFun extends JavaPlugin implements Listener {
        private final Map<Player, Cuboid> REGIONS = new HashMap<>();

        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }

        @EventHandler
        public void onMove(PlayerMoveEvent event) {
            if (event.getTo().getBlockX() == event.getFrom().getBlockX() &&
                    event.getTo().getBlockY() == event.getFrom().getBlockY() &&
                    event.getTo().getBlockZ() == event.getFrom().getBlockZ()) {
                return; // user didnt actually move a full block
            }

            Player player = event.getPlayer();

            // user actually moved, check if they changed to a different region.
            Cuboid newRegion = RegionManager.getRegion(event.getTo());
            Cuboid oldRegion = REGIONS.get(player);
            if (newRegion == oldRegion) {
                return; // user is still inside the same region as last time
            }

            // user has changed regions. send exit message of last region
            if (oldRegion != null && oldRegion.hasExitMessage()) {
                player.sendMessage(oldRegion.getExitMessage());
            }

            // check if user has left all regions
            if (newRegion == null) {
                // user has left all regions
                REGIONS.remove(player);
                return; // done.
            }

            // user is entering a new region
            REGIONS.put(player, newRegion);
            if (newRegion.hasEnterMessage()) {
                player.sendMessage(newRegion.getEnterMessage());
            }
        }
       
        @EventHandler
        public void onQuit(PlayerQuitEvent event) {
            REGIONS.remove(event.getPlayer()); // dont forget to clean up after yourself! ;)
        }
    }
    ^_^
     
    • Winner Winner x 2
  12. Thanks! I will try that, and test if lags are less than other technique ^^
     
  13. Does WorldGuard lag for you? Because that's how they handle it, too. It's honestly the best way to do it. ;)
     
    • Agree Agree x 1
  14. Oh, such a great way to do it, nice, I'll update my code x), thanks a lot :p

    WG make huge lag for me so I recoded my plugin with region, I'll test back WG maybe more optimized now (4 years after :p )
     
    • Like Like x 1
  15. Not lag but i want to create my own plugin and use a 1Mb plugin for one fonction isn’t good xD
     
  16. I dont use WG either. I was just pointing out that the code doesnt lag ;)
     
    • Like Like x 1