Plugin Eclipse 1.8 chat event

02/15/2020 15:24 ProDielerNR(1)#1
Hallo alle zusammen ich verzweifel an einem event
Code:
 public void onchat(AsyncPlayerChatEvent e) {

          }
Ich versuche schon die ganze zeit raus zu bekommen wie ich abfragen kann
ob ein Spieler eine nachricht versendet hat
Kann mir einer da helfen (wüsste jemand eine Methode das abzufragen)?
Ich hab schon echt viel aus dem internet ausprobiert und auch selbst rumprobiert aber ich komme nicht drauf was mein fehler sein könnte.

LG
ProdielerNR(1)
02/15/2020 17:03 elmarcia#2

First you need to create a class to keep your listener
Code:
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

public class MyCustomEventListener implements Listener {
    @ EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {

        Player player = event.getPlayer();

        if ( event.getMessage().equalsIgnoreCase("hi")) {
            event.setMessage("I replaced your hi message, how do u feel?");
            player.sendMessage("I go before");
        }
    }
}
Then you need to register that listener to work

in your OnEnable method

Code:
   @ Override
    public void onEnable() {
        // Plugin startup logic
        this.getLogger().info("Plugin loaded yay!");
        this.getServer().getPluginManager().registerEvents(new MyCustomEventListener(),this);
    }
But i don't know if that is what you want, if you need chat to execute plugin commands, that is not the way to go...

In order to execute commands in chat you need:
  1. Define your command in resources/plugin.yml file
  2. Create your command listener class, one for each command
  3. Register your command listeners


1 - Defining your commands
Here is an extract code of my yaml file of my plugin

plugin.yml


Creating command listener

Example of my start command

CommandIrcBotStart.java

Registering your command in onEnable

Main.java

If you have any doubts, check the source of my plugin too
[Only registered and activated users can see links. Click Here To Register...]

Btw: if you need permission for your commands you will need to check them and add them too
[Only registered and activated users can see links. Click Here To Register...]
02/15/2020 21:43 ProDielerNR(1)#3
when i send something written something should happen, in my case a sound should be played by a player.

that should happen automatically, no matter what I write, without a command.

i hope you can this understand (Power by google translate) xD

LG
ProDielerNr(1)

Code:
    @ EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {

        Player player = event.getPlayer();
        String msg = event.getMessage();
        if ( event.getMessage().equalsIgnoreCase(msg)) {
            player.playSound(player.getLocation(),Sound.CHICKEN_EGG_POP,3,2);
        }
    }
}
I got it done after trying a few things!

Closed Quest
02/15/2020 22:21 elmarcia#4
Quote:
Originally Posted by ProDielerNR(1) View Post
when i send something written something should happen, in my case a sound should be played by a player.

that should happen automatically, no matter what I write, without a command.

i hope you can this understand (Power by google translate) xD

LG
ProDielerNr(1)

Code:
    @ EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {

        Player player = event.getPlayer();
        String msg = event.getMessage();
        if ( event.getMessage().equalsIgnoreCase(msg)) {
            player.playSound(player.getLocation(),Sound.CHICKEN_EGG_POP,3,2);
        }
    }
}
I got it done after trying a few things!

Closed Quest
U don't have to compare with anything, the event is fired every time a chat message is sent so no need to use that if, also if you want the sound to be heared by other players you should consider playing the sound in the world and not the player

Code:
    @ EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {

        Player player = event.getPlayer();
        World world = player.getWorld();            
        world.playSound(player.getLocation(),Sound.CHICKEN_EGG_POP,3,2);
        
    }
}