本帖最后由 IMENDERDRAGON 于 2017-7-9 13:07 编辑
比如说我要在X:50至X:100之间从X:50开始每1秒放置一个方块,然后就是如下代码:worldIn, y, z变量是在上面的事件里定义的
int i = 50;
while(i <= 100){
i++;
try{
Thread.sleep(1000);
worldIn.setBlockStste(new BlockPos(i, y, z), 那一个方块.getDefaultState(), 3);
}
catch(Exception e){
System.out.println(e);
}
}
可是为什么必须得循环结束后放置?
求解答。。
比如说我要在X:50至X:100之间从X:50开始每1秒放置一个方块,然后就是如下代码:worldIn, y, z变量是在上面的事件里定义的
int i = 50;
while(i <= 100){
i++;
try{
Thread.sleep(1000);
worldIn.setBlockStste(new BlockPos(i, y, z), 那一个方块.getDefaultState(), 3);
}
catch(Exception e){
System.out.println(e);
}
}
可是为什么必须得循环结束后放置?
求解答。。
Thread.sleep()是卡服
请使用BukkitRunnable
请使用BukkitRunnable
强行sleep最为致命。
建议楼主使用简单易用的BukkitRunnable,另外除非是整个服务器的定时任务(比如开一个监视线程)这类的最好别自己对主线程操作,反正你这么写服务器肯定是要卡1秒的。
建议楼主使用简单易用的BukkitRunnable,另外除非是整个服务器的定时任务(比如开一个监视线程)这类的最好别自己对主线程操作,反正你这么写服务器肯定是要卡1秒的。
还好只卡了50秒(卡60秒服务器就崩溃了)
凋灵兔子 发表于 2017-7-10 23:36
强行sleep最为致命。
建议楼主使用简单易用的BukkitRunnable,另外除非是整个服务器的定时任务(比如开一个 ...
有没有是使用forge的?
IMENDERDRAGON 发表于 2017-7-14 08:15
有没有是使用forge的?
是不是原理都一样,卡线程了,单独schedule就好了,这样异步操作主线程我记得forge有方法提供的,不然无法操作
直接sleep会堵住主线程
这种情况最好使用内置的线程池。bukkit是BukkitRunnable。
这种情况最好使用内置的线程池。bukkit是BukkitRunnable。
csj3120 发表于 2017-7-14 08:46
是不是原理都一样,卡线程了,单独schedule就好了,这样异步操作主线程我记得forge有方法提供的,不然无 ...
好吧,谢谢
很棒的作品!
本帖最后由 IMENDERDRAGON 于 2017-8-9 22:44 编辑
我现在遇到一个问题,不知道你能不能解决
用循环批量生成自己做的实体时,会报java.util.ConcurrentModificationException错 错误报告:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at com.google.common.collect.Iterators$3.next(Iterators.java:163)
at net.minecraft.world.chunk.Chunk.getEntitiesWithinAABBForEntity(Chunk.java:951)
at net.minecraft.world.World.getEntitiesInAABBexcluding(World.java:3200)
at net.minecraft.world.World.getEntitiesWithinAABBExcludingEntity(World.java:3180)
at net.minecraft.world.World.getCollisionBoxes(World.java:1465)
at net.minecraft.entity.Entity.move(Entity.java:812)
at 那个实体的类.onUpdate(EntityBlockEntity.java:94)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2115)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:876)
at net.minecraft.world.World.updateEntity(World.java:2082)
at net.minecraft.world.World.updateEntities(World.java:1895)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:648)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:795)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:699)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:548)
at java.lang.Thread.run(Unknown Source)
x00494 发表于 2017-7-18 07:44
很棒的作品!
我现在遇到一个问题,不知道你能不能解决
用循环批量生成自己做的实体时,会报java.util.ConcurrentModificationException错 错误报告:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at com.google.common.collect.Iterators$3.next(Iterators.java:163)
at net.minecraft.world.chunk.Chunk.getEntitiesWithinAABBForEntity(Chunk.java:951)
at net.minecraft.world.World.getEntitiesInAABBexcluding(World.java:3200)
at net.minecraft.world.World.getEntitiesWithinAABBExcludingEntity(World.java:3180)
at net.minecraft.world.World.getCollisionBoxes(World.java:1465)
at net.minecraft.entity.Entity.move(Entity.java:812)
at 那个实体的类.onUpdate(EntityBlockEntity.java:94)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2115)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:876)
at net.minecraft.world.World.updateEntity(World.java:2082)
at net.minecraft.world.World.updateEntities(World.java:1895)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:648)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:795)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:699)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:548)
at java.lang.Thread.run(Unknown Source)
IMENDERDRAGON 发表于 2017-8-9 16:08
我现在遇到一个问题,不知道你能不能解决
用循环批量生成自己做的实体时,会报java.util.ConcurrentModifi ...
自行百度这个Exception
因为你阻塞了主线程啊,你的客户端当然只能在循环结束才能看到
显然你应该是用后台线程,每1秒钟通知主线程一次更新
显然你应该是用后台线程,每1秒钟通知主线程一次更新
BukkitRunnable已经过时了,请使用Runnable