From 65ee43fd11b77343a60db0ff16ae140f7c2f6968 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 10 Sep 2021 11:31:45 +0700 Subject: [PATCH] Machine redstone controls --- .../mc/otm/block/BlockMattery.java | 10 ++++ .../entity/BlockEntityAndroidStation.java | 3 ++ .../block/entity/BlockEntityBatteryBank.java | 3 ++ .../entity/BlockEntityMatterBottler.java | 8 +++ .../otm/block/entity/BlockEntityMattery.java | 50 ++++++++++++++++++- .../mc/otm/block/entity/RedstoneSetting.java | 31 ++++++++++++ .../worker/BlockEntityMatteryWorker.java | 16 ++++++ .../overdrive_that_matters/lang/en_us.json | 8 +++ 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ru/dbotthepony/mc/otm/block/entity/RedstoneSetting.java diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/BlockMattery.java b/src/main/java/ru/dbotthepony/mc/otm/block/BlockMattery.java index 61254d04e..f54390e9c 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/BlockMattery.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/BlockMattery.java @@ -54,4 +54,14 @@ public abstract class BlockMattery extends Block { return super.use(p_60503_, level, pos, ply, p_60507_, p_60508_); } + + @Override + @SuppressWarnings("deprecation") + public void neighborChanged(BlockState state, Level level, BlockPos pos, Block sender, BlockPos sender_pos, boolean flag) { + super.neighborChanged(state, level, pos, sender, sender_pos, flag); + + if (this instanceof EntityBlock && !level.isClientSide && level.getBlockEntity(pos) instanceof BlockEntityMattery tile) { + tile.setRedstoneSignal(level.getBestNeighborSignal(pos)); + } + } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java index 3e21f1156..53a0c322d 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityAndroidStation.java @@ -50,6 +50,9 @@ public class BlockEntityAndroidStation extends BlockEntityMatteryPowered impleme if (t instanceof BlockEntityAndroidStation tile) { tile.batteryChargeLoop(); + if (tile.isBlockedByRedstone()) + return; + BlockPos pos = tile.getBlockPos(); List entities = tile.getLevel().getEntitiesOfClass(LivingEntity.class, new AABB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 2, pos.getZ() + 1)); diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java index df7e79c0f..368487805 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityBatteryBank.java @@ -317,6 +317,9 @@ public class BlockEntityBatteryBank extends BlockEntityMattery { public static void tick(Level level, BlockPos blockPos, BlockState blockState, T t) { if (t instanceof BlockEntityBatteryBank tile) { + if (tile.isBlockedByRedstone()) + return; + BlockEntity get_entity = level.getBlockEntity(blockPos.offset(tile.getBlockState().getValue(BlockMatteryRotatable.FACING).getNormal())); if (get_entity != null) { diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java index e2a99343e..2eefcc93f 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMatterBottler.java @@ -231,6 +231,14 @@ public class BlockEntityMatterBottler extends BlockEntityMatteryPowered implemen if (t instanceof BlockEntityMatterBottler tile) { tile.batteryChargeLoop(); + if (tile.isBlockedByRedstone()) { + if (tile.getBlockState().getValue(WorkerState.SEMI_WORKER_STATE) != WorkerState.IDLE) { + level.setBlock(tile.getBlockPos(), tile.getBlockState().setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE), Block.UPDATE_CLIENTS); + } + + return; + } + ItemStack work_stack = null; IMatterHandler capability = null; int align = tile.work_flow ? 0 : 3; diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMattery.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMattery.java index 49b506817..02ec1f3ba 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMattery.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityMattery.java @@ -2,6 +2,7 @@ package ru.dbotthepony.mc.otm.block.entity; import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.core.BlockPos; +import net.minecraft.nbt.ByteTag; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.StringTag; import net.minecraft.network.chat.Component; @@ -24,6 +25,8 @@ import java.util.UUID; @ParametersAreNonnullByDefault public abstract class BlockEntityMattery extends BlockEntity implements MenuProvider { protected Component display_name; + protected RedstoneSetting redstone_setting = RedstoneSetting.LOW; + protected int redstone_signal = 0; public BlockEntityMattery(BlockEntityType p_155228_, BlockPos p_155229_, BlockState p_155230_) { super(p_155228_, p_155229_, p_155230_); @@ -44,7 +47,45 @@ public abstract class BlockEntityMattery extends BlockEntity implements MenuProv @Override abstract public AbstractContainerMenu createMenu(int containerID, Inventory inventory, Player ply); - public boolean canOpenMachine(Player ply) { + protected void redstoneStatusUpdated(boolean new_blocked, boolean old_blocked) { + + } + + public void setRedstoneSignal(int level) { + final var old = isBlockedByRedstone(); + redstone_signal = level; + final var state = isBlockedByRedstone(); + + if (old != state) { + redstoneStatusUpdated(state, old); + } + } + + public void setRedstoneSetting(RedstoneSetting setting) { + final var old = isBlockedByRedstone(); + redstone_setting = setting; + final var state = isBlockedByRedstone(); + + if (old != state) { + redstoneStatusUpdated(state, old); + } + } + + public RedstoneSetting getRedstoneSetting() { + return redstone_setting; + } + + protected boolean isBlockedByRedstone() { + switch (redstone_setting) { + case LOW -> { + return redstone_signal > 0; + } + + case HIGH -> { + return redstone_signal <= 0; + } + } + return false; } @@ -53,13 +94,18 @@ public abstract class BlockEntityMattery extends BlockEntity implements MenuProv if (display_name != null) nbt.putString("Name", Component.Serializer.toJson(display_name)); + nbt.putByte("redstone", (byte) redstone_setting.ordinal()); + return super.save(nbt); } public void load(CompoundTag nbt) { super.load(nbt); - if (nbt.contains("Name") && nbt.get("Name") instanceof StringTag tag) + if (nbt.get("Name") instanceof StringTag tag) display_name = Component.Serializer.fromJson(tag.getAsString()); + + if (nbt.get("redstone") instanceof ByteTag tag) + redstone_setting = RedstoneSetting.get(tag.getAsByte()); } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/RedstoneSetting.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/RedstoneSetting.java new file mode 100644 index 000000000..1732c294d --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/RedstoneSetting.java @@ -0,0 +1,31 @@ +package ru.dbotthepony.mc.otm.block.entity; + +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TranslatableComponent; + +public enum RedstoneSetting { + IGNORED(new TranslatableComponent("otm.gui.redstone.ignored"), new TranslatableComponent("otm.gui.redstone.ignored.description")), + LOW(new TranslatableComponent("otm.gui.redstone.low"), new TranslatableComponent("otm.gui.redstone.low.description")), + HIGH(new TranslatableComponent("otm.gui.redstone.high"), new TranslatableComponent("otm.gui.redstone.high.description")); + + public final Component name; + public final Component description; + private static final RedstoneSetting[] values = RedstoneSetting.values(); + + RedstoneSetting(TranslatableComponent name, TranslatableComponent description) { + this.name = name; + this.description = description; + } + + public RedstoneSetting next() { + return values[(ordinal() + 1) % values.length]; + } + + public static RedstoneSetting get(int index) { + if (index < 0 || index > 2) { + return LOW; + } + + return values[index]; + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/worker/BlockEntityMatteryWorker.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/worker/BlockEntityMatteryWorker.java index e83864da5..bc12c2d4b 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/worker/BlockEntityMatteryWorker.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/worker/BlockEntityMatteryWorker.java @@ -95,6 +95,12 @@ abstract public class BlockEntityMatteryWorker extends BlockEntityMatteryPowered private int working_ticks_anim = 0; private int error_ticks_anim = 0; + @Override + protected void redstoneStatusUpdated(boolean new_blocked, boolean old_blocked) { + super.redstoneStatusUpdated(new_blocked, old_blocked); + is_idling = new_blocked; + } + protected void workerLoop() { if (level != null && error_ticks_anim > 20 && getBlockState().hasProperty(WorkerState.WORKER_STATE) && getBlockState().getValue(WorkerState.WORKER_STATE) != WorkerState.ERROR) { level.setBlock(getBlockPos(), getBlockState().setValue(WorkerState.WORKER_STATE, WorkerState.ERROR), Block.UPDATE_CLIENTS); @@ -123,6 +129,11 @@ abstract public class BlockEntityMatteryWorker extends BlockEntityMatteryPowered } if (current_job == null) { + if (isBlockedByRedstone()) { + is_idling = true; + return; + } + MachineJob input = getNextJob(); if (input == null) { @@ -134,6 +145,11 @@ abstract public class BlockEntityMatteryWorker extends BlockEntityMatteryPowered current_job = input; } + if (isBlockedByRedstone()) { + is_idling = true; + return; + } + if (current_job.power_consumption_multiplier().compareTo(BigDecimal.ZERO) != 0 && energy.getBatteryLevel().compareTo(BigDecimal.ZERO) == 0) { idle_ticks_anim++; diff --git a/src/main/resources/assets/overdrive_that_matters/lang/en_us.json b/src/main/resources/assets/overdrive_that_matters/lang/en_us.json index 678124e8b..c6661cd6a 100644 --- a/src/main/resources/assets/overdrive_that_matters/lang/en_us.json +++ b/src/main/resources/assets/overdrive_that_matters/lang/en_us.json @@ -20,6 +20,14 @@ "otm.gui.pattern.percentage_level": "Fill level: %s%%", "otm.gui.pattern.format": "Stored patterns: %s / %s", + "otm.gui.redstone.ignored": "Redstone mode: Ignored", + "otm.gui.redstone.low": "Redstone mode: Low", + "otm.gui.redstone.high": "Redstone mode: High", + + "otm.gui.redstone.ignored.description": "Redstone signal does not affect machine's function", + "otm.gui.redstone.low.description": "Machine work if no redstone signal is present", + "otm.gui.redstone.high.description": "Machine work only if any redstone signal is present", + "otm.death_reason": "Decommissioned!", "otm.item.power.infinite.storage": "Stored energy: Infinity / Infinity",