跳到主要内容

37可投掷射弹

代码

增加一个股子方块

public class DiceBlock extends Block {
public static DirectionProperty FACING = DirectionProperty.of("number",
Direction.UP,
Direction.NORTH,
Direction.EAST,
Direction.SOUTH,
Direction.WEST,
Direction.DOWN);

public DiceBlock(Settings settings) {
super(settings);
}

@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return getRandomBlockState();
}

public BlockState getRandomBlockState() {
return this.getDefaultState().with(FACING, getRandomDirection());
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
}

private Direction getRandomDirection() {
Direction[] dirs = new Direction[] {
Direction.UP,
Direction.NORTH,
Direction.EAST,
Direction.SOUTH,
Direction.WEST,
Direction.DOWN
};

return dirs[Random.create().nextBetween(0, dirs.length-1)];
}
}

在ModBlocks中注册:

public static final Block DICE_BLOCK = Registry.register(Registries.BLOCK, new Identifier(TutorialMod.MOD_ID, "dice_block"),
new DiceBlock(FabricBlockSettings.copyOf(Blocks.STONE)));

增加一个股子项目实体:

public class DiceProjectileEntity extends ThrownItemEntity {
public DiceProjectileEntity(EntityType<? extends ThrownItemEntity> entityType, World world) {
super(entityType, world);
}

public DiceProjectileEntity(LivingEntity livingEntity, World world) {
super(ModEntities.DICE_PROJECTILE, livingEntity, world);
}

@Override
protected Item getDefaultItem() {
return ModItems.DICE;
}

@Override
public Packet<ClientPlayPacketListener> createSpawnPacket() {
return new EntitySpawnS2CPacket(this);
}

@Override
protected void onBlockHit(BlockHitResult blockHitResult) {
if(!this.getWorld().isClient()) {
this.getWorld().sendEntityStatus(this, (byte)3);
this.getWorld().setBlockState(getBlockPos(), ((DiceBlock) ModBlocks.DICE_BLOCK).getRandomBlockState(), 3);
}

this.discard();
super.onBlockHit(blockHitResult);
}
}

在ModEntities中注册:

public static final EntityType<DiceProjectileEntity> DICE_PROJECTILE = Registry.register(Registries.ENTITY_TYPE,
new Identifier(TutorialMod.MOD_ID, "dice_projectile"),
FabricEntityTypeBuilder.<DiceProjectileEntity>create(SpawnGroup.MISC, DiceProjectileEntity::new)
.dimensions(EntityDimensions.fixed(0.25f, 0.25f)).build());

增加自定义物品:

public class DiceItem extends Item {
public DiceItem(Settings settings) {
super(settings);
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack itemStack = user.getStackInHand(hand);
world.playSound(null, user.getX(), user.getY(), user.getZ(),
SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL, 0.5f, 0.4f / (world.getRandom().nextFloat() * 0.4f + 0.8f));

if (!world.isClient) {
DiceProjectileEntity diceProjectileEntity = new DiceProjectileEntity(user, world);
diceProjectileEntity.setItem(itemStack);
diceProjectileEntity.setVelocity(user, user.getPitch(), user.getYaw(), 0.0f, 1.5f, 1.0f);
world.spawnEntity(diceProjectileEntity);
}

user.incrementStat(Stats.USED.getOrCreateStat(this));
if (!user.getAbilities().creativeMode) {
itemStack.decrement(1);
}
return TypedActionResult.success(itemStack, world.isClient());
}
}

在ModItems中注册

public static final Item DICE = registerItem("dice", new DiceItem(new FabricItemSettings()));

添加到物品组ModItemGroups:

entries.add(ModItems.DICE);

添加代码生成:

itemModelGenerator.register(ModItems.DICE, Models.GENERATED);

主类加载:TutorialModClient

EntityRendererRegistry.register(ModEntities.DICE_PROJECTILE, FlyingItemEntityRenderer::new);

效果

image-20240825200805017