Command issues an Event

Discussion in 'Plugin Development' started by Jabbers9999, Jan 24, 2016.

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

    Jabbers9999

    So I would like it if I did /nojoinmessage off it would turn off the join message.
    Heres my code:
    Code:Java
    1.  
    2. package me.jabbers99.argumenttest;
    3.  
    4. import org.bukkit.ChatColor;
    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.player.PlayerJoinEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin {
    13. public boolean onCommand(CommandSender sender, Command cmd, String label,
    14.  
    15. String[] args) {
    16.  
    17. if (cmd.getName().equalsIgnoreCase("joinmessageedit")) {
    18.  
    19. if (args.length == 2) {
    20.  
    21. if (!args[0].equalsIgnoreCase("set")) {
    22. sender.sendMessage(ChatColor.RED + "Invalid usage!");
    23. }
    24. }
    25.  
    26. else
    27.  
    28. {
    29.  
    30. sender.sendMessage(ChatColor.RED
    31. + "Incorrent Number of Arguments!");
    32.  
    33. }
    34.  
    35. String SettJoinMessage = args[1];
    36. JoinEvent(SettJoinMessage, null);
    37.  
    38. return false;
    39.  
    40.  
    41. }
    42. return false;
    43. }
    44.  
    45. @EventHandler
    46. public void JoinEvent(String settJoinMessage, PlayerJoinEvent event) {
    47. Player player = event.getPlayer();
    48. event.setJoinMessage(settJoinMessage);
    49. player.sendMessage("You set the join message " + settJoinMessage);
    50. }
    51.  
    52. }
    53.  
    54.  
    55. [/Syntax=Java]
    56. Sorry for the horrible formatting, but when I copied from eclipse it seemed to have unformatted itself.
    57. Thanks in advance.
     
    Last edited: Jan 26, 2016
  2. Offline

    87pen

    use cmd.getName() instead of label. Save a Boolean and change it depending on whether arg0 is off or on.
     
  3. Offline

    Zombie_Striker

    This shows you do not have enough understanding of Java to be working on bukkit projects. Arrays and collections start at 0. Please pick a tutorial from the following link, take some time to fully understand Java, and then come back. Everything should make sense after that.
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/
     
  4. Offline

    Jabbers9999

    Well I have been learning java for a while now, and I do know what an array is. But it doesn't work when I add @EventHandler inside the Boolean
    Thats not the point. I'm trying to change the event inside the boolean in which it won't let me
     
    Last edited: Jan 24, 2016
  5. Offline

    87pen

    @Jabbers9999 waht. I'm sort of confused on what your problem is then?
     
  6. Offline

    Jabbers9999

    So I can get the argument side of it, but I don't know how you would make it issue the event
     
  7. Offline

    87pen

    @Jabbers9999 Why would you want to issue an event? Just save a Boolean value that turns true or false depending on arg[0] and whenever the PlayerJoinEvent is fired check if the Boolean is false if it is don't send the join message.
     
  8. Offline

    Jabbers9999

    oh okay thanks

    but how would I check if the boolean is true or false from the @Event handler?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  9. Offline

    87pen

    @Jabbers9999 ? from the player join event. In the event, if(!(Boolean)){ // turn off message}
     
  10. Offline

    Jabbers9999

    S
    So would it be:
    Code:
        @EventHandler
        public void JoinEvent(PlayerJoinEvent event) {
            if(!(onCommand == false)){
                // turn off message}
            }
    
    obviously I would change the //turn off message
     
  11. Offline

    Zombie_Striker

    @Jabbers9999
    The same way you check a boolean in any other sort of method. What @87pen provided is good for starting off, but one tool you can use is the "?" syntax. This will compare the boolean, and give two option depending on whether it is true or false. An example (and what you're looking for) is the following
    Code:
    boolean sendMessage = false;
    
    //inside the event
    event.setMessage(sendMessage ? /*true*/ event.getMessage() :
    /* : means else, so this gets trigged if it is false*/ " " /*empty string. Nothing is sent*/);
     
    87pen likes this.
  12. Offline

    87pen

    @Jabbers9999 Yea, but you don't have to do == false, and it wouldn't be onCommand. Make a Boolean, set it's value to true or false depending on arg[0], and then use it in the JoinEvent. You don't need == false, because your Boolean is a Boolean you don't need to compare it. But as @Zombie_Striker said I would get more knowledgeable in Java, will help you in the long run so you don't need to ask so many questions.
    Edit: ninja'd
     
    Zombie_Striker likes this.
  13. Offline

    Jabbers9999

    @87pen So I would create another boolean, how would I exactly do this?? Yes I am still learning Java
     
  14. Offline

    87pen

  15. Offline

    Jabbers9999

  16. Offline

    87pen

     
  17. Offline

    Jabbers9999

    ok thanks
     
  18. Offline

    Jabbers9999

    The Array doesn't seem to carry to the @EventHandler
     
  19. Offline

    Zombie_Striker

    @Jabbers9999
    Please tag someone if you want them to respond.

    Can you explain what you mean? There have never been any mentions of arrays besides those from the args, and they don't deal with events at all.
     
  20. Offline

    Jabbers9999

    @Zombie_Striker @87pen Well I mean, I want to set the args to the join message, and I can't do that since the args inside the public boolean, won't carry to the @EventHandler.
     
  21. Offline

    Zombie_Striker

    @Jabbers9999
    A Simple way to get around this is to create a String field which will store the message. When a player triggers the command, a string builder will take the args array and turn it into a string. From there, set the String field to be equal to that new string. Now that the String field is set to the message you want to send, change the message by using the following bit of code
    Code:
    event.setMessage(THE_STRING_FIELD);
     
  22. Offline

    Jabbers9999

    @Zombie_Striker I've managed to get that working, but if you look at my code at the very top, the command won't work. You are meant to do /joinmessageedit set <JoinMessage> and that will set the join message. For some reason, I do the command ingame and it says "An internal error occured while performing this command". Can you please tell me why this is?
     
  23. Offline

    Zombie_Striker

    @Jabbers9999
    The problem is that you have a lack of knowledge of how Events work. You cannot trigger an event that way. An event can only contain the Event instance in the parameters, So you would have to remove the string.

    Please try what I said and make a string field at the top of the class that will contain the message you want. After that, use a StringBuilder to put all the args together into a single string. Then in the event you use the code I provided above to set the message to be equal to that field.

    If you do not fully understand anything that I am saying, use google to find out how to use those objects.
     
  24. Offline

    Jabbers9999

    @Zombie_Striker Why could I not directly setJoinMessage to Array?
     
  25. Offline

    Zombie_Striker

    @Jabbers9999
    Because setJoinMessage requires a String, not an array.
     
  26. Offline

    Jabbers9999

    ok thanks
     
Thread Status:
Not open for further replies.

Share This Page