1.14.4 AnvilGUI

Discussion in 'Spigot Plugin Development' started by Ardno, Jan 19, 2020.

  1. Hello guys,
    I'm making a anvil GUI, but if I add cast (AnvilInventory), the console will give me a problem.
    How can I create AnvilInventory with items and method .setRepairCost(int)

    Code (Java):
    AnvilInventory anvil = (AnvilInventory) Bukkit.createInventory(null, InventoryType.ANVIL);
           
            Paper paper = new Paper();
            anvil.setRepairCost(0);
            anvil.setItem(0, paper.getItem());
     
  2. There should be no problem casting, show the stacktrace
     
  3. Code (Java):
    public class Anvil {
        public Inventory openAnvil() {
            AnvilInventory anvil = (AnvilInventory) Bukkit.createInventory(null, InventoryType.ANVIL);
         
            Paper paper = new Paper();
            anvil.setRepairCost(0);
            anvil.setItem(0, paper.getItem());
         
            return anvil;
        }
    }
    Code (Java):
    public class Paper {
        public ItemStack getItem() {
            ItemStack item = new ItemStack(Material.PAPER);
            ItemMeta itemMeta = item.getItemMeta();
         
            item.setAmount(1);
            itemMeta.setDisplayName("Kolik tokenů? (přepiš)");
         
            item.setItemMeta(itemMeta);
            return item;
        }
    }
     
    Code (Java):
    public class TokensBank implements CommandExecutor {
        public Main plugin;
        public TokensBank(Main main) {
            plugin = main;
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(label.equalsIgnoreCase("tokensbank")) {
                if(sender instanceof Player) {
                    Player p = (Player) sender;
                    if(p.hasPermission(plugin.prefixPermission + "command")) {
                        Anvil anvil = new Anvil();
                        p.openInventory(anvil.openAnvil());
                        return true;
                    }
                    p.sendMessage(ChatUtil.format(plugin.prefixChat + plugin.getConfig().getString("messages.error.no-perm")));
                    return true;
                }
                sender.sendMessage(plugin.prefixPlugin + plugin.getConfig().getString("messages.error.sender"));
                return true;
            }
            return false;
        }
     
    }
     
    #3 Ardno, Jan 19, 2020
    Last edited: Jan 19, 2020
  4. As robertlit asked, what's the stacktrace (error) that it gives?
     
    • Funny Funny x 1
  5. This isn't a stacktrace
     
  6. Code (Text):
    [18:51:00] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tokensbank' in plugin TokensBank v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_14_R1.inventory.CraftInventoryCustom cannot be cast to org.bukkit.inventory.AnvilInventory
            at cz.ardno.tokensbank.inventories.Anvil.openAnvil(Anvil.java:11) ~[?:?]
            at cz.ardno.tokensbank.commands.TokensBank.onCommand(TokensBank.java:24) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            ... 17 more
     
    • Friendly Friendly x 1
  7. I rewrite the code, but...
    Code (Text):
    [19:04:53] [Server thread/FATAL]: Error executing task on Server
    java.lang.UnsupportedOperationException: Not supported yet.
            at net.minecraft.server.v1_14_R1.ContainerAccess.getWorld(ContainerAccess.java:11) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.ContainerAccess.getLocation(ContainerAccess.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.ContainerAnvil.getBukkitView(ContainerAnvil.java:394) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.ContainerAnvil.e(ContainerAnvil.java:137) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.ContainerAnvil.a(ContainerAnvil.java:123) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.ContainerAnvil$1.update(ContainerAnvil.java:42) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.Slot.d(SourceFile:85) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.Container.a(Container.java:381) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:2141) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Code (Java):
    Inventory anvil = Bukkit.createInventory(null, InventoryType.ANVIL);
           
            Paper paper = new Paper();
            anvil.setItem(0, paper.getItem());
           
            return anvil;
    But I cannot use setRepairCost();
     
  8. If you had read the linked thread you would have understood that casting from Inventory to AnvilInventory is not possible with custom inventories
     
  9. Ahhh, I don't know.
    Can you give me please an example?
     
  10. Bruh
     
  11. Yeah, but which from inventories will I use? Or what is a specifical inventory?
     
    #13 Ardno, Jan 19, 2020
    Last edited: Jan 19, 2020