Solved Skulls not stacking because they were generated with diferent UUIDs

Discussion in 'Spigot Plugin Development' started by Zendovo, Sep 29, 2018.

Thread Status:
Not open for further replies.
  1. For some reason, the skulls are not stacking with each other it doesn't matter if I try to stack them manually.
    Any help will be appreciated! :)

    Here is my code below:
    Code (Java):
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
         
            String type = args[0];
            int amount = Integer.parseInt(args[1]);
            Player targetPlayer = this.getServer().getPlayer(args[2]);
         
                while (amount > 0) {
                        targetPlayer.getInventory().addItem(getSkull(getConfig().getString("apples." + type + ".url")));
                        amount--;
                }
         
            return false;
        }
     
            public ItemStack getSkull(String url) {
                    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                    if (url.isEmpty()) return skull;
                 
                    SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
                    GameProfile profile = new GameProfile(UUID.randomUUID(), null);
                    profile.getProperties().put("textures", new Property("textures", url));
                    Field profileField = null;
                 
                    try {
                        profileField = skullMeta.getClass().getDeclaredField("profile");
                    } catch (NoSuchFieldException | SecurityException e) {
                        e.printStackTrace();
                    }
                    profileField.setAccessible(true);
                    try {
                        profileField.set(skullMeta, profile);
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                 
                    skull.setItemMeta(skullMeta);
                    return skull;
                }
     
    #1 Zendovo, Sep 29, 2018
    Last edited: Sep 29, 2018
  2. Also, I can stack the heads with /stack from WorldGuard. Anyone knows the solution?
     
  3. You are generating random UUIDs for each head, so the meta is obviously not the same. That while loop doesn't make much sense to me, you should just pass an amount parameter to the getSkull method and use this in the ItemStack constructor.
     
    • Like Like x 1
  4. Now that this sums up my stupidity...

    I got it working. Thanks a lot! The while loop really wasn't needed, haha.
     
  5. ¯\_(ツ)_/¯ It's normal to miss the forest for the trees now and again while programming ^^

    One more thing I noticed in your code is that you're not at all checking your command arguments, like if enough arguments are entered, if args[0] is actually numeric, and if the player really exists. Also, that the command always returns false doesn't make sense. That will make Bukkit print the command usage information from the plugin.yml as if the command failed.
     
  6. Yeah, I am doing that right now. As I really just had to test everything I didn't even add a permission.
     
Thread Status:
Not open for further replies.