我为 fabric 1.18.1 创建了一个块,但它根本没有出现在游戏中



我在IntelliJ中创建了一个块,并运行了客户端,游戏没有出现任何错误,但该块不在库存或命令中。

这个区块的想法是创造一堆像泡菜一样工作的金币,你可以在一个区块上放置多个。

请查看我的代码,帮助我理解为什么它不会出现。

这是我对区块本身的代码:

`package net.runic.runicscurrencymod.block.custom;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.entity.ai.pathing.NavigationType;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable;
import java.util.Random;
public class GoldCoinPile extends Block {
public GoldCoinPile(Settings settings) {
super(settings);
this.setDefaultState((BlockState)((BlockState)((BlockState)this.stateManager.getDefaultState()).with(PILES, 1)).with(WATERLOGGED, true));
}
public static final int MAX_PILES = 4;
public static final IntProperty PILES = net.runic.runicscurrencymod.state.Properties.PILES;
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
protected static final VoxelShape ONE_PILE_SHAPE = Block.createCuboidShape(6.0, 0.0, 6.0, 10.0, 6.0, 10.0);
protected static final VoxelShape TWO_PILES_SHAPE = Block.createCuboidShape(3.0, 0.0, 3.0, 13.0, 6.0, 13.0);
protected static final VoxelShape THREE_PILES_SHAPE = Block.createCuboidShape(2.0, 0.0, 2.0, 14.0, 6.0, 14.0);
protected static final VoxelShape FOUR_PILES_SHAPE = Block.createCuboidShape(2.0, 0.0, 2.0, 14.0, 7.0, 14.0);

@Override
@Nullable
public BlockState getPlacementState(ItemPlacementContext ctx) {
BlockState blockState = ctx.getWorld().getBlockState(ctx.getBlockPos());
if (blockState.isOf(this)) {
return (BlockState)blockState.with(PILES, Math.min(4, blockState.get(PILES) + 1));
}
FluidState fluidState = ctx.getWorld().getFluidState(ctx.getBlockPos());
boolean bl = fluidState.getFluid() == Fluids.WATER;
return (BlockState)super.getPlacementState(ctx).with(WATERLOGGED, bl);
}
public static boolean isDry(BlockState state) {
return state.get(WATERLOGGED) == false;
}
@Override
public boolean canReplace(BlockState state, ItemPlacementContext context) {
if (!context.shouldCancelInteraction() && context.getStack().isOf(this.asItem()) && state.get(PILES) < 4) {
return true;
}
return super.canReplace(state, context);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
switch (state.get(PILES)) {
default: {
return ONE_PILE_SHAPE;
}
case 2: {
return TWO_PILES_SHAPE;
}
case 3: {
return THREE_PILES_SHAPE;
}
case 4: 
}
return FOUR_PILES_SHAPE;
}
@Override
public FluidState getFluidState(BlockState state) {
if (state.get(WATERLOGGED).booleanValue()) {
return Fluids.WATER.getStill(false);
}
return super.getFluidState(state);
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(PILES, WATERLOGGED);
}

@Override
public boolean canPathfindThrough(BlockState state, BlockView world, BlockPos pos, NavigationType type) {
return false;
}
}
`

我用Properties.java创建了piles属性,其中的代码是:

/*
* Decompiled with CFR 0.0.9 (FabricMC cc05e23f).
*/
package net.runic.runicscurrencymod.state;
import net.minecraft.block.enums.*;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.math.Direction;
public class Properties {
``` public static final IntProperty PILES = IntProperty.of(name:"piles",min:1,max:4);

在mod块文件中,我这样注册它:

public static final Block GOLD_COIN_PILE = registerBlock("gold_coin_pile",
new GoldCoinPile(FabricBlockSettings.of(Material.METAL).strength(6f)), ItemGroup.MISC); 

在blockstates文件夹中,我有一个gold_coin_pile.json,上面写着:

{
"variants": {
"piles=1": [
{
"model": "minecraft:block/gold_coin_pile"
},
{
"model": "minecraft:block/gold_coin_pile",
"y": 90
},
{
"model": "minecraft:block/gold_coin_pile",
"y": 180
},
{
"model": "minecraft:block/gold_coin_pile",
"y": 270
}
],
"piles=2": [
{
"model": "minecraft:block/two_gold_coin_piles"
},
{
"model": "minecraft:block/two_gold_coin_piles",
"y": 90
},
{
"model": "minecraft:block/two_gold_coin_piles",
"y": 180
},
{
"model": "minecraft:block/two_gold_coin_piles",
"y": 270
}
],
"piles=3": [
{
"model": "minecraft:block/three_gold_coin_piles"
},
{
"model": "minecraft:block/three_gold_coin_piles",
"y": 90
},
{
"model": "minecraft:block/three_gold_coin_piles",
"y": 180
},
{
"model": "minecraft:block/three_gold_coin_piles",
"y": 270
}
],
"piles=4": [
{
"model": "minecraft:block/four_gold_coin_piles"
},
{
"model": "minecraft:block/four_gold_coin_piles",
"y": 90
},
{
"model": "minecraft:block/four_gold_coin_piles",
"y": 180
},
{
"model": "minecraft:block/four_gold_coin_piles",
"y": 270
}
]
}
}

在实际的blocks文件夹中,我有四个json文件用于每个blockstates,但这里只有一个文件用于1堆(如果您需要所有4个lmk(

{
"parent": "block/block",
"textures": {
"particle": "block/gold_coin_pile",
"all": "block/gold_coin_pile"
},
"elements": [
{   "from": [ 6, 0, 6 ],
"to": [ 10, 6, 10 ],
"faces": {
"down":  { "uv": [  8, 1,  12, 5 ], "texture": "#all", "cullface": "down" },
"up":    { "uv": [  4, 1,  8, 5 ], "texture": "#all" },
"north": { "uv": [ 4, 5, 8, 11 ], "texture": "#all" },
"south": { "uv": [ 0, 5, 4, 11 ], "texture": "#all" },
"west":  { "uv": [ 8, 5, 12, 11 ], "texture": "#all" },
"east":  { "uv": [ 12, 5, 16, 11 ], "texture": "#all" }
}
},
{
"from": [ 6, 5.95, 6 ],
"to": [ 10, 5.95, 10 ],
"faces": {
"up": {"uv": [  8, 1,  12, 5 ], "texture": "#all"}
}
},
{
"from": [ 7.5, 5.2, 8 ],
"to": [ 8.5, 8.7, 8 ],
"rotation": { "origin": [ 8, 8, 8 ], "axis": "y", "angle": 45, "rescale": true },
"shade": false,
"faces": {
"north": { "uv": [ 1, 0, 3, 5 ], "texture": "#all" },
"south": { "uv": [ 3, 0, 1, 5 ], "texture": "#all" }
}
},
{
"from": [ 8, 5.2, 7.5 ],
"to": [ 8, 8.7, 8.5 ],
"rotation": { "origin": [ 8, 8, 8 ], "axis": "y", "angle": 45, "rescale": true },
"shade": false,
"faces": {
"west": { "uv": [ 13, 0, 15, 5 ], "texture": "#all" },
"east": { "uv": [ 15, 0, 13, 5 ], "texture": "#all" }
}
}
]
}

在en_us.json lang文件中,我有这样的文件:

"block.runicscurrencymod.gold_coin_pile": "Pile of Gold Coins", 

然后我有了纹理中的gold_coin_pile.png文件。

我不知道发生了什么,但它根本不会出现在比赛中。

您没有正确注册块。在初始化器类中添加此行,它应该可以工作。

Registry.register(Registry.BLOCK, new Identifier("example", "example_block"), EXAMPLE_BLOCK);

最新更新