User-friendly update scheduling

This commit is contained in:
jglrxavpok 2020-05-01 21:54:01 +02:00
parent feae9f1cd3
commit 08a32c4492
3 changed files with 41 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.ChunkUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.Dimension;
import java.io.File;
@ -353,4 +354,14 @@ public abstract class Instance implements BlockModifier, DataContainer {
return entities != null ? entities : new CopyOnWriteArraySet<>();
}
/**
* Schedule a block update at a given position.
* Does nothing if no custom block is present at 'position'.
* Cancelled if the block changes between this call and the actual update
* @param time in how long this update must be performed?
* @param unit in what unit is the time expressed
* @param position the location of the block to update
*/
public abstract void scheduleUpdate(int time, TimeUnit unit, BlockPosition position);
}

View File

@ -1,6 +1,7 @@
package net.minestom.server.instance;
import io.netty.buffer.ByteBuf;
import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerBlockBreakEvent;
@ -14,10 +15,13 @@ import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.network.packet.server.play.UnloadChunkPacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.particle.ParticleCreator;
import net.minestom.server.timer.TaskRunnable;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.ChunkUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.SerializerUtils;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.world.Dimension;
import java.io.File;
@ -376,4 +380,24 @@ public class InstanceContainer extends Instance {
chunk.sendPacketToViewers(blockChangePacket);
}
@Override
public void scheduleUpdate(int time, TimeUnit unit, BlockPosition position) {
Instance instance = this;
CustomBlock toUpdate = getCustomBlock(position);
if(toUpdate == null) {
return;
}
MinecraftServer.getSchedulerManager().addDelayedTask(new TaskRunnable() {
@Override
public void run() {
CustomBlock currentBlock = instance.getCustomBlock(position);
if(currentBlock == null)
return;
if(currentBlock.getCustomBlockId() != toUpdate.getCustomBlockId()) { // block changed
return;
}
currentBlock.update(instance, position, getBlockData(position));
}
}, new UpdateOption(time, unit));
}
}

View File

@ -6,6 +6,7 @@ import net.minestom.server.instance.batch.BlockBatch;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.TimeUnit;
import java.io.File;
import java.util.Collection;
@ -154,6 +155,11 @@ public class SharedInstance extends Instance {
instanceContainer.setSeparateBlocks(x, y, z, blockId, customBlockId, data);
}
@Override
public void scheduleUpdate(int time, TimeUnit unit, BlockPosition position) {
instanceContainer.scheduleUpdate(time, unit, position);
}
public InstanceContainer getInstanceContainer() {
return instanceContainer;
}