Anyone know why this doesn't work?

Discussion in 'Plugin Development' started by the_cows, Jul 29, 2014.

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

    the_cows

    Hi,

    I was just wondering if anyone knew why this doesn't work:
    Code:java
    1. public void onPlayerMove(PlayerMoveEvent event){
    2. Player player = event.getPlayer();
    3. if(player.getWorld().equals("KitPvP")) {
    4. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    5. if(block.getType() == Material.WATER) {
    6. int x = -1042;
    7. int y = 3;
    8. int z = 1378;
    9. World world = Bukkit.getWorld("Map");
    10. Location loc = new Location(world,x,y,z);
    11. player.teleport(loc);


    All it's meant to do is:
    1) Check if a player is in a certain world
    2) Check if the Block they are walking in is water
    3) Teleport them to "loc."

    But no errors or anything, just doesn't work.
    Any ideas?
     
  2. Offline

    Starfire1337

    Did you register events?
     
  3. Offline

    MomsKnife

    Make sure you have your event handlers and make sure you register the events in your onEnable method
    Add debug messages
     
  4. I would say you don't have it on your onEnable(), but looking at your code... maybe you forgot the "@EventHandler" before the "public void..."
     
  5. Offline

    xTigerRebornx

    the_cows
    Code:
    if(player.getWorld().equals("KitPvP")) 
    Will never be true. getWorld() returns a World, which is not a String, therefor will never be equal to one.
    Perhaps you are looking for World#getName() which returns the name of the World in form of a String.
     
  6. Lol didn't see that, very true. Also you might want to use equalsIgnoreCase (It's up to you, but It's what I always use, even if not needed :p)
     
  7. Offline

    the_cows

    XXLuigiMario xTigerRebornx Both of you, thanks :p. After coding a 550 line plugin when you aren't the best at coding it's sometimes very easy to forget these xD

    XXLuigiMario xTigerRebornx Hmm, this doesn't work. Once again, no error, just doesn't teleport. This is the code I'm using:

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent event){
    3. Player player = event.getPlayer();
    4. if(player.getWorld().getName().equalsIgnoreCase("KitPvP")) {
    5. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    6. if(block.getType() == Material.WATER) {
    7. int x = -1359;
    8. int y = 88;
    9. int z = 1651;
    10. World world = Bukkit.getWorld("Map");
    11. Location loc = new Location(world,x,y,z);
    12. player.teleport(loc);


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

    xTigerRebornx

    the_cows Debug and see if the listener is actually fired. If it isn't, you've forgotten to register your events.
     
  9. You should never hardcode stuff, although if it's a custom plugin for your server it's ok.
    Idk, why it isn't working. But I don't like that wierd spacing in the if statemnts and stuff.
    Maybe it's because you directly fall to the water, and then it counts like the water is not below you??
    I would try with another block first.
     
  10. Offline

    the_cows

    xTigerRebornx The listener is being fired. I'm using many other event's in my plugin like PlayerDamageEvent etc and they all work perfectly =/

    XXLuigiMario Yeah it's only a custom plugin. I'll try with another block then and see if it works.

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

    Necrodoom

    the_cows when in doubt, debug everything.
     
  12. Offline

    the_cows

    XXLuigiMario Would it be the event I'm firing? All I want it to do is that when a player touches water it teleports them, so would a different event be better? I've tried with other blocks and they didn't work either =/
     
  13. Offline

    xTigerRebornx

    the_cows Like i said, debug that specific listener. If there is no debug whatsoever (aka, you put one that is guaranteed to executed but doesn't), then it isn't registered.
    If it is firing, debug. Print out values that you are preforming checks on, see what they are and what you expect them to be.
     
  14. Offline

    the_cows

    xTigerRebornx
    I changed the code to this:
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent event){
    3. Player player = event.getPlayer();
    4. player.sendMessage("PlayerMoveEvent is being fired.");
    5. if(player.getWorld().getName().equalsIgnoreCase("KitPvP")) {
    6. Block block = player.getLocation().getBlock().getRelative(BlockFace.UP);
    7. if(block.getType() == Material.DISPENSER) {
    8. int x = -1359;
    9. int y = 88;
    10. int z = 1651;
    11. World world = Bukkit.getWorld("Map");
    12. Location loc = new Location(world,x,y,z);
    13. player.teleport(loc);
    and I did get the "PlayerMoveEvent is being fired." message whenever I moved, so the event is being fired. And as you can see from above, I tested with a dispenser rather than Water and it still did not work. Any ideas?

    bump....

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

    FabeGabeMC

    the_cows
    Try instead of getting the face, doing this:
    Code:java
    1. Block b = new Location(p.getWorld(), p.getLocation().getBlockX(), p.getLocation().getBlockY() + 1, p.getLocation().getBlockZ()).getBlock();

    It will get the block above the player.
     
  16. Offline

    Jalau

    the_cows
    So you want to check if the block above the player is a dispenser? Your code won't work, because player.getLocation() does return the location of the feet and not of the head of the player, so do player.getLocation().add(0,2,0).getBlock();

    For the other problem, because you wanted to get the block below the player:
    Material.WATER is the Item Water... There are multiple types of Water, like stationary and flowing... Use: 'getType().toString().contains("WATER")' and you'll be fine :) Worked at least for me ^^

    FabeGabeMC
    Nope, it won't... It will get the players head location, as I already explained :)
     
  17. Offline

    FabeGabeMC

    Jalau
    Then it would be + 2 instead of +1.
     
  18. Offline

    Jalau

    FabeGabeMC
    Or simply just .add(0,2,0); as i said above
     
  19. Offline

    Beeperdp

    Also,
    Code:java
    1. player.getWorld().getName().equalsIgnoreCase("KitPvP")

    could be
    Code:java
    1. player.getWorld() == Bukkit.getWorld("KitPvP")

    That could help - It is certainly more sound of a method. (I believe)
     
  20. Offline

    stonar96

    Add more debug-messages (for every if-statement). Then you can see where it fails.
     
  21. Offline

    FabeGabeMC

Thread Status:
Not open for further replies.

Share This Page