-
Notifications
You must be signed in to change notification settings - Fork 0
/
PotionNBTHelper.java
87 lines (74 loc) · 4.54 KB
/
PotionNBTHelper.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
package net.mehvahdjukaar.selene.util;
import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.renderer.EffectInstance;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.alchemy.Potions;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import static net.minecraft.world.item.alchemy.PotionUtils.*;
public class PotionNBTHelper {
private static final MutableComponent EMPTY = (new TranslatableComponent("effect.none")).withStyle(TextFormatting.GRAY);
//I need this cause I'm using block entity tag so I can't give PotionUtil methods an itemStack directly
public static void addPotionTooltip( @Nullable CompoundTag com, List<Component> tooltip, float durationFactor) {
List<EffectInstance> list = getAllEffects(com);
List<Pair<Attribute, AttributeModifier>> list1 = Lists.newArrayList();
if (list.isEmpty()) {
tooltip.add(EMPTY);
} else {
for(EffectInstance effectinstance : list) {
MutableComponent MutableComponent = new TranslatableComponent(effectinstance.getDescriptionId());
Effect effect = effectinstance.getEffect();
Map<Attribute, AttributeModifier> map = effect.getAttributeModifiers();
if (!map.isEmpty()) {
for(Map.Entry<Attribute, AttributeModifier> entry : map.entrySet()) {
AttributeModifier attributemodifier = entry.getValue();
AttributeModifier attributemodifier1 = new AttributeModifier(attributemodifier.getName(), effect.getAttributeModifierValue(effectinstance.getAmplifier(), attributemodifier), attributemodifier.getOperation());
list1.add(new Pair<>(entry.getKey(), attributemodifier1));
}
}
if (effectinstance.getAmplifier() > 0) {
MutableComponent = new TranslatableComponent("potion.withAmplifier", MutableComponent, new TranslatableComponent("potion.potency." + effectinstance.getAmplifier()));
}
if (effectinstance.getDuration() > 20) {
MutableComponent = new TranslatableComponent("potion.withDuration", MutableComponent, EffectUtils.formatDuration(effectinstance, durationFactor));
}
tooltip.add(MutableComponent.withStyle(effect.getCategory().getTooltipFormatting()));
}
}
if (!list1.isEmpty()) {
tooltip.add(StringTextComponent.EMPTY);
tooltip.add((new TranslatableComponent("potion.whenDrank")).withStyle(TextFormatting.DARK_PURPLE));
for(Pair<Attribute, AttributeModifier> pair : list1) {
AttributeModifier attributemodifier2 = pair.getSecond();
double d0 = attributemodifier2.getAmount();
double d1;
if (attributemodifier2.getOperation() != AttributeModifier.Operation.MULTIPLY_BASE && attributemodifier2.getOperation() != AttributeModifier.Operation.MULTIPLY_TOTAL) {
d1 = attributemodifier2.getAmount();
} else {
d1 = attributemodifier2.getAmount() * 100.0D;
}
if (d0 > 0.0D) {
tooltip.add((new TranslatableComponent("attribute.modifier.plus." + attributemodifier2.getOperation().toValue(), ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(d1), new TranslatableComponent(pair.getFirst().getDescriptionId()))).withStyle(TextFormatting.BLUE));
} else if (d0 < 0.0D) {
d1 = d1 * -1.0D;
tooltip.add((new TranslatableComponent("attribute.modifier.take." + attributemodifier2.getOperation().toValue(), ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(d1), new TranslatableComponent(pair.getFirst().getDescriptionId()))).withStyle(TextFormatting.RED));
}
}
}
}
public static int getColorFromNBT(@Nullable CompoundTag com) {
if (com != null && com.contains("CustomPotionColor", 99)) {
return com.getInt("CustomPotionColor");
} else {
return getPotion(com) == Potions.EMPTY ? 16253176 : getColor(getAllEffects(com));
}
}
}