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.
Related
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'm not good at English, but i have to ask about this bug, because i can't fine it anywhere.
i am making minecraft plugin to back to place which player selelcted.
but i can't fix this bug...
it's whole error log
[10:57:29] [Server thread/INFO]: Player issued server command: /setposition
[10:57:29] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'setposition' in plugin Main v0.1
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:723) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1658) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1498) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [Server.jar:git-Spigot-6de3d4b-fc24934]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer cannot be cast to package1.PlayerPosition
at package1.SetPosition.onCommand(SetPosition.java:16) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
... 17 more
it's back command
player can tp to selected place by setposition command
package package1;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.md_5.bungee.api.ChatColor;
public class Back implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(sender instanceof Player) {
Player p = (Player) sender;
PlayerPosition pp = (PlayerPosition) p;
if(pp.location.equals(null)) {
p.sendMessage(ChatColor.RED + "Set Your Position To Back First By Using SetPosition.");
return false;
}
else
{
p.teleport(pp.location);
return true;
}
}
return false;
}
}
it's PlayerPosition class
this class extends Player
and a add location
package package1;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public abstract class PlayerPosition implements Player {
public Location location;
PlayerPosition(Location l){
this.location = l;
}
}
and it's setposition command.
package package1;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.md_5.bungee.api.ChatColor;
public class SetPosition implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(sender instanceof Player) {
Player p = (Player) sender;
PlayerPosition pp = (PlayerPosition) p;
pp.location = p.getLocation();
p.sendMessage(ChatColor.BLUE + "Position Setting Complete.");
return true;
}
return false;
}
}
How can i fix it?
Player is an interface, not a class, so any class implementing Player interface must also implement all the methods in Player.
Example:
public interface MyInterface {
public void aMethod();
}
If you have a class that implements myIntefrace then your class must implement aMethod:
public class MyClass implements MyInterface {
public void aMethod() {
//Do something
}
}
For more info refer to here: Java Interfaces
So if you wanted PlayerPosition to implement Player you would have to implement yourself a large number of methods contained in Player interface. Thus not recommended at all.
It is because of this that you cannot cast from CraftPlayer to PlayerPosition.
A workaround would be to save the player location in a HashMap on /setposition and to get it from it on back.
A solution to your problem could be the following one:
SetPosition.java:
public class SetPosition implements CommandExecutor {
#Override
public boolean onCommand(CommandSender theSender, Command command, String s, String[] strings) {
if (theSender instanceof Player) {
Player p = (Player) theSender;
PluginCore.getInstance().setLocation(p, p.getLocation());
p.sendMessage(ChatColor.BLUE + "Position Setting Complete.");
}
return false;
}
}
Back.java:
public class Back implements CommandExecutor {
#Override
public boolean onCommand(CommandSender theSender, Command command, String s, String[] strings) {
if (theSender instanceof Player) {
Player p = (Player) theSender;
Location location = PluginCore.getInstance().getLocation(p);
if (location == null) {
p.sendMessage(ChatColor.RED + "Set Your Position To Back First By Using SetPosition.");
} else {
p.teleport(location);
}
}
return false;
}
}
PluginCore.java: (Main class)
public class PluginCore extends JavaPlugin {
private static PluginCore instance;
private HashMap<Player, Location> locations = new HashMap<Player, Location>();
#Override
public void onEnable() {
instance = this;
this.getCommand("back").setExecutor(new Back());
this.getCommand("setposition").setExecutor(new SetPosition());
}
public static PluginCore getInstance() {
return instance;
}
public Location getLocations(Player p) {
return locations.get(p);
}
public void setLocation(Player p, Location location) {
this.locations.put(p, location);
}
}
I hope it helped!
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.
I am following a YouTube tutorial and I came across this error.
At this line, #SuppressWarnings("deprecation") comes up.
Player targerPlayer = Bukkit.getServer().getPlayer(args[0]);
Here is my simple healing plugin.
package me.roofer.youtube;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
public class youtube extends JavaPlugin {
public static Logger logger = Logger.getLogger("Minecraft");
public static youtube plugin;
#Override
public void onDisable() {
PluginDescriptionFile pdfFile = this.getDescription();
youtube.logger.info(pdfFile.getName() + " has been disabled!");
}
#Override
public void onEnable() {
PluginDescriptionFile pdfFile = this.getDescription();
youtube.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " has been Enabled!");
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
#SuppressWarnings("unused")
Player player = (Player) sender;
if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")) {
// heal >> 0 args | heal roofer777 >> 1 arg
if (args.length == 0){
player.setHealth(20);
player.sendMessage(ChatColor.RED + "Healed!");
}else if(args.length == 1){
#SuppressWarnings({"unused", "deprecation"})
Player targerPlayer = Bukkit.getServer().getPlayer(args[0]);
targetPlayer.setHealth(20);
}
}
return false;
}
}
That is not an error.
You should read up on the definition of deprecation.
A program element annotated #Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
The reason why that specific method is deprecated is because Bukkit is now moving over to the new UUID system, so using names is not the best way to get a Player object.