Add work penalty type

This commit is contained in:
alexcrea 2024-08-31 15:09:04 +02:00
parent 98172a11c9
commit 519ab1853c
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
4 changed files with 85 additions and 5 deletions

View file

@ -0,0 +1,50 @@
package xyz.alexcrea.cuanvil.config;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public enum WorkPenaltyType {
DEFAULT("default", true, true),
INCREASE("increase_only", true, false),
ADDITIVE("add_only", false, true),
DISABLED("disabled", false, false),
;
private final String name;
private final boolean penaltyIncrease;
private final boolean penaltyAdditive;
WorkPenaltyType(String name, boolean penaltyIncrease, boolean penaltyAdditive) {
this.name = name;
this.penaltyIncrease = penaltyIncrease;
this.penaltyAdditive = penaltyAdditive;
}
public boolean isPenaltyIncreasing() {
return penaltyIncrease;
}
public boolean isPenaltyAdditive() {
return penaltyAdditive;
}
private boolean doRepresentThisType(String toTest){
return name.equalsIgnoreCase(toTest);
}
@NotNull
public static WorkPenaltyType fromString(@Nullable String toTest){
if(toTest == null) return DEFAULT;
// Test if it matches any of values
for (WorkPenaltyType value : values()) {
if(value.doRepresentThisType(toTest)){
return value;
}
}
// Use default if not found
return DEFAULT;
}
}

View file

@ -3,6 +3,7 @@ package io.delilaheve.util
import io.delilaheve.CustomAnvil
import io.delilaheve.util.EnchantmentUtil.enchantmentName
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.config.WorkPenaltyType
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
/**
@ -33,6 +34,8 @@ object ConfigOptions {
const val PERMISSION_NEEDED_FOR_COLOR = "permission_needed_for_color"
const val USE_OF_COLOR_COST = "use_of_color_cost"
const val WORK_PENALTY_TYPE = "work_penalty_type"
private const val DEFAULT_LIMIT_PATH = "default_limit"
const val ENCHANT_LIMIT_ROOT = "enchant_limits"
@ -248,6 +251,17 @@ object ConfigOptions {
?: DEFAULT_USE_OF_COLOR_COST
}
/**
* How many xp should use of color should cost
*/
val workPenaltyType: WorkPenaltyType
get() {
return WorkPenaltyType.fromString(
ConfigHolder.DEFAULT_CONFIG
.config
.getString(WORK_PENALTY_TYPE));
}
/**
* Default enchantment limit
*/

View file

@ -85,16 +85,21 @@ object AnvilXpUtil {
fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack, unitRepair: Boolean): Int {
// Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation
// Calculate work penalty
val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0
val penaltyType = ConfigOptions.workPenaltyType;
val leftPenalty =
if(!penaltyType.isPenaltyAdditive) 0
else {
(left.itemMeta as? Repairable)?.repairCost ?: 0
}
val rightPenalty =
if (right == null) {
0
} else {
if (right == null || !penaltyType.isPenaltyAdditive) 0
else {
(right.itemMeta as? Repairable)?.repairCost ?: 0
}
// Increase penalty on fusing or unit repair
if(right != null || unitRepair){
if(penaltyType.isPenaltyIncreasing && (right != null || unitRepair)){
result.itemMeta?.let {
(it as? Repairable)?.repairCost = leftPenalty * 2 + 1
result.itemMeta = it

View file

@ -72,6 +72,17 @@ permission_needed_for_color: true
# Valid values include 0 to 1000.
use_of_color_cost: 0
# Every time an item is used on the anvil. item work penalty is added to the price and increase
# Vanilla work penalty formula can be represented as 2 * previous_penalty + 1. with start value equal to 0
# See https://minecraft.wiki/w/Anvil_mechanics#Anvil_uses for more detail
#
# Valid work penalty type is:
# - default: work penalty added and increased
# - increase_only: work penalty increased but not added
# - add_only: work penalty added but not increased
# - disabled: work penalty disabled
work_penalty_type: default
# Default limit to apply to any enchants missing from enchant_limits
#
# Valid values include 1 to 1000