minecraft - Minecraft 插件加载但命令未知
问题描述
我一直在为 Eclipse 上的 Minecraft 创建一个插件,但我遇到了一个错误。插件加载正常;我可以在控制台中看到它,但是当我尝试运行命令时它会显示未知命令。
这是我的命令代码:
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;
}
}
这是我的 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));
}
}
这是我的 plugin.yml 文件:
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
请帮忙!谢谢。
解决方案
private Main plugin;
public HelloCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("sword").setExecutor(this);
public void onEnable() {
getCommand("sword").setExecutor(new HelloCommand(this));
}
你为什么要注册你的命令两次?没必要。仅在您的 onenable 中注册该命令。此外,对于你给玩家剑的方法,你不应该使用 p.performCommand();,因为即使他们有权限,他们仍然需要 op 才能使用 /give。
尝试使用这个:
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++;
}
}
}
推荐阅读
- c# - c# 中使用的“#if”指令是什么,“符号”代表什么?
- java - 在抽象类的方法中,我如何知道我正在使用哪个实例?
- ruby-on-rails - 使用 Rack::Deflator 添加 GZIP 会导致 Heroku 上出现“客户端请求中断”错误
- pandas - Pandas - 根据变量值过滤数据框
- java - 使用 Spring 云网关和 Nginx 作为反向代理的网关超时
- macos - 如何处理“无法连接到 Kubernetes master ip”
- matlab - matlab不能赋值数组
- c# - Application Lifecycle for Xamarin.iOS
- javascript - 将方法本身作为方法调用
- c# - 创建字节数组而不用 0 初始化