Check if inside area (+ time)

Discussion in 'Plugin Development' started by Zarkopafilis, May 8, 2013.

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

    Zarkopafilis

    Please help!
     
  2. Offline

    savagesun

    You don't need to keep a list of every location inside the area.. You could just check the player's position in relation to the two corners.
    Code:
        public Location topLeftCorner = new Location(x,y,z);
        public Location bottomRightCorner = new Location(x,y,z);
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent evt) {
            Location pLoc = evt.getTo();
           
            if((pLoc.getBlockX() > topLeftCorner.getBlockX()) && (pLoc.getBlockX() < bottomRightCorner.getBlockX())) {
                if((pLoc.getBlockZ() < topLeftCorner.getBlockZ()) && (pLoc.getBlockZ() > bottomRightCorner.getBlockZ())) {
                    //now we know the player is in the area.
                }
            }
        }
     
  3. Offline

    Zarkopafilis

    I have 3 sets , one for each team,
    red green and blue , they have the player names' inside , I can check with them maybe?
     
  4. Offline

    Suraski

    http://stackoverflow.com/questions/5345031/checking-if-a-point-is-inside-a-specified-rectangle

    Then yes, just loop through it. Check a look at that link thought it should give you some insight on how to do it :)

    If you create a rectangle, but theirs no need if theirs a way with math to achieve the same purpose right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  5. Offline

    Zarkopafilis

    Yup but main target is for ME to UNDERSTAND , I think....
    Now I want to start a timer if capturing = true and after 5 mins (300000 ms ) check again if player is inside , but it will trigger every time a player moves , but without triggerring when a player moves , I cant pause capture if an opponnent is inside....!Too confusing
     
  6. Offline

    Suraski

    Wha?


    So you're coding domination for Bukkit? Sweet, ok let's get started!

    First: Check if the player is within a rectangle on a move event like we discussed earlier.

    Second: If the player IS infact within the rectangle, schedule an event to check if they are and each time add a percentage to the total completeness of capturing the flag.

    Third: If the player is killed, or moves out of the square while the scheduler is still running, cancel the scheduler and reset the score (Resetting the score isn't needed if you structure it properly).

    Fourth: If all goes well and the player successfully captures the flag, add 1 to the total score of the team and move on? Don't forget to set it so they team knows they won!
     
  7. Offline

    savagesun

    To cancel an event you use the scheduler and feed it the ID of the task you want to cancel.
    Code:
    Bukkit.getScheduler().cancelTask(id)
    To get the id of the task you need to save an integer when you first schedule the task. Something like:
    Code:
    //make it a field, so others can access it
    public int id;
     
    id = Bukkit.getScheduler().scheduleSyncRepeatingTask(blah blah);
    
    Edit: as for your task that you set up, do not use a while loop. For one thing, you are setting the value to 1 every time, not adding. The real reason here though is that because it is a repeating task it will be called every few ticks (according to your specifications). So maybe use a field from another class and add to that value each time. Something like:
    Code:
    public class CaptureTick implements Runnable {
     
    @Override
    public void run() {
    ScoreUtility.redTeamScore += 1;
    }
    }
     
  8. Offline

    savagesun

    I can't really tell you if it will work. You should be able to tell by compiling and running your plugin. I can however tell you there are some pretty big logic issues. When you're listening to the PlayerMoveEvent you check if the player is in the rectangle, and then you do a loop through every online player and check if they are in the rectangle. This is rather pointless. If I understand correctly you're trying to add points if the player is moving inside the rectangle. Don't do a loop through every player each time. The listener will do that on its own for the most part...
    The logic should look more like:
    Code:
    //Guy in area
    if(red.contains(n1)) {
    capture = "red";
    }
    else if(blue.contains(n1)) {
    capture = "blue";
    }
    else if(green.contains(n1)) {
    capture = "green";
    }
     
    isCapturing = true;
    Ideally you wouldn't be doing any of the actual capturing in the PlayerMoveEvent, only adding/removing to lists of players, but that seems a little beyond your level as of right now. If you really want the results you were looking for you might want to consider starting over and only recycling the code/structure you know is useful. You might also want to consider a simpler plugin to help learn bukkit.
     
Thread Status:
Not open for further replies.

Share This Page