Today I started on my first big project, which is a Minecraft Spigot plugin for my server called Pixel Network. When creating the /help command I encountered a problem. Whenever I called the command it just returned itself. I know that this is a question frequently asked, but I just couldn't get it to work. Here is my code:
Main Class
package gq.pixelnetwork.main;
import gq.pixelnetwork.listeners.CommandListener;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public void onEnable() {
System.out.println("If you see this, the Pixel Network plugin is loaded!");
Bukkit.getServer().getPluginManager().registerEvents(new CommandListener(), this);
}
public void onDisable() { System.out.println("If you see this, the Pixel Network plugin is unloaded!"); }
}
And my Command Listener Class
package gq.pixelnetwork.listeners;
import gq.pixelnetwork.modules.Colors;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
public class CommandListener implements Listener{
Colors c = new Colors();
// Returning false will return the command to the sender!
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (cmd.getName().equalsIgnoreCase("help")) {
if (!(sender instanceof Player)) {
sender.sendMessage(c.red + "This is a Player-only command!");
return true;
} else{
openHelp();
return true;
}
}
return true;
}
private void openHelp() {
Inventory helpGUI = Bukkit.createInventory(null, 27, "§a§lHelp Menu");
createDisplay(Material.BOOK, helpGUI, 11, "§7/spawn", "§fUse it to get to Spawn.");
createDisplay(Material.BOOK, helpGUI, 13, "§7/hub", "§fUse it to get to HUB.");
createDisplay(Material.BOOK, helpGUI, 15, "§7/help", "§fUse it to see this menu.");
}
private static void createDisplay(Material material, Inventory inv, int Slot, String name, String lore) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
ArrayList<String> Lore = new ArrayList<String>();
Lore.add(lore);
meta.setLore(Lore);
item.setItemMeta(meta);
inv.setItem(Slot, item);
}
}
The reference to the Colors class is not the problem, as that is just a small 'module' I have made to make using colors easier.
I hope that someone can help me with this.
Thanks in advance,
- Xaaf
Unless the Spigot API has changed significantly since I've used it, your problem is that you're using onCommand in an event handler, not in your JavaPlugin class.
For example, you can have something like this
public class Whatever extends JavaPlugin {
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(command.getName().equalsIgnoreCase("hello")){
sender.sendMessage("Hello!");
return true;
}
return false;
}
}
Which will work fine, but you're registering your CommandListener as an Event Listener, meaning that when bukkit parses the class, it looks for any methods with an #EventHandler annotation and processes them as an event when they're fired.
You can also a Command Executor, which would look something like this:
public class Whatever extends JavaPlugin {
#Override
public void onEnable(){
this.getCommand("test").setExecutor(new MyCommandExecutor(this));
// register the class MyCommandExecutor as the executor for the "test" command
}
#Override
public void onDisable(){
}
}
then have another class like so:
public class MyCommandExecutor implements CommandExecutor {
private final Whatever plugin;
public MyPluginCommandExecutor(Whatever plugin) {
this.plugin = plugin; // Store the plugin in situations where you need it.
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(command.getName().equalsIgnoreCase("test")){
sender.sendMessage("Hello World!");
return true;
}
return false;
}
}
Essentially, you're trying to implement a CommandExecutor inside an EventListener, which shouldn't work, as far as I'm aware.
You're also returning true by default if the command isn't the one your plugin is handling, which is wrong, you should return false if it's not your command.
Related
I use gradle for build, Java, minecraft, Paper plugin
ctrl+f , figure out " the problem "
I made each of these plugins separately. Now, I am putting them together into one plugin but separating each class. Then, this error occurred. I want to solve this problem.
I thought third_function.third_listener could be used for that place, but It seems to be worng...
package com;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class mainclass extends JavaPlugin implements Listener {
#Override
public void onEnable() {
getLogger().warning("Server started.");
first_function first = new first_function(this);
first.onEnable();
second_function second = new second_function(this);
second.onEnable();
third_function third = new third_function(this);
third.onEnable();
PlayerJoinEvent third_listener;
third.onPlayerJoin(third_function.third_listener); // the problem. please help me, how can I get rid of this error?
}
}
class first_function implements Listener {
private final mainclass plugin;
first_function(mainclass plugin) {
this.plugin = plugin;
}
void onEnable() {
this.plugin.getLogger().warning("Hello, world!");
}
}
class second_function implements Listener {
private final mainclass plugin;
second_function(mainclass plugin) {
this.plugin = plugin;
}
void onEnable() {
World world = this.plugin.getServer().getWorld("world");
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
}
}
class third_function implements Listener {
private final mainclass plugin;
third_function(mainclass plugin) {
this.plugin = plugin;
}
Listener third_listener = (this);
void onEnable() {
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendActionBar(Component.text("Welcome",(NamedTextColor.BLUE)));
}
}
You should register the listener and not try to call the method yourself. For example:
#Override
public void onEnable() {
third_function third = new third_function(this);
getServer().getPluginManager().registerEvents(third, this);
}
Also, I mostly suggest you to use global java names convention, such as name ThirdFunction instead of third_function for example
I'm new in minecraft plugins development and I have a problem than i can't solve.
I try to response to the sender when he write /point in the chat. I have this in my first class:
package fr.azmog25.pointsaver.pointsaver;
import org.bukkit.plugin.java.JavaPlugin;
public final class PointSaver extends JavaPlugin {
#Override
public void onEnable() {
// Plugin startup logic
System.out.println("Serveur lancé !");
System.out.println(this.getCommand("point"));
}
#Override
public void onDisable() {
// Plugin shutdown logic
System.out.println("Serveur éteint !");
}
}
My second class look's like this :
package fr.azmog25.pointsaver.pointsaver;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class PointAdder implements CommandExecutor {
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(!(sender instanceof Player)) {
System.out.println("Vous devez être un joueur !");
return false;
}
Player player = (Player) sender;
player.sendMessage("bravo ça marche !");
return true;
}
}
and my plugin.yml have :
name: PointSaver
version: 1.0
main: fr.azmog25.pointsaver.pointsaver.PointSaver
commands:
point:
description: Save coordinates
permission: point.use
But the getCommand() always return null and it's my first time in plugin development...
Don't you need to add an executor to your command to make it return something ?
this.getCommand("point").setExecutor(new PointAdder());
It should probably fix your stuff.
(+ According to your last comment the plugin.yml file should be at the root of your jar file.)
I use eclipse (keep in mind, I'm a beginner), I have made a bukkit plugin which doesn't work - when it is put into the plugins folder of my Minecraft server, it doesn't change anything, and the custom commands don't work. I have installed the correct version of bukkit. Does anyone know what's wrong?
Pastebin - https://pastebin.com/PbhLFic2PbhLFic2
package customcommands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class customcommands extends JavaPlugin {
#Override
public void onEnable() {
getLogger().info("This plugin is now on and fuctioning");
}
#Override
public void onDisable() {
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("hello") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("Hello, " + player.getName() + "!");
}
return false;
}
public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("discord") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("§c§lDiscord §8§l- §e Want to connect with us? §e§dhttps://discord.gg/T9fhEDh");
}
return false;
}
public boolean onCommand2(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("website") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("§c§lShop §8§l- §e Want to visit the server website? §e§dhttps://noname-mc.enjin.com/");
return true;
}
return false;
}
}
There are multiple issues with the snippet you have provided. I highly suggest you read through some tutorials, specifically for this problem, have a read of this Spigot Wiki post; the process is the same for Bukkit and Spigot.
Your command won't work because you haven't registered a CommandExecutor, you would need to make the following changes to customcommands:
public class customcommands extends JavaPlugin implements CommandExecutor {
#Override
public void onEnable() {
getLogger().info("This plugin is now on and functioning");
this.getCommand("hello").setExecutor(this);
}
But you have multiple onCommand methods, CommandExecutors don't work like that, you'll need to make a new CommandExecutor for each new /command, for which you will need to place in other classes and register them.
You're also returning false which will prompt the CommandExecutor to return back to the player the default usage for the command, as supplied in your plugin.yml; if the command was successful, you should return true.
I would suggest that you try Java tutorials first before diving into the Bukkit API as you will struggle otherwise.
I made a Plugin that registers an event, I want to make a command that unregisters it, how should I do it, I already searched for 2 h and I found nothing.
I want to make /Pvpeventon to start the event and /Pvpeventoff to turn it off
that is the code I already made:
package me.leopa.R1.FFA;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MainFFA extends JavaPlugin implements Listener{
#Override
public void onEnable() {
System.out.println("[INFO Leopa] Start");
super.onEnable();
}
#Override
public void onDisable() {
System.out.println("[INFO Leopa] Stop");
super.onDisable();
}
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(command.getName().equalsIgnoreCase("PVPEVENTon")) {
getServer().getPluginManager().registerEvents(this, this);
}
if(command.getName().equalsIgnoreCase("PVPEVENToff")) {
getServer().getPluginManager().
}
return super.onCommand(sender, command, label, args);
}
#EventHandler
public void onDeathPVPEVENT(PlayerDeathEvent pvpevent) {
Player p = pvpevent.getEntity();
p.sendMessage("HI");
}
}`
Instead of unregister the event you should simplify it and add a boolean as variable which turns into false when the pvp should be disabled and to true if pvp is allowed:
//Some Listener class
...
private YourPlugin plugin; //example
...
#EventHandler
public void playerDeath(PlayerDeathEvent event) {
if(plugin.isEventMode()) { //TODO when event mode is on }
}
Plugin class
...
public class YourPlugin extends JavaPlugin {
...
private boolean eventMode; //false per default
...
public boolean toggleEventMode() {
eventMode = !eventMode; //negation so if it is true it will be turned into false if it is false it will be turned to true
return eventMode;
}
public boolean isEventMode() {
return eventMode;
}
}
Command toggle event mode:
//is declared somewhere
boolean eventMode = plugin.toggleEventMode();
//true if eventMode is on false if not.
Note you can also use a setEventMode method.
You also can use the unregisterAll method to unregister all events in a Listener or a Plugin:
HandlerList.unregisterAll(this); //takes a listener or a plugin. In your case you got all stuff in one class it should still work.
Check these methods:
HandlerList#unregister(Plugin)
HandlerList#unregister(Listener)
HandlerList#unregisterAll(Plugin)
HandlerList#unregisterAll(Listener)
why isn't this working? I don't get any response if I've been killed! So as you can see I have tested multiple ways. But no one is working.
package net.gameforce.testing;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this,this);
getLogger().info("Testing Plugin Started");
}
public void onDisable() {}
#EventHandler
public void onPlayerInteract(PlayerInteractEvent event){
Bukkit.broadcastMessage("test");
event.getPlayer().setExp(100);
}
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}
public boolean onDeath (PlayerDeathEvent event) {
Player Player = event.getEntity();
Bukkit.broadcastMessage(Player.getKiller().getDisplayName() + ", has killed you!");
if (Player.getKiller() != null) {
Bukkit.broadcastMessage("No Player");
}
else {
Bukkit.broadcastMessage("IDK");
}
return true;
}
}
Have I done something wrong?
Quotation from http://bukkit.gamepedia.com/Event_API_Reference#.40EventHandler
"Before this method can be invoked by Bukkit when the "Event" is fired, we need to annotate it. We do this with EventHandlers."
You've added this annotation to your onPlayerInteract method, but none of your others, such as your onDeath method. If your listener is setup correctly, adding the #EventHandler annotation to these methods will allow bukkit to correctly invoke them, like so:
#EventHandler
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}