From a1af4a85ff838c946ef078e84d523498386d40c4 Mon Sep 17 00:00:00 2001 From: piexel Date: Fri, 9 Oct 2026 22:02:47 +0200 Subject: [PATCH] iris:loot: Loot and BlockDrops providers, loot table resolution and item drawing moved out of core --- source-patterns.txt | 1 + .../iris/engine/loot/BlockDropping.java | 66 +++++++ .../arcane/iris/engine/loot/LootGiving.java | 114 ++++++++++++ .../arcane/iris/engine/loot/LootItems.java | 151 ++++++++++++++++ .../arcane/iris/engine/loot/LootModule.java | 50 ++++++ .../arcane/iris/engine/loot/LootTables.java | 162 ++++++++++++++++++ .../mantle/components/ObjectLocating.java | 18 ++ .../engine/object/loot/IrisBlockDrops.java | 37 ++-- .../iris/engine/object/loot/IrisLoot.java | 2 +- .../engine/object/loot/IrisLootTable.java | 2 +- .../object/loot/IrisVanillaLootTable.java | 2 +- 11 files changed, 590 insertions(+), 15 deletions(-) create mode 100644 src/main/java/art/arcane/iris/engine/loot/BlockDropping.java create mode 100644 src/main/java/art/arcane/iris/engine/loot/LootGiving.java create mode 100644 src/main/java/art/arcane/iris/engine/loot/LootItems.java create mode 100644 src/main/java/art/arcane/iris/engine/loot/LootModule.java create mode 100644 src/main/java/art/arcane/iris/engine/loot/LootTables.java diff --git a/source-patterns.txt b/source-patterns.txt index b09f3c28b..f4f3d130d 100644 --- a/source-patterns.txt +++ b/source-patterns.txt @@ -16,3 +16,4 @@ art/arcane/iris/engine/framework/StructureVerticalBounds.java art/arcane/iris/engine/framework/PlacedObject.java art/arcane/iris/engine/framework/PlacedStructurePiece.java art/arcane/iris/builtin/** +art/arcane/iris/engine/loot/** diff --git a/src/main/java/art/arcane/iris/engine/loot/BlockDropping.java b/src/main/java/art/arcane/iris/engine/loot/BlockDropping.java new file mode 100644 index 000000000..e1ff26a61 --- /dev/null +++ b/src/main/java/art/arcane/iris/engine/loot/BlockDropping.java @@ -0,0 +1,66 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import art.arcane.iris.core.modules.IrisPackWorld; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.loot.IrisBlockDrops; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.spi.IrisPlatforms; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.volmlib.util.collection.KList; +import com.volmit.item.ItemDrop; +import com.volmit.service.BlockDrops; +import com.volmit.world.PackWorld; + +import java.util.ArrayList; +import java.util.List; + +/** + * How {@code iris:loot} drops items for broken blocks: the block drops of the biome, the region and the dimension, the + * biome's first and the biome able to keep the others from dropping. + */ +public final class BlockDropping implements BlockDrops { + @Override + public Result drops(PackWorld world, Broken broken) { + Engine engine = IrisPackWorld.of(world).engine(); + int x = broken.position().x(); + int y = broken.position().y(); + int z = broken.position().z(); + PlatformBlockState block = IrisPlatforms.get().registries().block(broken.block().key()); + + IrisBiome biome = engine.getBiome(x, y, z); + List providers = dropsFor(biome.getBlockDrops(), block, engine); + if (providers.stream().noneMatch(IrisBlockDrops::isSkipParents)) { + IrisRegion region = engine.getRegion(x, z); + providers.addAll(dropsFor(region.getBlockDrops(), block, engine)); + providers.addAll(dropsFor(engine.getDimension().getBlockDrops(), block, engine)); + } + + LootItems items = new LootItems(world.resources().items(), false); + List drops = new ArrayList<>(); + providers.forEach(provider -> provider.fillDrops(items, drops)); + return new Result(drops, providers.stream().anyMatch(IrisBlockDrops::isReplaceVanillaDrops)); + } + + private static List dropsFor(KList candidates, PlatformBlockState block, Engine engine) { + return new KList<>(candidates.stream().filter(drops -> drops.shouldDropFor(block, engine.getData())).toList()); + } +} diff --git a/src/main/java/art/arcane/iris/engine/loot/LootGiving.java b/src/main/java/art/arcane/iris/engine/loot/LootGiving.java new file mode 100644 index 000000000..f747148a8 --- /dev/null +++ b/src/main/java/art/arcane/iris/engine/loot/LootGiving.java @@ -0,0 +1,114 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import art.arcane.iris.core.modules.IrisPackWorld; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.LootResolver; +import art.arcane.iris.engine.framework.PlacedObject; +import art.arcane.iris.engine.object.loot.IrisLootTable; +import art.arcane.iris.engine.object.loot.IrisVanillaLootTable; +import art.arcane.iris.engine.object.placement.IrisObjectPlacement; +import art.arcane.iris.spi.IrisPlatforms; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.volmlib.util.collection.KList; +import art.arcane.volmlib.util.math.RNG; +import com.volmit.item.ItemDrop; +import com.volmit.service.Loot; +import com.volmit.world.PackWorld; + +import java.util.List; + +/** + * How {@code iris:loot} fills containers: the tables of the object placed around a container, then those of the + * dimension, region and biomes at its column, and the items each table gives. Draws are deterministic per container, so + * a container always holds the same loot. + */ +public final class LootGiving implements Loot { + @Override + public List tables(PackWorld world, Site site) { + Engine engine = engine(world); + int x = site.position().x(); + int z = site.position().z(); + int relativeY = site.position().y(); + RNG rng = LootResolver.containerRng(lootSeed(engine), x, relativeY + engine.getMinHeight(), z); + + List tables = new KList<>(); + boolean objectLootDefined = site.nativeLootDefined(); + IrisObjectPlacement placement = site.container() == null ? null : placementAt(engine, x, relativeY, z); + if (placement != null) { + objectLootDefined = objectLootDefined || placement.getLoot().isNotEmpty() || placement.getVanillaLoot().isNotEmpty(); + IrisLootTable table = placement.getTable(platformState(site), engine.getData(), rng); + if (table != null) { + tables.add(table); + if (placement.isOverrideGlobalLoot()) { + return sourcesOf(engine, site, List.of(table)); + } + } + } + + LootTables.resolveEnvironmentSources(tables, engine, rng, x, relativeY, z, objectLootDefined, table -> table); + return sourcesOf(engine, site, tables); + } + + @Override + public List items(PackWorld world, Site site, String key) { + Engine engine = engine(world); + IrisLootTable table = engine.getData().getLootLoader().load(key); + if (table == null) { + return List.of(); + } + int worldY = site.position().y() + engine.getMinHeight(); + return new LootItems(world.resources().items(), site.debug()) + .draw(table, lootSeed(engine), site.position().x(), worldY, site.position().z()); + } + + private static IrisObjectPlacement placementAt(Engine engine, int x, int relativeY, int z) { + PlacedObject placed = engine.getObjectPlacement(x, relativeY, z); + return placed == null ? null : placed.getPlacement(); + } + + private static List sourcesOf(Engine engine, Site site, List tables) { + int worldY = site.position().y() + engine.getMinHeight(); + List sources = new KList<>(); + for (IrisLootTable table : tables) { + if (table instanceof IrisVanillaLootTable vanilla) { + long seed = LootTables.seed(lootSeed(engine), vanilla, site.position().x(), worldY, site.position().z()); + sources.add(new PlatformTable(vanilla.getLootTableKey(), seed)); + continue; + } + if (table != null && table.getLoadKey() != null) { + sources.add(new Table(table.getLoadKey())); + } + } + return sources; + } + + private static PlatformBlockState platformState(Site site) { + return IrisPlatforms.get().registries().block(site.container().key()); + } + + private static long lootSeed(Engine engine) { + return engine.getSeedManager().getLoot(); + } + + private static Engine engine(PackWorld world) { + return IrisPackWorld.of(world).engine(); + } +} diff --git a/src/main/java/art/arcane/iris/engine/loot/LootItems.java b/src/main/java/art/arcane/iris/engine/loot/LootItems.java new file mode 100644 index 000000000..d1b3247d5 --- /dev/null +++ b/src/main/java/art/arcane/iris/engine/loot/LootItems.java @@ -0,0 +1,151 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import art.arcane.iris.engine.framework.LootResolver; +import art.arcane.iris.engine.object.entity.IrisAttributeModifier; +import art.arcane.iris.engine.object.loot.IrisEnchantment; +import art.arcane.iris.engine.object.loot.InventorySlotType; +import art.arcane.iris.engine.object.loot.IrisLoot; +import art.arcane.iris.engine.object.loot.IrisLootTable; +import art.arcane.volmlib.util.math.RNG; +import com.volmit.item.ItemAttribute; +import com.volmit.item.ItemDrop; +import com.volmit.item.ItemEnchantment; +import com.volmit.item.ItemRegistry; + +import java.util.ArrayList; +import java.util.List; +import java.util.OptionalInt; + +/** + * Turns loot entries into the items they give: every random choice is made here, in the order Iris always made them + * (amount, enchantments, attribute modifiers, damage), so that a container's loot stays what it was. What the platform + * must do with the result is the host's. + */ +public final class LootItems { + private static final String AIR = "air"; + private static final String MINECRAFT_PREFIX = "minecraft:"; + private static final int MIN_AMOUNT = 1; + + private final ItemRegistry items; + private final boolean debug; + + /** @param debug whether the items say where they came from */ + public LootItems(ItemRegistry items, boolean debug) { + this.items = items; + this.debug = debug; + } + + /** + * The items {@code table} gives at the spot {@code x}, {@code y}, {@code z} (world coordinates): picks entries + * from the table's own random stream until it has the number of items the table asks for or runs out of tries. + */ + public List draw(IrisLootTable table, long lootSeed, int x, int y, int z) { + List drops = new ArrayList<>(); + List entries = table.getLoot(); + if (entries.isEmpty() || table.getMaxTries() <= 0) { + return drops; + } + + RNG rng = LootTables.rng(lootSeed, table, x, y, z); + int wanted = Math.max(0, LootResolver.inclusive(rng, table.getMinPicked(), table.getMaxPicked())); + int tries = 0; + while (drops.size() < wanted && tries++ < table.getMaxTries()) { + int index = rng.nextInt(entries.size()); + IrisLoot entry = entries.get(index); + if (entry == null || entry.getSlotTypes() != InventorySlotType.STORAGE) { + continue; + } + ItemDrop drop = drawEntry(entry, table, rng, lootSeed, index, x, y, z); + if (drop != null) { + drops.add(drop); + } + } + return drops; + } + + /** The item {@code entry} gives when nothing else decides whether it does, such as a block's drop or a mob's equipment; null for no item. */ + public ItemDrop give(IrisLoot entry, RNG rng) { + return resolve(entry, rng, null); + } + + private ItemDrop drawEntry(IrisLoot entry, IrisLootTable table, RNG rng, long lootSeed, int index, int x, int y, int z) { + long combinedRarity = LootResolver.combinedRarity(entry.getRarity(), table.getRarity()); + if (!LootTables.spatialOneIn(lootSeed, table, index, x, y, z, combinedRarity)) { + return null; + } + return resolve(entry, rng, table); + } + + private ItemDrop resolve(IrisLoot entry, RNG rng, IrisLootTable table) { + String type = entry.getTypeKey(); + OptionalInt maxDurability = items.maxDurability(type); + if (maxDurability.isEmpty() || isAir(type)) { + return null; + } + + int amount = Math.max(MIN_AMOUNT, LootResolver.inclusive(rng, entry.getMinAmount(), entry.getMaxAmount())); + List enchantments = drawEnchantments(entry, rng); + List attributes = drawAttributes(entry, rng); + Double damage = maxDurability.getAsInt() > 0 ? 1D - rng.d(entry.getMinDurability(), entry.getMaxDurability()) : null; + return new ItemDrop(type, amount, entry.getDisplayName(), entry.getLore(), enchantments, attributes, + entry.isUnbreakable(), entry.getItemFlags(), entry.getCustomModel(), damage, entry.getLeatherColor(), + entry.getDyeColorKey(), entry.getCustomNbt(), debug ? sourceOf(entry, table) : null); + } + + private static List drawEnchantments(IrisLoot entry, RNG rng) { + List enchantments = new ArrayList<>(); + for (IrisEnchantment enchantment : entry.getEnchantments()) { + String key = enchantment.getEnchantment(); + if (key == null || key.isBlank() || rng.nextDouble() >= enchantment.getChance()) { + continue; + } + enchantments.add(new ItemEnchantment(key, Math.max(1, enchantment.getLevel(rng)))); + } + return enchantments; + } + + private static List drawAttributes(IrisLoot entry, RNG rng) { + List attributes = new ArrayList<>(); + for (IrisAttributeModifier modifier : entry.getAttributes()) { + if (rng.nextDouble() >= modifier.getChance()) { + continue; + } + String attribute = modifier.getAttribute(); + if (attribute == null || attribute.isBlank()) { + continue; + } + attributes.add(new ItemAttribute(attribute, modifier.getName(), modifier.getOperation(), modifier.getAmount(rng))); + } + return attributes; + } + + private static ItemDrop.Source sourceOf(IrisLoot entry, IrisLootTable table) { + if (table == null) { + return new ItemDrop.Source(null, 0, entry.getRarity()); + } + return new ItemDrop.Source(table.getName(), table.getRarity(), entry.getRarity()); + } + + private static boolean isAir(String type) { + String key = type.startsWith(MINECRAFT_PREFIX) ? type.substring(MINECRAFT_PREFIX.length()) : type; + return AIR.equalsIgnoreCase(key); + } +} diff --git a/src/main/java/art/arcane/iris/engine/loot/LootModule.java b/src/main/java/art/arcane/iris/engine/loot/LootModule.java new file mode 100644 index 000000000..ff32bc104 --- /dev/null +++ b/src/main/java/art/arcane/iris/engine/loot/LootModule.java @@ -0,0 +1,50 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import com.volmit.FeatureRegistry; +import com.volmit.IrisModule; +import com.volmit.ModuleInfo; +import com.volmit.service.BlockDrops; +import com.volmit.service.Loot; +import com.volmit.stage.StageGraph; + +/** + * Module {@code iris:loot}: the loot tables of the pack and the items they and the block drops of the dimension, regions + * and biomes give. It has no generation stage; Iris asks it through {@link Loot} when a container is filled and through + * {@link BlockDrops} when a player breaks a block. + */ +public final class LootModule implements IrisModule { + private static final String VERSION = "built-in"; + + @Override + public ModuleInfo info() { + return ModuleInfo.of("iris:loot", "Loot", VERSION); + } + + @Override + public void install(StageGraph graph) { + } + + @Override + public void register(FeatureRegistry registry) { + registry.registerService(Loot.class, new LootGiving()); + registry.registerService(BlockDrops.class, new BlockDropping()); + } +} diff --git a/src/main/java/art/arcane/iris/engine/loot/LootTables.java b/src/main/java/art/arcane/iris/engine/loot/LootTables.java new file mode 100644 index 000000000..021c3c2b1 --- /dev/null +++ b/src/main/java/art/arcane/iris/engine/loot/LootTables.java @@ -0,0 +1,162 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.engine.loot; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.LootResolver; +import art.arcane.iris.engine.object.loot.IrisLootMode; +import art.arcane.iris.engine.object.loot.IrisLootReference; +import art.arcane.iris.engine.object.loot.IrisLootTable; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.volmlib.util.collection.KList; +import art.arcane.volmlib.util.math.RNG; + +import java.util.List; +import java.util.Objects; +import java.util.function.Function; + +/** + * Which loot tables a place in the world draws from, and the random streams that draw from them: the part of loot + * that reads the pack's tables, loot references and layout. The streams themselves are + * {@link LootResolver}'s; here a table is named by its {@linkplain #identity identity}. + */ +public final class LootTables { + private LootTables() { + } + + /** What names a table in the random streams: its key, else its name. */ + public static String identity(IrisLootTable table) { + if (table == null) { + return ""; + } + String loadKey = table.getLoadKey(); + if (loadKey != null && !loadKey.isBlank()) { + return loadKey; + } + String name = table.getName(); + return name == null ? "" : name; + } + + public static RNG rng(long lootSeed, IrisLootTable table, int x, int y, int z) { + return LootResolver.tableRng(lootSeed, identity(table), x, y, z); + } + + public static long seed(long lootSeed, IrisLootTable table, int x, int y, int z) { + return LootResolver.tableSeed(lootSeed, identity(table), x, y, z); + } + + public static boolean spatialOneIn(long lootSeed, IrisLootTable table, int entryIndex, int x, int y, int z, long rarity) { + return LootResolver.spatialOneIn(lootSeed, identity(table), entryIndex, x, y, z, rarity); + } + + /** + * Adds the tables of the dimension, the region, the surface biome and (below the surface) the cave biome at a + * column to {@code sources}, as their loot references say, then scales their number by the areas' multipliers. + * {@code mapper} turns a table into the caller's own source; a null result skips it. + */ + public static void resolveEnvironmentSources( + List sources, + Engine engine, + RNG rng, + int x, + int relativeY, + int z, + boolean objectLootDefined, + Function mapper + ) { + IrisRegion region = engine.getComplex().getRegionStream().get(x, z); + IrisBiome surfaceBiome = engine.getComplex().getTrueBiomeStream().get(x, z); + double terrainHeight = engine.getComplex().getHeightStream().get(x, z); + IrisBiome contextualBiome = relativeY < terrainHeight + ? engine.getCaveBiome(x, relativeY, z) + : surfaceBiome; + if (contextualBiome == null) { + contextualBiome = surfaceBiome; + } + + boolean distinctContextualBiome = !sameBiome(surfaceBiome, contextualBiome); + double multiplier = engine.getDimension().getLoot().getMultiplier() + * region.getLoot().getMultiplier() + * surfaceBiome.getLoot().getMultiplier(); + if (distinctContextualBiome) { + multiplier *= contextualBiome.getLoot().getMultiplier(); + } + + boolean fallback = !objectLootDefined; + injectReference(sources, engine.getDimension().getLoot(), engine, mapper, fallback); + injectReference(sources, region.getLoot(), engine, mapper, fallback); + injectReference(sources, surfaceBiome.getLoot(), engine, mapper, fallback); + if (distinctContextualBiome) { + injectReference(sources, contextualBiome.getLoot(), engine, mapper, fallback); + } + LootResolver.scaleSources(sources, multiplier, rng); + } + + /** Adds {@code additions} to {@code sources} the way {@code mode} says; {@code fallback} is whether nothing else defined a table. */ + public static void injectSources( + List sources, + List additions, + IrisLootMode mode, + boolean fallback + ) { + if (mode == IrisLootMode.FALLBACK && !fallback) { + return; + } + if (mode == IrisLootMode.CLEAR) { + sources.clear(); + return; + } + if (mode == IrisLootMode.REPLACE) { + sources.clear(); + } + sources.addAll(additions); + } + + static boolean sameBiome(IrisBiome first, IrisBiome second) { + if (first == second) { + return true; + } + if (first == null || second == null) { + return false; + } + return Objects.equals(first.getLoadKey(), second.getLoadKey()); + } + + private static void injectReference( + List sources, + IrisLootReference reference, + Engine engine, + Function mapper, + boolean fallback + ) { + KList tables = reference.getLootTables(engine.getComplex()); + KList additions = new KList<>(); + for (IrisLootTable table : tables) { + if (table == null) { + continue; + } + T source = mapper.apply(table); + if (source != null) { + additions.add(source); + } + } + injectSources(sources, additions, reference.getMode(), fallback); + } +} diff --git a/src/main/java/art/arcane/iris/engine/mantle/components/ObjectLocating.java b/src/main/java/art/arcane/iris/engine/mantle/components/ObjectLocating.java index 2f002ae1e..b022b03f3 100644 --- a/src/main/java/art/arcane/iris/engine/mantle/components/ObjectLocating.java +++ b/src/main/java/art/arcane/iris/engine/mantle/components/ObjectLocating.java @@ -1,3 +1,21 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package art.arcane.iris.engine.mantle.components; import art.arcane.iris.core.modules.IrisPackWorld; diff --git a/src/main/java/art/arcane/iris/engine/object/loot/IrisBlockDrops.java b/src/main/java/art/arcane/iris/engine/object/loot/IrisBlockDrops.java index fa2aa2f57..c3c26608c 100644 --- a/src/main/java/art/arcane/iris/engine/object/loot/IrisBlockDrops.java +++ b/src/main/java/art/arcane/iris/engine/object/loot/IrisBlockDrops.java @@ -22,6 +22,7 @@ import art.arcane.iris.engine.object.material.IrisBlockData; import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.engine.data.cache.AtomicCache; import art.arcane.iris.engine.framework.LootResolver; +import art.arcane.iris.engine.loot.LootItems; import art.arcane.iris.engine.object.annotations.ArrayType; import art.arcane.iris.engine.object.annotations.Desc; import art.arcane.iris.engine.object.annotations.Required; @@ -33,8 +34,9 @@ import lombok.Data; import lombok.NoArgsConstructor; import art.arcane.iris.spi.PlatformBlockState; import lombok.experimental.Accessors; -import org.bukkit.block.data.BlockData; -import org.bukkit.inventory.ItemStack; +import com.volmit.item.ItemDrop; + +import java.util.List; @Snippet("block-drops") @Accessors(chain = true) @@ -43,7 +45,7 @@ import org.bukkit.inventory.ItemStack; @Desc("Represents a block drop list") @Data public class IrisBlockDrops { - private final transient AtomicCache> data = new AtomicCache<>(); + private final transient AtomicCache> data = new AtomicCache<>(); @Required @ArrayType(min = 1, type = IrisBlockData.class) @Desc("The blocks that drop loot") @@ -58,23 +60,23 @@ public class IrisBlockDrops { @Desc("Removes the default vanilla block drops and only drops the given items & any parent loot tables specified for this block type.") private boolean replaceVanillaDrops = false; - public boolean shouldDropFor(BlockData data, IrisData rdata) { - KList list = this.data.aquire(() -> { - KList b = new KList<>(); + public boolean shouldDropFor(PlatformBlockState broken, IrisData rdata) { + KList list = this.data.aquire(() -> { + KList b = new KList<>(); for (IrisBlockData i : getBlocks()) { PlatformBlockState state = i.getBlockData(rdata); if (state != null) { - b.add((BlockData) state.nativeHandle()); + b.add(state); } } return b.removeDuplicates(); }); - for (BlockData i : list) { - if (exactBlocks ? i.equals(data) : i.getMaterial().equals(data.getMaterial())) { + for (PlatformBlockState i : list) { + if (exactBlocks ? i.key().equals(broken.key()) : materialOf(i).equals(materialOf(broken))) { return true; } } @@ -82,11 +84,22 @@ public class IrisBlockDrops { return false; } - public void fillDrops(boolean debug, KList d) { + /** Adds the items of the drops that pass their rarity to {@code d}. */ + public void fillDrops(LootItems items, List d) { for (IrisLoot i : getDrops()) { - if (LootResolver.oneIn(RNG.r, i.getRarity())) { - d.add(i.get(debug, RNG.r)); + if (!LootResolver.oneIn(RNG.r, i.getRarity())) { + continue; + } + ItemDrop drop = items.give(i, RNG.r); + if (drop != null) { + d.add(drop); } } } + + private static String materialOf(PlatformBlockState state) { + String key = state.key(); + int properties = key.indexOf('['); + return properties < 0 ? key : key.substring(0, properties); + } } diff --git a/src/main/java/art/arcane/iris/engine/object/loot/IrisLoot.java b/src/main/java/art/arcane/iris/engine/object/loot/IrisLoot.java index 8ad144f9c..30251684e 100644 --- a/src/main/java/art/arcane/iris/engine/object/loot/IrisLoot.java +++ b/src/main/java/art/arcane/iris/engine/object/loot/IrisLoot.java @@ -184,7 +184,7 @@ public class IrisLoot { public ItemStack get(boolean debug, IrisLootTable table, RNG rng, long lootSeed, int entryIndex, int x, int y, int z) { long combinedRarity = LootResolver.combinedRarity(rarity, table.getRarity()); - if (!LootResolver.spatialOneIn(lootSeed, table, entryIndex, x, y, z, combinedRarity)) { + if (!art.arcane.iris.engine.loot.LootTables.spatialOneIn(lootSeed, table, entryIndex, x, y, z, combinedRarity)) { return null; } diff --git a/src/main/java/art/arcane/iris/engine/object/loot/IrisLootTable.java b/src/main/java/art/arcane/iris/engine/object/loot/IrisLootTable.java index ba9342ca5..9b85aac40 100644 --- a/src/main/java/art/arcane/iris/engine/object/loot/IrisLootTable.java +++ b/src/main/java/art/arcane/iris/engine/object/loot/IrisLootTable.java @@ -80,7 +80,7 @@ public class IrisLootTable extends IrisRegistrant { return lootf; } - RNG rng = LootResolver.tableRng(lootSeed, this, x, y, z); + RNG rng = art.arcane.iris.engine.loot.LootTables.rng(lootSeed, this, x, y, z); int m = 0; int c = 0; int mx = Math.max(0, LootResolver.inclusive(rng, getMinPicked(), getMaxPicked())); diff --git a/src/main/java/art/arcane/iris/engine/object/loot/IrisVanillaLootTable.java b/src/main/java/art/arcane/iris/engine/object/loot/IrisVanillaLootTable.java index c66c624f1..e61521405 100644 --- a/src/main/java/art/arcane/iris/engine/object/loot/IrisVanillaLootTable.java +++ b/src/main/java/art/arcane/iris/engine/object/loot/IrisVanillaLootTable.java @@ -78,7 +78,7 @@ public class IrisVanillaLootTable extends IrisLootTable { IrisLogging.warn("Unknown vanilla loot table " + lootTableKey); return new KList<>(); } - RNG rng = LootResolver.tableRng(lootSeed, this, x, y, z); + RNG rng = art.arcane.iris.engine.loot.LootTables.rng(lootSeed, this, x, y, z); return new KList<>(table.populateLoot(rng, new LootContext.Builder(new Location(world, x, y, z)).build())); }