diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/config/IToolConfiguration.java b/common/src/main/java/fr/rakambda/fallingtree/common/config/IToolConfiguration.java index 57164e03..cce45eeb 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/config/IToolConfiguration.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/config/IToolConfiguration.java @@ -2,6 +2,7 @@ import fr.rakambda.fallingtree.common.FallingTreeCommon; import fr.rakambda.fallingtree.common.config.enums.DamageRounding; +import fr.rakambda.fallingtree.common.config.enums.DurabilityMode; import fr.rakambda.fallingtree.common.wrapper.IItem; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -13,7 +14,7 @@ public interface IToolConfiguration{ @NotNull Collection getAllowedItems(@NotNull FallingTreeCommon common); - boolean isPreserve(); + DurabilityMode getDurabilityMode(); boolean isIgnoreTools(); diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/config/enums/DurabilityMode.java b/common/src/main/java/fr/rakambda/fallingtree/common/config/enums/DurabilityMode.java new file mode 100644 index 00000000..50a8afa2 --- /dev/null +++ b/common/src/main/java/fr/rakambda/fallingtree/common/config/enums/DurabilityMode.java @@ -0,0 +1,21 @@ +package fr.rakambda.fallingtree.common.config.enums; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import java.util.function.BiFunction; + +@Getter +@RequiredArgsConstructor +public enum DurabilityMode{ + ABORT(true, (breakCount, breakableCount) -> breakCount <= breakableCount ? -1 : breakCount), + SAVE(true, (breakCount, breakableCount) -> breakCount <= breakableCount ? breakCount - 1 : breakCount), + NORMAL(true, (breakCount, breakableCount) -> breakCount), + BYPASS(false, (breakCount, breakableCount) -> breakableCount); + + private final boolean allowAbort; + private final BiFunction postProcessor; + + public int postProcess(int breakCount, int breakableCount){ + return postProcessor.apply(breakCount, breakableCount); + } +} diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/config/proxy/ToolProxyConfiguration.java b/common/src/main/java/fr/rakambda/fallingtree/common/config/proxy/ToolProxyConfiguration.java index d06f6365..edc5f1f2 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/config/proxy/ToolProxyConfiguration.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/config/proxy/ToolProxyConfiguration.java @@ -1,6 +1,7 @@ package fr.rakambda.fallingtree.common.config.proxy; import fr.rakambda.fallingtree.common.FallingTreeCommon; +import fr.rakambda.fallingtree.common.config.enums.DurabilityMode; import fr.rakambda.fallingtree.common.wrapper.IItem; import fr.rakambda.fallingtree.common.config.IResettable; import fr.rakambda.fallingtree.common.config.IToolConfiguration; @@ -39,8 +40,8 @@ public Collection getAllowedItems(@NotNull FallingTreeCommon mod){ } @Override - public boolean isPreserve(){ - return delegate.isPreserve(); + public DurabilityMode getDurabilityMode(){ + return delegate.getDurabilityMode(); } @Override diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/config/real/ToolConfiguration.java b/common/src/main/java/fr/rakambda/fallingtree/common/config/real/ToolConfiguration.java index 945161ab..c3f108b1 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/config/real/ToolConfiguration.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/config/real/ToolConfiguration.java @@ -2,6 +2,7 @@ import com.google.gson.annotations.Expose; import fr.rakambda.fallingtree.common.FallingTreeCommon; +import fr.rakambda.fallingtree.common.config.enums.DurabilityMode; import fr.rakambda.fallingtree.common.wrapper.IItem; import fr.rakambda.fallingtree.common.config.IResettable; import fr.rakambda.fallingtree.common.config.IToolConfiguration; @@ -23,7 +24,7 @@ public class ToolConfiguration implements IToolConfiguration, IResettable{ @NotNull private List denied = new ArrayList<>(); @Expose - private boolean preserve = false; + private DurabilityMode durabilityMode = DurabilityMode.NORMAL; @Expose private boolean ignoreTools = false; @Expose diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/FallingAnimationTreeBreakingHandler.java b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/FallingAnimationTreeBreakingHandler.java index 581f69be..cfcbeb3e 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/FallingAnimationTreeBreakingHandler.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/FallingAnimationTreeBreakingHandler.java @@ -29,13 +29,13 @@ public boolean breakTree(@NotNull IPlayer player, @NotNull Tree tree) throws Bre var level = tree.getLevel(); var toolHandler = new ToolDamageHandler(tool, mod.getConfiguration().getTools().getDamageMultiplicand(), - mod.getConfiguration().getTools().isPreserve(), + mod.getConfiguration().getTools().getDurabilityMode(), tree.getBreakableCount(), mod.getConfiguration().getTrees().getMaxSize(), mod.getConfiguration().getTrees().getMaxSizeAction(), mod.getConfiguration().getTools().getDamageRounding()); - if(mod.getConfiguration().getTools().isPreserve() && toolHandler.getMaxBreakCount() <= 0){ + if(toolHandler.isPreserveTool()){ log.debug("Didn't break tree at {} as {}'s tool was about to break", tree.getHitPos(), player); mod.notifyPlayer(player, mod.translate("chat.fallingtree.prevented_break_tool")); return false; diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/InstantaneousTreeBreakingHandler.java b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/InstantaneousTreeBreakingHandler.java index 23c038f4..079ee3d7 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/InstantaneousTreeBreakingHandler.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/InstantaneousTreeBreakingHandler.java @@ -23,13 +23,13 @@ public boolean breakTree(@NotNull IPlayer player, @NotNull Tree tree) throws Bre var level = tree.getLevel(); var toolHandler = new ToolDamageHandler(tool, mod.getConfiguration().getTools().getDamageMultiplicand(), - mod.getConfiguration().getTools().isPreserve(), + mod.getConfiguration().getTools().getDurabilityMode(), tree.getBreakableCount(), mod.getConfiguration().getTrees().getMaxSize(), mod.getConfiguration().getTrees().getMaxSizeAction(), mod.getConfiguration().getTools().getDamageRounding()); - if(mod.getConfiguration().getTools().isPreserve() && toolHandler.getMaxBreakCount() <= 0){ + if(toolHandler.isPreserveTool()){ log.debug("Didn't break tree at {} as {}'s tool was about to break", tree.getHitPos(), player); mod.notifyPlayer(player, mod.translate("chat.fallingtree.prevented_break_tool")); return false; diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java index 975d51a8..327bc016 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ShiftDownTreeBreakingHandler.java @@ -47,13 +47,13 @@ private boolean breakElements(@NotNull Tree tree, @NotNull ILevel level, @NotNul var damageMultiplicand = mod.getConfiguration().getTools().getDamageMultiplicand(); var toolHandler = new ToolDamageHandler(tool, damageMultiplicand, - mod.getConfiguration().getTools().isPreserve(), + mod.getConfiguration().getTools().getDurabilityMode(), count, mod.getConfiguration().getTrees().getMaxSize(), mod.getConfiguration().getTrees().getMaxSizeAction(), mod.getConfiguration().getTools().getDamageRounding()); - if(toolHandler.getMaxBreakCount() <= 0){ + if(toolHandler.isPreserveTool()){ log.debug("Didn't break tree at {} as {}'s tool was about to break", tree.getHitPos(), player); mod.notifyPlayer(player, mod.translate("chat.fallingtree.prevented_break_tool")); return false; diff --git a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ToolDamageHandler.java b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ToolDamageHandler.java index 70e289f1..21afbfd1 100644 --- a/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ToolDamageHandler.java +++ b/common/src/main/java/fr/rakambda/fallingtree/common/tree/breaking/ToolDamageHandler.java @@ -1,6 +1,7 @@ package fr.rakambda.fallingtree.common.tree.breaking; import fr.rakambda.fallingtree.common.config.enums.DamageRounding; +import fr.rakambda.fallingtree.common.config.enums.DurabilityMode; import fr.rakambda.fallingtree.common.config.enums.MaxSizeAction; import fr.rakambda.fallingtree.common.wrapper.IItemStack; import lombok.Getter; @@ -15,8 +16,10 @@ public class ToolDamageHandler{ private final int maxDurabilityTaken; @Getter private final int maxBreakCount; + @Getter + private boolean preserveTool; - public ToolDamageHandler(@NotNull IItemStack tool, double damageMultiplicand, boolean preserve, int breakableCount, int maxSize, @NotNull MaxSizeAction maxSizeAction, @NotNull DamageRounding damageRounding) throws BreakTreeTooBigException{ + public ToolDamageHandler(@NotNull IItemStack tool, double damageMultiplicand, @NotNull DurabilityMode durabilityMode, int breakableCount, int maxSize, @NotNull MaxSizeAction maxSizeAction, @NotNull DamageRounding damageRounding) throws BreakTreeTooBigException{ this.tool = tool; this.damageMultiplicand = damageMultiplicand; this.damageRounding = damageRounding; @@ -29,16 +32,16 @@ public ToolDamageHandler(@NotNull IItemStack tool, double damageMultiplicand, bo int tempMaxBreakCount; if(tool.isDamageable()){ var breakCount = damageMultiplicand == 0 ? maxSize : (int) Math.floor(getToolDurability() / damageMultiplicand); - if(preserve && breakCount <= breakableCount){ - breakCount--; - } - + breakCount = durabilityMode.postProcess(breakCount, breakableCount); tempMaxBreakCount = breakCount; } else{ tempMaxBreakCount = maxSize; } + preserveTool = tempMaxBreakCount < 0; + tempMaxBreakCount = Math.max(0, tempMaxBreakCount); + maxBreakCount = Math.min(maxSize, tempMaxBreakCount); maxDurabilityTaken = getDamage(maxBreakCount); } diff --git a/common/src/main/resources/assets/fallingtree/lang/en_us.json b/common/src/main/resources/assets/fallingtree/lang/en_us.json index 530db16d..7bfbe364 100644 --- a/common/src/main/resources/assets/fallingtree/lang/en_us.json +++ b/common/src/main/resources/assets/fallingtree/lang/en_us.json @@ -66,8 +66,8 @@ "text.autoconfig.fallingtree.option.tools.allowed.@Tooltip": "Additional list of tools that can be used to chop down a tree.\n\nINFO: Items marked with the axe tag will already be allowed.", "text.autoconfig.fallingtree.option.tools.denied": "Denied tools", "text.autoconfig.fallingtree.option.tools.denied.@Tooltip": "List of tools that should not be considered as tools.\n\nINFO: This wins over the allow list.", - "text.autoconfig.fallingtree.option.tools.preserve": "Preserve tools", - "text.autoconfig.fallingtree.option.tools.preserve.@Tooltip": "When set to true, when a tree is broken and the tool is about to break we will just break enough blocks so that the tool is left at 1 of durability.", + "text.autoconfig.fallingtree.option.tools.durabilityMode": "Durability mode", + "text.autoconfig.fallingtree.option.tools.durabilityMode.@Tooltip": "Defines how tool durability is handled when it is about to break.\n\nABORT: Will abort tree breaking, tool will be saved and nothing is broken.\nSAVE: The most blocks will be broken, leaving 1 durability. If tool has 1 durability it'll break.\nNORMAL: Will break the most blocks possible, breaking the tool.\nBYPASS: Will break the whole tree regardless of durability left. Tool will break as a result.", "text.autoconfig.fallingtree.option.tools.ignoreTools": "Ignore tool", "text.autoconfig.fallingtree.option.tools.ignoreTools.@Tooltip": "When set to true, the mod will be activated no matter what you have in your hand (or empty hand).\n\nINFO: Deny list still can be use to restrict some tools.", "text.autoconfig.fallingtree.option.tools.damageMultiplicand": "Damage multiplicand", diff --git a/fabric/src/client/java/fr/rakambda/fallingtree/fabric/client/cloth/ClothConfigHook.java b/fabric/src/client/java/fr/rakambda/fallingtree/fabric/client/cloth/ClothConfigHook.java index c238991e..22f81c72 100644 --- a/fabric/src/client/java/fr/rakambda/fallingtree/fabric/client/cloth/ClothConfigHook.java +++ b/fabric/src/client/java/fr/rakambda/fallingtree/fabric/client/cloth/ClothConfigHook.java @@ -7,6 +7,7 @@ import fr.rakambda.fallingtree.common.config.enums.BreakOrder; import fr.rakambda.fallingtree.common.config.enums.DamageRounding; import fr.rakambda.fallingtree.common.config.enums.DetectionMode; +import fr.rakambda.fallingtree.common.config.enums.DurabilityMode; import fr.rakambda.fallingtree.common.config.enums.MaxSizeAction; import fr.rakambda.fallingtree.common.config.enums.NotificationMode; import fr.rakambda.fallingtree.common.config.enums.SneakMode; @@ -317,11 +318,11 @@ private void fillToolsConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tool .setTooltip(getTooltips("tools", "speedMultiplicand")) .setSaveConsumer(config::setSpeedMultiplicand) .build(); - var preserveEntry = builder.entryBuilder() - .startBooleanToggle(translatable(getFieldName("tools", "preserve")), config.isPreserve()) - .setDefaultValue(false) - .setTooltip(getTooltips("tools", "preserve")) - .setSaveConsumer(config::setPreserve) + var durabilityModeEntry = builder.entryBuilder() + .startEnumSelector(translatable(getFieldName("tools", "durabilityMode")), DurabilityMode.class, config.getDurabilityMode()) + .setDefaultValue(DurabilityMode.NORMAL) + .setTooltip(getTooltips("tools", "durabilityMode")) + .setSaveConsumer(config::setDurabilityMode) .build(); var forceToolUsageEntry = builder.entryBuilder() .startBooleanToggle(translatable(getFieldName("tools", "forceToolUsage")), config.isForceToolUsage()) @@ -337,7 +338,7 @@ private void fillToolsConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tool tools.addEntry(damageMultiplicandEntry); tools.addEntry(damageRoundingEntry); tools.addEntry(speedMultiplicandEntry); - tools.addEntry(preserveEntry); + tools.addEntry(durabilityModeEntry); tools.addEntry(forceToolUsageEntry); } diff --git a/neoforge/src/main/java/fr/rakambda/fallingtree/neoforge/client/cloth/ClothConfigHook.java b/neoforge/src/main/java/fr/rakambda/fallingtree/neoforge/client/cloth/ClothConfigHook.java index ab7af58e..e0385110 100644 --- a/neoforge/src/main/java/fr/rakambda/fallingtree/neoforge/client/cloth/ClothConfigHook.java +++ b/neoforge/src/main/java/fr/rakambda/fallingtree/neoforge/client/cloth/ClothConfigHook.java @@ -7,6 +7,7 @@ import fr.rakambda.fallingtree.common.config.enums.BreakOrder; import fr.rakambda.fallingtree.common.config.enums.DamageRounding; import fr.rakambda.fallingtree.common.config.enums.DetectionMode; +import fr.rakambda.fallingtree.common.config.enums.DurabilityMode; import fr.rakambda.fallingtree.common.config.enums.MaxSizeAction; import fr.rakambda.fallingtree.common.config.enums.NotificationMode; import fr.rakambda.fallingtree.common.config.enums.SneakMode; @@ -317,11 +318,11 @@ private void fillToolsConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tool .setTooltip(getTooltips("tools", "speedMultiplicand")) .setSaveConsumer(config::setSpeedMultiplicand) .build(); - var preserveEntry = builder.entryBuilder() - .startBooleanToggle(translatable(getFieldName("tools", "preserve")), config.isPreserve()) - .setDefaultValue(false) - .setTooltip(getTooltips("tools", "preserve")) - .setSaveConsumer(config::setPreserve) + var durabilityModeEntry = builder.entryBuilder() + .startEnumSelector(translatable(getFieldName("tools", "durabilityMode")), DurabilityMode.class, config.getDurabilityMode()) + .setDefaultValue(DurabilityMode.NORMAL) + .setTooltip(getTooltips("tools", "durabilityMode")) + .setSaveConsumer(config::setDurabilityMode) .build(); var forceToolUsageEntry = builder.entryBuilder() .startBooleanToggle(translatable(getFieldName("tools", "forceToolUsage")), config.isForceToolUsage()) @@ -337,7 +338,7 @@ private void fillToolsConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tool tools.addEntry(damageMultiplicandEntry); tools.addEntry(damageRoundingEntry); tools.addEntry(speedMultiplicandEntry); - tools.addEntry(preserveEntry); + tools.addEntry(durabilityModeEntry); tools.addEntry(forceToolUsageEntry); }