本帖最后由 Hrtzee 于 2021-8-25 14:08 编辑
如题,我的点击事件想只能点击一次,就给我的"ManaCrystal"添加的Turning默认设置为false,在第一次点击之后凭借if的判断和State的改变限制点击次数,但是如下代码执行之后state.setValue(TURNING,true); 并没有让Turning变成true,但是其他代码正常执行,导致仍然不能令点击事件限制次数为一,所以应该是改本方块State的代码错了。请大佬赐教如果要改变这个方块的State要怎么设置?或者是不该State要怎么实现我的思路?
另:不知道worldIn.addParticle(ParticleTypes.HAPPY_VILLAGER, pos.getX(), pos.getY(), pos.getZ(), 0, 5.0E-4D, 0);粒子效果是怎么实现的,在游戏中就是没有出现,也望大佬再赐教。
代码已附如下
@Nonnull
@Override
public ActionResultType use(@Nonnull BlockState state, World worldIn, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand handIn, @Nonnull BlockRayTraceResult hit) {
if (!worldIn.isClientSide && player.getItemInHand(Hand.MAIN_HAND).getItem() == ItemRegistry.jade.get() && !worldIn.getBlockState(pos).getValue(TURNING)) {
ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
itemStack.shrink(1);
state.setValue(TURNING,true);
if (worldIn.getBlockState(pos).getValue(WATERLOGGED)) {
worldIn.addParticle(ParticleTypes.HAPPY_VILLAGER, pos.getX(), pos.getY(), pos.getZ(), 0, 5.0E-4D, 0);
new Object() {
private int ticks = 0;
private float waitTicks;
public void start(int waitTicks) {
this.waitTicks = waitTicks;
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void tick(TickEvent.ServerTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
this.ticks += 1;
if (this.ticks >= this.waitTicks)
run();
}
}
private void run() {
worldIn.setBlock(pos, BlockRegistry.manaRubikCube.get().defaultBlockState(), 3);
MinecraftForge.EVENT_BUS.unregister(this);
}
}.start((int) 60);
}
}
return ActionResultType.SUCCESS;
}
如题,我的点击事件想只能点击一次,就给我的"ManaCrystal"添加的Turning默认设置为false,在第一次点击之后凭借if的判断和State的改变限制点击次数,但是如下代码执行之后state.setValue(TURNING,true); 并没有让Turning变成true,但是其他代码正常执行,导致仍然不能令点击事件限制次数为一,所以应该是改本方块State的代码错了。请大佬赐教如果要改变这个方块的State要怎么设置?或者是不该State要怎么实现我的思路?
另:不知道worldIn.addParticle(ParticleTypes.HAPPY_VILLAGER, pos.getX(), pos.getY(), pos.getZ(), 0, 5.0E-4D, 0);粒子效果是怎么实现的,在游戏中就是没有出现,也望大佬再赐教。
代码已附如下
@Nonnull
@Override
public ActionResultType use(@Nonnull BlockState state, World worldIn, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand handIn, @Nonnull BlockRayTraceResult hit) {
if (!worldIn.isClientSide && player.getItemInHand(Hand.MAIN_HAND).getItem() == ItemRegistry.jade.get() && !worldIn.getBlockState(pos).getValue(TURNING)) {
ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
itemStack.shrink(1);
state.setValue(TURNING,true);
if (worldIn.getBlockState(pos).getValue(WATERLOGGED)) {
worldIn.addParticle(ParticleTypes.HAPPY_VILLAGER, pos.getX(), pos.getY(), pos.getZ(), 0, 5.0E-4D, 0);
new Object() {
private int ticks = 0;
private float waitTicks;
public void start(int waitTicks) {
this.waitTicks = waitTicks;
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void tick(TickEvent.ServerTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
this.ticks += 1;
if (this.ticks >= this.waitTicks)
run();
}
}
private void run() {
worldIn.setBlock(pos, BlockRegistry.manaRubikCube.get().defaultBlockState(), 3);
MinecraftForge.EVENT_BUS.unregister(this);
}
}.start((int) 60);
}
}
return ActionResultType.SUCCESS;
}
state.setValue(TURNING,true); 并没有让Turning变成true
因为你没有把 state.setValue 的结果重新传递回世界中去。一个特定的 BlockState 只有在世界中时才有意义,否则就只是一个容器。
- world.setBlockState(pos, state.setValue(TURNING,true));
是的,实际上BlockState是方块所有状态的集合,而不是当前方块的状态,BlockState会在方块初始化时就计算好