Search code examples
javaminecraftbukkit

How to ask for user input and return it in a new inventory


I am 13, so please do not get impatient with me here as I might be making a dumb mistake or not thinking something in the right perspective.

I want to make a Spigot plugin to increase my Java knowledge a little bit, by making a project that is fun and educational. I'm following a tutorial on how to make a GUI, but wanted to create it with a twist that is not too complex.

I was poking around and found something that is similar to show an example of this LobbyCompass.

Here is my "GuiConstruct" class


import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.inventory.Inventory;

public class GuiConstruct {

    private String input(String input) {
        return input;
    }


    public Inventory newGui(String Name, int size) {
        return Bukkit.getServer().createInventory(null,9, ChatColor.RED + input());
    }

}

Here is my main class

package me.yarkosharko.worldswitcher;

import org.bukkit.plugin.java.JavaPlugin;

public final class WorldSwitcher extends JavaPlugin {

    @Override
    public void onEnable() {
        // Plugin startup logic

    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}

Am I thinking of this the right way? Do I have to do something entirely different? Just to be clear I want to make something like this:

/worldswitcher 'args'

then it shows an inventory GUI with the name 'args'


Solution

  • I maybe didn't understand your question, as it wasn't straight-forward. As I get it, you want to create command that opens up a GUI with specified name. There are multiple ways to do that... The simplest way is to implement method onCommand in plugin main class.

    public final class WorldSwitcher extends JavaPlugin {
    
        @Override
        public void onEnable() {
            // Plugin startup logic
    
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label,String[] args) {
            // executor must be player
            if(!(sender instanceof Player)) {
                sender.sendMessage("§cYou must be player to execute this command!");
                return true;
            }
            
            // If command name is "worldswitcher", handle logic
            if(command.getName().equalsIgnoreCase("worldswitcher")) {
                // If an argument is present, we can work with it
                if(args.length > 0){
                    // cast player from command sender
                    Player executor = (Player) commandSender;
                    // get first argument
                    String worldName = args[0];
                    // openup a gui for him
                    MyGUIClass.openGui(executor, worldName/*, other parameters*/)
                
                } else {
                    // we require single argument, but it is missing
                    sender.sendMessage("§cMissing argument");
                }
            }
            return true;
        }
    }
    
    

    And you must add this to your plugin.yml

    commands:
        worldswitcher: {}
    

    This code maybe explains how it works, but if not there are a lot of great resources for learning how commands work: Tutorial from TheSourceCode: CommandExecutors, Tutorial from Pogostick(Noah Rubin): Basic commands