Getting type of item stack returns generic type (Oak Log) instead of specific type (Birch Log)

Discussion in 'Plugin Development' started by Archyx, Mar 27, 2020.

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

    Archyx

    I am making a program that creates individual item stacks if the item stack amount is above 64 because item stacks above 64 appear invisible in game. When I get the type of the item stack from an array of item stacks, it returns the generic item type such as oak log when I want it to return birch log. The program works when the item stack amount is below 64 when I directly use the item stack element in the array to drop the item in the world.

    How do I make it return the specific block type?

    Code:
    public class ItemsMulitply implements Listener{
       
        public static double dropAmount = 1;
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            if (player.getGameMode() == GameMode.SURVIVAL) {
                Block block = event.getBlock();
                Location loc = block.getLocation();
                event.setDropItems(false);
                Collection<ItemStack> dropsCollection = block.getDrops();
                ItemStack[] dropsArray = dropsCollection.toArray(new ItemStack[0]);
                for (int i = 0; i < dropsArray.length; i++) {
                    if ((int)(dropAmount) <= 64) {
                        dropsArray[i].setAmount((int)dropAmount);
                        Main.world.dropItemNaturally(loc, dropsArray[i]);
                    }
                    else {
                        for (int j = 0; j < (int)(dropAmount)/64; j++) {
                            ItemStack items = new ItemStack(dropsArray[i].getType(), 64);
                            Main.world.dropItemNaturally(loc, items);
                        }
                        if (dropAmount%64 != 0) {
                            ItemStack excessItems = new ItemStack(dropsArray[i].getType(), (int)(dropAmount)%64);
                            Main.world.dropItemNaturally(loc, excessItems);
                        }
                    }
                }
                if (dropsArray.length != 0) {
                    dropAmount*=2;
                    player.sendMessage("Drops have been multiplied!");
                }
            }
        }
    }
    
     
  2. Offline

    CraftCreeper6

    @Archyx
    You need to check the data value.

    See ItemStack#getData()

    EDIT: This may have been replaced by getDurability() but I cannot be 100% certain without trying it.
     
  3. Offline

    Archyx

    @CraftCreeper6 I added items.setData(dropsArray.getData()); but it still dropped oak logs. I could not figure out how to convert Material Data to Material or itemstack without a depricated method.
     
  4. Offline

    CraftCreeper6

    @Archyx
    It's fine to use a deprecated method, it's still there for a reason. I'm not sure of another way to do it (though I'm sure there's another one). So if anyone on here knows the updated method, feel free to jump in.
     
  5. Offline

    Archyx

    Turn out I didn't need to get the type of the item stack array, I just set the new item stack to the array element and set the amount to 64
    Code:
    ItemStack items = dropsArray[i];
    items.setAmount(64);
     
Thread Status:
Not open for further replies.

Share This Page