diff --git a/src/main/java/com/volmit/item/ItemAttribute.java b/src/main/java/com/volmit/item/ItemAttribute.java new file mode 100644 index 000000000..cf1c45404 --- /dev/null +++ b/src/main/java/com/volmit/item/ItemAttribute.java @@ -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"); + } +} diff --git a/src/main/java/com/volmit/item/ItemDrop.java b/src/main/java/com/volmit/item/ItemDrop.java new file mode 100644 index 000000000..fccc8a38a --- /dev/null +++ b/src/main/java/com/volmit/item/ItemDrop.java @@ -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 lore, List enchantments, + List attributes, boolean unbreakable, List flags, Integer customModel, + Double damage, String leatherColor, String dyeColor, Map 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) { + } +} diff --git a/src/main/java/com/volmit/item/ItemEnchantment.java b/src/main/java/com/volmit/item/ItemEnchantment.java new file mode 100644 index 000000000..e1de9d3d7 --- /dev/null +++ b/src/main/java/com/volmit/item/ItemEnchantment.java @@ -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); + } +} diff --git a/src/main/java/com/volmit/item/ItemRegistry.java b/src/main/java/com/volmit/item/ItemRegistry.java new file mode 100644 index 000000000..7a83e573e --- /dev/null +++ b/src/main/java/com/volmit/item/ItemRegistry.java @@ -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); +} diff --git a/src/main/java/com/volmit/resource/PackResources.java b/src/main/java/com/volmit/resource/PackResources.java index f7e164bdb..733ba0319 100644 --- a/src/main/java/com/volmit/resource/PackResources.java +++ b/src/main/java/com/volmit/resource/PackResources.java @@ -1,6 +1,7 @@ package com.volmit.resource; import com.volmit.block.BlockRegistry; +import com.volmit.item.ItemRegistry; import com.volmit.noise.NoiseFactory; import java.util.List; import java.util.NoSuchElementException; @@ -21,6 +22,9 @@ public interface PackResources { UUID snapshotId(); BlockRegistry blocks(); + /** Asks the platform about items, such as how much damage one takes. */ + ItemRegistry items(); + /** Builds the noise of the {@link com.volmit.noise.NoiseStyle}s the pack's settings describe. */ NoiseFactory noise(); diff --git a/src/main/java/com/volmit/service/BlockDrops.java b/src/main/java/com/volmit/service/BlockDrops.java new file mode 100644 index 000000000..b1156b0e8 --- /dev/null +++ b/src/main/java/com/volmit/service/BlockDrops.java @@ -0,0 +1,44 @@ +package com.volmit.service; + +import com.volmit.block.BlockState; +import com.volmit.item.ItemDrop; +import com.volmit.world.BlockPosition; +import com.volmit.world.PackWorld; +import java.util.List; +import java.util.Objects; + +/** + * Items a module drops when a block breaks, in addition to or instead of the platform's. The host asks every provider + * when a player breaks a block in a world generated with the pack, drops the items of all answers where the block was + * and keeps the platform's own drops unless an answer replaces them. + */ +@Service(Providers.ALL) +public interface BlockDrops { + /** What this module drops for the block {@code broken}; {@link Result#NONE} when it does nothing for that block. */ + Result drops(PackWorld world, Broken broken); + + /** + * The block that was broken. + * + * @param position world X and Z, Y the height above the dimension's bottom + * @param block the block as it was + */ + record Broken(BlockPosition position, BlockState block) { + public Broken { + Objects.requireNonNull(position, "position"); + Objects.requireNonNull(block, "block"); + } + } + + /** + * @param drops the items to drop + * @param replacesVanillaDrops whether the platform's own drops of the block are not dropped + */ + record Result(List drops, boolean replacesVanillaDrops) { + public static final Result NONE = new Result(List.of(), false); + + public Result { + drops = List.copyOf(drops); + } + } +} diff --git a/src/main/java/com/volmit/service/Loot.java b/src/main/java/com/volmit/service/Loot.java new file mode 100644 index 000000000..d6eee283d --- /dev/null +++ b/src/main/java/com/volmit/service/Loot.java @@ -0,0 +1,64 @@ +package com.volmit.service; + +import com.volmit.block.BlockState; +import com.volmit.item.ItemDrop; +import com.volmit.world.BlockPosition; +import com.volmit.world.PackWorld; +import java.util.List; +import java.util.Objects; + +/** + * The loot a module gives containers and mobs. The host asks in two steps so that it, and plugins listening to it, can + * see and change which tables are used: {@link #tables} names the tables a container draws from, {@link #items} turns + * one table into items. The host asks every provider and puts the answers together; a provider answers {@link #items} + * only for the tables it knows. A container is filled with items the host creates on the platform, in the order of the + * tables. + */ +@Service(Providers.ALL) +public interface Loot { + /** + * The tables the container at {@code site} draws from, in the order their items are added. Empty when the module + * gives that container nothing. + */ + List tables(PackWorld world, Site site); + + /** The items table {@code key} gives at {@code site}; empty for a table this module does not know. */ + List items(PackWorld world, Site site, String key); + + /** + * Where loot is given. + * + * @param position the container, or the mob's spawn spot: world X and Z, Y the height above the dimension's + * bottom + * @param container the container's block when it is a storage chest the module may give the loot of the object + * placed there, null for any other spot + * @param nativeLootDefined whether the platform already gave the container a loot table of its own + * @param debug whether items should say where they came from ({@link ItemDrop#source()}) + */ + record Site(BlockPosition position, BlockState container, boolean nativeLootDefined, boolean debug) { + public Site { + Objects.requireNonNull(position, "position"); + } + } + + /** Where a container's items come from. */ + sealed interface Source permits Table, PlatformTable { + } + + /** A loot table of the pack, named by its key as packs write it ({@code dungeon/chest}). */ + record Table(String key) implements Source { + public Table { + Objects.requireNonNull(key, "key"); + } + } + + /** + * A loot table of the platform, such as {@code minecraft:chests/simple_dungeon}. The host draws it with {@code seed} + * so that the same container always gets the same items. + */ + record PlatformTable(String key, long seed) implements Source { + public PlatformTable { + Objects.requireNonNull(key, "key"); + } + } +} diff --git a/src/test/java/com/volmit/support/EmptyResources.java b/src/test/java/com/volmit/support/EmptyResources.java index dc7d483eb..777685530 100644 --- a/src/test/java/com/volmit/support/EmptyResources.java +++ b/src/test/java/com/volmit/support/EmptyResources.java @@ -1,6 +1,7 @@ package com.volmit.support; import com.volmit.block.BlockRegistry; +import com.volmit.item.ItemRegistry; import com.volmit.noise.NoiseFactory; import com.volmit.resource.PackResources; import com.volmit.resource.ResourceId; @@ -28,6 +29,11 @@ public final class EmptyResources implements PackResources { throw new AssertionError("Unused blocks"); } + @Override + public ItemRegistry items() { + throw new AssertionError("Unused items"); + } + @Override public NoiseFactory noise() { throw new AssertionError("Unused noise");