Skip to content

Commit

Permalink
Machine Pausing (#2311)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungOnionMC authored Nov 12, 2024
1 parent 141f1a3 commit 02dfd15
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/generated/resources/assets/gtceu/lang/en_ud.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"behaviour.soft_hammer": "sǝuıɥɔɐW sǝʇɐʌıʇɔɐǝᗡ puɐ sǝʇɐʌıʇɔⱯ",
"behaviour.soft_hammer.disabled": "pǝןqɐsıᗡ buıʞɹoM",
"behaviour.soft_hammer.enabled": "pǝןqɐuƎ buıʞɹoM",
"behaviour.soft_hammer.idle_after_cycle": "ǝןɔʎɔ ʇuǝɹɹnɔ ɹǝʇɟɐ ǝuıɥɔɐɯ ǝsnɐԀ",
"behaviour.wrench": "ʞɔıןɔʇɥbıᴚ uo sʞɔoןᗺ sǝʇɐʇoᴚ",
"block.filter_casing.tooltip": "ʇuǝɯuoɹıʌuǝ ㄥ§ǝǝɹℲ-ǝןɔıʇɹɐԀɐ§ ɐ sǝʇɐǝɹƆ",
"block.gtceu.acid_hazard_sign_block": "ʞɔoןᗺ ubıS pɹɐzɐH pıɔⱯ",
Expand Down
1 change: 1 addition & 0 deletions src/generated/resources/assets/gtceu/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"behaviour.soft_hammer": "Activates and Deactivates Machines",
"behaviour.soft_hammer.disabled": "Working Disabled",
"behaviour.soft_hammer.enabled": "Working Enabled",
"behaviour.soft_hammer.idle_after_cycle": "Pause machine after current cycle",
"behaviour.wrench": "Rotates Blocks on Rightclick",
"block.filter_casing.tooltip": "Creates a §aParticle-Free§7 environment",
"block.gtceu.acid_hazard_sign_block": "Acid Hazard Sign Block",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface IControllable {
* @param isWorkingAllowed true if the workable can work, otherwise false
*/
void setWorkingEnabled(boolean isWorkingAllowed);

default void setSuspendAfterFinish(boolean suspendAfterFinish) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public class GTToolType {
.build();
public static final GTToolType SOFT_MALLET = GTToolType.builder("mallet")
.toolTag(TagUtil.createItemTag("tools/mallets", false))
.toolStats(b -> b.crafting().cannotAttack().attackSpeed(-2.4F))
.toolStats(b -> b.crafting().cannotAttack().attackSpeed(-2.4F).sneakBypassUse()
.behaviors(ToolModeSwitchBehavior.INSTANCE))
.sound(GTSoundEntries.SOFT_MALLET_TOOL)
.symbol('r')
.build();
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/gregtechceu/gtceu/api/machine/MetaMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,18 @@ protected InteractionResult onWrenchClick(Player playerIn, InteractionHand hand,
return InteractionResult.CONSUME;
var itemStack = playerIn.getItemInHand(hand);
var tagCompound = getBehaviorsTag(itemStack);
ToolModeSwitchBehavior.ModeType type = ToolModeSwitchBehavior.ModeType.values()[tagCompound
ToolModeSwitchBehavior.WrenchModeType type = ToolModeSwitchBehavior.WrenchModeType.values()[tagCompound
.getByte("Mode")];

if (type == ToolModeSwitchBehavior.ModeType.ITEM || type == ToolModeSwitchBehavior.ModeType.BOTH) {
if (type == ToolModeSwitchBehavior.WrenchModeType.ITEM ||
type == ToolModeSwitchBehavior.WrenchModeType.BOTH) {
if (this instanceof IAutoOutputItem autoOutputItem &&
(!hasFrontFacing() || gridSide != getFrontFacing())) {
autoOutputItem.setOutputFacingItems(gridSide);
}
}
if (type == ToolModeSwitchBehavior.ModeType.FLUID || type == ToolModeSwitchBehavior.ModeType.BOTH) {
if (type == ToolModeSwitchBehavior.WrenchModeType.FLUID ||
type == ToolModeSwitchBehavior.WrenchModeType.BOTH) {
if (this instanceof IAutoOutputFluid autoOutputFluid &&
(!hasFrontFacing() || gridSide != getFrontFacing())) {
autoOutputFluid.setOutputFacingFluids(gridSide);
Expand All @@ -407,9 +409,14 @@ protected InteractionResult onSoftMalletClick(Player playerIn, InteractionHand h
var controllable = GTCapabilityHelper.getControllable(getLevel(), getPos(), gridSide);
if (controllable != null) {
if (!isRemote()) {
controllable.setWorkingEnabled(!controllable.isWorkingEnabled());
playerIn.sendSystemMessage(Component.translatable(controllable.isWorkingEnabled() ?
"behaviour.soft_hammer.enabled" : "behaviour.soft_hammer.disabled"));
if (!playerIn.isShiftKeyDown() || !controllable.isWorkingEnabled()) {
controllable.setWorkingEnabled(!controllable.isWorkingEnabled());
playerIn.sendSystemMessage(Component.translatable(controllable.isWorkingEnabled() ?
"behaviour.soft_hammer.enabled" : "behaviour.soft_hammer.disabled"));
} else {
controllable.setSuspendAfterFinish(true);
playerIn.sendSystemMessage(Component.translatable("behaviour.soft_hammer.idle_after_cycle"));
}
}
playerIn.swing(hand);
return InteractionResult.CONSUME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ default void setWorkingEnabled(boolean isWorkingAllowed) {
getRecipeLogic().setWorkingEnabled(isWorkingAllowed);
}

@Override
default void setSuspendAfterFinish(boolean suspendAfterFinish) {
getRecipeLogic().setSuspendAfterFinish(suspendAfterFinish);
}

@Override
default int getProgress() {
return getRecipeLogic().getProgress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public enum Status {
@Persisted
@Getter
protected long totalContinuousRunningTime;
@Persisted
@Setter
protected boolean suspendAfterFinish = false;
@Getter
protected final Map<RecipeCapability<?>, Object2IntMap<?>> chanceCaches = makeChanceCaches();
protected TickableSubscription subscription;
Expand Down Expand Up @@ -473,13 +476,18 @@ public void onRecipeFinish() {
}
}
// try it again
if (!recipeDirty &&
if (!recipeDirty && !suspendAfterFinish &&
lastRecipe.matchRecipe(this.machine).isSuccess() &&
lastRecipe.matchTickRecipe(this.machine).isSuccess() &&
lastRecipe.checkConditions(this).isSuccess()) {
setupRecipe(lastRecipe);
} else {
setStatus(Status.IDLE);
if (suspendAfterFinish) {
setStatus(Status.SUSPEND);
suspendAfterFinish = false;
} else {
setStatus(Status.IDLE);
}
progress = 0;
duration = 0;
isActive = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gregtechceu.gtceu.common.item.tool.behavior;

import com.gregtechceu.gtceu.api.item.tool.GTToolType;
import com.gregtechceu.gtceu.api.item.tool.ToolHelper;
import com.gregtechceu.gtceu.api.item.tool.behavior.IToolBehavior;

import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -27,7 +29,10 @@ protected ToolModeSwitchBehavior() {}

@Override
public void addBehaviorNBT(@NotNull ItemStack stack, @NotNull CompoundTag tag) {
tag.putByte("Mode", (byte) ModeType.BOTH.ordinal());
var toolTypes = ToolHelper.getToolTypes(stack);
if (toolTypes.contains(GTToolType.WRENCH)) {
tag.putByte("Mode", (byte) WrenchModeType.BOTH.ordinal());
}
IToolBehavior.super.addBehaviorNBT(stack, tag);
}

Expand All @@ -37,10 +42,14 @@ public void addBehaviorNBT(@NotNull ItemStack stack, @NotNull CompoundTag tag) {
var itemStack = player.getItemInHand(hand);
var tagCompound = getBehaviorsTag(itemStack);
if (player.isShiftKeyDown()) {
tagCompound.putByte("Mode", (byte) ((tagCompound.getByte("Mode") + 1) % ModeType.values().length));

player.displayClientMessage(Component.translatable("metaitem.machine_configuration.mode",
ModeType.values()[tagCompound.getByte("Mode")].getName()), true);
var toolTypes = ToolHelper.getToolTypes(itemStack);
if (toolTypes.contains(GTToolType.WRENCH)) {
tagCompound.putByte("Mode",
(byte) ((tagCompound.getByte("Mode") + 1) % WrenchModeType.values().length));
player.displayClientMessage(Component.translatable("metaitem.machine_configuration.mode",
WrenchModeType.values()[tagCompound.getByte("Mode")].getName()), true);
}
return InteractionResultHolder.success(itemStack);
}

Expand All @@ -51,20 +60,24 @@ public void addBehaviorNBT(@NotNull ItemStack stack, @NotNull CompoundTag tag) {
public void addInformation(@NotNull ItemStack stack, @Nullable Level world, @NotNull List<Component> tooltip,
@NotNull TooltipFlag flag) {
var tagCompound = getBehaviorsTag(stack);
tooltip.add(Component.translatable("metaitem.machine_configuration.mode",
ModeType.values()[tagCompound.getByte("Mode")].getName()));

var toolTypes = ToolHelper.getToolTypes(stack);
if (toolTypes.contains(GTToolType.WRENCH)) {
tooltip.add(Component.translatable("metaitem.machine_configuration.mode",
WrenchModeType.values()[tagCompound.getByte("Mode")].getName()));
}
}

public static enum ModeType {
@Getter
public enum WrenchModeType {

ITEM(Component.translatable("gtceu.mode.item")),
FLUID(Component.translatable("gtceu.mode.fluid")),
BOTH(Component.translatable("gtceu.mode.both"));

@Getter
private final Component name;

private ModeType(Component name) {
WrenchModeType(Component name) {
this.name = name;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ public static void init(RegistrateLangProvider provider) {
provider.add("behaviour.soft_hammer", "Activates and Deactivates Machines");
provider.add("behaviour.soft_hammer.enabled", "Working Enabled");
provider.add("behaviour.soft_hammer.disabled", "Working Disabled");
provider.add("behaviour.soft_hammer.idle_after_cycle", "Pause machine after current cycle");
provider.add("behaviour.lighter.tooltip.description", "Can light things on fire");
provider.add("behaviour.lighter.tooltip.usage", "Shift-right click to open/close");
provider.add("behaviour.lighter.fluid.tooltip", "Can light things on fire with Butane or Propane");
Expand Down

0 comments on commit 02dfd15

Please sign in to comment.