概要
- 温泉を再現した流体を追加し、ポーションと合わせて様々な効能(ポーション効果)を与える流体を作る!
コード
- 今回は入ると再生の効果を与える再生の湯と入ったら幸運の効果を与える幸運の湯を作ります。
- 適宜変数名などは自分の好みで書き換えてください。
流体の追加
まずは流体を追加していきましょう
バケツと流体ブロックの作成
バケツ
package com.example.examplemod.mc_05_mysword;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.material.Fluid;
import java.util.function.Supplier;
public class HoneyBucket extends BucketItem {
public HoneyBucket(Supplier<? extends Fluid> fluidSupplier) {
super(fluidSupplier, new Item.Properties().tab(CreativeModeTab.TAB_MISC).stacksTo(1));
}
}
流体ブロック
package com.example.examplemod.mc_01_myblock;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.material.Material;
public class HoneyBlock extends LiquidBlock {
public HoneyBlock(java.util.function.Supplier<? extends FlowingFluid> pFluid){
super(pFluid, BlockBehaviour.Properties.of(Material.WATER));
}
}
Examplemod
まずマイクラ内での流体の描画の元となるテクスチャのResourceLocationを指定します。
public static final ResourceLocation WATER_STILL_RL=new ResourceLocation("block/water_still");
public static final ResourceLocation WATER_FLOWING_RL=new ResourceLocation("block/water_flow");
public static final ResourceLocation WATER_OVERLAY_RL=new ResourceLocation("block/water_overlay");
その後に水源となるFluidと流体となるFlowing、それらを制御するPropertiesを用意します。
public static FlowingFluid HONEY_FLUID;
public static FlowingFluid HONEY_FLOWING;
public static ForgeFlowingFluid.Properties HONEY_PROPERTIES;
先ほど作っておいたバケツと流体ブロックを登録します。
public static final Item HONEY_BUCKET=new HoneyBucket(
()->ExampleMod.HONEY_FLUID
).setRegistryName(MODID,"honey_bucket");
public static final Block HONEY_BLOCK=new HoneyBlock(
()->ExampleMod.HONEY_FLUID
).setRegistryName(MODID,"honey_block");
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public static class RegistryEvents {
private static final RegisterBlockData[] registerBlocks = {
// ここにBlockを書いてね!
new RegisterBlockData(HONEY_BLOCK)
};
private static final Item[] registerItems = {
// ここにItemを書いてね!
HONEY_BUCKET
};
最後に専用のレジストリに処理を書きます。
@SubscribeEvent
public static void onFlowingFluidsRegistry(final RegistryEvent.Register<Fluid> event){
ExampleMod.HONEY_PROPERTIES=new ForgeFlowingFluid.Properties(
()->ExampleMod.HONEY_FLUID,
()->ExampleMod.HONEY_FLOWING,
FluidAttributes.builder(ExampleMod.WATER_STILL_RL,ExampleMod.WATER_FLOWING_RL)
.density(100).luminosity(15).viscosity(2)
.sound(SoundEvents.HONEY_DRINK)
.overlay(ExampleMod.WATER_OVERLAY_RL)
.color(0xbffcba03)
//densityは密度なので動きやすさ?
//luminosity明るさなので光源になったりする?
//viscosityは粘度なので広がり具合が制御できそう。
//colorは色が変えられます。
).slopeFindDistance(2).levelDecreasePerBlock(2)
.block(()-> (LiquidBlock) ExampleMod.HONEY_BLOCK)
.bucket(()->ExampleMod.HONEY_BUCKET);
ExampleMod.HONEY_FLUID=new ForgeFlowingFluid.Source(ExampleMod.HONEY_PROPERTIES);
ExampleMod.HONEY_FLOWING=new ForgeFlowingFluid.Flowing(ExampleMod.HONEY_PROPERTIES);
ExampleMod.HONEY_FLUID.setRegistryName(MODID,"honey_fluid");
ExampleMod.HONEY_FLOWING.setRegistryName(MODID,"honey_flowing");
event.getRegistry().registerAll(ExampleMod.HONEY_FLUID,ExampleMod.HONEY_FLOWING);
}
テクスチャなどの登録
バケツのテクスチャを登録します
{
"parent": "item/generated",
"textures": {
"layer0": "examplemod:items/honey_bucket"
}
}
data/minecraft/tagsの編集
data/minecraft/tags/fluidsディレクトリ構造を作りwater.jsonを格納します。内容は以下です。
{
"replace": false,
"values": [
"examplemod:honey_flowing",
"examplemod:honey_fluid"
]
}
湯気が出てる感じにする
流体の追加ができたら、温泉っぽさを演出していきます。
LiquidBlockを継承したクラス内でanimateTickメソッドをオーバーライドしてパーティクルを出します。
@Override
public void animateTick(BlockState pState, Level pLevel, BlockPos pPos, Random pRand) {
if (pRand.nextInt(5)==0||pRand.nextInt(5)==4) {
pLevel.addParticle(ParticleTypes.CAMPFIRE_COSY_SMOKE, pPos.getX() , pPos.getY()+0.5D, pPos.getZ() , pRand.nextFloat() / 4.0F, pRand.nextFloat()/4.0F, pRand.nextFloat() / 4.0F);
}
}
流体ブロック内にいることを検知するイベントハンドラ
PlayerTickEventで監視します。
package com.example.examplemod.hotspring;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber
public class IsPlayerInHotSpring {
@SubscribeEvent
public static void onPlayerTick(TickEvent.PlayerTickEvent event){
Player player= event.player;
Level level=player.getLevel();
BlockPos blockPos=player.blockPosition();
BlockState blockState=level.getBlockState(blockPos);
if(blockState.getBlock() instanceof RegenerationWaterBlock){
player.addEffect(new MobEffectInstance(MobEffects.REGENERATION,500,1));
}
else if(blockState.getBlock() instanceof LuckWaterBlock){
player.addEffect(new MobEffectInstance(MobEffects.LUCK,500,1));
}
}
}