简体   繁体   中英

Minecraft Plugin Gamemode changer GUI

I am trying to make a minecraft plugin which is a gamemode changer GUI which when I click a block it changes my gamemode. My plugin successfully shows up in console and lets me view the GUI in game but doesn't do anything of what I coded it to do:

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();
            }
        }
    }
}

What happens in game is I click one of the blocks and I can drag it around and it doesn't do anything. It also doesn't put the blocks back in until I restart the server.

I'm new to java and coding.

You haven't registered your main class as an event listener. Try adding this to your onEnable:

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

You need to register the command but also the event you are adding stuff to. You can do this by doing the following:

public void onEnable() {
   getCommand("gmgui").setExecutor(this);
   getServer().getPluginManager.registerListener(this, this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM