我正在尝试制作一个minecraft插件,它是一个游戏模式改变器GUI当我单击一个块时,它会改变我的游戏模式。我的插件成功地显示在控制台中,让我查看游戏中的GUI,但不做任何我编码它要做的事情:
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
getLogger().info("[GamemodeGUI] Plugin has been enabled!");
}
@Override
public void onDisable() {
getLogger().info("[GamemodeGUI] Plugin has been disabled!");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("gmgui")) {
player.openInventory(myInventory);
}
return true;
}
public static Inventory myInventory = Bukkit.createInventory(null, 9, "GamemodeGUI");
static {
myInventory.setItem(0, new ItemStack(Material.IRON_BLOCK, 1)); // Survival
myInventory.setItem(1, new ItemStack(Material.DIAMOND_BLOCK, 1)); // Creative
myInventory.setItem(2, new ItemStack(Material.GOLD_BLOCK, 1)); // Adventure
myInventory.setItem(3, new ItemStack(Material.LAPIS_BLOCK, 1)); // Spectator
myInventory.setItem(8, new ItemStack(Material.STAINED_GLASS, 1)); // Cancel
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
ItemStack clicked = event.getCurrentItem();
Inventory inventory = event.getInventory();
if (inventory.getName().equals(myInventory.getName())) {
if (clicked.getType() == Material.IRON_BLOCK) {
event.setCancelled(true);
player.closeInventory();
if (player.hasPermission("gamemodegui.survival")) {
player.setGameMode(GameMode.SURVIVAL);
player.sendMessage(ChatColor.RED + "Gamemode set to survival");
} else {
player.sendMessage(ChatColor.RED + "Error: You do not have the permission to use that command ");
}
}
else if (clicked.getType() == Material.DIAMOND_BLOCK) {
event.setCancelled(true);
player.closeInventory();
if (player.hasPermission("gamemodegui.creative")) {
player.setGameMode(GameMode.CREATIVE);
player.sendMessage(ChatColor.RED + "Gamemode set to creative");
} else {
player.sendMessage(ChatColor.RED + "Error: You do not have the permission to use that command ");
}
}
if (clicked.getType() == Material.LAPIS_BLOCK) {
event.setCancelled(true);
player.closeInventory();
if (player.hasPermission("gamemodegui.adventure")) {
player.setGameMode(GameMode.ADVENTURE);
player.sendMessage(ChatColor.RED + "Gamemode set to adventure");
} else {
player.sendMessage(ChatColor.RED + "Error: You do not have the permission to use that command ");
}
}
if (clicked.getType() == Material.IRON_BLOCK) {
event.setCancelled(true);
player.closeInventory();
if (player.hasPermission("gamemodegui.spectator")) {
player.setGameMode(GameMode.SPECTATOR);
player.sendMessage(ChatColor.RED + "Gamemode set to spectator");
} else {
player.sendMessage(ChatColor.RED + "Error: You do not have the permission to use that command ");
}
} else {
event.setCancelled(true);
player.closeInventory();
}
}
}
}
在游戏中,我点击其中一个方块,我可以拖动它,它什么也不做。它也不会把方块放回去,直到我重新启动服务器。
我是java和编码的新手。
您尚未将主类注册为事件侦听器。尝试将其添加到您的onEnable:
getServer().getPluginManager().registerListener(this, this);
您需要注册命令以及要添加内容的事件。您可以通过执行以下操作来做到这一点:
public void onEnable() {
getCommand("gmgui").setExecutor(this);
getServer().getPluginManager.registerListener(this, this);
}