gpt4 book ai didi

java - Minecraft 插件游戏模式转换器 GUI

转载 作者:行者123 更新时间:2023-11-30 06:45:50 25 4
gpt4 key购买 nike

我正在尝试制作一个 Minecraft 插件,它是一个游戏模式更改器 GUI,当我单击一个 block 时,它会更改我的游戏模式。我的插件成功地出现在控制台中,让我可以在游戏中查看 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();
}
}
}
}

在游戏中发生的事情是我点击其中一个方 block ,然后我可以拖动它,但它什么也没做。在我重新启动服务器之前,它也不会放回 block 。

我是 java 和编码的新手。

最佳答案

您还没有将主类注册为事件监听器。尝试将其添加到您的 onEnable 中:

 getServer().getPluginManager().registerListener(this, this);

关于java - Minecraft 插件游戏模式转换器 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48311289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com