Skip to content

Commit

Permalink
Swap melee protection's to an attack speed boost
Browse files Browse the repository at this point in the history
Killing enemies faster means you are more protected. Ended up being the simpliest of the melee ideas, and is consistent with cobalt's other attack speed boosts
  • Loading branch information
KnightMiner committed Jun 12, 2022
1 parent 42c3ffc commit 97dda74
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,30 @@
public abstract class AbstractProtectionModifier<T extends ModifierMaxLevel> extends IncrementalModifier {
private final TinkerDataKey<T> key;

/** Creates a new data instance */
/** @deprecated use {@link #createData(EquipmentChangeContext)}*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
protected abstract T createData();

/** Called when the last piece of equipment is removed to reset the data */
/** Creates a new data instance */
protected T createData(EquipmentChangeContext context) {
return createData();
}

/** @deprecated use {@link #reset(ModifierMaxLevel, EquipmentChangeContext)} */
@Deprecated
protected void reset(T data) {}

/** Called when the last piece of equipment is removed to reset the data */
protected void reset(T data, EquipmentChangeContext context) {
reset(data);
}

/** Called to apply updates to the piece */
protected void set(T data, EquipmentSlot slot, float scaledLevel, EquipmentChangeContext context) {
data.set(slot, scaledLevel);
}

@Override
public void onUnequip(IToolStackView tool, int level, EquipmentChangeContext context) {
LivingEntity entity = context.getEntity();
Expand All @@ -36,9 +54,9 @@ public void onUnequip(IToolStackView tool, int level, EquipmentChangeContext con
context.getTinkerData().ifPresent(data -> {
T modData = data.get(key);
if (modData != null) {
modData.set(slot, 0);
set(modData, slot, 0, context);
if (modData.getMax() == 0) {
reset(modData);
reset(modData, context);
}
}
});
Expand All @@ -55,11 +73,11 @@ public void onEquip(IToolStackView tool, int level, EquipmentChangeContext conte
T modData = data.get(key);
if (modData == null) {
// not calculated yet? add all vanilla values to the tracker
modData = createData();
modData = createData(context);
data.put(key, modData);
}
// add ourself to the data
modData.set(slot, scaledLevel);
set(modData, slot, scaledLevel, context);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.EntityDamageSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.TooltipFlag;
import slimeknights.tconstruct.library.modifiers.impl.IncrementalModifier;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.data.ModifierMaxLevel;
import slimeknights.tconstruct.library.tools.capability.TinkerDataCapability.TinkerDataKey;
import slimeknights.tconstruct.library.tools.context.EquipmentChangeContext;
import slimeknights.tconstruct.library.tools.context.EquipmentContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.TooltipKey;

import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;

public class MeleeProtectionModifier extends IncrementalModifier {
public class MeleeProtectionModifier extends AbstractProtectionModifier<ModifierMaxLevel> {
private static final UUID SPEED_UUID = UUID.fromString("6f030b1e-e9e1-11ec-8fea-0242ac120002");
private static final TinkerDataKey<ModifierMaxLevel> KEY = TConstruct.createKey("melee_protection");

public MeleeProtectionModifier() {
super(KEY);
}

/** Checks if the damage source is blocked by this modifier */
private static boolean doesApply(DamageSource source) {
if (source.isBypassMagic() || source.isProjectile() || source.isBypassInvul()) {
return false;
Expand All @@ -26,7 +42,33 @@ private static boolean doesApply(DamageSource source) {
} else {
// for non-entity damage, require it to not be any other type
// blocks dall damage, falling blocks, cactus, but not starving, drowning, freezing
return (source.isFall() || !source.isBypassArmor()) && !source.isFire() && !source.isMagic() && !source.isExplosion();
return !source.isBypassArmor() && !source.isFire() && !source.isMagic() && !source.isExplosion();
}
}

@Override
protected void set(ModifierMaxLevel data, EquipmentSlot slot, float scaledLevel, EquipmentChangeContext context) {
float oldMax = data.getMax();
super.set(data, slot, scaledLevel, context);
float newMax = data.getMax();
// 5% bonus attack speed for the largest level
if (oldMax != newMax) {
AttributeInstance instance = context.getEntity().getAttribute(Attributes.ATTACK_SPEED);
if (instance != null) {
instance.removeModifier(SPEED_UUID);
if (newMax != 0) {
instance.addTransientModifier(new AttributeModifier(SPEED_UUID, "tconstruct.melee_protection", 0.03 * newMax, Operation.MULTIPLY_BASE));
}
}
}
}

@Override
protected void reset(ModifierMaxLevel data, EquipmentChangeContext context) {
super.reset(data, context);
AttributeInstance instance = context.getEntity().getAttribute(Attributes.ATTACK_SPEED);
if (instance != null) {
instance.removeModifier(SPEED_UUID);
}
}

Expand All @@ -42,4 +84,17 @@ public float getProtectionModifier(IToolStackView tool, int level, EquipmentCont
public void addInformation(IToolStackView tool, int level, @Nullable Player player, List<Component> tooltip, TooltipKey tooltipKey, TooltipFlag tooltipFlag) {
AbstractProtectionModifier.addResistanceTooltip(this, tool, level, 2.0f, tooltip);
}

@Override
protected ModifierMaxLevel createData() {
return new ModifierMaxLevel();
}

private static class MeleeAttackSpeed extends ModifierMaxLevel {

@Override
public void set(EquipmentSlot slot, float level) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"text": "Grants +8% resistance against melee damage, such as from zombies, swords, and falling. Caps at 80% across the whole set, or 10 levels."
},
{
"text": "Melee damage is mainly caused by non-projectile entities, but some other sources such as fall damage also count as melee.",
"text": "Grants +3% attack speed per level of the highest level piece.",
"paragraph": true
}
],
Expand Down

0 comments on commit 97dda74

Please sign in to comment.