OpenInventory command

Discussion in 'Plugin Development' started by ShadowDisruptor, Sep 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ShadowDisruptor

    Hello! My goal is to use this command to get the idea of loading an inventory. When running it in-game and called the command (Code below) I got 'Internal server error when attempting to preform this command' There were no errors in Eclipse, so can someone tell me what went wrong?
    Code:
    //COMMAND FOR AUCTIONING HAND ITEM
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("auctionhand")) {
    if (args.length == 1) {
    Player player = (Player) sender;
    String material = player.getItemInHand().getType().toString();
    //THIS SECTION NOT NORMAL FOR HERE
    String test = (ChatColor.LIGHT_PURPLE + "Auction House: You put" + ChatColor.BLUE + " " + material.toLowerCase() + ChatColor.LIGHT_PURPLE + " for sale!");
    Inventory inv = Bukkit.createInventory(null, 27, ChatColor.AQUA + test);
     
    ItemStack meta1 =  new ItemStack(player.getItemInHand());
    ItemMeta metaMeta = meta1.getItemMeta();
     
    metaMeta.setDisplayName(ChatColor.GREEN + "Price: " + args[0]);
    meta1.setItemMeta(metaMeta);
    inv.setItem(1,meta1);
    player.openInventory(inv);
    //END OF NOT NORMAL FOR HERE
    }else
    {
    sender.sendMessage("Improper use! /auctionhand ammount");
    }
    }
    return true;
    }
    
    (I cannot figure out how to copy the console text, sorry)
     
  2. Offline

    97WaterPolo

    ShadowDisruptor
    Any chance you could take a screenshot of the error? Or at least tell us what line it is on? Does it say NullPointerException?

    Try

    if (player.getItemInHand() == null){
    player.sendMessage("You must be holding an item");
    return false;
    }
     
  3. Offline

    CaptainUniverse

    here ShadowDisruptor to copy the log from the console do this:
    First, right click the outer part of the console (like the border)
    click on Edit> then do Select-all
    once its all selected just click ENTER and it will automatically copy it.
    then just paste it where yu would put code and VOILA .
    Signature
    Code:java
    1. public void isTheCaptainRight(boolean YourGuess) {
    2. return true;
    3. }
     
  4. Offline

    Gerov

    ShadowDisruptor I am just giving a tip, before you cast Player to sender, check if sender is an instance of the Player object, because if someone tries this command from a server console, it can cause big errors.
     
  5. Offline

    ShadowDisruptor

    Show Spoiler

    15:36:20 [INFO] footballfan12 issued server command: /auctionhand 5
    15:36:20 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'auct
    ionhand' in plugin Auction_House v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    r.java:534)
    at net.minecraft.network.NetServerHandler.func_72566_d(NetServerHandler.
    java:1406)
    at net.minecraft.network.NetServerHandler.chat(NetServerHandler.java:127
    0)
    at net.minecraft.network.NetServerHandler.func_72481_a(NetServerHandler.
    java:1202)
    at net.minecraft.network.packet.Packet3Chat.func_73279_a(Packet3Chat.jav
    a:68)
    at net.minecraft.network.TcpConnection.func_74428_b(TcpConnection.java:4
    80)
    at net.minecraft.network.NetServerHandler.func_72570_d(NetServerHandler.
    java:234)
    at net.minecraft.network.NetworkListenThread.func_71747_b(NetworkListenT
    hread.java:54)
    at net.minecraft.server.dedicated.DedicatedServerListenThread.func_71747
    _b(DedicatedServerListenThread.java:37)
    at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.jav
    a:917)
    at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(Dedicated
    Server.java:331)
    at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.jav
    a:784)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:666)
    at net.minecraft.server.ThreadMinecraftServer.run(SourceFile:583)
    Caused by: java.lang.IllegalArgumentException: Title cannot be longer than 32 ch
    aracters
    at org.apache.commons.lang.Validate.isTrue(Validate.java:136)
    at org.bukkit.craftbukkit.v1_6_R3.inventory.CraftInventoryCustom$Minecra
    ftInventory.<init>(CraftInventoryCustom.java:46)
    at org.bukkit.craftbukkit.v1_6_R3.inventory.CraftInventoryCustom.<init>(
    CraftInventoryCustom.java:24)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.createInventory(CraftServe
    r.java:1280)
    at org.bukkit.Bukkit.createInventory(Bukkit.java:578)
    at me.ShadowDisruptor.Auction_House.Main.onCommand(Main.java:107)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more

    Above: When I call command
    Below: Updated code
    Code:
    package me.ShadowDisruptor.Auction_House;
     
    import java.util.Map;
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
    //Simple variables setup
    //SELLITEMS MAP INFO
    Map saleItems = new HashMap();
    Integer ammount = 0;
     
    //Main code
    static void main() {
    }
     
    //BUKKIT PLUGIN STUFFS
    //ON PLUGIN ENABLE
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    }
     
    //ON PLUGIN DISABLE
    public void onDisable() {
     
    }
     
    //FUNCTIONS
    //FUNCTION TO OPEN THE GUI
    public void openGUI(Player player, Integer ammount) {
    //ADD SOMETHING TO ROUND TO CLOSEST 9, RETURNS TO slots
    Integer slots = 9;
    Inventory inv = Bukkit.createInventory(null, slots, ChatColor.AQUA + "Auction House!");
    }
     
    //FUNCTION TO ADD TO THE GUI
    public void addItem() {
    //ADD A WAY TO ADD ITEMS VIA A TABLE - REFER TO VIDEO
     
    }
     
    //FUNCTION TO ADD TO ITEMS MAP
    public void sellItem(String[] args) {
    if (args.length == 4) {
    Map tempMap = new HashMap();
    tempMap.put("Material",args[0]);
    tempMap.put("ID",args[1]);
    tempMap.put("Amount",args[2]);
    tempMap.put("Seller",args[3]);
    ammount = ammount + 1;
    saleItems.put(("Item "+ammount), tempMap);
    }else {
    System.out.println("ERROR.Auction_House: Sell hand failed!");
    }
    }
     
    //EVENTS
    //EVENT FOR HANDLING SIGN CLICKS
    @EventHandler
    public void onInteract(PlayerInteractEvent e){
    Player p = e.getPlayer();
     
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
    if (e.getClickedBlock().getState() instanceof Sign) {
    Sign s = (Sign) e.getClickedBlock().getState();
    if(s.getLine(0).equalsIgnoreCase("[Auction Sign]")){
    //INSERT CODE TO OPEN THE GUI
    p.sendMessage(ChatColor.LIGHT_PURPLE + "You clicked an auction sign!");
    }
    }
    }else{
    return;
    }
     
    }
     
    //COMMANDS
    //COMMAND FOR AUCTIONING HAND ITEM
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("auctionhand")) {
    if(sender instanceof Player) {
    if (args.length == 1) {
    Player player = (Player) sender;
    if(player.getItemInHand() == null) {
    player.sendMessage(ChatColor.RED + "AuctionHouse: You can not auction air!");
    }else {
    String material = player.getItemInHand().getType().toString();
    //THIS SECTION NOT NORMAL FOR HERE
    String test = (ChatColor.AQUA + "Auction House: You put" + ChatColor.BLUE + " " + material.toLowerCase() + ChatColor.AQUA + " for sale!");
    Inventory inv = Bukkit.createInventory(null, 27, test);
     
    ItemStack meta1 = new ItemStack(player.getItemInHand());
    ItemMeta metaMeta = meta1.getItemMeta();
     
    metaMeta.setDisplayName(ChatColor.GREEN + "Price: " + args[0]);
    meta1.setItemMeta(metaMeta);
    inv.setItem(1,meta1);
    player.openInventory(inv);
    //END OF NOT NORMAL FOR HERE
    }
    }else
    {
    sender.sendMessage(ChatColor.RED + "AuctionHand: Improper use! /auctionhand ammount");
    }
    }else {
    System.out.println("You must be a player to use /auctionhand!");
    }
    }
    return true;
    }
    }
    
     
  6. Offline

    Funergy

    ShadowDisruptor
    The GUI title is too long try to do it a little bit shorter
    "Title cannot be longer than 32 characters"
    String test = (ChatColor.AQUA + "Auction House: You put" + ChatColor.BLUE + " " + material.toLowerCase() + ChatColor.AQUA + " for sale!");
     
    ShadowDisruptor likes this.
  7. Offline

    ShadowDisruptor

    Works, thanks for the stack decode ;)
     
  8. Offline

    Funergy

Thread Status:
Not open for further replies.

Share This Page