Sa as the title says, I have a plugin that detects swear words. What it does now is that it sends a message to the player. But I also want it to execute a command that is already in the game or from another plugin. I'm not sure how to do this. How is that done? Here is my current code:
package
import com.beam.sweardetector.SwearDetector;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class EventsClass implements Listener {
SwearDetector plugin = SwearDetector.getPlugin(SwearDetector.class);
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD "This server is running AntiSwear v1.0 by BeamCRASH");
}
@EventHandler
public void chatevent (AsyncPlayerChatEvent event) {
for(String s: event.getMessage().split(" ")) {
if(plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.DARK_RED "§lSwearing is not allowed on this server!");
}
}
}
}
Any help will be appreciated. Thank you!
CodePudding user response:
You can execute command as player :
event.getPlayer().performCommand("mycommand");
But, it will not if the player don't have enough permission.
To run command as console, use this :
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mycommand");
Also, you don't need the /
at the beginning of command because it's only to say that it's a command and it can change between players.
Warn: you should run those code in sync. If you are async (specially from AsyncEvent
), you should use this code :
Bukkit.getScheduler().runTask(plugin, () -> {
// my code
player.performCommand("kill");
});