mirror of
https://github.com/architectury/architectury-api.git
synced 2026-03-28 03:56:59 -05:00
[ci skip] Add loot table modification event (#287)
* Add loot table modification event Closes #42. It's a simple wrapper around the platform events. * Add param for builtin loot tables Co-authored-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of architectury.
|
||||||
|
* Copyright (C) 2020, 2021, 2022 architectury
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.architectury.event.events.common;
|
||||||
|
|
||||||
|
import dev.architectury.event.Event;
|
||||||
|
import dev.architectury.event.EventFactory;
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootPool;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootTables;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Events related to loot tables and loot generation.
|
||||||
|
*/
|
||||||
|
public interface LootEvent {
|
||||||
|
/**
|
||||||
|
* An event to modify loot tables when they are loaded.
|
||||||
|
* This can be used to add new drops via new loot pools to existing loot tables
|
||||||
|
* without replacing the entire table.
|
||||||
|
*
|
||||||
|
* <h2>Built-in loot tables</h2>
|
||||||
|
* <p>{@linkplain ModifyLootTable The event interface} includes a {@code builtin} parameter.
|
||||||
|
* If it's {@code true}, the loot table is built-in to vanilla or a mod.
|
||||||
|
* Otherwise, it's from a user data pack. The parameter can be used to only modify built-in loot tables
|
||||||
|
* and let user-provided loot tables act as full "overwrites".
|
||||||
|
*
|
||||||
|
* <p>This event only runs for built-in loot tables on Forge due to the limitations of
|
||||||
|
* {@code LootTableLoadEvent}.
|
||||||
|
*
|
||||||
|
* <h2>Example: adding diamonds as a drop for dirt</h2>
|
||||||
|
* <pre>{@code
|
||||||
|
* LootEvent.MODIFY_LOOT_TABLE.register((lootTables, id, context, builtin) -> {
|
||||||
|
* // Check that the loot table is dirt and built-in
|
||||||
|
* if (builtin && Blocks.DIRT.getLootTable().equals(id)) {
|
||||||
|
* // Create a loot pool with a single item entry of Items.DIAMOND
|
||||||
|
* LootPool.Builder pool = LootPool.lootPool().add(LootItem.lootTableItem(Items.DIAMOND));
|
||||||
|
* context.addPool(pool);
|
||||||
|
* }
|
||||||
|
* });
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @see ModifyLootTable#modifyLootTable(LootTables, ResourceLocation, LootTableModificationContext, boolean)
|
||||||
|
*/
|
||||||
|
Event<ModifyLootTable> MODIFY_LOOT_TABLE = EventFactory.createLoop();
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
interface ModifyLootTable {
|
||||||
|
/**
|
||||||
|
* Modifies a loot table.
|
||||||
|
*
|
||||||
|
* @param lootTables the {@link LootTables} instance containing all loot tables
|
||||||
|
* @param id the loot table ID
|
||||||
|
* @param context the context used to modify the loot table
|
||||||
|
* @param builtin if {@code true}, the loot table is built-in;
|
||||||
|
* if {@code false}, it is from a user data pack
|
||||||
|
*/
|
||||||
|
void modifyLootTable(LootTables lootTables, ResourceLocation id, LootTableModificationContext context, boolean builtin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A platform-specific bridge for modifying a specific loot table.
|
||||||
|
*/
|
||||||
|
@ApiStatus.NonExtendable
|
||||||
|
interface LootTableModificationContext {
|
||||||
|
/**
|
||||||
|
* Adds a pool to the loot table.
|
||||||
|
*
|
||||||
|
* @param pool the pool to add
|
||||||
|
*/
|
||||||
|
void addPool(LootPool pool);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a pool to the loot table.
|
||||||
|
*
|
||||||
|
* @param pool the pool to add
|
||||||
|
*/
|
||||||
|
default void addPool(LootPool.Builder pool) {
|
||||||
|
addPool(pool.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ import dev.architectury.event.events.client.ClientTooltipEvent;
|
|||||||
import dev.architectury.event.events.common.CommandRegistrationEvent;
|
import dev.architectury.event.events.common.CommandRegistrationEvent;
|
||||||
import dev.architectury.event.events.common.InteractionEvent;
|
import dev.architectury.event.events.common.InteractionEvent;
|
||||||
import dev.architectury.event.events.common.LifecycleEvent;
|
import dev.architectury.event.events.common.LifecycleEvent;
|
||||||
|
import dev.architectury.event.events.common.LootEvent;
|
||||||
import dev.architectury.event.events.common.TickEvent;
|
import dev.architectury.event.events.common.TickEvent;
|
||||||
import net.fabricmc.api.EnvType;
|
import net.fabricmc.api.EnvType;
|
||||||
import net.fabricmc.api.Environment;
|
import net.fabricmc.api.Environment;
|
||||||
@@ -40,6 +41,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
|
|||||||
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
|
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
|
||||||
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
|
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
|
||||||
import net.fabricmc.fabric.api.event.player.UseItemCallback;
|
import net.fabricmc.fabric.api.event.player.UseItemCallback;
|
||||||
|
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
|
||||||
|
|
||||||
public class EventHandlerImpl {
|
public class EventHandlerImpl {
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
@@ -75,6 +77,8 @@ public class EventHandlerImpl {
|
|||||||
UseItemCallback.EVENT.register((player, world, hand) -> InteractionEvent.RIGHT_CLICK_ITEM.invoker().click(player, hand).asMinecraft());
|
UseItemCallback.EVENT.register((player, world, hand) -> InteractionEvent.RIGHT_CLICK_ITEM.invoker().click(player, hand).asMinecraft());
|
||||||
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> InteractionEvent.RIGHT_CLICK_BLOCK.invoker().click(player, hand, hitResult.getBlockPos(), hitResult.getDirection()).asMinecraft());
|
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> InteractionEvent.RIGHT_CLICK_BLOCK.invoker().click(player, hand, hitResult.getBlockPos(), hitResult.getDirection()).asMinecraft());
|
||||||
AttackBlockCallback.EVENT.register((player, world, hand, pos, face) -> InteractionEvent.LEFT_CLICK_BLOCK.invoker().click(player, hand, pos, face).asMinecraft());
|
AttackBlockCallback.EVENT.register((player, world, hand, pos, face) -> InteractionEvent.LEFT_CLICK_BLOCK.invoker().click(player, hand, pos, face).asMinecraft());
|
||||||
|
|
||||||
|
LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> LootEvent.MODIFY_LOOT_TABLE.invoker().modifyLootTable(lootManager, id, new LootTableModificationContextImpl(tableBuilder), source.isBuiltin()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Environment(EnvType.SERVER)
|
@Environment(EnvType.SERVER)
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of architectury.
|
||||||
|
* Copyright (C) 2020, 2021, 2022 architectury
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.architectury.event.fabric;
|
||||||
|
|
||||||
|
import dev.architectury.event.events.common.LootEvent;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootPool;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootTable;
|
||||||
|
|
||||||
|
final class LootTableModificationContextImpl implements LootEvent.LootTableModificationContext {
|
||||||
|
private final LootTable.Builder tableBuilder;
|
||||||
|
|
||||||
|
LootTableModificationContextImpl(LootTable.Builder tableBuilder) {
|
||||||
|
this.tableBuilder = tableBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addPool(LootPool pool) {
|
||||||
|
tableBuilder.pool(pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addPool(LootPool.Builder pool) {
|
||||||
|
tableBuilder.withPool(pool);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ import net.minecraft.world.item.ItemStack;
|
|||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.minecraft.world.level.LevelAccessor;
|
import net.minecraft.world.level.LevelAccessor;
|
||||||
import net.minecraftforge.event.CommandEvent;
|
import net.minecraftforge.event.CommandEvent;
|
||||||
|
import net.minecraftforge.event.LootTableLoadEvent;
|
||||||
import net.minecraftforge.event.RegisterCommandsEvent;
|
import net.minecraftforge.event.RegisterCommandsEvent;
|
||||||
import net.minecraftforge.event.ServerChatEvent;
|
import net.minecraftforge.event.ServerChatEvent;
|
||||||
import net.minecraftforge.event.TickEvent.LevelTickEvent;
|
import net.minecraftforge.event.TickEvent.LevelTickEvent;
|
||||||
@@ -415,6 +416,11 @@ public class EventHandlerImplCommon {
|
|||||||
ChunkEvent.LOAD_DATA.invoker().load(event.getChunk(), level instanceof ServerLevel ? (ServerLevel) level : null, event.getData());
|
ChunkEvent.LOAD_DATA.invoker().load(event.getChunk(), level instanceof ServerLevel ? (ServerLevel) level : null, event.getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent(priority = EventPriority.HIGH)
|
||||||
|
public static void event(LootTableLoadEvent event) {
|
||||||
|
LootEvent.MODIFY_LOOT_TABLE.invoker().modifyLootTable(event.getLootTableManager(), event.getName(), new LootTableModificationContextImpl(event.getTable()), true);
|
||||||
|
}
|
||||||
|
|
||||||
public interface LevelEventAttachment {
|
public interface LevelEventAttachment {
|
||||||
LevelAccessor architectury$getAttachedLevel();
|
LevelAccessor architectury$getAttachedLevel();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of architectury.
|
||||||
|
* Copyright (C) 2020, 2021, 2022 architectury
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.architectury.event.forge;
|
||||||
|
|
||||||
|
import dev.architectury.event.events.common.LootEvent;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootPool;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootTable;
|
||||||
|
|
||||||
|
final class LootTableModificationContextImpl implements LootEvent.LootTableModificationContext {
|
||||||
|
private final LootTable table;
|
||||||
|
|
||||||
|
LootTableModificationContextImpl(LootTable table) {
|
||||||
|
this.table = table;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addPool(LootPool pool) {
|
||||||
|
table.addPool(pool);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import dev.architectury.test.entity.TestEntity;
|
|||||||
import dev.architectury.test.events.DebugEvents;
|
import dev.architectury.test.events.DebugEvents;
|
||||||
import dev.architectury.test.gamerule.TestGameRules;
|
import dev.architectury.test.gamerule.TestGameRules;
|
||||||
import dev.architectury.test.item.TestBlockInteractions;
|
import dev.architectury.test.item.TestBlockInteractions;
|
||||||
|
import dev.architectury.test.loot.TestLoot;
|
||||||
import dev.architectury.test.networking.TestModNet;
|
import dev.architectury.test.networking.TestModNet;
|
||||||
import dev.architectury.test.particle.TestParticles;
|
import dev.architectury.test.particle.TestParticles;
|
||||||
import dev.architectury.test.registry.TestRegistries;
|
import dev.architectury.test.registry.TestRegistries;
|
||||||
@@ -56,6 +57,7 @@ public class TestMod {
|
|||||||
TestParticles.initialize();
|
TestParticles.initialize();
|
||||||
TestModNet.initialize();
|
TestModNet.initialize();
|
||||||
TestBlockInteractions.init();
|
TestBlockInteractions.init();
|
||||||
|
TestLoot.init();
|
||||||
TestWorldGeneration.initialize();
|
TestWorldGeneration.initialize();
|
||||||
EnvExecutor.runInEnv(Env.CLIENT, () -> TestMod.Client::initializeClient);
|
EnvExecutor.runInEnv(Env.CLIENT, () -> TestMod.Client::initializeClient);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of architectury.
|
||||||
|
* Copyright (C) 2020, 2021, 2022 architectury
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.architectury.test.loot;
|
||||||
|
|
||||||
|
import dev.architectury.event.events.common.LootEvent;
|
||||||
|
import net.minecraft.world.item.Items;
|
||||||
|
import net.minecraft.world.level.block.Blocks;
|
||||||
|
import net.minecraft.world.level.storage.loot.LootPool;
|
||||||
|
import net.minecraft.world.level.storage.loot.entries.LootItem;
|
||||||
|
|
||||||
|
public class TestLoot {
|
||||||
|
public static void init() {
|
||||||
|
LootEvent.MODIFY_LOOT_TABLE.register((lootTables, id, context, builtin) -> {
|
||||||
|
// Check that the loot table is dirt and built-in
|
||||||
|
if (builtin && Blocks.DIRT.getLootTable().equals(id)) {
|
||||||
|
// Create a loot pool with a single item entry of Items.DIAMOND
|
||||||
|
LootPool.Builder pool = LootPool.lootPool().add(LootItem.lootTableItem(Items.DIAMOND));
|
||||||
|
context.addPool(pool);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user