How would I do this?

Discussion in 'Plugin Development' started by TrilisconPvP, Dec 28, 2013.

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

    TrilisconPvP

    So I would like to have a boolean in my main class set to false, and in my commands class I would like to set that boolean to true then in my events class when someone talks if the boolean is true the cannot talk and the event will cancel. if you could help me with this it would be great

    -Marsh
     
  2. Offline

    Door Knob

    If you mean per-player, you would need to have a HashSet/List called, for example, "canTalk". If canTalk contains a player's name, then let that player talk.

    If you mean everyone can't talk all at once, then the way you have it is fine.
     
  3. Offline

    TrilisconPvP

    but this is not working for me

    Main
    Code:java
    1. package me.marshall;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7.  
    8. public class Main extends JavaPlugin implements Listener{
    9.  
    10. public boolean ChatStatus = false;
    11.  
    12. @Override
    13. public void onEnable(){
    14. Bukkit.getServer().getPluginManager().registerEvents(new Events(), this);
    15. getCommand("grv fjoin").setExecutor(new Triggers(this));
    16. getCommand("grv fleave").setExecutor(new Triggers(this));
    17. getCommand("grv help").setExecutor(new Triggers(this));
    18. getCommand("grv").setExecutor(new Triggers(this));
    19. getCommand("grv status").setExecutor(new Triggers(this));
    20. getCommand("grv fop").setExecutor(new Triggers(this));
    21. getCommand("grv fdeop").setExecutor(new Triggers(this));
    22. getCommand("grv clearchat").setExecutor(new Triggers(this));
    23. getCommand("grv broadcast").setExecutor(new Triggers(this));
    24. getCommand("grv optimize").setExecutor(new Triggers(this));
    25. getCommand("grv stop").setExecutor(new Triggers(this));
    26. getCommand("grv oplist").setExecutor(new Triggers(this));
    27. getCommand("grv suspendchat").setExecutor(new Triggers(this));
    28. }
    29.  
    30. @Override
    31. public void onDisable(){
    32. }
    33. }



    Triggers
    Code:java
    1. if(args[0].equalsIgnoreCase("chat")){
    2. if(args[1].equalsIgnoreCase("suspend")){
    3. if(ChatStatus == false){
    4. ChatStatus = true;
    5. for(int i = 0; i < 120; i++){
    6. Bukkit.getServer().broadcastMessage(" ");
    7. if(i == 119){
    8. Bukkit.broadcastMessage(ChatColor.RED+"The chat has been temporaraly suspended!");
    9. }
    10. }
    11. }else{
    12. if(ChatStatus == true){
    13. ChatStatus = false;
    14. Bukkit.broadcastMessage(ChatColor.GREEN+"The chat is no longer suspended!");
    15. }
    16. }
    17. }
    18. }



    Events
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void ChatSuspended(AsyncPlayerChatEvent evt){
    3. Player player = evt.getPlayer();
    4. String name = player.getName();
    5. if(!player.hasPermission("grv.core.chat.bypass")){
    6. if(ChatStatus == false){
    7. evt.setCancelled(false);
    8. }else{
    9. if(ChatStatus == true){
    10. evt.setCancelled(true);
    11. player.sendMessage(ChatColor.RED+"I'm sorry "+name+", the chat is currently suspended");
    12. }
    13.  
    14. }
    15. }
    16. }


    Anyone know why the event returns the boolean as false even when I have the trigger set it to true?

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

    mrkirby153

    TrilisconPvP

    Two thingsActually, 3 things: 1) In your main class, you don't need to set the executor of every command unless you actually need different executors for different parameters. You can just do getCommand("grv").setExecutor() 2) Follow the java variable naming conventions. (Like your "ChatStatus" would become "chatStatus" as not to confuse it with a type. 3) You can change
    Code:java
    1. if(chatStatus = false){ event.setCanceled(false); }
    to
    Code:java
    1. if(!chatStatus) { event.setCancled(true);
     
  5. Offline

    TrilisconPvP

    @mrkirby153 it did not work, When i set the boolean to true with the command then i chat the event says the boolan is still set to false??

    i dont know why that was pink :$

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

    Door Knob

    Make sure ChatStatus isn't null. It doesn't throw exceptions when you compare a null.
     
Thread Status:
Not open for further replies.

Share This Page