Solved How to drop an item without stacking/moving

Discussion in 'Plugin Development' started by x8b, Jun 20, 2019.

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


    I've been trying to make a minigame similar to Hypixel Bedwars. I'm looking for a method where resources (iron, gold, ..) spawn in at their places:
    • it shouldn't become one entity when multiple are together
    • they shouldn't move when they spawn
    I'm aware of .dropItem() and .dropItemNaturally() but they do neither of these things.


    EDIT: I've fixed this, special thanks to Kars for the workaround
    If anyone sees this thread in the future, here is the code you can use (obviously you'll have to change a few variables)

    Dropping an item:
    ItemStack is = getNewItemStack();
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(Math.random() + "-" + im.getDisplayName()); // use a randomly generated number, it is very unlikely that you will get the same number twice.
    // getDisplayName() will return null if it doesn't have a custom name!
    // In my game, I only want certain items made by the server to not stack, and I made them a custom name each.
    Item it = world.dropItem(loc, is);
    it.setVelocity(new Vector()); // vectors default to 0 therefore the item won't move

    Picking up an item:
    public void onItemPickup(PlayerPickupItemEvent e) {
            ItemMeta im = e.getItem().getItemStack().getItemMeta();
            String[] names = im.getDisplayName().split("-"); // split the item's custom name by -, therefore splitting the random number and the name.
            // getDisplayName() returns null if there is no custom name, I currently don't know a workaround
            if (names.length <= 1) return;
            else {
                // accommodate for extra hyphens in custom names
                String newName = "";
                for (int i = 1; i < names.length; ++i) {
                    newName += names[i];
                    if (i+1 < names.length) newName += "-";
                // finally, set the name back.
    Last edited: Jun 24, 2019
  2. Online


    No experience with this, but:
    You could give all the dropped items a randomly generated name. That way they won't stack. Then you remove the name on pickup.

    A horrible workaround i know..
  3. Offline


    @x8b Why would you want to do this? I'm pretty sure the reason it does this, is to reduce the amount of entities.
  4. Offline


    I'm very New to coding, but couldn't you Just use maxStackSize(1) in the Item Meta of the gold, Iron etc..?
  5. Offline


    @AGreenPig ItemMeta does not have a 'setMaxStackSize(int)' method.
  6. Offline


    Late reply:
    Having the items stacking means they won't have a "circular" look like I'm going for.
    I think they also make ghost entities on the client and eventually the server will crash from the stacks.

    EDIT: Fixed ghost entities and server crashes by using BukkitRunnables instead of java timers
    Last edited: Jun 22, 2019
Thread Status:
Not open for further replies.

Share This Page