我已经尝试 "等待1秒 "在minecraft锻造和我使用 "线程 "来做到这一点.但我得到这个错误。
Exception in thread "Thread-14" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:218)
at io.github.bloodnighttw.WaitAndReplaceBlock.run(WaitAndReplaceBlock.java:22)
我已经尝试使用 world.scheduleBlockUpdate(......)
但在我打破块之后,任何事情都没有发生。
这是我的代码,有三个类。
ExampleMod.java
package io.github.bloodnighttw;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.apache.logging.log4j.Logger;
import io.github.bloodnighttw.event.Event;
@Mod(modid = ExampleMod.MODID, name = ExampleMod.NAME, version = ExampleMod.VERSION)
public class ExampleMod
{
public static final String MODID = "nothing";
public static final String NAME = "Bang!";
public static final String VERSION = "3.0";
private static Logger logger;
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
logger = event.getModLog();
logger.info("Bang");
}
@EventHandler
public void init(FMLInitializationEvent event)
{
// some example code
logger.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
MinecraftForge.EVENT_BUS.register(new Event());
}
}
Event.java
package io.github.bloodnighttw.event;
import io.github.bloodnighttw.WaitAndReplaceBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class Event {
@SubscribeEvent
public void breakBlockEvent(final BreakEvent e) {
e.getPlayer().sendMessage(new TextComponentString(e.getState().toString()+"222222"));
BlockPos a = e.getPos();
new WaitAndReplaceBlock(a,e).start();
}
}
WaitAndReplaceBlock.java
package io.github.bloodnighttw;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent;
public class WaitAndReplaceBlock extends Thread{
BlockPos a;
BlockEvent.BreakEvent e ;
public WaitAndReplaceBlock(BlockPos a, BlockEvent.BreakEvent e){
this.a = a ;
this.e = e ;
}
@Override
public void run() {
try {
wait(10);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
World world = e.getWorld();
world.setBlockState(a, Blocks.BEDROCK.getDefaultState());
}
}
我试着在谷歌上搜索,但搜索到的所有内容都太老了,无法在1.12.2中使用,或者对我来说不是一个解决方案,所以我决定在stackoverflow中询问。
编辑。这是我使用的代码 world.scheduleBlockUpdate(......)
只有 Event.java
有一些变化。
Event.java
包io.github.bloodnighttw.event。
import io.github.bloodnighttw.WaitAndReplaceBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class Event {
@SubscribeEvent
public void breakBlockEvent(final BreakEvent e) {
e.getPlayer().sendMessage(new TextComponentString(e.getState().toString()+"222222"));
BlockPos a = e.getPos();
//new WaitAndReplaceBlock(a,e).start();
e.getWorld().scheduleBlockUpdate(a,e.getState().getBlock(),10,1000000);
}
}
这就是我说的 "什么都没发生".视频。https:/youtu.betZMIRHDUnV4。