Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fine grained durability mode setting #728

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,7 @@ public interface IToolConfiguration{
@NotNull
Collection<IItem> getAllowedItems(@NotNull FallingTreeCommon<?> common);

boolean isPreserve();
DurabilityMode getDurabilityMode();

boolean isIgnoreTools();

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer, Integer> postProcessor;

public int postProcess(int breakCount, int breakableCount){
return postProcessor.apply(breakCount, breakableCount);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,8 +40,8 @@ public Collection<IItem> getAllowedItems(@NotNull FallingTreeCommon<?> mod){
}

@Override
public boolean isPreserve(){
return delegate.isPreserve();
public DurabilityMode getDurabilityMode(){
return delegate.getDurabilityMode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +24,7 @@ public class ToolConfiguration implements IToolConfiguration, IResettable{
@NotNull
private List<String> denied = new ArrayList<>();
@Expose
private boolean preserve = false;
private DurabilityMode durabilityMode = DurabilityMode.NORMAL;
@Expose
private boolean ignoreTools = false;
@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -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);
}

Expand Down
Loading