问题
Hey so I did look this issue up on multiple sites before I posted this and I did everything correct, but I still get this error when I try to load my plugin, it says error could not invalid plugin.yml, and then just gives a bunch of code lines and stuff (i'm assuming from the code in the bukkit files and whatnot, and yes my plugin.yml is saved in the src folder not a package, and when I export it I do export it as a .jar, anyway here's my plugin.yml file
name: ProtHome
main: com.yahoo.m1kesanders.ProtHome.ProtHome
version: 1.0.0
Description: A simple /home plugin
commands:
sethome:
Description: sets players home
home:
Description: teleports player to their home
and I also did use the 4 spaces and no tab keys were used two spaces after commands: and another two after each command
and here's my code for the plugin in eclipse in case you need it just to check name and what not
package com.yahoo.m1kesanders.ProtHome;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class ProtHome extends JavaPlugin{
public static ProtHome plugin;
public File folder = plugin.getDataFolder();
public static File file = new File("Homes.yml");
public static YamlConfiguration Homes = new YamlConfiguration();
public void onEnable(){
if(!folder.exists()){
folder.mkdir();
}
if(!file.exists()){
file.mkdir();
}
try {
Homes.load(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean onCommand(CommandSender cmdsender, Command cmd, Player player) throws FileNotFoundException, IOException, InvalidConfigurationException{
if(cmdsender.equals("sethome")){
ProtHome.Homes.load(ProtHome.file);
Homes.set(player.getName() + ".x", player.getLocation().getBlockX());
Homes.set(player.getName() + ".y", player.getLocation().getBlockY());
Homes.set(player.getName() + ".z", player.getLocation().getBlockZ());
Homes.set(player.getName() + ".world", player.getWorld().getName());
ProtHome.Homes.save(ProtHome.file);
}
else if(cmdsender.equals("home")){
int x = (int) Homes.get(player.getName() + ".x");
int y = (int) Homes.get(player.getName() + ".y");
int z = (int) Homes.get(player.getName() + ".z");
String world = (String) Homes.get(player.getName() + ".world");
World realworld = Bukkit.getServer().getWorld(world);
Location loc = new Location(realworld,x,y,z);
player.teleport(loc);
}
return false;
}
}
if you guys can help me out it will mean a lot thanks for reading
回答1:
Part of the problem could be that you capitalized the D
in Description:
when in the bukkit example plugin.yml, all keys are lowercase.
Try exporting with a lowercase d
although this may not be the problem, it always helps to use proper coding grammar. Bukkit is very knit picky with it's yaml parser.
Also, for future reference, usually packagenames do not have capital letters, only classes.
Your package com.yahoo.m1kesanders.ProtHome
Most packages com.yahoo.m1kesanders.prothome
I cannot be certain, but it is usually especially recommended for bukkit plugins to follow this general rule. I do not know how bukkit's class loader works, but this capitalized package name surely doesn't help.
回答2:
As well as what TheJavaCoder16 said it also appears you are not using the correct onCommand
method. This is what you have (excluding the throws ...
and main content):
public boolean onCommand(CommandSender cmdsender, Command cmd, Player player)
The correct method is:
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
To get the player check if sender instanceof Player
then cast sender to Player
:
if (sender instanceof Player) {
Player player = (Player) sender;
//Rest of your code goes here
}
What you should also be wary of is returning false
will send an error to the sender warning them of incorrect/invalid usage of the command. You should try returning true unless you want it to send this message.
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) throws FileNotFoundException, IOException, InvalidConfigurationException{
if (sender instanceof Player) {
Player player = (Player) sender;
if(cmdsender.equals("sethome")){
ProtHome.Homes.load(ProtHome.file);
Homes.set(player.getName() + ".x", player.getLocation().getBlockX());
Homes.set(player.getName() + ".y", player.getLocation().getBlockY());
Homes.set(player.getName() + ".z", player.getLocation().getBlockZ());
Homes.set(player.getName() + ".world", player.getWorld().getName());
ProtHome.Homes.save(ProtHome.file);
} else if(cmdsender.equals("home")){
int x = (int) Homes.get(player.getName() + ".x");
int y = (int) Homes.get(player.getName() + ".y");
int z = (int) Homes.get(player.getName() + ".z");
String world = (String) Homes.get(player.getName() + ".world");
World realworld = Bukkit.getServer().getWorld(world);
Location loc = new Location(realworld,x,y,z);
player.teleport(loc);
}
}
return true;
}
回答3:
Often times, you can literally do everything right, and still it will throw an error or exception. This may seem like a silly answer, but have you refreshed the plugin.yml
file BEFORE exporting it? This can often happen and is more common than you think.
来源:https://stackoverflow.com/questions/24423136/invalid-plugin-yml-bukkit-plugin-error