Skip to content

Commit

Permalink
Combatify 1.2.0 1.20.5 Beta 4 - Armour stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandra-Myers committed Apr 7, 2024
1 parent 0a96f4e commit 85d9f8a
Show file tree
Hide file tree
Showing 16 changed files with 436 additions and 71 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
// Minecraft 1.20.5 (24w14a) upwards uses Java 21.
it.options.release = 21
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs = -Xmx3G
org.gradle.parallel = true

# Mod Properties
version = 1.20.5-1.2.0-BETA-3
version = 1.20.5-1.2.0-BETA-4
maven_group = net.atlas
archives_base_name = combatify
owo_version=0.11.0+1.20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.atlas.combatify.item.WeaponType;
import net.atlas.combatify.util.BlockingType;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.crafting.Ingredient;

public class ConfigurableItemData {
public final Double damage;
Expand All @@ -25,7 +26,11 @@ public class ConfigurableItemData {
public final Double piercingLevel;
public final Boolean canSweep;
public final Tier tier;
ConfigurableItemData(Double attackDamage, Double attackSpeed, Double attackReach, Double chargedReach, Integer stackSize, Integer cooldown, Boolean cooldownAfter, WeaponType weaponType, BlockingType blockingType, Double blockStrength, Double blockKbRes, Integer enchantability, Boolean isEnchantable, Boolean hasSwordEnchants, Boolean isPrimaryForSwordEnchants, Integer useDuration, Double piercingLevel, Boolean canSweep, Tier tier, Integer durability) {
public final Integer defense;
public final Double toughness;
public final Double armourKbRes;
public final Ingredient repairIngredient;
ConfigurableItemData(Double attackDamage, Double attackSpeed, Double attackReach, Double chargedReach, Integer stackSize, Integer cooldown, Boolean cooldownAfter, WeaponType weaponType, BlockingType blockingType, Double blockStrength, Double blockKbRes, Integer enchantability, Boolean isEnchantable, Boolean hasSwordEnchants, Boolean isPrimaryForSwordEnchants, Integer useDuration, Double piercingLevel, Boolean canSweep, Tier tier, Integer durability, Integer defense, Double toughness, Double armourKbRes, Ingredient repairIngredient) {
damage = clamp(attackDamage, -10, 1000);
speed = clamp(attackSpeed, -1, 7.5);
reach = clamp(attackReach, 0, 1024);
Expand All @@ -46,13 +51,23 @@ public class ConfigurableItemData {
this.piercingLevel = clamp(piercingLevel, 0, 1);
this.canSweep = canSweep;
this.tier = tier;
this.defense = max(defense, 1);
this.toughness = max(toughness, 0);
this.armourKbRes = clamp(armourKbRes, 0, 1);
this.repairIngredient = repairIngredient;
}
public static Integer max(Integer value, int min) {
if (value == null)
return null;
return Math.max(value, min);
}

public static Double max(Double value, double min) {
if (value == null)
return null;
return Math.max(value, min);
}

public static Integer clamp(Integer value, int min, int max) {
if (value == null)
return null;
Expand Down
279 changes: 255 additions & 24 deletions src/main/java/net/atlas/combatify/config/ItemConfig.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.atlas.combatify.item.WeaponType;
import net.atlas.combatify.util.BlockingType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.Tiers;
import net.minecraft.world.item.component.ItemAttributeModifiers;
Expand Down Expand Up @@ -85,4 +86,12 @@ default Tier getTierFromConfig() {
return Combatify.ITEMS.configuredItems.get(self()).tier;
return null;
}
default boolean canRepairThroughConfig(ItemStack stack) {
if (Combatify.ITEMS != null && Combatify.ITEMS.configuredItems.containsKey(self())) {
ConfigurableItemData configurableItemData = Combatify.ITEMS.configuredItems.get(self());
if (configurableItemData.repairIngredient != null)
return configurableItemData.repairIngredient.test(stack);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public int getEnchantmentValue() {
}

public boolean isValidRepairItem(ItemStack itemStack, ItemStack itemStack2) {
return getConfigTier().getRepairIngredient().test(itemStack2) || super.isValidRepairItem(itemStack, itemStack2);
return getConfigTier().getRepairIngredient().test(itemStack2) || canRepairThroughConfig(itemStack2);
}

@Override
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/net/atlas/combatify/mixin/CombatRulesMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.atlas.combatify.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import net.atlas.combatify.Combatify;
import net.minecraft.world.damagesource.CombatRules;
import net.minecraft.world.damagesource.DamageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(CombatRules.class)
public class CombatRulesMixin {
@ModifyReturnValue(method = "getDamageAfterAbsorb", at = @At("RETURN"))
private static float changeArmourCalcs(float original, @Local(ordinal = 0, argsOnly = true) float amount, @Local(ordinal = 0, argsOnly = true) DamageSource source, @Local(ordinal = 1, argsOnly = true) float armour, @Local(ordinal = 2, argsOnly = true) float toughness) {
if (Combatify.ITEMS != null && Combatify.ITEMS.armourCalcs != null) {
original = Combatify.ITEMS.armourCalcs.armourCalcs(amount, source, armour, toughness);
Combatify.LOGGER.info("Damage: " + amount + " Result: " + original);
} else
Combatify.LOGGER.info("armour formula is null");
return original;
}
@ModifyReturnValue(method = "getDamageAfterMagicAbsorb", at = @At("RETURN"))
private static float changeEnchant(float original, @Local(ordinal = 0, argsOnly = true) float amount, @Local(ordinal = 1, argsOnly = true) float enchantLevel) {
if (Combatify.ITEMS != null && Combatify.ITEMS.armourCalcs != null) {
return Combatify.ITEMS.armourCalcs.enchantCalcs(amount, enchantLevel);
}
return original;
}
}
27 changes: 27 additions & 0 deletions src/main/java/net/atlas/combatify/mixin/ElytraItemMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.atlas.combatify.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import net.atlas.combatify.extensions.ItemExtensions;
import net.minecraft.world.item.ElytraItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(ElytraItem.class)
public class ElytraItemMixin extends Item implements ItemExtensions {
public ElytraItemMixin(Properties properties) {
super(properties);
}

@ModifyReturnValue(method = "isValidRepairItem", at = @At(value = "RETURN"))
public boolean canRepair(boolean original, @Local(ordinal = 1, argsOnly = true) ItemStack stack) {
return original || canRepairThroughConfig(stack);
}

@Override
public Item self() {
return this;
}
}
46 changes: 28 additions & 18 deletions src/main/java/net/atlas/combatify/mixin/ItemMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.atlas.combatify.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import net.atlas.combatify.Combatify;
import net.atlas.combatify.config.ConfigurableItemData;
import net.atlas.combatify.extensions.ItemExtensions;
Expand Down Expand Up @@ -34,6 +36,29 @@ public abstract class ItemMixin implements ItemExtensions {

@Inject(method = "verifyComponentsAfterLoad", at = @At(value = "HEAD"))
public void editModifiers(ItemStack itemStack, CallbackInfo ci) {
editModifiers(itemStack);
}

@Override
public Item self() {
return Item.class.cast(this);
}
@Unique
public void setDurability(ItemStack stack, int value) {
if (self() instanceof TieredShieldItem)
value *= 2;
if (!stack.has(DataComponents.DAMAGE))
stack.set(DataComponents.DAMAGE, 0);
if ((!components.has(DataComponents.MAX_DAMAGE) && !stack.has(DataComponents.MAX_DAMAGE)) || Objects.equals(components.get(DataComponents.MAX_DAMAGE), stack.get(DataComponents.MAX_DAMAGE))) {
stack.set(DataComponents.MAX_DAMAGE, value);
}
}
@ModifyReturnValue(method = "isValidRepairItem", at = @At(value = "RETURN"))
public boolean canRepair(boolean original, @Local(ordinal = 1, argsOnly = true) ItemStack stack) {
return original || canRepairThroughConfig(stack);
}
@Unique
public void editModifiers(ItemStack itemStack) {
boolean maxDamageChanged = false;
if (Combatify.ITEMS != null && Combatify.ITEMS.configuredItems.containsKey(self())) {
ConfigurableItemData configurableItemData = Combatify.ITEMS.configuredItems.get(self());
Expand All @@ -46,15 +71,15 @@ public void editModifiers(ItemStack itemStack, CallbackInfo ci) {
}
if (maxStackSize != null
&& ((!components.has(DataComponents.MAX_STACK_SIZE) && !itemStack.has(DataComponents.MAX_STACK_SIZE))
|| Objects.equals(components.get(DataComponents.MAX_STACK_SIZE), itemStack.get(DataComponents.MAX_STACK_SIZE)))) {
|| Objects.equals(components.get(DataComponents.MAX_STACK_SIZE), itemStack.get(DataComponents.MAX_STACK_SIZE)))) {
itemStack.set(DataComponents.MAX_STACK_SIZE, maxStackSize);
}
if (tier != null && self() instanceof DiggerItem && Objects.equals(components.get(DataComponents.TOOL), itemStack.get(DataComponents.TOOL))) {
Tool original = components.get(DataComponents.TOOL);
AtomicReference<Tool> tool = new AtomicReference<>();

assert original != null;
original.rules().forEach(rule -> {
assert original != null;
original.rules().forEach(rule -> {
if (rule.blocks() instanceof HolderSet.Named<Block> named) {
tool.set(tier.createToolProperties(named.key()));
}
Expand All @@ -67,19 +92,4 @@ public void editModifiers(ItemStack itemStack, CallbackInfo ci) {
setDurability(itemStack, getTierFromConfig().getUses());
MethodHandler.updateModifiers(itemStack);
}

@Override
public Item self() {
return Item.class.cast(this);
}
@Unique
public void setDurability(ItemStack stack, int value) {
if (self() instanceof TieredShieldItem)
value *= 2;
if (!stack.has(DataComponents.DAMAGE))
stack.set(DataComponents.DAMAGE, 0);
if ((!components.has(DataComponents.MAX_DAMAGE) && !stack.has(DataComponents.MAX_DAMAGE)) || Objects.equals(components.get(DataComponents.MAX_DAMAGE), stack.get(DataComponents.MAX_DAMAGE))) {
stack.set(DataComponents.MAX_DAMAGE, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void isDamageSourceBlocked(DamageSource source, CallbackInfoReturnable<Bo
hurtArmor(source, (float) (amount * (1 + piercingLevel)));
double armourStrength = getArmorValue();
double toughness = getAttributeValue(Attributes.ARMOR_TOUGHNESS);
amount = CombatRules.getDamageAfterAbsorb(amount, source, (float) (armourStrength - (armourStrength * piercingLevel)), (float) (toughness - (toughness * piercingLevel)));
amount = CombatRules.getDamageAfterAbsorb(amount, source, (float) (armourStrength - (armourStrength * piercingLevel)), (float) toughness);
}

return amount;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/atlas/combatify/mixin/MaceItemMixin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package net.atlas.combatify.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import net.atlas.combatify.Combatify;
import net.atlas.combatify.extensions.WeaponWithType;
import net.atlas.combatify.item.WeaponType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.MaceItem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(MaceItem.class)
public class MaceItemMixin extends Item implements WeaponWithType {
Expand All @@ -24,6 +28,11 @@ public WeaponType getWeaponType() {
return WeaponType.MACE;
}

@ModifyReturnValue(method = "isValidRepairItem", at = @At(value = "RETURN"))
public boolean canRepair(boolean original, @Local(ordinal = 1, argsOnly = true) ItemStack stack) {
return original || canRepairThroughConfig(stack);
}

@Override
public Item self() {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.atlas.combatify.mixin;

import net.atlas.combatify.util.MethodHandler;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.PlayerHeadItem;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -9,9 +8,9 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerHeadItem.class)
public class PlayerHeadItemMixin {
public class PlayerHeadItemMixin extends ItemMixin {
@Inject(method = "verifyComponentsAfterLoad", at = @At(value = "HEAD"))
public void editModifiers(ItemStack itemStack, CallbackInfo ci) {
MethodHandler.updateModifiers(itemStack);
public void editModifiers1(ItemStack itemStack, CallbackInfo ci) {
editModifiers(itemStack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@
import com.llamalad7.mixinextras.sugar.ref.LocalFloatRef;
import net.atlas.combatify.Combatify;
import net.atlas.combatify.config.ConfigurableItemData;
import net.atlas.combatify.enchantment.DefendingEnchantment;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.ItemAttributeModifiers;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
62 changes: 54 additions & 8 deletions src/main/java/net/atlas/combatify/util/MethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,27 @@ public static void updateModifiers(ItemStack itemStack) {
AtomicReference<ItemAttributeModifiers.Entry> speed = new AtomicReference<>();
boolean modReach = false;
AtomicReference<ItemAttributeModifiers.Entry> reach = new AtomicReference<>();
boolean modDefense = false;
AtomicReference<ItemAttributeModifiers.Entry> defense = new AtomicReference<>();
boolean modToughness = false;
AtomicReference<ItemAttributeModifiers.Entry> toughness = new AtomicReference<>();
boolean modKnockbackResistance = false;
AtomicReference<ItemAttributeModifiers.Entry> knockbackResistance = new AtomicReference<>();
modifier.modifiers().forEach(entry -> {
boolean bl = entry.attribute().is(Attributes.ATTACK_DAMAGE)
|| entry.attribute().is(Attributes.ATTACK_SPEED)
|| entry.attribute().is(Attributes.ENTITY_INTERACTION_RANGE);
if (!bl)
builder.add(entry.attribute(), entry.modifier(), entry.slot());
else if (entry.attribute().is(Attributes.ATTACK_DAMAGE))
if (entry.attribute().is(Attributes.ATTACK_DAMAGE))
damage.set(entry);
else if (entry.attribute().is(Attributes.ENTITY_INTERACTION_RANGE))
reach.set(entry);
else
else if (entry.attribute().is(Attributes.ATTACK_SPEED))
speed.set(entry);
else if (entry.attribute().is(Attributes.ARMOR))
defense.set(entry);
else if (entry.attribute().is(Attributes.ARMOR_TOUGHNESS))
toughness.set(entry);
else if (entry.attribute().is(Attributes.KNOCKBACK_RESISTANCE))
knockbackResistance.set(entry);
else
builder.add(entry.attribute(), entry.modifier(), entry.slot());
});
if (configurableItemData.damage != null) {
modDamage = true;
Expand All @@ -112,13 +121,50 @@ else if (entry.attribute().is(Attributes.ENTITY_INTERACTION_RANGE))
new AttributeModifier(WeaponType.BASE_ATTACK_REACH_UUID, "Config modifier", configurableItemData.reach - 2.5, AttributeModifier.Operation.ADD_VALUE),
EquipmentSlotGroup.MAINHAND);
}
UUID uuid = UUID.fromString("C1D3F271-8B8E-BA4A-ACE0-6020A98928B2");
EquipmentSlotGroup slotGroup = EquipmentSlotGroup.ARMOR;
if (itemStack.getItem() instanceof Equipable equipable)
slotGroup = EquipmentSlotGroup.bySlot(equipable.getEquipmentSlot());
uuid = switch (slotGroup) {
case HEAD -> UUID.fromString("2AD3F246-FEE1-4E67-B886-69FD380BB150");
case CHEST -> UUID.fromString("9F3D476D-C118-4544-8365-64846904B48E");
case LEGS -> UUID.fromString("D8499B04-0E66-4726-AB29-64469D734E0D");
case FEET -> UUID.fromString("845DB27C-C624-495F-8C9F-6020A9A58B6B");
case BODY -> UUID.fromString("C1C72771-8B8E-BA4A-ACE0-81A93C8928B2");
default -> uuid;
};
if (configurableItemData.defense != null) {
modDefense = true;
builder.add(Attributes.ARMOR,
new AttributeModifier(uuid, "Config modifier", configurableItemData.defense, AttributeModifier.Operation.ADD_VALUE),
slotGroup);
}
if (configurableItemData.toughness != null) {
modToughness = true;
builder.add(Attributes.ARMOR_TOUGHNESS,
new AttributeModifier(uuid, "Config modifier", configurableItemData.toughness, AttributeModifier.Operation.ADD_VALUE),
slotGroup);
}
if (configurableItemData.armourKbRes != null) {
modKnockbackResistance = true;
if (configurableItemData.armourKbRes > 0)
builder.add(Attributes.KNOCKBACK_RESISTANCE,
new AttributeModifier(uuid, "Config modifier", configurableItemData.armourKbRes, AttributeModifier.Operation.ADD_VALUE),
slotGroup);
}
if (!modDamage && damage.get() != null)
builder.add(damage.get().attribute(), damage.get().modifier(), damage.get().slot());
if (!modSpeed && speed.get() != null)
builder.add(speed.get().attribute(), speed.get().modifier(), speed.get().slot());
if (!modReach && reach.get() != null)
builder.add(reach.get().attribute(), reach.get().modifier(), reach.get().slot());
if (modDamage || modSpeed || modReach)
if (!modDefense && defense.get() != null)
builder.add(defense.get().attribute(), defense.get().modifier(), defense.get().slot());
if (!modToughness && toughness.get() != null)
builder.add(toughness.get().attribute(), toughness.get().modifier(), toughness.get().slot());
if (!modKnockbackResistance && knockbackResistance.get() != null)
builder.add(knockbackResistance.get().attribute(), knockbackResistance.get().modifier(), knockbackResistance.get().slot());
if (modDamage || modSpeed || modReach || modDefense || modToughness || modKnockbackResistance)
itemStack.set(DataComponents.ATTRIBUTE_MODIFIERS, builder.build());
}
}
Expand Down
Loading

0 comments on commit 85d9f8a

Please sign in to comment.