Skip to content

ItemDrop

S'pugn edited this page Nov 11, 2021 · 1 revision

Notes

ItemDrop is a feature of PartyDungeons to select from a random collection of items and spit it out in the world for players to collect. This requires the use of ScheduleHandler (more info here) as it requires the main thread to work.

An ItemDrop table map is formatted as Map<ItemStack, Integer>, the ItemStack being the item and amount to drop and Integer being the "weight" of the item.

The higher an item's weight, the more likely it will be to drop (item_weight / total_weight). For example:

Item Item Weight Chance ( item_weight / total_weight )
Dirt 10 10/100
Stone 50 50/100
Grass 20 20/100
Sand 20 20/100

Script Examples

Creating your ItemDrop table

function table() {
    const ItemStack = Java.type("org.bukkit.inventory.ItemStack");
    const Material = Java.type("org.bukkit.Material");
    const HashMap = Java.type("java.util.HashMap");
    const table = new HashMap();
    // DIRT x5 : 10 WEIGHT
    table.put(new ItemStack(Material.DIRT, 5), 10);
    // STONE x10 : 50 WEIGHT
    table.put(new ItemStack(Material.STONE, 10), 50);
    // GRASS x1 : 20 WEIGHT
    table.put(new ItemStack(Material.GRASS, 1), 20);
    // SAND x8 : 20 WEIGHT
    table.put(new ItemStack(Material.SAND, 8), 20);
    return table;
}

Creating and running an ItemDrop object

/**
 * @param player {Player} A player object.
 * @param amount {number} Amount of items to choose from the table and dispense to the world.
 */
function run_item_drop(player, amount) {
    const ScheduleHandler = load(`ScheduleHandler.js`);

    // Drop items at a player's location
    const ItemDrop = sm.createItemDrop(player.getLocation(), table());
    ScheduleHandler(() => ItemDrop.run(amount));

    // Drop items at {X:100, Y:70, Z:200} (in the player's world)
    const Location = Java.type("org.bukkit.Location");
    const ItemDrop2 = sm.createItemDrop(new Location(player.getWorld() 100, 70, 200), table());
    ScheduleHandler(() => ItemDrop2.run(amount));
}

function table() {
    // Code from the previous example...
    return table;
}

Debugging your ItemDrop table

If for some reason you want to preview the items and drop rates of your ItemDrop table, there are two methods to do so.

  • ItemDrop#debug()
  • ItemDrop#debugGUI(Player player, Integer page)

ItemDrop#debug()

This will return an ArrayList of strings containing information about the ItemStack type, amount, and chance percentage.
The string will be formatted like: <material_type> (x<amount>) - <percentage_x.xx>%

ItemDrop#debugGUI(Player player, Integer page)

Running this function in-game will open an inventory GUI for the player. There is a 54 inventory slot restriction through this method, so changing the page is required if your ItemDrop table has more than 54 items. Through this method you can take out items from the inventory, but do note that these items will have added lore to them informing the player about their <item_weight> / <total_weight> and their drop percentage.