From 21e742435a6d86e8ae2fc8fdd7c191c21f6bc1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Tue, 19 Jan 2021 11:37:20 +0100 Subject: [PATCH] Give block module a cooldown. --- .../li/cil/oc2/api/bus/device/Device.java | 2 +- .../item/BlockOperationsModuleDevice.java | 50 +++++++++++++++++++ .../item/ByteBufferFlashMemoryVMDevice.java | 2 +- .../common/bus/device/item/MemoryDevice.java | 10 ++-- .../item/RedstoneInterfaceCardItemDevice.java | 11 ++-- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/main/java/li/cil/oc2/api/bus/device/Device.java b/src/main/java/li/cil/oc2/api/bus/device/Device.java index 1299e880..a6da78a0 100644 --- a/src/main/java/li/cil/oc2/api/bus/device/Device.java +++ b/src/main/java/li/cil/oc2/api/bus/device/Device.java @@ -22,6 +22,6 @@ public interface Device extends INBTSerializable { } @Override - default void deserializeNBT(final CompoundNBT nbt) { + default void deserializeNBT(final CompoundNBT tag) { } } diff --git a/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java b/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java index 1435ca43..88ae7717 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java @@ -8,6 +8,7 @@ import li.cil.oc2.api.bus.device.rpc.RPCDevice; import li.cil.oc2.api.bus.device.rpc.RPCMethod; import li.cil.oc2.api.capabilities.Robot; import li.cil.oc2.common.Config; +import li.cil.oc2.common.Constants; import li.cil.oc2.common.bus.device.util.IdentityProxy; import li.cil.oc2.common.tags.ItemTags; import li.cil.oc2.common.util.FakePlayerUtils; @@ -16,12 +17,14 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.*; +import net.minecraft.nbt.CompoundNBT; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; import net.minecraft.util.Direction; import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.world.GameType; import net.minecraft.world.World; @@ -34,15 +37,24 @@ import javax.annotation.Nullable; import java.util.List; public final class BlockOperationsModuleDevice extends IdentityProxy implements RPCDevice, ItemDevice { + private static final String LAST_OPERATION_TAG_NAME = "cooldown"; + + private static final int COOLDOWN = Constants.TICK_SECONDS; + + /////////////////////////////////////////////////////////////////// + public enum PlacementDirection { FRONT, UP, DOWN, } + /////////////////////////////////////////////////////////////////// + private final Entity entity; private final Robot robot; private final ObjectDevice device; + private long lastOperation; /////////////////////////////////////////////////////////////////// @@ -55,6 +67,18 @@ public final class BlockOperationsModuleDevice extends IdentityProxy /////////////////////////////////////////////////////////////////// + @Override + public CompoundNBT serializeNBT() { + final CompoundNBT tag = new CompoundNBT(); + tag.putLong(LAST_OPERATION_TAG_NAME, lastOperation); + return tag; + } + + @Override + public void deserializeNBT(final CompoundNBT tag) { + lastOperation = MathHelper.clamp(tag.getLong(LAST_OPERATION_TAG_NAME), 0, entity.getEntityWorld().getGameTime()); + } + @Override public List getTypeNames() { return device.getTypeNames(); @@ -67,6 +91,12 @@ public final class BlockOperationsModuleDevice extends IdentityProxy @Callback public boolean excavate(@Parameter("direction") @Nullable final PlacementDirection direction) { + if (isOnCooldown()) { + return false; + } + + beginCooldown(); + final World world = entity.getEntityWorld(); if (!(world instanceof ServerWorld)) { return false; @@ -95,6 +125,12 @@ public final class BlockOperationsModuleDevice extends IdentityProxy @Callback public boolean place(@Parameter("direction") @Nullable final PlacementDirection direction) { + if (isOnCooldown()) { + return false; + } + + beginCooldown(); + final World world = entity.getEntityWorld(); if (!(world instanceof ServerWorld)) { return false; @@ -140,6 +176,12 @@ public final class BlockOperationsModuleDevice extends IdentityProxy @Callback public boolean repair() { + if (isOnCooldown()) { + return false; + } + + beginCooldown(); + if (identity.getDamage() == 0) { return false; } @@ -171,6 +213,14 @@ public final class BlockOperationsModuleDevice extends IdentityProxy /////////////////////////////////////////////////////////////////// + private void beginCooldown() { + lastOperation = entity.getEntityWorld().getGameTime(); + } + + private boolean isOnCooldown() { + return entity.getEntityWorld().getGameTime() - lastOperation < COOLDOWN; + } + private Direction getAdjustedDirection(@Nullable final PlacementDirection placementDirection) { if (placementDirection == PlacementDirection.UP) { return Direction.UP; diff --git a/src/main/java/li/cil/oc2/common/bus/device/item/ByteBufferFlashMemoryVMDevice.java b/src/main/java/li/cil/oc2/common/bus/device/item/ByteBufferFlashMemoryVMDevice.java index 7b7bd6eb..8d6b137a 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/item/ByteBufferFlashMemoryVMDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/item/ByteBufferFlashMemoryVMDevice.java @@ -86,7 +86,7 @@ public final class ByteBufferFlashMemoryVMDevice extends IdentityProxy implements VMDe } @Override - public void deserializeNBT(final CompoundNBT nbt) { - if (nbt.hasUniqueId(BLOB_HANDLE_TAG_NAME)) { - blobHandle = nbt.getUniqueId(BLOB_HANDLE_TAG_NAME); + public void deserializeNBT(final CompoundNBT tag) { + if (tag.hasUniqueId(BLOB_HANDLE_TAG_NAME)) { + blobHandle = tag.getUniqueId(BLOB_HANDLE_TAG_NAME); } - if (nbt.contains(ADDRESS_TAG_NAME, NBTTagIds.TAG_LONG)) { - address.set(nbt.getLong(ADDRESS_TAG_NAME)); + if (tag.contains(ADDRESS_TAG_NAME, NBTTagIds.TAG_LONG)) { + address.set(tag.getLong(ADDRESS_TAG_NAME)); } } diff --git a/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java b/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java index d206583d..6fd98215 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java @@ -29,7 +29,6 @@ import javax.annotation.Nullable; import java.util.List; public final class RedstoneInterfaceCardItemDevice extends IdentityProxy implements RPCDevice, DocumentedDevice, ItemDevice, ICapabilityProvider { - private static final String OUTPUT_TAG_NAME = "output"; private static final String GET_REDSTONE_INPUT = "getRedstoneInput"; @@ -73,14 +72,14 @@ public final class RedstoneInterfaceCardItemDevice extends IdentityProxy