From bb3097e68c7325899fa7fc469106e5d363445d18 Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Tue, 14 Oct 2025 17:20:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E5=9D=97=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84=E6=B3=A8=E5=86=8CBuilder=E5=92=8C=E5=BD=A2?= =?UTF-8?q?=E7=8A=B6=E3=80=81=E5=A3=B0=E9=9F=B3=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../lib39/core/event/CommonHandler.java | 40 +++++++++- .../lib39/core/network/NetworkHandler.java | 1 + .../util/block/BlockRegistryBuilder.java | 73 +++++++++++++++++++ .../lib39/util/shape/ShapeUtil.java | 13 ++++ .../lib39/util/sound/SoundUtil.java | 14 ++++ 6 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 src/main/java/top/r3944realms/lib39/util/block/BlockRegistryBuilder.java create mode 100644 src/main/java/top/r3944realms/lib39/util/shape/ShapeUtil.java create mode 100644 src/main/java/top/r3944realms/lib39/util/sound/SoundUtil.java diff --git a/gradle.properties b/gradle.properties index 2985816..9c3af22 100644 --- a/gradle.properties +++ b/gradle.properties @@ -33,7 +33,7 @@ mod_name=3944Realms 's Lib Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=MIT # The mod version. See https://semver.org/ -mod_version=0.0.2 +mod_version=0.0.3 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/top/r3944realms/lib39/core/event/CommonHandler.java b/src/main/java/top/r3944realms/lib39/core/event/CommonHandler.java index c0ef88c..bae0651 100644 --- a/src/main/java/top/r3944realms/lib39/core/event/CommonHandler.java +++ b/src/main/java/top/r3944realms/lib39/core/event/CommonHandler.java @@ -1,20 +1,29 @@ package top.r3944realms.lib39.core.event; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.level.block.Block; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.BuildCreativeModeTabContentsEvent; import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.entity.EntityJoinLevelEvent; import net.minecraftforge.event.entity.EntityLeaveLevelEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.registries.RegistryObject; import top.r3944realms.lib39.Lib39; import top.r3944realms.lib39.api.event.SyncManagerRegisterEvent; import top.r3944realms.lib39.core.sync.ISyncData; import top.r3944realms.lib39.core.sync.SyncData2Manager; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + public class CommonHandler { - @Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE) + @net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.FORGE) public static class Game extends CommonHandler { static volatile SyncData2Manager syncData2Manager; public static SyncData2Manager getSyncData2Manager() { @@ -61,4 +70,31 @@ public class CommonHandler { } } } + @net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.MOD) + public static class Mod extends CommonHandler { + private static final Map, ResourceKey[]> itemAddMap = new ConcurrentHashMap<>(); + private static final Map, List>> tabToItemsMap = new ConcurrentHashMap<>(); + + @SafeVarargs + public static void addItemToTabs(RegistryObject item, ResourceKey... tabs) { + itemAddMap.put(item, tabs); + + // 更新反向映射 + for (ResourceKey tab : tabs) { + tabToItemsMap.computeIfAbsent(tab, k -> new ArrayList<>()).add(item); + } + } + + @SubscribeEvent + public static void onBuildCreativeTabContents(BuildCreativeModeTabContentsEvent event) { + List> itemsForTab = tabToItemsMap.get(event.getTabKey()); + if (itemsForTab != null) { + itemsForTab.forEach(event::accept); + } + } + + public static Map, ResourceKey[]> getItemAddMap() { + return itemAddMap; + } + } } diff --git a/src/main/java/top/r3944realms/lib39/core/network/NetworkHandler.java b/src/main/java/top/r3944realms/lib39/core/network/NetworkHandler.java index 9af0765..5adc388 100644 --- a/src/main/java/top/r3944realms/lib39/core/network/NetworkHandler.java +++ b/src/main/java/top/r3944realms/lib39/core/network/NetworkHandler.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull; import top.r3944realms.lib39.Lib39; import top.r3944realms.lib39.core.network.toClient.SyncNBTDataS2CPack; +@SuppressWarnings("unused") public class NetworkHandler { private static int cid = 0; public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( diff --git a/src/main/java/top/r3944realms/lib39/util/block/BlockRegistryBuilder.java b/src/main/java/top/r3944realms/lib39/util/block/BlockRegistryBuilder.java new file mode 100644 index 0000000..84e817e --- /dev/null +++ b/src/main/java/top/r3944realms/lib39/util/block/BlockRegistryBuilder.java @@ -0,0 +1,73 @@ +package top.r3944realms.lib39.util.block; + +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.CreativeModeTabs; +import net.minecraft.world.level.block.Block; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.RegistryObject; +import top.r3944realms.lib39.core.event.CommonHandler; + +import java.util.function.Supplier; + +@SuppressWarnings({"UnusedReturnValue", "unused"}) +public class BlockRegistryBuilder { + private String registryName; + private RegistryObject blockObject; + + /** + * 创建新的构建器实例 + */ + public static BlockRegistryBuilder create() { + return new BlockRegistryBuilder(); + } + + /** + * 设置注册名称 + */ + public BlockRegistryBuilder withName(String name) { + this.registryName = name; + return this; + } + /** + * 注册方块(不自动注册物品) + */ + public BlockRegistryBuilder registerBlock(DeferredRegister blockRegister, Supplier blockSupplier) { + this.blockObject = blockRegister.register(this.registryName, blockSupplier); + return this; + } + + /** + * 内部方法:注册对应的方块物品 + */ + @SafeVarargs + private void registerBlockItem(RegistryObject blockObject, ResourceKey... creativeTabs) { + CommonHandler.Mod.addItemToTabs(blockObject, creativeTabs); + } + + /** + * 注册方块和物品到建筑标签页 + */ + public BlockRegistryBuilder registerWithBuildingTab(DeferredRegister blockRegister, Supplier blockSupplier) { + registerBlock(blockRegister, blockSupplier); + registerBlockItem(this.blockObject, CreativeModeTabs.BUILDING_BLOCKS); + return this; + } + + /** + * 注册方块和物品到功能标签页 + */ + public BlockRegistryBuilder registerWithFunctionalTab(DeferredRegister blockRegister, Supplier blockSupplier) { + registerBlock(blockRegister, blockSupplier); + registerBlockItem(this.blockObject, CreativeModeTabs.FUNCTIONAL_BLOCKS); + return this; + } + + /** + * 获取注册的方块对象 + */ + public RegistryObject build() { + return this.blockObject; + } + +} diff --git a/src/main/java/top/r3944realms/lib39/util/shape/ShapeUtil.java b/src/main/java/top/r3944realms/lib39/util/shape/ShapeUtil.java new file mode 100644 index 0000000..1022001 --- /dev/null +++ b/src/main/java/top/r3944realms/lib39/util/shape/ShapeUtil.java @@ -0,0 +1,13 @@ +package top.r3944realms.lib39.util.shape; + +import net.minecraft.world.phys.shapes.Shapes; +import net.minecraft.world.phys.shapes.VoxelShape; + +public class ShapeUtil { + /** + * 创建基于像素的碰撞箱(将像素坐标转换为方块坐标) + */ + public static VoxelShape createPixelBasedShape(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) { + return Shapes.box(minX / 16.0d, minY / 16.0d, minZ / 16.0d, maxX / 16.0d, maxY / 16.0d, maxZ / 16.0d); + } +} diff --git a/src/main/java/top/r3944realms/lib39/util/sound/SoundUtil.java b/src/main/java/top/r3944realms/lib39/util/sound/SoundUtil.java new file mode 100644 index 0000000..3ec905d --- /dev/null +++ b/src/main/java/top/r3944realms/lib39/util/sound/SoundUtil.java @@ -0,0 +1,14 @@ +package top.r3944realms.lib39.util.sound; + +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.entity.LivingEntity; + +public class SoundUtil { + /** + * 为实体播放声音 + */ + public static void playSoundForEntity(LivingEntity entity, SoundEvent soundEvent, SoundSource soundCategory, float volume, float pitch) { + entity.level().playSound(null, entity.getX(), entity.getY(), entity.getZ(), soundEvent, soundCategory, volume, pitch); + } +}