Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21] Fix duplicate registration error for empty loot table #1151

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.DynamicOps;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenCustomHashSet;
import java.io.File;
import java.util.EnumSet;
Expand Down Expand Up @@ -100,6 +101,7 @@
import net.minecraft.world.level.portal.PortalShape;
import net.minecraft.world.level.storage.PlayerDataStorage;
import net.minecraft.world.level.storage.ServerLevelData;
import net.minecraft.world.level.storage.loot.LootDataType;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
Expand Down Expand Up @@ -680,12 +682,18 @@ public static boolean onProjectileImpact(Projectile projectile, HitResult ray) {
return NeoForge.EVENT_BUS.post(new ProjectileImpactEvent(projectile, ray)).isCanceled();
}

/**
* Fires the {@link LootTableLoadEvent} for non-empty loot tables and returns the table if the event was not
* canceled and the table was not set to {@link LootTable#EMPTY} in the event. Otherwise returns {@code null}
* which maps to an empty {@link Optional} in {@link LootDataType#deserialize(ResourceLocation, DynamicOps, Object)}
*/
@Nullable
public static LootTable loadLootTable(ResourceLocation name, LootTable table) {
if (table == LootTable.EMPTY) // Empty table has a null name, and shouldn't be modified anyway.
return table;
return null;
LootTableLoadEvent event = new LootTableLoadEvent(name, table);
if (NeoForge.EVENT_BUS.post(event).isCanceled())
return LootTable.EMPTY;
if (NeoForge.EVENT_BUS.post(event).isCanceled() || event.getTable() == LootTable.EMPTY)
return null;
return event.getTable();
}

Expand Down
Loading