Skip to content

Commit

Permalink
Convert loot table injections to common Architectury loot event
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeow committed Jan 17, 2023
1 parent 58f3b47 commit 9a8918e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.architectury.event.EventResult;
import dev.architectury.event.events.common.EntityEvent;
import dev.architectury.event.events.common.InteractionEvent;
import dev.architectury.event.events.common.LootEvent;
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.architectury.utils.PlatformExpectedError;
import dev.itsmeow.betteranimalsplus.common.entity.*;
Expand Down Expand Up @@ -41,6 +42,10 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.JukeboxBlockEntity;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTables;
import net.minecraft.world.level.storage.loot.entries.LootTableReference;
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -75,6 +80,7 @@ public static void init() {
EntityEvent.ADD.register(CommonEventHandler::entityAdd);
InteractionEvent.RIGHT_CLICK_BLOCK.register(CommonEventHandler::rightClickBlock);
InteractionEvent.RIGHT_CLICK_ITEM.register(CommonEventHandler::rightClickItem);
LootEvent.MODIFY_LOOT_TABLE.register(CommonEventHandler::modifyLootTable);
CommonEventHandler.registerPlatformEvents();
}

Expand Down Expand Up @@ -172,6 +178,17 @@ public static EventResult entityAttack(LivingEntity entity, DamageSource source,
return EventResult.pass();
}

public static void modifyLootTable(LootTables lootTables, ResourceLocation id, LootEvent.LootTableModificationContext context, boolean builtin) {
for (ResourceLocation rl : CommonEventHandler.LOOT_TABLE_INJECTIONS.keys()) {
if (id.equals(rl)) {
for (ResourceLocation ref : CommonEventHandler.LOOT_TABLE_INJECTIONS.get(rl)) {
context.addPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1)).add(LootTableReference.lootTableReference(ref)));
}
break;
}
}
}

public static void modifyDropsList(Collection<ItemEntity> drops, DamageSource source, LivingEntity entity) {
if(source.getEntity() != null && !(entity instanceof Player) && NO_ATTACKED_DROPS.stream().anyMatch(predicate -> predicate.test(source.getEntity())) && (!(source.getEntity() instanceof IBucketable) || !((IBucketable) source.getEntity()).isFromContainer())) {
drops.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
public class CommonEventHandlerImpl {

public static void registerPlatformEvents() {
LootTableEvents.MODIFY.register((ResourceManager resourceManager, LootTables lootManager, ResourceLocation id, LootTable.Builder tableBuilder, LootTableSource source) -> {
for (ResourceLocation rl : CommonEventHandler.LOOT_TABLE_INJECTIONS.keys()) {
if (id.equals(rl)) {
for (ResourceLocation ref : CommonEventHandler.LOOT_TABLE_INJECTIONS.get(rl)) {
tableBuilder.withPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1)).add(LootTableReference.lootTableReference(ref)));
}
break;
}
}
});
}

public static void setSquirrelKills(Player player, int kills) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,9 @@
public class CommonEventHandlerImpl {

public static void registerPlatformEvents() {
MinecraftForge.EVENT_BUS.addListener(CommonEventHandlerImpl::lootLoad);
MinecraftForge.EVENT_BUS.addListener(CommonEventHandlerImpl::livingDrops);
}

public static void lootLoad(LootTableLoadEvent event) {
for (ResourceLocation rl : CommonEventHandler.LOOT_TABLE_INJECTIONS.keys()) {
if (event.getName().equals(rl)) {
for (ResourceLocation ref : CommonEventHandler.LOOT_TABLE_INJECTIONS.get(rl)) {
event.getTable().addPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1)).add(LootTableReference.lootTableReference(ref)).build());
}
break;
}
}
}

public static void livingDrops(LivingDropsEvent event) {
CommonEventHandler.modifyDropsList(event.getDrops(), event.getSource(), event.getEntity());
}
Expand Down

0 comments on commit 9a8918e

Please sign in to comment.