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.)
Related
So as the title says I need my config file to modify my servers motd, I feel like I've done everything the right way but obviously something is wrong, so let me show you what I'm working with
me.zavage.motd.Main
package me.zavage.motd;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
saveDefaultConfig();
}
}
me.zavage.motd.listeners.PingListener
package me.zavage.motd.listeners;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import me.zavage.motd.Main;
import me.zavage.motd.utils.Utils;
public class PingListener implements Listener {
private Main plugin;
#EventHandler
public void onPing(ServerListPingEvent e) {
e.setMaxPlayers(plugin.getConfig().getInt("maxplayers"));
e.setMotd(Utils.chat(plugin.getConfig().getString("motd" + "\n" + (Utils.chat(plugin.getConfig().getString("motd_line_2"))))));
try {
e.setServerIcon(Bukkit.loadServerIcon(new File(plugin.getConfig().getString("server_icon_path"))));
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
Then we have the config.yml
#can only be a NUMBER with NO DECIMALS!
maxplayers: ''
motd: ''
#next line on motd
motd_line_2:
#create a folder inside the first page of your server files, call it "icon" and drop you server icon in there
#server icon MUST be 64 x 64 pixels
# #NameOfIcon#
#path example "C:\Users\user\Desktop\minecraft test server\icon\icon.png
server_icon_path: ''
If the code that you show if everything that you are using, there is few issues.
You don't register the listener
package me.zavage.motd;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(new PingListener(this), this);
}
}
You don't set the plugin variable in the listener, and you don't have constructor
public class PingListener implements Listener {
private Main plugin;
public PingListener(Main plugin) {
this.plugin = plugin;
}
}
Finally, you are searching for the motd\n motd_line_2 message. You have to use this in your ServerListPingEvent listener :
e.setMaxPlayers(plugin.getConfig().getInt("maxplayers"));
e.setMotd(Utils.chat(plugin.getConfig().getString("motd") + "\n" + plugin.getConfig().getString("motd_line_2")));
try {
e.setServerIcon(Bukkit.loadServerIcon(new File(plugin.getConfig().getString("server_icon_path"))));
} catch (Exception exc) {
exc.printStackTrace();
}
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)
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.
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);
}