-
Notifications
You must be signed in to change notification settings - Fork 754
/
GoldGuardModifier.java
124 lines (113 loc) · 5.65 KB
/
GoldGuardModifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package slimeknights.tconstruct.tools.modifiers.traits.skull;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.EquipmentSlot.Type;
import net.minecraft.world.entity.LivingEntity;
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.mantle.client.TooltipKey;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.hook.armor.EquipmentChangeModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.display.TooltipModifierHook;
import slimeknights.tconstruct.library.modifiers.impl.NoLevelsModifier;
import slimeknights.tconstruct.library.module.ModuleHookMap.Builder;
import slimeknights.tconstruct.library.tools.capability.TinkerDataCapability.ComputableDataKey;
import slimeknights.tconstruct.library.tools.context.EquipmentChangeContext;
import slimeknights.tconstruct.library.tools.item.ModifiableArmorItem;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.Util;
import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;
public class GoldGuardModifier extends NoLevelsModifier implements EquipmentChangeModifierHook, TooltipModifierHook {
private static final UUID GOLD_GUARD_UUID = UUID.fromString("fbae11f1-b547-47e8-ae0c-f2cf24a46d93");
private static final ComputableDataKey<GoldGuardGold> TOTAL_GOLD = TConstruct.createKey("gold_guard", GoldGuardGold::new);
@Override
protected void registerHooks(Builder hookBuilder) {
super.registerHooks(hookBuilder);
hookBuilder.addHook(this, ModifierHooks.EQUIPMENT_CHANGE, ModifierHooks.TOOLTIP);
}
@Override
public void onEquip(IToolStackView tool, ModifierEntry modifier, EquipmentChangeContext context) {
// adding a helmet? activate bonus
if (context.getChangedSlot() == EquipmentSlot.HEAD) {
context.getTinkerData().ifPresent(data -> {
GoldGuardGold gold = data.get(TOTAL_GOLD);
if (gold == null) {
data.computeIfAbsent(TOTAL_GOLD).initialize(context);
} else {
gold.setGold(EquipmentSlot.HEAD, tool.getVolatileData().getBoolean(ModifiableArmorItem.PIGLIN_NEUTRAL), context.getEntity());
}
});
}
}
@Override
public void onUnequip(IToolStackView tool, ModifierEntry modifier, EquipmentChangeContext context) {
if (context.getChangedSlot() == EquipmentSlot.HEAD) {
IToolStackView newTool = context.getReplacementTool();
// when replacing with a helmet that lacks this modifier, remove bonus
if (newTool == null || newTool.getModifierLevel(this) == 0) {
context.getTinkerData().ifPresent(data -> data.remove(TOTAL_GOLD));
AttributeInstance instance = context.getEntity().getAttribute(Attributes.MAX_HEALTH);
if (instance != null) {
instance.removeModifier(GOLD_GUARD_UUID);
}
}
}
}
@Override
public void onEquipmentChange(IToolStackView tool, ModifierEntry modifier, EquipmentChangeContext context, EquipmentSlot slotType) {
// adding a helmet? activate bonus
EquipmentSlot changed = context.getChangedSlot();
if (slotType == EquipmentSlot.HEAD && changed.getType() == Type.ARMOR) {
LivingEntity living = context.getEntity();
boolean hasGold = ChrysophiliteModifier.hasGold(context, changed);
context.getTinkerData().ifPresent(data -> data.computeIfAbsent(TOTAL_GOLD).setGold(changed, hasGold, living));
}
}
@Override
public void addTooltip(IToolStackView tool, ModifierEntry entry, @Nullable Player player, List<Component> tooltip, TooltipKey tooltipKey, TooltipFlag tooltipFlag) {
if (player != null && tooltipKey == TooltipKey.SHIFT) {
AttributeInstance instance = player.getAttribute(Attributes.MAX_HEALTH);
if (instance != null) {
AttributeModifier modifier = instance.getModifier(GOLD_GUARD_UUID);
if (modifier != null) {
tooltip.add(applyStyle(Component.literal(Util.BONUS_FORMAT.format(modifier.getAmount()) + " ")
.append(Component.translatable(getTranslationKey() + "." + "health"))));
}
}
}
}
/** Internal logic to update gold on the player */
private static class GoldGuardGold extends ChrysophiliteModifier.TotalGold {
/** Adds the health boost to the player */
private void updateAttribute(LivingEntity living) {
// update attribute
AttributeInstance instance = living.getAttribute(Attributes.MAX_HEALTH);
if (instance != null) {
if (instance.getModifier(GOLD_GUARD_UUID) != null) {
instance.removeModifier(GOLD_GUARD_UUID);
}
// +2 hearts per level, and a bonus of 2 for having the modifier
instance.addTransientModifier(new AttributeModifier(GOLD_GUARD_UUID, "tconstruct.gold_guard", getTotalGold() * 4, Operation.ADDITION));
}
}
/** Sets the slot to having gold or not and updates the attribute */
public void setGold(EquipmentSlot slotType, boolean value, LivingEntity living) {
if (setGold(slotType, value)) {
updateAttribute(living);
}
}
@Override
public void initialize(EquipmentChangeContext context) {
super.initialize(context);
updateAttribute(context.getEntity());
}
}
}