Quote:
Originally Posted by ProDielerNR(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)
|
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:
- Define your command in resources/plugin.yml file
- Create your command listener class, one for each command
- Register your command listeners
1 - Defining your commands
Here is an extract code of my yaml file of my plugin
plugin.yml
Code:
# default data
# ...
commands:
start:
usage: /ircbot:start
description: Start the irc twitch bot
aliases: [irsta]
# ...
Creating command listener
Example of my start command
CommandIrcBotStart.java
Code:
//... Dont forget your imports
public class CommandIrcBotStart implements CommandExecutor {
@ Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(args.length > 0) return false;
if(sender instanceof Player) {
try
{
if(BotManager.getInstance().start())
{
ChatUtils.log(sender,"Bot Started!");
ChatUtils.info(sender,"Bot listening on channel https://www.twitch.tv/" + BotManager.getInstance().getCurrentChannel());
}
else
{
ChatUtils.warn(sender,"Bot already started ");
}
}
catch (Exception e)
{
ChatUtils.error(sender,"Exception ocurred " + e.getMessage());
}
}
return true;
}
}
Registering your command in onEnable
Main.java
Code:
@ Override
public void onEnable() {
// Plugin startup logic
this.getLogger().info("Plugin loaded yay!");
this.getCommand("start").setExecutor(new CommandIrcBotStart());
}
If you have any doubts, check the source of my plugin too
Btw: if you need permission for your commands you will need to check them and add them too