Add ItemDrop, ItemRegistry and the Loot and BlockDrops services

This commit is contained in:
piexel committed 2026-10-09 22:02:47 +02:00
1 parent ba96e0cbc2
commit ad581be494
8 files changed
+221

No files matched your search

@@ -0,0 +1,18 @@
package com.volmit.item;
import java.util.Objects;
/** An attribute modifier of an {@link ItemDrop} or an entity, with the amount the module decided. The host creates a
* modifier under an id it makes unique from {@code name}, for every equipment slot.
*
* @param attribute the platform attribute's key, such as {@code max_health} or {@code minecraft:armor}
* @param name identifies the modifier among those of one item or entity; not shown to players
* @param operation {@code ADD_NUMBER}, {@code ADD_SCALAR} or {@code MULTIPLY_SCALAR_1}
*/
public record ItemAttribute(String attribute, String name, String operation, double amount) {
public ItemAttribute {
Objects.requireNonNull(attribute, "attribute");
Objects.requireNonNull(name, "name");
Objects.requireNonNull(operation, "operation");
}
}
@@ -0,0 +1,57 @@
package com.volmit.item;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* One stack of items a module decided to give: what a loot entry, a block's drops or an entity's equipment turned into
* after the module made every random choice. The host creates the stack on the platform. Names and lore are as authored
* in the pack, with {@code &} colour codes; the host translates and wraps them.
*
* @param item the item's key as the pack wrote it, see {@link ItemRegistry}
* @param amount how many, at least 1
* @param displayName the item's name, or null to keep the platform's
* @param lore lines of text under the name
* @param enchantments applied in order
* @param attributes attribute modifiers applied in order
* @param unbreakable whether the item never breaks
* @param flags platform item flags to set, by name; names the platform lacks are ignored
* @param customModel custom model number, or null
* @param damage the fraction of the item's durability already used, 0 to 1, or null to leave it undamaged
* @param leatherColor colour of leather armour as {@code #rrggbb}, or null
* @param dyeColor dye colour name, or null
* @param customNbt custom data the platform stores on the item, or empty
* @param source where the item came from, set only when the host asked for it to be shown (debug loot)
*/
public record ItemDrop(String item, int amount, String displayName, List<String> lore, List<ItemEnchantment> enchantments,
List<ItemAttribute> attributes, boolean unbreakable, List<String> flags, Integer customModel,
Double damage, String leatherColor, String dyeColor, Map<String, Object> customNbt, Source source) {
public ItemDrop {
Objects.requireNonNull(item, "item");
if (amount < 1) throw new IllegalArgumentException("Item amount must be at least 1: " + amount);
lore = List.copyOf(lore);
enchantments = List.copyOf(enchantments);
attributes = List.copyOf(attributes);
flags = List.copyOf(flags);
customNbt = customNbt == null ? Map.of() : Collections.unmodifiableMap(new LinkedHashMap<>(customNbt));
}
/** {@code amount} items of kind {@code item} and nothing else. */
public static ItemDrop of(String item, int amount) {
return new ItemDrop(item, amount, null, List.of(), List.of(), List.of(), false, List.of(), null, null, null, null,
Map.of(), null);
}
/**
* Where a shown item came from: the loot table, when it came from one, and the 1-in-N chances it passed.
*
* @param table the table's name, or null for an item that came from no table
* @param tableRarity the table's rarity, 1 in this many
* @param entryRarity the entry's rarity within the table
*/
public record Source(String table, int tableRarity, int entryRarity) {
}
}
@@ -0,0 +1,13 @@
package com.volmit.item;
import java.util.Objects;
/** An enchantment of an {@link ItemDrop}, named by the key the platform's registry knows ({@code sharpness},
* {@code mymod:vorpal}) with the level the module decided.
*/
public record ItemEnchantment(String key, int level) {
public ItemEnchantment {
Objects.requireNonNull(key, "key");
if (level < 1) throw new IllegalArgumentException("Enchantment level must be at least 1: " + level);
}
}
@@ -0,0 +1,15 @@
package com.volmit.item;
import java.util.OptionalInt;
/** What a module may ask the platform about items, so that its decisions follow the item they concern. Item keys are
* written as in packs: {@code diamond_sword}, {@code minecraft:diamond_sword}, or a custom item of a provider such as
* {@code itemsadder:ruby}. Safe for concurrent use.
*/
public interface ItemRegistry {
/**
* The most damage an item of this kind takes before it breaks, 0 for an item that cannot be damaged; empty when no
* item is known by {@code key}.
*/
OptionalInt maxDurability(String key);
}