Updated multiple files

This commit is contained in:
Németh Noel 2021-06-30 01:54:11 +02:00
parent 25183973dc
commit aaa3b91424
4 changed files with 20 additions and 21 deletions

View File

@ -20,13 +20,13 @@ import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.time.Cooldown; import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.biomes.Biome; import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.time.Duration;
import java.util.Set; import java.util.Set;
/** /**
@ -143,10 +143,10 @@ public class DynamicChunk extends Chunk {
final CustomBlock customBlock = getCustomBlock(index); final CustomBlock customBlock = getCustomBlock(index);
// Update cooldown // Update cooldown
final UpdateOption updateOption = customBlock.getUpdateOption(); final Duration updateFrequency = customBlock.getUpdateFrequency();
if (updateOption != null) { if (updateFrequency != null) {
final long lastUpdate = updatableBlocksLastUpdate.get(index); final long lastUpdate = updatableBlocksLastUpdate.get(index);
final boolean hasCooldown = Cooldown.hasCooldown(time, lastUpdate, updateOption); final boolean hasCooldown = Cooldown.hasCooldown(time, lastUpdate, updateFrequency);
if (hasCooldown) if (hasCooldown)
continue; continue;

View File

@ -14,12 +14,12 @@ import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.server.play.BlockBreakAnimationPacket; import net.minestom.server.network.packet.server.play.BlockBreakAnimationPacket;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.time.Duration;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@ -67,12 +67,12 @@ public abstract class CustomBlock {
} }
/** /**
* Calling delay depends on {@link #getUpdateOption()} which should be overridden. * Calling delay depends on {@link #getUpdateFrequency()} which should be overridden.
* *
* @param instance the instance of the block * @param instance the instance of the block
* @param blockPosition the position of the block * @param blockPosition the position of the block
* @param data the data associated with the block * @param data the data associated with the block
* @throws UnsupportedOperationException if {@link #getUpdateOption()} * @throws UnsupportedOperationException if {@link #getUpdateFrequency()}
* is not null but the update method is not overridden * is not null but the update method is not overridden
*/ */
public void update(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data) { public void update(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data) {
@ -89,7 +89,7 @@ public abstract class CustomBlock {
* @return the update option of the block, null if not any * @return the update option of the block, null if not any
*/ */
@Nullable @Nullable
public UpdateOption getUpdateOption() { public Duration getUpdateFrequency() {
return null; return null;
} }
@ -172,14 +172,14 @@ public abstract class CustomBlock {
/** /**
* Gets if this {@link CustomBlock} requires any tick update. * Gets if this {@link CustomBlock} requires any tick update.
* *
* @return true if {@link #getUpdateOption()} is not null and the value is positive * @return true if {@link #getUpdateFrequency()} is not null and the value is positive
*/ */
public boolean hasUpdate() { public boolean hasUpdate() {
final UpdateOption updateOption = getUpdateOption(); final Duration updateFrequency = getUpdateFrequency();
if (updateOption == null) if (updateFrequency == null)
return false; return false;
return updateOption.getValue() > 0; return !updateFrequency.isZero();
} }

View File

@ -7,15 +7,15 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.time.Duration;
import java.util.Set; import java.util.Set;
public class CustomBlockSample extends CustomBlock { public class CustomBlockSample extends CustomBlock {
private static final UpdateOption UPDATE_OPTION = new UpdateOption(3, TimeUnit.TICK); private static final Duration DURATION = Duration.of(3, TimeUnit.SERVER_TICK);
public CustomBlockSample() { public CustomBlockSample() {
super(Block.GOLD_BLOCK, "custom_block"); super(Block.GOLD_BLOCK, "custom_block");
@ -45,10 +45,9 @@ public class CustomBlockSample extends CustomBlock {
//instance.refreshBlockStateId(blockPosition, (short) (blockId+1)); //instance.refreshBlockStateId(blockPosition, (short) (blockId+1));
} }
@Nullable
@Override @Override
public UpdateOption getUpdateOption() { public Duration getUpdateFrequency() {
return UPDATE_OPTION; return DURATION;
} }
@Override @Override

View File

@ -7,14 +7,14 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.util.Set; import java.util.Set;
public class UpdatableBlockDemo extends CustomBlock { public class UpdatableBlockDemo extends CustomBlock {
private static final UpdateOption UPDATE_OPTION = new UpdateOption(20, TimeUnit.TICK); private static final Duration DURATION = Duration.of(20, TimeUnit.SERVER_TICK);
public UpdatableBlockDemo() { public UpdatableBlockDemo() {
super(Block.DIRT, "updatable"); super(Block.DIRT, "updatable");
@ -41,8 +41,8 @@ public class UpdatableBlockDemo extends CustomBlock {
} }
@Override @Override
public UpdateOption getUpdateOption() { public Duration getUpdateFrequency() {
return UPDATE_OPTION; return DURATION;
} }
@Override @Override