mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
feat(fabric): 优化方块注册和渲染逻辑
This commit is contained in:
@@ -26,7 +26,6 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.player.PlayerRegisterChannelEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -17,6 +17,7 @@ import net.minecraft.registry.Registries;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.world.biome.FoliageColors;
|
||||
import net.momirealms.craftengine.fabric.client.blocks.CustomBlock;
|
||||
import net.momirealms.craftengine.fabric.client.config.ModConfig;
|
||||
import net.momirealms.craftengine.fabric.client.network.CraftEnginePayload;
|
||||
|
||||
@@ -38,8 +39,10 @@ public class CraftEngineFabricModClient implements ClientModInitializer {
|
||||
public static void registerRenderLayer() {
|
||||
Registries.BLOCK.forEach(block -> {
|
||||
Identifier id = Registries.BLOCK.getId(block);
|
||||
if (id.getNamespace().equals(CraftEngineFabricModClient.MOD_ID)) {
|
||||
BlockRenderLayerMap.INSTANCE.putBlock(block, RenderLayer.getCutoutMipped());
|
||||
if (block instanceof CustomBlock customBlock) {
|
||||
if (customBlock.isTransparent()) {
|
||||
BlockRenderLayerMap.INSTANCE.putBlock(customBlock, RenderLayer.getCutoutMipped());
|
||||
}
|
||||
if (id.getPath().contains("leaves")) {
|
||||
registerColor(block);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package net.momirealms.craftengine.fabric.client.blocks;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.ShapeContext;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.shape.VoxelShape;
|
||||
import net.minecraft.world.BlockView;
|
||||
|
||||
public class CustomBlock extends Block {
|
||||
private final VoxelShape outlineShape;
|
||||
private final VoxelShape collisionShape;
|
||||
private final boolean isTransparent;
|
||||
|
||||
public CustomBlock(Settings settings, VoxelShape outlineShape, VoxelShape collisionShape, boolean isTransparent) {
|
||||
super(settings);
|
||||
this.outlineShape = outlineShape;
|
||||
this.collisionShape = collisionShape;
|
||||
this.isTransparent = isTransparent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||
return this.outlineShape;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||
return this.collisionShape;
|
||||
}
|
||||
|
||||
public boolean isTransparent() {
|
||||
return this.isTransparent;
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,8 @@ public class CraftEngineFabricMod implements ModInitializer {
|
||||
RegisterBlocks.register(
|
||||
replacedBlockId.getPath() + "_" + i,
|
||||
BlockUtils.canPassThrough(blockState),
|
||||
BlockUtils.getShape(blockState)
|
||||
BlockUtils.getShape(blockState),
|
||||
BlockUtils.isTransparent(blockState)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ package net.momirealms.craftengine.fabric.util;
|
||||
import net.minecraft.block.AbstractBlock;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.TransparentBlock;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.shape.VoxelShape;
|
||||
import net.minecraft.util.shape.VoxelShapes;
|
||||
import net.minecraft.world.BlockView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -64,4 +64,13 @@ public class BlockUtils {
|
||||
return VoxelShapes.fullCube();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isTransparent(BlockState state) {
|
||||
if (state == null) return true;
|
||||
Block block = state.getBlock();
|
||||
if (block instanceof TransparentBlock) {
|
||||
return true;
|
||||
}
|
||||
return !state.isOpaque();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,24 +2,21 @@ package net.momirealms.craftengine.fabric.util;
|
||||
|
||||
import net.minecraft.block.AbstractBlock;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.ShapeContext;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.registry.RegistryKey;
|
||||
import net.minecraft.registry.RegistryKeys;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.shape.VoxelShape;
|
||||
import net.minecraft.util.shape.VoxelShapes;
|
||||
import net.minecraft.world.BlockView;
|
||||
import net.momirealms.craftengine.fabric.CraftEngineFabricMod;
|
||||
import net.momirealms.craftengine.fabric.client.blocks.CustomBlock;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class RegisterBlocks {
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public static Block register(String name, boolean canPassThrough, VoxelShape outlineShape) {
|
||||
public static Block register(String name, boolean canPassThrough, VoxelShape outlineShape, boolean isTransparent) {
|
||||
AbstractBlock.Settings settings = Block.Settings.create().nonOpaque().strength(-1.0F, 3600000.0F);
|
||||
VoxelShape collisionShape;
|
||||
if (canPassThrough) {
|
||||
@@ -28,7 +25,7 @@ public class RegisterBlocks {
|
||||
} else {
|
||||
collisionShape = outlineShape;
|
||||
}
|
||||
return register(name, (settingsParam) -> new CustomBlock(settingsParam, outlineShape, collisionShape), settings);
|
||||
return register(name, (settingsParam) -> new CustomBlock(settingsParam, outlineShape, collisionShape, isTransparent), settings);
|
||||
}
|
||||
|
||||
public static Block register(String name, Function<AbstractBlock.Settings, Block> blockFactory, AbstractBlock.Settings settings) {
|
||||
@@ -44,23 +41,3 @@ public class RegisterBlocks {
|
||||
|
||||
}
|
||||
|
||||
class CustomBlock extends Block {
|
||||
private final VoxelShape outlineShape;
|
||||
private final VoxelShape collisionShape;
|
||||
|
||||
public CustomBlock(Settings settings, VoxelShape outlineShape, VoxelShape collisionShape) {
|
||||
super(settings);
|
||||
this.outlineShape = outlineShape;
|
||||
this.collisionShape = collisionShape;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||
return this.outlineShape;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||
return this.collisionShape;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user