添加方块相关的注册Builder和形状、声音工具类

This commit is contained in:
叁玖领域 2025-10-14 17:20:53 +08:00
parent 38e52ac38a
commit bb3097e68c
6 changed files with 140 additions and 3 deletions

View File

@ -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

View File

@ -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<RegistryObject<Block>, ResourceKey<CreativeModeTab>[]> itemAddMap = new ConcurrentHashMap<>();
private static final Map<ResourceKey<CreativeModeTab>, List<RegistryObject<Block>>> tabToItemsMap = new ConcurrentHashMap<>();
@SafeVarargs
public static void addItemToTabs(RegistryObject<Block> item, ResourceKey<CreativeModeTab>... tabs) {
itemAddMap.put(item, tabs);
// 更新反向映射
for (ResourceKey<CreativeModeTab> tab : tabs) {
tabToItemsMap.computeIfAbsent(tab, k -> new ArrayList<>()).add(item);
}
}
@SubscribeEvent
public static void onBuildCreativeTabContents(BuildCreativeModeTabContentsEvent event) {
List<RegistryObject<Block>> itemsForTab = tabToItemsMap.get(event.getTabKey());
if (itemsForTab != null) {
itemsForTab.forEach(event::accept);
}
}
public static Map<RegistryObject<Block>, ResourceKey<CreativeModeTab>[]> getItemAddMap() {
return itemAddMap;
}
}
}

View File

@ -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(

View File

@ -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<Block> blockObject;
/**
* 创建新的构建器实例
*/
public static BlockRegistryBuilder create() {
return new BlockRegistryBuilder();
}
/**
* 设置注册名称
*/
public BlockRegistryBuilder withName(String name) {
this.registryName = name;
return this;
}
/**
* 注册方块不自动注册物品
*/
public BlockRegistryBuilder registerBlock(DeferredRegister<Block> blockRegister, Supplier<? extends Block> blockSupplier) {
this.blockObject = blockRegister.register(this.registryName, blockSupplier);
return this;
}
/**
* 内部方法注册对应的方块物品
*/
@SafeVarargs
private void registerBlockItem(RegistryObject<Block> blockObject, ResourceKey<CreativeModeTab>... creativeTabs) {
CommonHandler.Mod.addItemToTabs(blockObject, creativeTabs);
}
/**
* 注册方块和物品到建筑标签页
*/
public BlockRegistryBuilder registerWithBuildingTab(DeferredRegister<Block> blockRegister, Supplier<? extends Block> blockSupplier) {
registerBlock(blockRegister, blockSupplier);
registerBlockItem(this.blockObject, CreativeModeTabs.BUILDING_BLOCKS);
return this;
}
/**
* 注册方块和物品到功能标签页
*/
public BlockRegistryBuilder registerWithFunctionalTab(DeferredRegister<Block> blockRegister, Supplier<? extends Block> blockSupplier) {
registerBlock(blockRegister, blockSupplier);
registerBlockItem(this.blockObject, CreativeModeTabs.FUNCTIONAL_BLOCKS);
return this;
}
/**
* 获取注册的方块对象
*/
public RegistryObject<Block> build() {
return this.blockObject;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}