Solved On Chat/Command event

Discussion in 'Plugin Development' started by Dablakbandit, Nov 13, 2013.

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

    Dablakbandit

    Hi i am new to programming, and am wanting to write a program that detects when someone either chats or runs any command. I know how to write a code for when a person does a specific command, but have tried writing for anyone doing any command. Please consider that i am writing for a 1.2.5 Tekkit server and some events won't be implemented.

    Thanks in advance for any help.
    Dablakbandit
     
  2. Offline

    TeeePeee

    You can listen for the PlayerCommandPreProcessCommand event (if it exists in 1.2.5?) for listening in on every player command.

    And the AsyncPlayerChatEvent for normal chat.
     
  3. Offline

    Dablakbandit

    Can i get an example? I'm at a loss as how to do this :confused:
     
  4. Offline

    Sweatyyyy

    Dablakbanit
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent event){
    3. //what you want to happen goes here
    4. }
    5.  
    6. @EventHandler
    7. public void preCommand(PlayerCommandPreProcessCommand event){
    8. //what you want to happen goes here
    9. }


    In 1.2.5 AsyncPlayerChatEvent may have been just PlayerChatEvent or ChatEvent - Something along those lines
     
  5. Offline

    Dablakbandit

    Okay, so i put those in here my code

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    3. {
    4. if(commandLabel.equalsIgnoreCase("Dablakbandit")){
    5. String str1 = String.valueOf(start);
    6. ((Player) sender).sendMessage(ChatColor.BLUE + str1);
    7. }
    8. return true;
    9. }
    10.  
    11. @EventHandler
    12. public void onChat(PlayerChatEvent event){
    13. start = 1;
    14. }
    15.  
    16. @EventHandler
    17. public void preCommand(PlayerCommandPreprocessEvent event){
    18. start = 2;
    19. }


    but when i do /dablakbandit nothing happened just "unkown command"

    Fixed, i added Dablakbandit into the plugin.yml, now when i do it it just constantly just says 1 even after doing /help etc which should come up with 2 , any help?

    Help?

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

    TeeePeee

    Dablakbanit
    If you want me to see your replies, please tag me in your post. I'm going to assume this is all in your main class, in which case you need to do this in the onEnable:

    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);


    And you need to add "implements Listener" after "extends JavaPlugin".

    Keep in mind, when this works, it should technically always output 2 when a player executes it because the CommandPreprocess happens before the command does.
     
  7. Offline

    Dablakbandit

    TeeePeee
    Thanks! it worked, now how would i make a countdown thing, that would countdown for say 10 minutes? and would reset if someone chat's or does a command? But if the timer reaches say 2 minutes it would broadcast "Please speak or ran a command, or the server will restart in 2Mins", then when reaches 0 it runs console command "restart".

    Effectively this is a automatic restarter for when the server crashes.
    Thanks for the help!
    Dablakbandit
     
  8. Offline

    TeeePeee

    I can't provide code for this at the moment, but essentially, you would want to run a synchronous task using the following.

    Code:
    Plugin.getServer().getScheduler().runTaskTimerAsynchronously(Plugin, new Runnable() {
    public void run() {
        Plugin.minutesRemaining--;
        if(Plugin.minutesRemaining == 0) {
            Bukkit.dispatchCommand("restart");
        } else if(Plugin.minutesRemaining == 2) {
            Bukkit.broadcastMessage("Server restarting in 2 minutes. Please speak or run a command to continue playing.");
        }
    }, 120L);
    You would have to create in your main class minutesRemaining = 10; and 'Plugin' in the code example must be an instance of your main class. In your chat and command listener, simply set Plugin.minutesRemaining to 10.
     
  9. Offline

    Dablakbandit

    TeeePeee

    It comes up with an error on the "Plugin.getServer().", also do i put this in a new class file? or just put it in the main one?

    Thanks,
    Dablakbandit
     
  10. Offline

    TeeePeee

    If you put in in the main class, just remove "Plugin." completely.
     
  11. Offline

    Dablakbandit

    TeeePeee

    On:
    Code:java
    1. {
    2. getServer().getScheduler().runTaskTimerAsynchronously(new Runnable() {


    i get the error
    [​IMG]

    TeeePeee

    Also

    Code:java
    1. @EventHandler
    2. public void onChat(PlayerChatEvent event){
    3. String remaining = String.valueOf(minutesRemaining);
    4. log.info("Someone has talked!" + remaining);
    5. int minutesRemaining = 10;
    6. }


    says
    minutesRemaining cannot be resolved to a variable

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

    TeeePeee

    Alright let's try restarting here.

    In your main class in the onEnable method:
    Code:java
    1. instance = this;
    2. getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() {
    3. public void run() {
    4. minutesRemaining--;
    5. if(minutesRemaining == 0) {
    6. Bukkit.dispatchCommand("restart");
    7. } else if(minutesRemaining == 2) {
    8. Bukkit.broadcastMessage("Server restarting in 2 minutes. Please speak or run a command to continue playing.");
    9. }
    10. }
    11. }, 120L);


    And in your main class, create the integer minutesRemaining as well as a static reference to your main class:
    Code:java
    1. int minutesRemaining = 10;
    2. public static <NAME OF MAIN CLASS> instance;


    And in your listeners:
    Code:java
    1. <NAME OF MAIN CLASS>.instance.minutesRemaining = 10;
     
  13. Offline

    Dablakbandit

    TeeePeee

    I did something different in my onEnable i did:
    Code:java
    1. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    2. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    3.  
    4. public void run() {
    5. minutesRemaining--;
    6. String str1 = String.valueOf(minutesRemaining);
    7. log.info(str1);
    8. if(minutesRemaining == 0) {
    9. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Stop");
    10. } else if(minutesRemaining == 2) {
    11. Bukkit.broadcastMessage("Server crash detected, please speak to reset timer.");
    12. }
    13.  
    14. }
    15. }, 120L, 120L);


    At the end the "120L" how do i change it so that it times for 1 minute?

    Thanks for the help!
     
  14. Offline

    AoH_Ruthless

    Dablakbandit
    The time is represented in Minecraft ticks, so 20L is equal to one second. So you can just use 1200L or use 60 * 20L. Whichever one works best for you.
     
  15. Offline

    Dablakbandit

    AoH_Ruthless

    Thanks! Well i finally finished the plugin, probably did half of it wrong :p but its done. Now is there like a guide for adding a config.yml ?

    Thanks
     
  16. Offline

    AoH_Ruthless

  17. Offline

    Dablakbandit

  18. Offline

    AoH_Ruthless

    Dablakbandit

    Top of this thread > Thread Tools > Prefix > Solved
     
  19. Offline

    TeeePeee

    Dablakbandit
    Thread Tools > Prefix > Solved
    or something along those lines.
    [edit]
    Snip'd :(
     
Thread Status:
Not open for further replies.

Share This Page