Skip to content

Commit

Permalink
Add LootTableEvents.LOADED event (#3352)
Browse files Browse the repository at this point in the history
* Implement `LootTableEvents.LOADED` event

* Update for checkstyle

* rename event

* Update fabric-loot-api-v2/src/main/java/net/fabricmc/fabric/api/loot/v2/LootTableEvents.java

Co-authored-by: Juuz <[email protected]>

---------

Co-authored-by: modmuss <[email protected]>
Co-authored-by: Juuz <[email protected]>
(cherry picked from commit 96dfa95)
  • Loading branch information
LLytho authored and modmuss50 committed Oct 8, 2023
1 parent e3d2bf3 commit 3ba460f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ private LootTableEvents() {
}
});

/**
* This event can be used for post-processing after all loot tables have been loaded and modified by Fabric.
*/
public static final Event<Loaded> ALL_LOADED = EventFactory.createArrayBacked(Loaded.class, listeners -> (resourceManager, lootManager) -> {
for (Loaded listener : listeners) {
listener.onLootTablesLoaded(resourceManager, lootManager);
}
});

public interface Replace {
/**
* Replaces loot tables.
Expand Down Expand Up @@ -116,4 +125,14 @@ public interface Modify {
*/
void modifyLootTable(ResourceManager resourceManager, LootManager lootManager, Identifier id, LootTable.Builder tableBuilder, LootTableSource source);
}

public interface Loaded {
/**
* Called when all loot tables have been loaded and {@link LootTableEvents#REPLACE} and {@link LootTableEvents#MODIFY} have been invoked.
*
* @param resourceManager the server resource manager
* @param lootManager the loot manager
*/
void onLootTablesLoaded(ResourceManager resourceManager, LootManager lootManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ private void applyLootTableEvents(ResourceManager resourceManager, LootManager l
});

this.keyToValue = newTables.build();
LootTableEvents.ALL_LOADED.invoker().onLootTablesLoaded(resourceManager, lootManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,13 @@ public void onInitialize() {
tableBuilder.modifyPools(poolBuilder -> poolBuilder.with(ItemEntry.builder(Items.EMERALD)));
}
});

LootTableEvents.ALL_LOADED.register((resourceManager, lootManager) -> {
LootTable blackWoolTable = lootManager.getLootTable(Blocks.BLACK_WOOL.getLootTableId());

if (blackWoolTable == LootTable.EMPTY) {
throw new AssertionError("black wool loot table should not be empty");
}
});
}
}

0 comments on commit 3ba460f

Please sign in to comment.