Tutorial: Owner PM Bot - Listener
Instead of making our main class do everything, we will create a dedicated class to handle events. This class must implement the Listener interface.
Create OwnerPMListener.java:
java
package com.example.pmbot;
import xin.bbtt.mcbot.Bot;
import xin.bbtt.mcbot.event.EventHandler;
import xin.bbtt.mcbot.event.Listener;
import xin.bbtt.mcbot.events.PrivateChatEvent;
public class OwnerPMListener implements Listener {
private final String ownerName = "YourMinecraftUsername"; // Change this to your in-game username
// The @EventHandler annotation marks this method as an event listener
@EventHandler
public void onPrivateMessage(PrivateChatEvent event) {
String sender = event.getSender().getName();
String message = event.getMessage();
// Check if the sender is the owner
if (sender.equals(ownerName)) {
// Check if the message starts with our trigger prefix
if (message.startsWith("!run ")) {
// Extract the actual command (e.g., "!run help" becomes "help")
String commandToExecute = message.substring(5);
Bot.INSTANCE.getCommandManager().callCommand(commandToExecute);
}
}
}
}Xinbot Components Used
Listener: A marker interface fromxin.bbtt.mcbot.event.Listener. Implementing this interface tells Xinbot that this class is intended to contain event handling methods.@EventHandler: An annotation used to mark a specific method as an event handler. When an event occurs, Xinbot's EventManager looks for methods with this annotation that take the corresponding event type as their parameter.PrivateChatEvent: Represents an incoming private message (whisper) from the server. It contains information such as theGameProfileof the sender and theStringcontent of the message.Bot.INSTANCE: The singleton instance of the mainBotclass. This is the central access point for interacting with Xinbot's core systems (like sending messages, accessing managers, or disconnecting).Bot.INSTANCE.getCommandManager().callCommand(...): TheCommandManagerhandles all registered commands.callCommandexecutes built-in or plugin-provided Xinbot commands locally (such aslist,stop, orplugins).
Congratulations on completing the tutorial! You can now explore the Advanced APIs to learn more.
