Leave the player in an area

Discussion in 'Plugin Development' started by ChillxHunter, Mar 4, 2019.

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

    ChillxHunter

    Hello, I'm doing a pvp box on my server and when players arrive on the platform above the spawn, they must choose their kit with a compass but the problem is that players jump from the starting area without having taken the kit. I would need to leave the players inside the zone as long as they do not have their chosen kit. Do you know how to do it ?
     
  2. Offline

    CraftCreeper6

    @ChillxHunter
    Just make the box have no exits, then once they pick a kit, teleport them to a new box where they max jump down.

    Or add a region where players can't go without a kit, this could be buggy though if you are making them fall down.
     
  3. Offline

    MrDaniel

    @ChillxHunter, one way I would go by doing this is creating two locations, so they form a rectangle. Then in the onEnable() method, you would create a task that runs every 5 ticks or something that checks if the player isn't between those two locations, then you would have a hashlist<Player> and when the player picks a kit to add him to the list. When the player leaves the location you would check if he is in the list, if he isn't then tp him back to spawn and send him back to the spawn.

    A method that I saw back then of how to check if a player is in between two locations:
    Code:
    public static boolean isInside(Location loc, Location l1, Location l2) {
    
            int x1 = Math.min(l1.getBlockX(), l2.getBlockX());
    
            int y1 = Math.min(l1.getBlockY(), l2.getBlockY());
    
            int z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
    
            int x2 = Math.max(l1.getBlockX(), l2.getBlockX());
    
            int y2 = Math.max(l1.getBlockY(), l2.getBlockY());
    
            int z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
    
    
            return loc.getX() >= x1 + 1 && loc.getX() <= x2 && loc.getY() >= y1 && loc.getY() <= y2 && loc.getZ() >= z1 + 1 && loc.getZ() <= z2;
    
    }
    
     
  4. Offline

    CraftCreeper6

    @MrDaniel
    Rather than having this task repeat when not necessary, call it in PlayerMoveEvent
     
    MrDaniel likes this.
  5. Offline

    MrDaniel

    Woa @CraftCreeper6, you just blew my mind, now I have to go through all of my code to utilize this groundbreaking discovery... How are the people on these forums so intelligent?
     
  6. Offline

    ChillxHunter


    I did not understand with what could I create a region. With worldguard ?
     
  7. Offline

    CraftCreeper6

    @ChillxHunter
    You could use worldguard but it's more recommended to use your own.

    It depends how accurate you want it, if it's chunk based, then use chunk claiming, if it's not then you'll have to use positional claiming. Which is where you take two points and create a bounding box, if the player leaves that bounding box without choosing a kit, you should move them back.
     
  8. Offline

    ChillxHunter

    @CraftCreeper6 @MrDaniel For real I'm not really good at development so I do not know how to do that but will you have a way to do with worldguard?
     
    Last edited: Mar 5, 2019
  9. Offline

    CraftCreeper6

    @ChillxHunter
    I've never used the world guard API, but I imagine it works in a similar way to the one I suggested,

    Consider each increment of 1 on the following graphs is 1 Minecraft block.

    If you were to select 2 points on a 2D plane like such: (marked with a red dot)
    [​IMG]
    https://i.imgur.com/YDSB1gV.png

    In order to get all the locations between those two values you'd loop over all the x values, and all the y values in turn, right?

    To do this we need the difference in x and the difference in y. The difference in x in this example would be 7 and the difference in y would be 4, you can do this by comparing the locations of two blocks in the real Minecraft world.
    [​IMG]
    https://i.imgur.com/z0PGFlP.png

    Once you loop over the x and y values you get this:[​IMG]
    https://i.imgur.com/ubwEmqb.png

    This is only in a 2D plane though, and you need a 3D plane I imagine.

    So it doesn't get much more complicated. Though the graph may seem confusing.

    To get from 2 dimensions to 3 you just add a new axis, the z axis.

    If you were to iterate over this 3rd axis, while simultaneously looping over the x and y axis, you'd get all the blocks in a cube. Like so:
    [​IMG]
    https://i.imgur.com/SO5BgAD.png

    The way to do this programmatically is to have a few nested for loops. Nested means that they are inside of one another, to put it simply.

    Code:
    for(int x = 0; x < maximumXvalue; x++)
            for (int y = 0; y < maximumYvalue; y++)
                     for (int z = 0; z < maximumZvalue; z++)
    
    I'm pretty sure it will make no difference what order you iterate in, because in any case you're getting all the values.

    Essentially what the snippet above does is loop over the x value, y value and z value.
    Say the x value is 2, y is 4, z is -36.
    You could get the block at the location (2,4,-36) and do something with it.

    To relate back to the question, you'd need to loop over each block between two locations and check if the user is about to go outside of that region, if they are, without selecting a kit, you should teleport them back to the centre of the cube, per say.
     
  10. Offline

    Tango_

    Although the post above is really informative and it is one way of doing it, if you wish to use world guard API it isn't difficult at all.

    You add world guard and world edit plugins to your java build path, then you add this code in your main class

    Code:
        private WorldGuardPlugin getWorldGuard() {
            Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
            if (plugin == null || !(plugin instanceof WorldGuardPlugin))  return null;
            return (WorldGuardPlugin) plugin;
        }
    Put getWorldGuard(); in your onEnable. Now that you have setup WorldGuardAPI, we can move on to the code you want.

    Add this code below to a listener class where you will need to listen for PlayerMoveEvent.

    Code:
        public boolean isInRegion(Location loc, String region) {
            if(loc != null) {
                com.sk89q.worldedit.Vector v = new com.sk89q.worldedit.Vector(loc.getX(), loc.getBlockY(), loc.getZ());
                return WGBukkit.getRegionManager(loc.getWorld()).getApplicableRegionsIDs(v).contains(region);
            }  
            return false;
        }
    This code allows us to check if the players location is inside of a region.

    Now we can use PlayerMoveEvent and take advantage of e.getFrom and e.getTo.
    Just create the region using world guard ingame and list it in the plugin

    Code:
        @EventHandler
        public void playerMove(PlayerMoveEvent e){
    
            Player p = e.getPlayer();
            String regionName = "Region123";
            boolean hasKit = p.getInventory().getChestplate() != null;
         
    
            if(!hasKit && e.getTo().getBlockX() != e.getFrom().getBlockX() || e.getTo().getBlockZ() != e.getFrom().getBlockZ()) {
                if(!isInRegion(e.getTo(), regionName) && isInRegion(e.getFrom(), regionName)) {
    
                    e.setCancelled(true); // Or TP them back 1 block
    
                }
            }
        }
     
  11. Offline

    ChillxHunter


    @Tango_ @CraftCreeper6

    Can one of you do the plugin because I do not know how to develop so I do not really understand what you are saying. It would be very nice of you because I really need to open my server on Saturday.
     
  12. Offline

    Tango_

    What does the player wear before selecting a kit? And this is simple so I can probably do this for you
     
  13. Offline

    ChillxHunter

    @Tango_ He does not wear anything, when he arrives in the zone he does not wear anything that is the one he has to choose a left otherwise he can not leave the zone
     
  14. Offline

    CraftCreeper6

    @ChillxHunter
    When they choose a kit will they be given armour?

    I think Tango_ is looking for a condition in which the person can / cannot leave the area. So if there's a condition unique to only people that have picked a kit that would be very helpful to him.
     
  15. Offline

    ChillxHunter

    @CraftCreeper6 Well, there are several kits that are selectable thanks to a compass and yes when a player chooses a kit, it gives him an armor
     
  16. Offline

    Tango_

    Try this:

    DOWNLOAD

    Permissions:

    If you are OP you have these by default:
    kitarea.bypass - Prevents the player being effected by the plugin
    kitarea.* - Gives permission to add or remove regions from the plugin


    Commands:

    /KitArea Add <RegionName> - Adds a region to the plugins list.
    /KitArea Remove <RegionName> - Removes a region from the list.
    (RegionName is case sensitive)
     
    Last edited: Mar 8, 2019
  17. Offline

    ChillxHunter

    @Tango_ Its doesn't work i put the region but the player can still go out of the region.
     
  18. Offline

    Tango_

    Are you trying it with an opped player? Try it with someone without access to kitarea.bypass
     
  19. Offline

    ChillxHunter

    @Tango_ I tried with a person who has no permissions and who is not op but it doesn’t work. Could you check the error by testing it?
     
  20. Offline

    Tango_

    What version are you using? And if you are seeing an error, please post it here
     
  21. Offline

    ChillxHunter

    @Tango_ I am in 1.8.8 And I don’t have errors
     
  22. Offline

    Tango_

    Try this one instead: DOWNLOAD
     
  23. Offline

    ChillxHunter

    @Tango_ Thank you it works but could you do that in the config file you could edit the plugin messages?

    @Tango_ And it would be nice if his spam not the message when the player tries to leave the area

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  24. Offline

    Tango_

    What do you want the message to be? It will be easier for me to just change it
     
  25. Offline

    ChillxHunter

    @Tango_ Message: Vous ne pouvez pas quitté la zone sans choisir de kit !
    And it would be nice if his spam not the message when the player tries to leave the area
     
  26. Offline

    Tango_

    Here you go: DOWNLOAD
     
Thread Status:
Not open for further replies.

Share This Page