Solved Player Move Event

Discussion in 'Plugin Development' started by NinjaRoyal, Aug 4, 2014.

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

    NinjaRoyal

    Hello there bukkit! I am a intermediate programmer and am still learning the bukkit API along with Java. I have made a lot of plugins just for fun and for a learning experience. I usually search up an issue if I have one but I could not find this issues solution for the life of me. :p. Please don't be rude to me if I don't understand some of the answers as I am still in the learning process and still learning.
    My problem:
    So I am currently working on a Server and my plugin is coming along VERY well. I am working on a bleeding system which fully works execpt for 1 thing: The player move event. Basicly how it works is one damage you have a small percentage chance to bleed. The only way to fix bleeding is to either die or use a rag or bandage. The rag works fine but today I was adding in the system where you don't instantly use the rag but have to not move for a little bit to add a realistic effect to it. This all works fine but the only problem I am having is with the move event. When you start patching yourself with a rag it adds you to the "patching" arraylist. Then using a Player move event I check if the player is in the "patching" arraylist and if he is it removes him and sends him a msg saying you failed to patch because you moved. My problem is that I want the event to only trigger if they move a block and not freelook. Currently if you move your mouse AT ALL it cancels using a bandage. I don't really want to give out my code as this will be a public server and I would not like my code to leak but this is the section that does this:
    Code:java
    1. @EventHandler
    2. public void onMove(PlayerMoveEvent e) {
    3. if (patching.contains(e.getPlayer().getName())) {
    4. patching.remove(e.getPlayer().getName());
    5. e.getPlayer().sendMessage(ChatColor.RED + "You moved and stopped bandaging your wounds!");
    6. return;
    7. }
    8. }

    Also If a experienced developer would please add me on skype to help me with further problems with my server. I will pm skype if needed. :D Thanks so much.
     
  2. Offline

    Dragonphase

    NinjaRoyal

    The PlayerMoveEvent stores two location variables - from and to. Simply check if the X, Y and Z of to is different to the X, Y and Z of from:

    Code:java
    1. @EventHandler
    2. public void onMove(PlayerMoveEvent e) {
    3. if (e.getTo().getBlockX() == e.getFrom().getBlockX() && e.getTo().getBlockY() == e.getFrom().getBlockY() && e.getTo().getBlockZ() == e.getFrom().getBlockZ()) return; //The player hasn't moved
    4. if (patching.contains(e.getPlayer().getName())) {
    5. patching.remove(e.getPlayer().getName());
    6. e.getPlayer().sendMessage(ChatColor.RED + "You moved and stopped bandaging your wounds!");
    7. return;
    8. }
    9. }
     
    Skye likes this.
  3. Offline

    NinjaRoyal

    Yea I thought it would have to do with the x,y,z. I did something almost exactly like this except I used an if statement to confirm that they did move then cancel the event and cancel patching but it didn't seem to work. This works perfectly. Thanks a ton for replying! P.S May I add you on skype or some other form of contact for later questions? You don't need to mentor me or anything I just want someone I can fall back on to answer my questions so I don't need to make a post every time a little error that I can't get around comes up :p, Thanks again.
     
  4. Offline

    mine-care

    Better learn java first and then the api :)
    "am still learning the bukkit API along with Java"
     
  5. Offline

    bombom3000

    Hello! If you want the cancel patch event to only fire when the player walks a block, you could use this if statement in your PlayerMoveEvent:
    Code:java
    1. if (e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) {
    2.  
    3. }

    Inside there you can now put your patch cancel code.

    I hope this helped!

    Charlie

    Whole code for that:

    Code:java
    1. @EventHandler
    2. public void onMove(PlayerMoveEvent e) {
    3. if (e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) {
    4. if (patching.contains(e.getPlayer().getName())) {
    5. patching.remove(e.getPlayer().getName());
    6. e.getPlayer().sendMessage(ChatColor.RED + "You moved and stopped bandaging your wounds!");
    7. return;
    8. }
    9. }
    10. }
    11.  
     
  6. Offline

    Dragonphase

    bombom3000

    This will do the exact opposite. My code checks to see if the player's block position hasn't changed, and if it hasn't, returns. The reason I do this is to reduce the amount of nested if statements.
     
  7. Offline

    bombom3000


    I've tested mine in a pedometer plugin it fires if the player moves over a block, if you look, NinjaRoyal 's original post says
    So I provided code for that.

    Just noticed, I wrote almost exactly what you wrote, oops sorry!

    Just saying about your return at the end of the block move detection line, that will make it not run the code below that stops your bandage, which he wanted it to stop when you move.

    I'm not arguing.

    Charlie
     
  8. Offline

    Dragonphase

    bombom3000

    Your code is only triggering if the player's block location is the same... so basically, it's running 99.9% of the time that the player is moving.
     
    _LB likes this.
  9. Offline

    bombom3000


    Sorry! I just noticed I missed out the return on my pedometer plugin, sorry for all that.

    Charlie
     
  10. Offline

    Skye

    bombom3000 That code wouldn't fire when the player makes a true grid movement, which is actually what you'd want to work from so that it isn't firing multiple times for the slightest movements. :oops:
     
  11. Offline

    Dragonphase

    bombom3000


    The code literally translates to "If the player is in the same X, Y and Z, don't run any more code."

     
Thread Status:
Not open for further replies.

Share This Page