GetY() Help

Discussion in 'Plugin Development' started by Aperx, Aug 19, 2013.

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

    Aperx

    Why won't this work?

    It should be getting the block 2 down in the Y direction and if it is one of the listed, then do whatever, but it does the 'whatever' whether the block two down is the listed.

    Code:
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(final PlayerMoveEvent e) {
    3. if (on == 1)
    4. {
    5. Player p = e.getPlayer();
    6. Location loc = p.getLocation();
    7. Location loc2 = p.getLocation();
    8. Location loc3 = p.getLocation();
    9. Location loc4 = p.getLocation();
    10. Location loc5 = p.getLocation();
    11. Location loc6 = p.getLocation();
    12. Location loc7 = p.getLocation();
    13. Location loc8 = p.getLocation();
    14. Location loc9 = p.getLocation();
    15. Location loc10 = p.getLocation();
    16.  
    17. loc.setY(loc.getY() - 2);
    18.  
    19. loc2.setY(loc2.getY() - 1);
    20. loc2.setX(loc2.getX() + 1);
    21. loc2.setZ(loc2.getZ() + 1);
    22.  
    23. loc3.setY(loc3.getY() - 1);
    24. loc3.setX(loc3.getX() - 1);
    25. loc3.setZ(loc3.getZ() - 1);
    26.  
    27. loc4.setY(loc4.getY() - 1);
    28. loc4.setX(loc4.getX() - 1);
    29. loc4.setZ(loc4.getZ() + 1);
    30.  
    31. loc5.setY(loc5.getY() - 1);
    32. loc5.setX(loc5.getX() + 1);
    33. loc5.setZ(loc5.getZ());
    34.  
    35. loc6.setY(loc6.getY() - 1);
    36. loc6.setX(loc6.getX());
    37. loc6.setZ(loc6.getZ() - 1);
    38.  
    39. loc7.setY(loc7.getY() - 1);
    40. loc7.setX(loc7.getX() - 1);
    41. loc7.setZ(loc7.getZ());
    42.  
    43. loc8.setY(loc8.getY() - 1);
    44. loc8.setX(loc8.getX());
    45. loc8.setZ(loc8.getZ() + 1);
    46.  
    47. loc9.setY(loc9.getY() - 1);
    48. loc9.setX(loc9.getX() + 1);
    49. loc9.setZ(loc9.getZ() - 1);
    50.  
    51. loc10.setY(loc10.getY() - 1);
    52.  
    53. Material b = loc.getBlock().getType();
    54.  
    55. if (b == Material.WATER && p.hasPermission("waterperk.ice") || b == Material.STATIONARY_WATER || b ==Material.ICE) {
    56.  
    57. loc10.getBlock().setType(Material.ICE);
    58. loc2.getBlock().setType(Material.ICE);
    59. loc3.getBlock().setType(Material.ICE);
    60. loc4.getBlock().setType(Material.ICE);
    61. loc5.getBlock().setType(Material.ICE);
    62. loc6.getBlock().setType(Material.ICE);
    63. loc7.getBlock().setType(Material.ICE);
    64. loc8.getBlock().setType(Material.ICE);
    65. loc9.getBlock().setType(Material.ICE);


    Just to elaborate a bit more, loc is to get the block two blocks below the player. i then check if that block is either Water, Stationary water and/or Ice and check if they have the permission. The problem is that the block two down can be grass to obsidian and it doesn't work. It everything still gets set to ice. Tried rearranging code, nothing works.

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

    Techy4198

    Aperx
    that's a totally messy code. use loops. try this
    Code:
    @EventHandler
    public void onPlayerMove(final PlayerMoveEvent e) {
      if (on == 1){
        Player p = e.getPlayer();
        Location loc = p.getLocation();
        loc.setY(loc.getY() - 1);
        Material b = loc.getBlock().getType();
     
        if (b == Material.WATER && p.hasPermission("waterperk.ice") || b == Material.STATIONARY_WATER || b == Material.ICE) {
          for(int ix = -1; ix < 2; ix++){
            for(int iy = -1; iy < 2; iy++){
              Location loc2 = loc;
              loc2.setX(loc2.getX() + ix);
              loc2.setY(loc2.getY() + iy);
              loc2.getBlock().setType(Material.ICE)
            }
          }
        }
      }
    }
    
     
  3. Offline

    Aperx

    That might work except my code looks messy as im making a small square where the player is Techy4198
    Unless thats what yours dose and i just don't understand it :p
     
  4. Offline

    Techy4198

    Aperx yep that's exactly what it does; makes a 3x3 square of ice under the player's feet
     
  5. Offline

    fireblast709

    Code:java
    1. BlockFace[] faces = new BlockFace[]
    2. {
    3. BlockFace.NORTH_WEST, BlockFace.NORTH, BlockFace.NORTH_EAST,
    4. BlockFace.WEST, BlockFace.SELF, BlockFace.EAST,
    5. BlockFace.SOUTH_WEST, BlockFace.SOUTH, BlockFace.SOUTH_EAST
    6. }
    7.  
    8. @EventHandler
    9. public void onPlayerMove(final PlayerMoveEvent e) {
    10. if (on == 1)
    11. {
    12. if(!p.hasPermission("waterperk.ice"))
    13. return;
    14.  
    15. Player p = e.getPlayer();
    16. Block b = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
    17. Block r;
    18. for(BlockFace face : faces)
    19. {
    20. r = b.getRelative(face);
    21. if (r.getType() == Material.WATER || r.getType() == Material.STATIONARY_WATER)
    22. {
    23. r.setType(Material.ICE);
    24. }
    25. }
    26. }
    27. }
    @Techy4198 Might even be a bit less messy :3
     
  6. Offline

    Aperx

  7. Offline

    DrTURTLE2

    Registering events?

    Aperx
     
  8. Offline

    xTrollxDudex

    Aperx
    Does the class implement Listener?
     
  9. Offline

    Aperx

    Registering events, and it does implement listener. The turning to ice itself works, but it should only be working if two blocks down is water, or ice.
    xTrollxDudex DrTURTLE2

    Can't get anything to work, really need this done, any help is appreciated, i've tried like everything

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

    Techy4198

    Aperx why 2 blocks down? that would be the block under the one that the player is standing on. where
    [zombie]
    [zombie]
    is the player, [brickblock] is the block they are standing on, and [sponge] is the block it is checking, it would be like this:
    [zombie]
    [zombie]
    [brickblock]
    [sponge]
    and i'm not sure that's what you want. don't you mean the block the player is actually on/above ? but ok. this one should work.

    Code:
    @EventHandler
    public void onPlayerMove(final PlayerMoveEvent e) {
      if (on == 1){
        Player p = e.getPlayer();
        Location loc = p.getLocation();
        loc.setY(loc.getY() - 2);
        Material b = loc.getBlock().getType();
     
        if (p.hasPermission("waterperk.ice") && (b == Material.WATER || b == Material.STATIONARY_WATER || b == Material.ICE)) {
          for(int ix = -1; ix < 2; ix++){
            for(int iz = -1; iy < 2; iy++){
              Location loc2 = loc;
              loc2.setX(loc2.getX() + ix);
              loc2.setZ(loc2.getZ() + iz);
              loc2.getBlock().setType(Material.ICE)
            }
          }
        }
      }
    }
    
     
  11. Offline

    Tarestudio

    Techy4198
    getY is the height, in your loop you have to change Z ;)
     
  12. Offline

    Techy4198

    Tarestudio fixed

    also Aperx where do you get the variable 'on'?

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

    Aperx

    Have it in my listener Techy4198

    Code:java
    1. public class WoWListener implements Listener, CommandExecutor {
    2.  
    3.  
    4. public static int on = 0;


    Its toggled between 0 and 1 by another listener

    That one doesn't work either.
    Also, i don't want people to be able to destroy bases with it, thus why i want it so there must be two layers of water before it allows the water to be turned to ice Techy4198
    And yet again im clueless to as why it won't work

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

    Techy4198

    Aperx oh I misunderstood you :p but now I see you want it to check BOTH blocks below the player. here is the same code just tweaked a bit (also forgot to change one of the iy's to iz in the last one)
    Code:
    @EventHandler
    public void onPlayerMove(final PlayerMoveEvent e) {
      if (on == 1){
        Player p = e.getPlayer();
        Location locP = p.getLocation();
        Location loc1 = locP.setY(loc.getY() - 1);
        Location loc2 = locP.setY(loc.getY() - 2);
        Material b = loc.getBlock().getType();
     
        if (p.hasPermission("waterperk.ice") && (b1 == Material.WATER || b1 == Material.STATIONARY_WATER || b1 == Material.ICE) && (b2 == Material.WATER || b2 == Material.STATIONARY_WATER)) {
          for(int ix = -1; ix < 2; ix++){
            for(int iz = -1; iz < 2; iz++){
              Location loc3 = loc1;
              loc3.setX(loc3.getX() + ix);
              loc3.setZ(loc3.getZ() + iz);
              if(loc3.getBlock().getType() == Material.WATER || loc3.getBlock().getType() == Material.STATIONARY_WATER{
                loc3.getBlock().setType(Material.ICE)
              }
            }
          }
        }
      }
    }
    
    hopefully this one works (and stop using an integer for on, use a boolean instead)

    what this does is check if the block below the player is either ice or water and the block below that is water, and if so it will turn all the water under the player's feet (in a 3x3 square) into ice.
    is this what you want? also if it still does not work, I will load it up in eclipse and see what I can do. what is your intention anyway? do you want people to be able to walk on water? if so, I recommend changing it to a 5x5 square
     
  15. Hello! My fellow friend Aperx!
    I dont know how to fix it I have a possible fix to all your problems If the code above didn't work, maybe you're not Registering the listener. [sheep] !
    How to do this? :eek:
    Easy!, Let me guide you! :D Heres an explanation for if youre doing this in 2 Different Classes (Main and the one for the Function)


    Code:java
    1. // This is the class where the Function onPlayerMove is
    2. // Replace with your packages name.
    3. package your.packages.name;
    4.  
    5. // You NEED these Imports
    6. import org.bukkit.event.player.PlayerMoveEvent;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.Location;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Block;
    13.  
    14. // replace <YourPluginsClassName> For your plugins name ._. (plop) lol
    15.  
    16. public class <YourPluginsClassName> implements Listener {
    17.  
    18. @EventHandler
    19. public void onPlayerMove(final PlayerMoveEvent e) {
    20. if (on == 1){
    21. Player p = e.getPlayer();
    22. Location loc = p.getLocation();
    23. loc.setY(loc.getY() - 2);
    24. Material b = loc.getBlock().getType();
    25.  
    26. if (p.hasPermission("waterperk.ice") && (b == Material.WATER || b == Material.STATIONARY_WATER || b == Material.ICE)) {
    27. for(int ix = -1; ix < 2; ix++){
    28. for(int iz = -1; iy < 2; iy++){
    29. Location loc2 = loc;
    30. loc2.setX(loc2.getX() + ix);
    31. loc2.setZ(loc2.getZ() + iz);
    32. loc2.getBlock().setType(Material.ICE)
    33. }
    34. }
    35. }
    36. }
    37. }
    38. }


    AND MOST IMPORTANT NOW!!! YOUR MAIN CLASS NEEDS TO REGISTER THE LISTENER FOR IT TO WORK!.

    Code:java
    1. // This is the Main class
    2. // Replace with your packages name.
    3. package your.packages.name;
    4.  
    5. // You NEED these Imports
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.entity.Player;
    8.  
    9. // replace <YourMainClassName> For your Main plugins name
    10.  
    11. public class <YourMainClassName> extends JavaPlugin {
    12.  
    13. @Override
    14. public void onEnable() {
    15. // THIS IS WHAT YOU NEED, Replace "YourPluginsClass()" with Your Plugins class name (The one with your onPlayerMove() function.)
    16. getServer().getPluginManager().registerEvents(new YourPluginsClass(), this);
    17.  
    18.  
    19. for(Player p: getServer().getOnlinePlayers()) {
    20. //sending a message to all the players online saying that Plugin has been Enabled. (When /Reloaded)
    21. p.sendMessage("All set and done, PLUGIN has been Enabled.");
    22. }
    23. }
    24. //just debugging, not needed
    25. @Override
    26. public void onDisable() {
    27. for(Player player: getServer().getOnlinePlayers()) {
    28. player.sendMessage("Plugin \u00A7cDISABLED");
    29. }
    30. }
    31.  
    32. }


    200 lines of code more This is all, I havent Officially tested it myself but I did something alike so it doesnt work I think it works ^^ I hope it helps you go through. ;)
    If it doesnt works its your fault! Its my fault Im sorry! I must have been wrong :mad: .

    PS: The important thing is "getServer().getPluginManager().registerEvents(new YourPluginsClass(), this);"
    IF you need the one to have all the code in the MAIN class only Reply and ill help.

    PS2: Excuse my english, Please, it's 3a.m. Lol. Pardon me.

    PS3: Tsss...~ [creeper]


    -KhriztianCraft​
     
  16. Offline

    Aperx

    I've never tried using a boolean?

    Anyways, line 6 & 7 of that i get some errors, as well as 10. I think thats to do with b1 and stuff. But i'm on my phone at the moment so i can fix it. Techy4198

    And yes, this is to walk on water, why would you suggest 5x5? i thought 3x3 is good as its not to big but still a decent size
     
  17. Offline

    Techy4198

    Aperx if someone runs really fast on water, a 3x3 can't keep up, so they fall off. and with the current code, it is cabable of making ice underwater, so give me the whole class and I can get it working perfectly. I know why it was giving errors on b1 and b2, I got the locations but forgot to actually initialize the blocks :p
     
Thread Status:
Not open for further replies.

Share This Page