From 327ac9901706e6ee70658f801aef850976bccfe3 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Wed, 2 Apr 2025 20:37:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E6=A4=8D=E5=B0=8F=E8=A1=A5=E7=BB=99?= =?UTF-8?q?=E7=AE=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/SmallContainerBlock.java | 2 -- .../entity/SmallContainerBlockEntity.java | 21 +++++++----- .../superbwarfare/init/ModItems.java | 4 +-- .../atsuishio/superbwarfare/init/ModTabs.java | 33 ++++++++++--------- .../item/SmallContainerBlockItem.java | 9 +++-- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java index 611cf64b8..1ddb2c821 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java @@ -40,7 +40,6 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; -@SuppressWarnings("deprecation") public class SmallContainerBlock extends BaseEntityBlock { public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; @@ -148,6 +147,5 @@ public class SmallContainerBlock extends BaseEntityBlock { level.getBlockEntity(pos, ModBlockEntities.SMALL_CONTAINER.get()).ifPresent((blockEntity) -> blockEntity.saveToItem(stack, level.registryAccess())); return stack; } - } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/SmallContainerBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/SmallContainerBlockEntity.java index 5c0ae051c..af1455357 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/SmallContainerBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/SmallContainerBlockEntity.java @@ -3,11 +3,14 @@ package com.atsuishio.superbwarfare.block.entity; import com.atsuishio.superbwarfare.block.SmallContainerBlock; import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.tools.ParticleTool; +import net.minecraft.advancements.CriteriaTriggers; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; @@ -22,6 +25,8 @@ import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.storage.loot.LootParams; +import net.minecraft.world.level.storage.loot.LootTable; +import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.phys.Vec3; import org.jetbrains.annotations.NotNull; @@ -38,7 +43,7 @@ import java.util.List; public class SmallContainerBlockEntity extends BlockEntity implements GeoBlockEntity { @Nullable - public ResourceLocation lootTable; + public ResourceKey lootTable; public long lootTableSeed; public int tick = 0; @Nullable @@ -99,7 +104,7 @@ public class SmallContainerBlockEntity extends BlockEntity implements GeoBlockEn super.loadAdditional(tag, registries); if (tag.contains("LootTable", 8)) { - this.lootTable = ResourceLocation.bySeparator(tag.getString("LootTable"), ':'); + this.lootTable = ResourceKey.create(Registries.LOOT_TABLE, ResourceLocation.parse(tag.getString("LootTable"))); this.lootTableSeed = tag.getLong("LootTableSeed"); } this.tick = tag.getInt("Tick"); @@ -111,7 +116,7 @@ public class SmallContainerBlockEntity extends BlockEntity implements GeoBlockEn super.saveAdditional(tag, registries); if (this.lootTable != null) { - tag.putString("LootTable", this.lootTable.toString()); + tag.putString("LootTable", this.lootTable.location().toString()); if (this.lootTableSeed != 0L) { tag.putLong("LootTableSeed", this.lootTableSeed); } @@ -142,18 +147,16 @@ public class SmallContainerBlockEntity extends BlockEntity implements GeoBlockEn BlockItem.setBlockEntityData(stack, this.getType(), tag); } - public void setLootTable(ResourceLocation pLootTable, long pLootTableSeed) { + public void setLootTable(ResourceKey pLootTable, long pLootTableSeed) { this.lootTable = pLootTable; this.lootTableSeed = pLootTableSeed; } public List unpackLootTable(@Nullable Player pPlayer) { if (this.lootTable != null && this.level != null && this.level.getServer() != null) { - - // TODO loot table resource key? -// LootTable loottable = this.level.getServer().reloadableRegistries().getLootTable(this.lootTable); + LootTable loottable = this.level.getServer().reloadableRegistries().getLootTable(this.lootTable); if (pPlayer instanceof ServerPlayer) { -// CriteriaTriggers.GENERATE_LOOT.trigger((ServerPlayer) pPlayer, this.lootTable); + CriteriaTriggers.GENERATE_LOOT.trigger((ServerPlayer) pPlayer, this.lootTable); } this.lootTable = null; @@ -163,7 +166,7 @@ public class SmallContainerBlockEntity extends BlockEntity implements GeoBlockEn builder.withLuck(pPlayer.getLuck()).withParameter(LootContextParams.THIS_ENTITY, pPlayer); } -// return loottable.getRandomItems(builder.create(LootContextParamSets.CHEST), this.lootTableSeed).stream().toList(); + return loottable.getRandomItems(builder.create(LootContextParamSets.CHEST), this.lootTableSeed).stream().toList(); } return Collections.emptyList(); } diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java b/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java index fdf1e083b..105ffd2f9 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java @@ -125,8 +125,6 @@ public class ModItems { */ public static final DeferredRegister ITEMS = DeferredRegister.create(Registries.ITEM, Mod.MODID); - public static final DeferredHolder CONTAINER = ITEMS.register("container", ContainerBlockItem::new); - public static final DeferredHolder SMALL_CONTAINER = ITEMS.register("small_container", SmallContainerBlockItem::new); public static final DeferredHolder SENPAI_SPAWN_EGG = ITEMS.register("senpai_spawn_egg", () -> new DeferredSpawnEggItem(ModEntities.SENPAI::value, -11584987, -14014413, new Item.Properties())); public static final DeferredHolder ANCIENT_CPU = ITEMS.register("ancient_cpu", () -> new Item(new Item.Properties().rarity(Rarity.RARE))); public static final DeferredHolder PROPELLER = ITEMS.register("propeller", () -> new Item(new Item.Properties())); @@ -278,6 +276,8 @@ public class ModItems { public static final DeferredHolder SILVER_BLOCK = block(ModBlocks.SILVER_BLOCK); public static final DeferredHolder CEMENTED_CARBIDE_BLOCK = block(ModBlocks.CEMENTED_CARBIDE_BLOCK); public static final DeferredHolder FUMO_25 = block(ModBlocks.FUMO_25); + public static final DeferredHolder CONTAINER = BLOCKS.register("container", ContainerBlockItem::new); + public static final DeferredHolder SMALL_CONTAINER = BLOCKS.register("small_container", SmallContainerBlockItem::new); /** * Perk Items diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java b/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java index a8465031e..b14547660 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java @@ -1,9 +1,7 @@ package com.atsuishio.superbwarfare.init; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.item.ArmorPlate; -import com.atsuishio.superbwarfare.item.BatteryItem; -import com.atsuishio.superbwarfare.item.C4Bomb; +import com.atsuishio.superbwarfare.item.*; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; import net.minecraft.world.item.CreativeModeTab; @@ -17,8 +15,6 @@ import net.neoforged.neoforge.registries.DeferredRegister; import java.util.function.Supplier; -import static com.atsuishio.superbwarfare.item.ContainerBlockItem.CONTAINER_ENTITIES; - @EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD) @SuppressWarnings("unused") public class ModTabs { @@ -67,16 +63,12 @@ public class ModTabs { .title(Component.translatable("item_group.superbwarfare.item")) .icon(() -> new ItemStack(ModItems.TARGET_DEPLOYER.get())) .displayItems((param, output) -> ModItems.ITEMS.getEntries().forEach(registryObject -> { - if (registryObject.get() == ModItems.CONTAINER.get()) { - CONTAINER_ENTITIES.stream().map(Supplier::get).forEach(output::accept); - } else { - output.accept(registryObject.get()); - if (registryObject.get() == ModItems.ARMOR_PLATE.get()) { - output.accept(ArmorPlate.getInfiniteInstance()); - } - if (registryObject.get() instanceof BatteryItem batteryItem) { - output.accept(batteryItem.makeFullEnergyStack()); - } + output.accept(registryObject.get()); + if (registryObject.get() == ModItems.ARMOR_PLATE.get()) { + output.accept(ArmorPlate.getInfiniteInstance()); + } + if (registryObject.get() instanceof BatteryItem batteryItem) { + output.accept(batteryItem.makeFullEnergyStack()); } })) .build()); @@ -86,7 +78,16 @@ public class ModTabs { .title(Component.translatable("item_group.superbwarfare.block")) .icon(() -> new ItemStack(ModItems.SANDBAG.get())) .withTabsBefore(ITEM_TAB.getKey()) - .displayItems((param, output) -> ModItems.BLOCKS.getEntries().forEach(registryObject -> output.accept(registryObject.get()))) + .displayItems((param, output) -> ModItems.BLOCKS.getEntries().forEach(registryObject -> { + if (registryObject.get() == ModItems.CONTAINER.get()) { + ContainerBlockItem.CONTAINER_ENTITIES.stream().map(Supplier::get).forEach(output::accept); + } else if (registryObject.get() == ModItems.SMALL_CONTAINER.get()) { + output.accept(registryObject.get()); + SmallContainerBlockItem.SMALL_CONTAINER_LOOT_TABLES.stream().map(Supplier::get).forEach(output::accept); + } else { + output.accept(registryObject.get()); + } + })) .build()); @SubscribeEvent diff --git a/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java b/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java index 7f54c1341..711a26755 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java @@ -6,10 +6,13 @@ import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlocks; import com.atsuishio.superbwarfare.init.ModItems; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; +import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.storage.loot.LootTable; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.client.extensions.common.IClientItemExtensions; @@ -68,13 +71,13 @@ public class SmallContainerBlockItem extends BlockItem implements GeoItem { } public static ItemStack createInstance(ResourceLocation lootTable) { - return createInstance(lootTable, 0L); + return createInstance(ResourceKey.create(Registries.LOOT_TABLE, lootTable), 0L); } - public static ItemStack createInstance(ResourceLocation lootTable, long lootTableSeed) { + public static ItemStack createInstance(ResourceKey lootTable, long lootTableSeed) { ItemStack stack = new ItemStack(ModBlocks.SMALL_CONTAINER.get()); CompoundTag tag = new CompoundTag(); - tag.putString("LootTable", lootTable.toString()); + tag.putString("LootTable", lootTable.location().toString()); if (lootTableSeed != 0L) { tag.putLong("LootTableSeed", lootTableSeed); }