Solved Need help with player getting damaged while touching a block

Discussion in 'Plugin Development' started by gabe4356, Sep 1, 2014.

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

    gabe4356

    So, I am making a simple plugin where a player gets damaged if they touch iron bars, but I can't get this to work (Its more of a really simple minigame)
    Code:
    Code:
    package com.octo;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("unused")
    public class OctoMainOod extends JavaPlugin implements Listener {
       
       
        @SuppressWarnings("deprecation")
        public void onPlayerMove(PlayerMoveEvent e){
            if(e.getTo().getBlock().getType() == Material.IRON_BLOCK){
                e.getPlayer().damage(20);
            }
        }
       
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("OctoJoin")) {
                player.sendMessage(ChatColor.AQUA + "You will be teleported to the next open game!");
               
            }
               
               
            if (cmd.getName().equalsIgnoreCase("Octo")) {
                    player.sendMessage(ChatColor.GOLD+ "=================================");
                    player.sendMessage(ChatColor.AQUA+ "        Octo Ood Commands        ");
                    player.sendMessage(ChatColor.GOLD+ "=================================");
                    player.sendMessage(ChatColor.AQUA+ "OctoJoin : Joins an Octo Ood game");
                    player.sendMessage(ChatColor.AQUA+ "OctoHelp : Lists these commands");
               
                } else if (foo()) {
                player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /octo");
                }
           
            return true;
        }
     
       
       
     
               
                private boolean foo() {
            // TODO Auto-generated method stub
            return false;
                }
     
     
                public void onDisable() {
                    getLogger().info(ChatColor.GREEN+  "Octo Ood plugin has been disabled!");
                    getServer().broadcastMessage(ChatColor.GREEN+ "Octo ood has been disabled!");
                    //On Disable method stub
                }
     
                public void onEnable() {
                    getLogger().info(ChatColor.GREEN+  "Octo ood plugin has been enabled!");
                    getServer().getPluginManager().registerEvents(this, this);
                    getServer().broadcastMessage(ChatColor.GREEN+ "Octo Ood has been enabled!");
               
                  //on Enable Method stub
                   
                   
                   
                   
            }
        }
     
     
     
     
     
    
     
  2. Offline

    Jace_oio

    PHP:
        @EventHandler
        
    public void onPlayerMove(PlayerMoveEvent event){
            if(
    event.isCancelled()){
                return;
            }
            if(
    event.getTo().getBlock().getType() == Material.IRON_FENCE){
                
    event.getPlayer().damage(1);
            }
        }
    then add 1 to the Y of the player and check the block there too to get the torso :)
     
  3. Offline

    gabe4356

    Where would I add Y? Sorry, I am sort of new to this.
     
  4. Offline

    Jace_oio

    and you miss @EventHandler :)

    Okay first off all add a @EventHandler above your event method. Second you would add 1 y to e.getTo() location so you also cover the chest area :) else you would only cover pants area

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

    gabe4356

    How would I add 1 y there?
    this is my code now:
    Code:java
    1. package com.octo;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class OctoMainOod extends JavaPlugin implements Listener {
    14.  
    15. @EventHandler
    16. public void onPlayerMove(PlayerMoveEvent event){
    17. if(event.isCancelled()){
    18. return;
    19. }
    20. if(event.getTo().getBlock().getType() == Material.IRON_FENCE){
    21. event.getPlayer().damage(1);
    22. }
    23. }
    24.  
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    27. Player player = (Player) sender;
    28. if (cmd.getName().equalsIgnoreCase("OctoJoin")) {
    29. player.sendMessage(ChatColor.AQUA + "You will be teleported to the next open game!");
    30.  
    31. }
    32.  
    33.  
    34. if (cmd.getName().equalsIgnoreCase("Octo")) {
    35. player.sendMessage(ChatColor.GOLD+ "=================================");
    36. player.sendMessage(ChatColor.AQUA+ " Octo Ood Commands ");
    37. player.sendMessage(ChatColor.GOLD+ "=================================");
    38. player.sendMessage(ChatColor.AQUA+ "Octo Join : Joins an Octo Ood game");
    39. player.sendMessage(ChatColor.AQUA+ "Octo Help : Lists these commands");
    40.  
    41. } else if (foo()) {
    42. player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /octo");
    43. }
    44.  
    45. return true;
    46. }
    47.  
    48.  
    49.  
    50.  
    51.  
    52. private boolean foo() {
    53. // TODO Auto-generated method stub
    54. return false;
    55. }
    56.  
    57.  
    58. public void onDisable() {
    59. getLogger().info(ChatColor.GREEN+ "Octo Ood plugin has been disabled!");
    60. getServer().broadcastMessage(ChatColor.GREEN+ "Octo ood has been disabled!");
    61. //On Disable method stub
    62. }
    63.  
    64. public void onEnable() {
    65. getLogger().info(ChatColor.GREEN+ "Octo ood plugin has been enabled!");
    66. getServer().getPluginManager().registerEvents(this, this);
    67. getServer().broadcastMessage(ChatColor.GREEN+ "Octo Ood has been enabled!");
    68.  
    69. //on Enable Method stub
    70.  
    71.  
    72.  
    73.  
    74. }
    75. }
    76.  
    77.  
    78.  
    79.  
    80.  
    81.  
     
  6. Offline

    Jace_oio

    PHP:
        @EventHandler
        
    public static void onPlayerMove(PlayerMoveEvent event){
     
            if(
    event.getTo().getBlock().getType() == Material.IRON_FENCE){
                
    event.getPlayer().damage(1);
            }
            
    Location loc event.getTo().add(010);
            if(
    loc.getBlock().getType() == Material.IRON_FENCE) {
                
    event.getPlayer().damage(1);
            }
        }
    try and see if it Works for you :)
     
  7. Offline

    Gnat008

  8. Offline

    Jace_oio

    Gnat008 it dosnt need to be static :), just make it
    PHP:
    public void ....
    @
     
  9. Offline

    gabe4356

    Thanks!
     
  10. Offline

    Jace_oio

    gabe4356 no problem, just remember to scroll through the code a few extra times then you actual think is needed. If you did so you would have spoted that the @EventHandler was missing :)
     
  11. Offline

    gabe4356

    This is now my code, but now when the player touches the iron block, it doesn't damage them :\
    Code:java
    1. package com.octo;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class OctoMainOod extends JavaPlugin implements Listener {
    15.  
    16. @EventHandler
    17. public static void onPlayerMove(PlayerMoveEvent event){
    18.  
    19. if(event.getTo().getBlock().getType() == Material.IRON_BLOCK){
    20. event.getPlayer().damage(10);
    21. }
    22. Location loc = event.getTo().add(0, 1, 0);
    23. if(loc.getBlock().getType() == Material.IRON_BLOCK) {
    24. event.getPlayer().damage(10);
    25. }
    26. }
    27.  
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    30. Player player = (Player) sender;
    31. if (cmd.getName().equalsIgnoreCase("OctoJoin")) {
    32. player.sendMessage(ChatColor.AQUA + "You will be teleported to the next open game!");
    33.  
    34. }
    35.  
    36.  
    37. if (cmd.getName().equalsIgnoreCase("Octo")) {
    38. player.sendMessage(ChatColor.GOLD+ "=================================");
    39. player.sendMessage(ChatColor.AQUA+ " Octo Ood Commands ");
    40. player.sendMessage(ChatColor.GOLD+ "=================================");
    41. player.sendMessage(ChatColor.AQUA+ "Octo Join : Joins an Octo Ood game");
    42. player.sendMessage(ChatColor.AQUA+ "Octo Help : Lists these commands");
    43.  
    44. } else if (foo()) {
    45. player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /octo");
    46. }
    47.  
    48. return true;
    49. }
    50.  
    51.  
    52.  
    53.  
    54.  
    55. private boolean foo() {
    56. // TODO Auto-generated method stub
    57. return false;
    58. }
    59.  
    60.  
    61. public void onDisable() {
    62. getLogger().info(ChatColor.GREEN+ "Octo Ood plugin has been disabled!");
    63. getServer().broadcastMessage(ChatColor.GREEN+ "Octo ood has been disabled!");
    64. //On Disable method stub
    65. }
    66.  
    67. public void onEnable() {
    68. getLogger().info(ChatColor.GREEN+ "Octo ood plugin has been enabled!");
    69. getServer().getPluginManager().registerEvents(this, this);
    70. getServer().broadcastMessage(ChatColor.GREEN+ "Octo Ood has been enabled!");
    71.  
    72. //on Enable Method stub
    73.  
    74.  
    75.  
    76.  
    77. }
    78. }
    79.  
    80.  
    81.  
    82.  
    83.  
    84.  
     
  12. Offline

    Necrodoom

    gabe4356 this would only fire if user was suffocating inside an iron block. Check around the player location instead.
     
  13. Offline

    Jace_oio

    Necrodoom you are completely right, but it would still Work for a iron fence though :) cuz its missing "fill"
     
  14. Offline

    Necrodoom

    Jace_oio a user could still stand beside an iron fence.
     
  15. Offline

    Jace_oio

  16. Offline

    gabe4356

    Ok, still doesn't work, I expanded it by one, but no work.
    Code:java
    1. @EventHandler
    2. public static void onPlayerMove(PlayerMoveEvent event){
    3.  
    4. if(event.getTo().getBlock().getType() == Material.IRON_BLOCK){
    5. event.getPlayer().damage(10);
    6. }
    7. Location loc = event.getTo().add(1, 1, 1);
    8. if(loc.getBlock().getType() == Material.IRON_BLOCK) {
    9. event.getPlayer().damage(10);
    10. }
    11. }
     
  17. Offline

    Jace_oio

    gabe4356 here you go :)

    PHP:
    /**
    @param loc is the location of the the location player just moved too
    @param r the radius you want to check for the {IRON_BLOCK}
    @param player the player who gets damaged if touching {IRON_BLOCK}
      */
        
    private void checkBlocks (Location locdouble rPlayer player) {
            
    int X loc.getBlockX();
            
    int Y loc.getBlockY();
            
    int Z loc.getBlockZ();
            
    int x = (int) (-r);
            
    int y = (int) (-r);
            
    int z = (int) (-r);
            
    int bx x;
            
    int bz z;
            for (
    int i=0i<r*2+1i++) {
                for (
    int j=0j<r*2+1j++) {
                    for (
    int k=0k<r*2+1k++) {
                        
    Block b loc.getWorld().getBlockAt(x,y,z);
                        if(
    b.getType() == Material.IRON_BLOCK) {player.damage(1.0);}
                        
    x++;
                    }
                    
    z++;
                    
    bx;
                }
                
    bz;
                
    bx;
                
    y++;
            }
        }
    PHP:
        @EventHandler
        
    public static void onPlayerMove(PlayerMoveEvent event) {
            if(
    event.isCancelled()) {
                return;
            }
     
            
    Location loc event.getTo();
            
    checkBlocks(loc0.5event.getPlayer());
     
        }
     
     
  18. Offline

    gabe4356

    Thanks!
     
  19. Offline

    Jace_oio

    gabe4356 np, remeber to get the one with parameter explanation for extra coolness :DDDD
     
  20. gabe4356 My god, please pretend static isn't a thing until you've learned what it does :)
     
  21. Offline

    Jace_oio

    gabe4356 Actully if the parameter r was a int your would not need to cast (int) to this: int x = (int) (X -r);int y = (int) (Y -r);int z = (int) (Z -r);

    it would just be redundant
     
Thread Status:
Not open for further replies.

Share This Page