版本信息
你使用的系统:<windows 64>
你是用的JDK: <1.8.0_251>
你使用的IDE:
你使用的IDE版本:<IntelliJ IDEA 2020.1.2 (Community Edition)>
Forge版本: < 1.15.2-31.2.0>
Minecraft版本: <1.15.2>
出错图
错误情况简述
为方块绑定了方块实体,并希望在右键点击方块时能触发一些事件,但并未生效,经过测试发现onBlockActivated()方法没有被运行.
报错日志
没有找到报ERROR的地方
相关代码
<TestMachine.java>
package com.svanur.ripple_tech.block.machines;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import javax.annotation.Nullable;
public class TestMachine extends Block {
public TestMachine(Properties properties) {
super(Properties.create(Material.ROCK).hardnessAndResistance(5));
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Nullable
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return new TestTileEntity();
}
/*
玩家点击该方块时将会发送一条消息
*/
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
System.out.println("onBlockActivated11111111");
if (!worldIn.isRemote && handIn == Hand.MAIN_HAND) {
System.out.println("onBlockActivated222222");
TestTileEntity testTileEntity = (TestTileEntity) worldIn.getTileEntity(pos);
//获取到消息
String say = testTileEntity.say();
//发送消息
TranslationTextComponent textComponents = new TranslationTextComponent("message.rippletech.test", say);
player.sendStatusMessage(textComponents, false);
}
return ActionResultType.SUCCESS;
}
}
<TestTileEntity.java>
package com.svanur.ripple_tech.block.machines;
import net.minecraft.tileentity.TileEntity;
public class TestTileEntity extends TileEntity {
public TestTileEntity() {
super(TileEntityTypeRegistry.testTileEntity.get());
}
public String say() {
return "你好,我是测试方块";
}
}
<TileEntityTypeRegistry.java>
package com.svanur.ripple_tech.block.machines;
import com.svanur.ripple_tech.block.BlockRegistry;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
public class TileEntityTypeRegistry {
public static final DeferredRegister<TileEntityType<?>> TILE_ENTITY_TYPE_DEFERRED_REGISTER
= new DeferredRegister<>(ForgeRegistries.TILE_ENTITIES, "rippletech");
public static RegistryObject<TileEntityType<TestTileEntity>> testTileEntity = TILE_ENTITY_TYPE_DEFERRED_REGISTER.register("test_tileentity", () -> {
return TileEntityType.Builder.create(
() -> {
return new TestTileEntity();
}, BlockRegistry.testMachines.get()
).build(null);
});
}
<RippleTech.java(此为mod的主类)>
package com.svanur.ripple_tech;
import com.svanur.ripple_tech.block.BlockRegistry;
import com.svanur.ripple_tech.block.machines.TileEntityTypeRegistry;
import com.svanur.ripple_tech.item.ItemRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod("rippletech")
public class RippleTech {
public RippleTech() {
ItemRegistry.ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
BlockRegistry.BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus());
TileEntityTypeRegistry.TILE_ENTITY_TYPE_DEFERRED_REGISTER.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}
查到的资料
根据教程https://neutrino.v2mcdev.com/tileentity/firsttileentity.html所写