Fix loot table NPE (#1286)

This commit is contained in:
IzzelAliz 2024-03-22 22:41:14 +08:00
parent 94d8220b84
commit aeb724674a
4 changed files with 32 additions and 5 deletions

View File

@ -0,0 +1,8 @@
package io.izzel.arclight.common.bridge.core.world.storage.loot;
import net.minecraft.world.level.storage.loot.LootTable;
public interface LootDataManagerBridge {
boolean bridge$isRegistered(LootTable lootTable);
}

View File

@ -1,10 +1,12 @@
package io.izzel.arclight.common.mixin.core.world.level.storage.loot; package io.izzel.arclight.common.mixin.core.world.level.storage.loot;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import io.izzel.arclight.common.bridge.core.world.storage.loot.LootDataManagerBridge;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.storage.loot.LootDataId; import net.minecraft.world.level.storage.loot.LootDataId;
import net.minecraft.world.level.storage.loot.LootDataManager; import net.minecraft.world.level.storage.loot.LootDataManager;
import net.minecraft.world.level.storage.loot.LootDataType; import net.minecraft.world.level.storage.loot.LootDataType;
import net.minecraft.world.level.storage.loot.LootTable;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -14,7 +16,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Map; import java.util.Map;
@Mixin(LootDataManager.class) @Mixin(LootDataManager.class)
public class LootDataManagerMixin { public class LootDataManagerMixin implements LootDataManagerBridge {
// @formatter:off // @formatter:off
@Shadow private Map<LootDataId<?>, ?> elements; @Shadow private Map<LootDataId<?>, ?> elements;
@ -28,4 +30,9 @@ public class LootDataManagerMixin {
this.elements.forEach((key, lootTable) -> lootTableToKeyBuilder.put(lootTable, key.location())); this.elements.forEach((key, lootTable) -> lootTableToKeyBuilder.put(lootTable, key.location()));
this.lootTableToKey = lootTableToKeyBuilder.buildKeepingLast(); this.lootTableToKey = lootTableToKeyBuilder.buildKeepingLast();
} }
@Override
public boolean bridge$isRegistered(LootTable lootTable) {
return this.lootTableToKey.containsKey(lootTable);
}
} }

View File

@ -1,6 +1,8 @@
package io.izzel.arclight.common.mixin.core.world.level.storage.loot; package io.izzel.arclight.common.mixin.core.world.level.storage.loot;
import io.izzel.arclight.common.bridge.core.world.storage.loot.LootDataManagerBridge;
import io.izzel.arclight.common.bridge.core.world.storage.loot.LootTableBridge; import io.izzel.arclight.common.bridge.core.world.storage.loot.LootTableBridge;
import io.izzel.arclight.common.mod.server.ArclightServer;
import io.izzel.arclight.mixin.Eject; import io.izzel.arclight.mixin.Eject;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -41,6 +43,9 @@ public abstract class LootTableMixin implements LootTableBridge {
if (!context.hasParam(LootContextParams.ORIGIN) && !context.hasParam(LootContextParams.THIS_ENTITY)) { if (!context.hasParam(LootContextParams.ORIGIN) && !context.hasParam(LootContextParams.THIS_ENTITY)) {
return list; return list;
} }
if (!((LootDataManagerBridge) ArclightServer.getMinecraftServer().getLootData()).bridge$isRegistered((LootTable) (Object) this)) {
return list;
}
LootGenerateEvent event = CraftEventFactory.callLootGenerateEvent(inv, (LootTable) (Object) this, context, list, false); LootGenerateEvent event = CraftEventFactory.callLootGenerateEvent(inv, (LootTable) (Object) this, context, list, false);
if (event.isCancelled()) { if (event.isCancelled()) {
ci.cancel(); ci.cancel();
@ -54,11 +59,14 @@ public abstract class LootTableMixin implements LootTableBridge {
LootContext context = (new LootContext.Builder(lootparams)).withOptionalRandomSeed(i).create(this.randomSequence); LootContext context = (new LootContext.Builder(lootparams)).withOptionalRandomSeed(i).create(this.randomSequence);
ObjectArrayList<ItemStack> objectarraylist = this.getRandomItems(context); ObjectArrayList<ItemStack> objectarraylist = this.getRandomItems(context);
RandomSource randomsource = context.getRandom(); RandomSource randomsource = context.getRandom();
if (((LootDataManagerBridge) ArclightServer.getMinecraftServer().getLootData()).bridge$isRegistered((LootTable) (Object) this)) {
LootGenerateEvent event = CraftEventFactory.callLootGenerateEvent(inv, (LootTable) (Object) this, context, objectarraylist, plugin); LootGenerateEvent event = CraftEventFactory.callLootGenerateEvent(inv, (LootTable) (Object) this, context, objectarraylist, plugin);
if (event.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
objectarraylist = event.getLoot().stream().map(CraftItemStack::asNMSCopy).collect(ObjectArrayList.toList()); objectarraylist = event.getLoot().stream().map(CraftItemStack::asNMSCopy).collect(ObjectArrayList.toList());
}
List<Integer> list = this.getAvailableSlots(inv, randomsource); List<Integer> list = this.getAvailableSlots(inv, randomsource);
this.shuffleAndSplitItems(objectarraylist, list.size(), randomsource); this.shuffleAndSplitItems(objectarraylist, list.size(), randomsource);

View File

@ -69,10 +69,14 @@ abstract class UploadFilesTask extends DefaultTask {
} }
} }
link("/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}/${modloader}", [type: 'object', value: sha1]) link("/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}/${modloader}", [type: 'object', value: sha1])
link("/arclight/branches/${branch.get()}/loaders/${modloader}/versions-snapshot/${version.get()}", [type: 'object', value: sha1])
link("/arclight/branches/${branch.get()}/latest-snapshot", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}", cache_seconds: 3600]) link("/arclight/branches/${branch.get()}/latest-snapshot", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}", cache_seconds: 3600])
link("/arclight/branches/${branch.get()}/loaders/${modloader}/latest-snapshot", [type: 'link', value: "/arclight/branches/${branch.get()}/loaders/${modloader}/versions-snapshot/${version.get()}", cache_seconds: 3600])
if (!snapshot.get()) { if (!snapshot.get()) {
link("/arclight/branches/${branch.get()}/versions-stable/${version.get()}/${modloader}", [type: 'object', value: sha1]) link("/arclight/branches/${branch.get()}/versions-stable/${version.get()}/${modloader}", [type: 'object', value: sha1])
link("/arclight/branches/${branch.get()}/loaders/${modloader}/versions-stable/${version.get()}", [type: 'object', value: sha1])
link("/arclight/branches/${branch.get()}/latest-stable", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-stable/${version.get()}", cache_seconds: 86400]) link("/arclight/branches/${branch.get()}/latest-stable", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-stable/${version.get()}", cache_seconds: 86400])
link("/arclight/branches/${branch.get()}/loaders/${modloader}/latest-stable", [type: 'link', value: "/arclight/branches/${branch.get()}/loaders/${modloader}/versions-stable/${version.get()}", cache_seconds: 86400])
} }
} }