Listener in conjunction with if statement

Discussion in 'Plugin Development' started by GeorgeMarx, Apr 21, 2014.

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

    GeorgeMarx

    I would like a example of this as i know you can not have a listener in a main method, so how would i use a if statement in a main method to trigger the listener? I will post my code if needed , but i think a example would be more beneficial.
     
  2. Offline

    Garris0n

    What you're saying does not make much of any sense. You're going to need to explain a bit better.
     
  3. Offline

    Bobit

    GeorgeMarx
    You have to have it inside the listener, unfortunately.
    Example:
    Code:java
    1. @Override
    2. public void interaction(PlayerInteractEvent event) {
    3. if ((player.getItemInHand().getType()) == Material.FLINT) {
    4. Skillz.fireball(this, player);
    5. }
    6. }
    7.  


    And, Garris0n, yeah.
     
  4. Offline

    GeorgeMarx

    Bobit So how would i use the listener in my main method , or is there a way around that?
     
  5. Offline

    Garris0n

    What exactly is a main method?
     
  6. Offline

    Bobit

    Your main class, you mean, there is no main method because this is not something you click on your computer to activate.

    No, there isn't. Think about it: it triggers on something, and then it has to start making the check. It can't make the check, and then trigger, because then it would always be triggering, and that would cause a lot of lag.

    And Garris0n lol ikr xD
     
  7. Offline

    GeorgeMarx

    Garris0n sorry the class i type my commands in didn't mean "Main" just meant the big one i guess but when i put it in there it says void is a invalid type for the variable

    My comment was stupid would i need to create a instance of something like a hashmap inside my listener then?
     
  8. Offline

    Garris0n

    Post the code you're using.
     
  9. Offline

    Bobit

  10. Offline

    GeorgeMarx

    Code:java
    1. package me.GeorgeMarx;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Damageable;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.EventPriority;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.entity.EntityDamageEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class PvpGod extends JavaPlugin implements Listener {
    20. HashMap<String, Boolean> map = new HashMap<String, Boolean>();
    21.  
    22. Logger PvpLogger = this.getLogger();
    23.  
    24. @Override
    25. public void onEnable() {
    26. // Startup message
    27. PvpLogger.info("Starting PvpGod");
    28. PvpLogger.warning("Be sure to have FUN");
    29. this.getServer().getPluginManager().registerEvents(this, this);
    30.  
    31. }
    32.  
    33.  
    34. @Override
    35. public void onDisable() {
    36. // Closing message
    37. PvpLogger.info("Disabling PvpGod");
    38. PvpLogger.severe("Disabling PvpGod HELP");
    39. }
    40.  
    41. public boolean onCommand(CommandSender theSender, Command cmd,
    42. String commandLabel, String[] args) {
    43.  
    44. if (!(theSender instanceof Player)) {
    45. theSender.sendMessage("Only a player can use this command, sorry!");
    46. return true;
    47. }
    48.  
    49. Player thePlayer = (Player) theSender;
    50. String ourPlayer = thePlayer.getName();
    51.  
    52. // when /cgod is typed
    53. if (cmd.getName().equalsIgnoreCase("cgod")) {
    54.  
    55.  
    56. thePlayer.sendMessage(ChatColor.GOLD + ourPlayer);
    57.  
    58. // The if statement i want inside my Listener
    59. if ((map.get(ourPlayer) == null) || !map.get(ourPlayer)) {
    60. thePlayer.sendMessage(ChatColor.GREEN + "Cgod enabled");
    61. map.put(ourPlayer, true);
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69. }
    70. else
    71. {
    72. thePlayer.sendMessage(ChatColor.DARK_RED + "Cgod disabled");
    73. map.put(ourPlayer, false);
    74. }
    75. }
    76.  
    77. double health = ((Damageable) thePlayer).getHealth();
    78. // if (health < 5)
    79. // {
    80. // ((Damageable) thePlayer).setHealth(20);
    81. // }
    82.  
    83. return true;
    84. }
    85. // Listener
    86. @SuppressWarnings("deprecation")
    87. @EventHandler
    88. public void onPlayerDamage(EntityDamageEvent event) {
    89. Entity e = event.getEntity();
    90. Player thePlayer = (Player) event.getEntity();
    91. if(e instanceof Player)
    92.  
    93. {
    94. Player player = (Player)e;
    95. Bukkit.broadcastMessage("PlayerDamaged");
    96. if ((map.get(ourPlayer) == true))
    97. {
    98. Bukkit.broadcastMessage("POOP");
    99. ((Damageable) thePlayer).setHealth(0);
    100. }
    101.  
    102. }
    103.  
    104. }
    105. }
    Bobit Garris0n
     
  11. Offline

    Bobit

    Just so I'm sure I understand what's happening here:

    You want to add an if statement and some code to a piece of code, but only if another if statement is true?
    That's easy!
    Just add
    Code:
    if((if statement is true) && (if statement has been activated)) {
     
    } 
    To your listener, and an ifstatementhasbeenactivated condition outside of it.
     
  12. Offline

    GeorgeMarx

    Bobit Yes but the if statement does not work outside of the main class. is there a way to create a instance of it with the same values because when the command cgod is typed then our player = true

    and i need it so when ourplayer damaged&& if ourplayer = true then broadcast message
     
  13. Offline

    Bobit

    Okay, what is this plugin supposed to do? Make you immune to pvp when...?
     
  14. Offline

    GeorgeMarx

    Bobit that does not matter to be honest but think of it like this

    when damaged && map ourplayer = true
    {
    //example placeholder
    Bukkit.broadcastMessage("PlayerDamaged");
    }
     
  15. Offline

    Bobit

    You can't do if(ourplayer ==true):

    Ourplayer is the player who just casted the command right that very moment! It's not every player that casted the command. So, yeah, you probably want a list of players who "are god."

    It's hard to help someone fix their code when that person doesn't tell you what the code is supposed to do and it has "magic" (meaning the name isn't descriptive) variables like ourplayer.
     
  16. Offline

    Garris0n

    So you want to access values from the main class from another class, then? When you create the other class have a constructor that accepts the main class. Then just pass the current instance when you instantiate the other class.
     
  17. Offline

    Gater12

    GeorgeMarx
    I am also quite confused. Maybe explaining in a bit more detail and we can help you further?
     
  18. Offline

    GeorgeMarx

    @Gater Garris0n ok basically i want to be able to type /cgod to make hashmap boolean true i have done that bit
    I also want me to be able to be damaged it say a command i have this bit

    Now i want it so that when i take damage it checks to see if cgod is true then if it is say the command
     
  19. Offline

    Garris0n

    And you want to access your map from a different class, correct?

     
  20. Offline

    Bobit

    I think the problem here is that there's only one ourplayer: the one who types the command.
    There should really be two: the one that types the command, and the one that gets hurt.
    And I gtg for the night.
     
  21. Offline

    GeorgeMarx

    Garris0n could i do it within one class?
     
  22. Offline

    Garris0n

    No, that doesn't make any sense at all. If you just want to use one class what exactly is the problem?
     
  23. Offline

    GeorgeMarx

    Garris0n the strings and map i have created are not carried over how would i use them with the same values?
     
  24. Offline

    Garris0n

    Carried over to what?
     
  25. Offline

    GeorgeMarx

  26. Offline

    Garris0n

    If the listener is in the same class then the map does carry over.
     
  27. Offline

    GeorgeMarx

    Garris0n i thought you said the listener cant be in the same class
     
  28. Offline

    Garris0n

    Nobody said that.
     
  29. Offline

    GeorgeMarx

    Garris0n Ok i referenced the hashmap using dot operators how would i reference ourPlayer though?
    its a string
     
  30. Offline

    Garris0n

    Well what does it represent?
     
Thread Status:
Not open for further replies.

Share This Page