Updated ItemEntity.java

This commit is contained in:
Németh Noel 2021-06-30 01:29:02 +02:00
parent 6db9ba63f4
commit 56017b90e2

View File

@ -10,10 +10,11 @@ import net.minestom.server.item.StackingRule;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.Set;
/**
@ -24,7 +25,7 @@ public class ItemEntity extends Entity {
/**
* Used to slow down the merge check delay
*/
private static UpdateOption mergeUpdateOption = new UpdateOption(10, TimeUnit.TICK);
private static Duration mergeDelay = Duration.of(10, TimeUnit.SERVER_TICK);
/**
* The last time that this item has checked his neighbors for merge
@ -60,8 +61,8 @@ public class ItemEntity extends Entity {
* @return the merge update option
*/
@Nullable
public static UpdateOption getMergeUpdateOption() {
return mergeUpdateOption;
public static Duration getMergeDelay() {
return mergeDelay;
}
/**
@ -69,15 +70,28 @@ public class ItemEntity extends Entity {
* Can be set to null to entirely remove the delay.
*
* @param mergeUpdateOption the new merge update option
*
* @deprecated Replaced by {@link #setMergeDelay(Duration)}
*/
public static void setMergeUpdateOption(@Nullable UpdateOption mergeUpdateOption) {
ItemEntity.mergeUpdateOption = mergeUpdateOption;
@SuppressWarnings("removal")
public static void setMergeUpdateOption(@Nullable net.minestom.server.utils.time.UpdateOption mergeUpdateOption) {
setMergeDelay(mergeUpdateOption == null ? null : mergeUpdateOption.toDuration());
}
/**
* Changes the merge delay.
* Can be set to null to entirely remove the delay.
*
* @param delay the new merge delay
*/
public static void setMergeDelay(@Nullable Duration delay) {
ItemEntity.mergeDelay = delay;
}
@Override
public void update(long time) {
if (isMergeable() && isPickable() &&
(mergeUpdateOption == null || !Cooldown.hasCooldown(time, lastMergeCheck, mergeUpdateOption))) {
(mergeDelay == null || !Cooldown.hasCooldown(time, lastMergeCheck, mergeDelay))) {
this.lastMergeCheck = time;
final Chunk chunk = instance.getChunkAt(getPosition());
@ -213,7 +227,7 @@ public class ItemEntity extends Entity {
}
/**
* Gets the pickup delay in milliseconds, defined by {@link #setPickupDelay(long, TimeUnit)}.
* Gets the pickup delay in milliseconds, defined by {@link #setPickupDelay(long, TemporalUnit)}.
*
* @return the pickup delay
*/
@ -225,10 +239,10 @@ public class ItemEntity extends Entity {
* Sets the pickup delay of the ItemEntity.
*
* @param delay the pickup delay
* @param timeUnit the unit of the delay
* @param temporalUnit the unit of the delay
*/
public void setPickupDelay(long delay, @NotNull TimeUnit timeUnit) {
this.pickupDelay = timeUnit.toMilliseconds(delay);
public void setPickupDelay(long delay, @NotNull TemporalUnit temporalUnit) {
this.pickupDelay = temporalUnit.getDuration().multipliedBy(delay).toMillis();
}
/**