My Command (Specifically only my cmd2 command) doesn't register, and the console displays an error when I start the server. The other command, cmd1, works, but cmd2 doesn't. I'm really not sure why, so I came here for help.
Some of my Main Class:
package me.Vamp.Test;
import me.Vamp.Test.Events.EventsClass;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private Commands commands = new Commands();
#Override
public void onEnable() {
/* Enabler */
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nTest Plugin has been enabled.\n\n");
/* Events Register */
getServer().getPluginManager().registerEvents(new EventsClass(), this);
/* Commands Register */
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
}
}
The following Class (Commands) only show for the errored command (cmd2). If the code for cmd1 is needed, I will show it.
Some of My Command Class:
package me.Vamp.Test;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
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 Commands implements Listener, CommandExecutor {
public String cmd2 = "getpickaxe";
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
/*
/getPickaxe Command
*/
if (cmd.getName().equalsIgnoreCase(cmd2)) {
Player player = (Player) sender;
if (args.length == 0) {
commandGetPickaxe(player);
return true;
} else {
player.sendMessage(Colors.chat("&c&lERROR &cToo many arguments&8."));
return true;
}
}
} else {
sender.sendMessage(Colors.chat("&c&lERROR &cOnly players can use this command&8."));
return true;
}
return false;
}
public void commandGetPickaxe(Player player){
Inventory inv = player.getInventory();
ItemStack item = new ItemStack(Material.WOOD_PICKAXE, 1);
ItemMeta meta = item.getItemMeta();
ArrayList<String> lore = new ArrayList<String>();
meta.setDisplayName(Colors.chat("&3Wooden Pickaxe"));
lore.add(Colors.chat("&7&oThe Starter Pickaxe&8&o."));
meta.setLore(lore);
item.setItemMeta(meta);
inv.addItem(new ItemStack(item));
player.sendMessage(Colors.chat("&8&l» &3You have received a Wooden Pickaxe&8."));
}
}
This is only the display error on my console.
My Console:
Console
Can I suggest you add a multitude of print statements to see what is null?
/* Commands Register */
System.out.println("cmd1 " + commands.cmd1);
System.out.println("cmd2 " + commands.cmd2);
System.out.println("cmdObj " + commands);
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
EDIT 1: It seems as though you are missing the command in your plugin.yml?
It's possible that it is a typo, look carefully. If you think everything is perfectly fine, and the error still occurs, please edit you original post and include the plugin.yml file. Thanks!
Related
This question already has an answer here:
How to get player's ping in spigot 1.16.4
(1 answer)
Closed 2 years ago.
I'm currently trying to code a Minecraft plugin for a server that a Streamer friend of mine wants to run. What I need is the following:
I need a function or method that lets me get the players ping towards the server. So that the player types /ping into the chatbox and gets a response from the server that looks something like [Server]: Your current Ping is 62ms.
I've looked in Bukkit documentation like http://docs.codelanx.com/Bukkit/1.7.10/ or http://bukkit.luricos.de/api/1.7.9-R0.2/, but I couldn't find something that fits my needs.
My Plugin.yml:
name: Beginning
version: ${project.version}
main: de.nightcore.beginning.Main
api-version: 1.16
commands:
date:
description: Shows the current time and Date
ping:
description: Gives the Player its current ping to the server
My Main Class:
package de.nightcore.beginning;
import de.nightcore.beginning.commands.DateCommand;
import de.nightcore.beginning.commands.PingCommand;
import de.nightcore.beginning.listeners.JoinListener;
import de.nightcore.beginning.listeners.QuitListener;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public final class Main extends JavaPlugin {
#Override
public void onEnable()
{
Bukkit.getLogger().fine("Plugin wird geladen!");
listenerRegistration();
commandRegistration();
}
#Override
public void onDisable()
{
Bukkit.getLogger().fine("Plugin wird beendet!");
}
public static String getPrefix()
{
return ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Server" + ChatColor.DARK_GRAY + "] " + ChatColor.WHITE;
}
private void listenerRegistration()
{
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new JoinListener(), this);
pluginManager.registerEvents(new QuitListener(), this);
}
private void commandRegistration()
{
getCommand("date").setExecutor(new DateCommand());
getCommand("ping").setExecutor(new PingCommand());
}
}
And finally my PingCommand Class:
package de.nightcore.beginning.commands;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
//TODO: Ping Command / Command that provides Connection Info
public class PingCommand implements CommandExecutor {
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = sender.getServer().getPlayer("%UUID%"); ;
Server server = sender.getServer();
//Source Code to provide information about the players current ping
return false;
}
}
You need to cast Player to CraftPlayer, after that get the EntityPlayer with CraftPlayer#getHandler(), where you can return the player ping as int value.
((CraftPlayer) player).getHandle().ping;
You also need to add the spigot dependency (the API doesn't seem to include it).
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
I have been creating a plugin for Minecraft on Eclipse, but I am encountering an error. The plugin loads fine; I can see it in the console, but it says unknown command when I try and run the command.
Here is my command code:
package me.TheThunder56.helloworld.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import me.TheThunder56.helloworld.Main;
#SuppressWarnings("unused")
public class HelloCommand implements CommandExecutor{
private Main plugin;
public HelloCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("sword").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
Player p = (Player) sender;
if(p.hasPermission("sword.use")) {
p.performCommand("/give TheThunder56 netherite_sword{Unbreakable:1,Enchantments:[{id:knockback,lvl:1000}]}");
return true;
}else {
p.sendMessage("You do not have permission to execute this command.");
}
return false;
}
}
Here is my Main.java:
package me.TheThunder56.helloworld;
import org.bukkit.plugin.java.JavaPlugin;
import me.TheThunder56.helloworld.commands.HelloCommand;
public class Main extends JavaPlugin{
#Override
public void onEnable() {
getCommand("sword").setExecutor(new HelloCommand(this));
}
}
And here is my plugin.yml file:
name: GodSword
main: me.TheThunder56.helloworld.Main
version: 1.0
author: TheThunder56
commands:
sword:
description: A command to give you a godly sword.
usage: /sword
Please help!
Thanks.
private Main plugin;
public HelloCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("sword").setExecutor(this);
public void onEnable() {
getCommand("sword").setExecutor(new HelloCommand(this));
}
Why are you registering your command twice? No need. Only register the command in your onenable. Also, for your method to give the player the sword, you should not use p.performCommand();, because even if they have the permission they still need op to use /give.
Try using this:
ItemStack sword = new ItemStack(Material.NETHERITE_SWORD));
ItemMeta meta = sword.getItemMeta();
meta.addUnsafeEnchantment(Enchantment.KNOCKBACK, 1000, true);
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
meta.setUnbreakable(true);
meta.setCustomName(ChatColor.GOLD + "GOD Sword"));
sword.setItemMeta(meta);
int i = 0;
while(true){
for(ItemStack it = p.getInventory(){
if(it == null){
p.getInventory().setItem(i, sword);
break;
}else{
i++;
}
}
}
I'm new in java and bukkit api and server can't load my plugin, error in console: https://pastebin.com/GzgLhHp6. Maybe problem in "private Plugin plugin" whick is not used, eclipse warned. My code:
Plugin.java
package Iaiao.main;
import org.bukkit.plugin.java.JavaPlugin;
public class Plugin extends JavaPlugin {
public void onEnable () {
getCommand("ip").setExecutor(new Commands(this));
getLogger().info("Enabled!");
}
}
Commands.java
package Iaiao.main;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
private Plugin plugin;
public Commands(Plugin plugin) {
this.plugin = plugin;
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if(args.length == 0) {
return false;
}
String name = args[0];
Player p = Bukkit.getPlayer(name);
if(p == null) {
sender.sendMessage("This player is offline or not registered");
return true;
}
sender.sendMessage("Ip: " + p.getAddress().getAddress());
return true;
}
}
plugin.yml
name: Plugin
main: Iaiao.main.Plugin
version: 1.0
commands:
ip:
description: Player's IP
usage: /ip <player>
Your plugin.yml is not valid:
org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml
Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
Somewhere in you plugin.yml is a tab character which must not be used.
Just read the whole error message. It gives you hints where errors occured.
I have been getting the following java.lang.NullPointerException when my plugin is being enabled. I don't really see the problem.
Error:
java.lang.NullPointerException
at tk.mypalsgaming.TARDIScraft.TARDIScraft.onDisable(TARDIScraft.java:31)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:401)
at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:394)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.disablePlugins(CraftServer.java:281)
at net.minecraft.server.v1_6_R2.MinecraftServer.stop(MinecraftServer.java:349)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:445)
at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
plugin.yml:
name: TARDIScraft
main: tk.mypalsgaming.TARDIScraft.TARDIScraft
version: 0.0.1
depend: [Vault]
commands:
tardis:
description: TARDIS Command Block and Admin Command
usage: /<command> <TARDIS command> [parameters]
permission: TARDIScraft.admin
permission-message: You are not a TARDIS Admin, so you do not have access to this command.
TARDIScraft.java:
package tk.mypalsgaming.TARDIScraft;
import java.util.logging.Logger;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class TARDIScraft extends JavaPlugin {
Logger console = getLogger();
#Override
public void onEnable() {
console.info("Enabling the TARDIS plugin...");
// TODO: onEnable code
}
#Override
public void onDisable() {
console.info("Disabling the TARDIS plugin...");
// TODO: onDisable code
}
public static Permission permission = null;
#SuppressWarnings("unused")
private boolean setupPermissions()
{
RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
if (permissionProvider != null) {
permission = permissionProvider.getProvider();
}
return (permission != null);
}
public void onPlayerJoin(PlayerJoinEvent evt) {
Player player = evt.getPlayer();
if ( player.hasPermission("TARDIScraft.admin") ) {
console.info("Admin " + player.getName() + " has joined the game.");
}
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if ( cmd.getName().equalsIgnoreCase("tardis") ) {
boolean senderIsPlayer;
if ( sender instanceof Player ) {
senderIsPlayer = true;
} else senderIsPlayer = false;
// TODO: tardis Command
if ( args[0].equalsIgnoreCase("admin") ) {
Player playerToAdmin = Bukkit.getPlayer(args[1]);
if ( playerToAdmin != null ) {
permission.playerAdd(playerToAdmin, "TARDIScraft.admin");
}
}
return true;
} else {
return false;
}
}
}
The NullPointerException is from the "getLogger()" call. The parent class "JavaPlugin" has to have initialize() called before the logger exists. Since you are getting the logger at instantiation time rather than after initialize() is called, the parent class returns null.
The documentation suggested simply calling getLogger() inside your onEnable() and onDisable(), likely because initialize() has been called by then.
Note: The source code say to NOT call initialize() yourself!
See:
https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPlugin.java#L246
http://wiki.bukkit.org/Plugin_Tutorial#Logging_a_message
Here's my code:
package me.chimericalhobo.BlockChanger;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class BlockChanger extends JavaPlugin
{
private static final Logger log = Logger.getLogger("Minecraft");
private final BlockChangerListener blockListener = new BlockChangerListener(this);
public final ArrayList<Player> BlockChangerUsers = new ArrayList<Player>();
#Override
public void onEnable()
{
log.info("[BlockChanger] has been enabled!");
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(this.blockListener, this);
}
#Override
public void onDisable()
{
log.info("[BlockChanger] has been disabled!");
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
if(commandLabel.equalsIgnoreCase("BlockChanger"))
toggleBlockChanger(sender);
return true;
}
private void toggleBlockChanger(CommandSender sender)
{
if(!enabled((Player) sender)){
BlockChangerUsers.add((Player) sender);
((Player) sender).sendMessage(ChatColor.BLUE + "BlockChanger has been enabled!");
}
else
{
BlockChangerUsers.remove((Player) sender);
((Player) sender).sendMessage(ChatColor.RED + "BlockChanger has been disabled!");
}
}
public boolean enabled(Player player)
{
return BlockChangerUsers.contains(player);
}
}
Every time I try to load it the command prompt says:
15:53:08 [SEVERE] Could not load 'plugins\BlockChanger.jar' in folder 'plugins'
org.bukkit.plugin.InvalidDescriptionException: name is not defined
In the plugin.yml add this:
name: (Plugin Name)
main: (Package.name.name(or whatever).(MainClass Ex: .Main)) Ex: me.name.plugin.Main
version: (Version number)
Optionally you can add things such as author: (author name).
Look at http://wiki.bukkit.org/Plugin_YAML for more information.
A plugin needs to consist of a name.
In the plugin.yml you will see a field that says:
name:
Here you must insert a name of your plugin.
A complete example of a plugin.yml looks like this:
name: Velocity Jump
main: com.weebly.foxgenesis.Main
version: 1.0
commands:
vjump:
description: make a player velocity jump to you
usage: /vjump <player> [toPlayer] [time]
default: op
For more info please click this link: http://wiki.bukkit.org/Plugin_YAML
Check your plugin.yml.
Are you sure you have set a name: <plugin name> field in it?