-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
SpectralShotAffix.java
94 lines (78 loc) · 3.47 KB
/
SpectralShotAffix.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
package shadows.apotheosis.adventure.affix.effect;
import java.util.Map;
import java.util.function.Consumer;
import com.google.gson.JsonObject;
import net.minecraft.ChatFormatting;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.AbstractArrow.Pickup;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import shadows.apotheosis.adventure.affix.Affix;
import shadows.apotheosis.adventure.affix.AffixHelper;
import shadows.apotheosis.adventure.affix.AffixType;
import shadows.apotheosis.adventure.loot.LootCategory;
import shadows.apotheosis.adventure.loot.LootRarity;
import shadows.placebo.util.StepFunction;
public class SpectralShotAffix extends Affix {
protected final Map<LootRarity, StepFunction> values;
public SpectralShotAffix(Map<LootRarity, StepFunction> values) {
super(AffixType.EFFECT);
this.values = values;
}
@Override
public boolean canApplyTo(ItemStack stack, LootRarity rarity) {
return LootCategory.forItem(stack).isRanged() && this.values.containsKey(rarity);
}
@Override
public void addInformation(ItemStack stack, LootRarity rarity, float level, Consumer<Component> list) {
list.accept(new TranslatableComponent("affix." + this.getId() + ".desc", ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(100 * getTrueLevel(rarity, level))).withStyle(ChatFormatting.YELLOW));
}
@Override
public void onArrowFired(ItemStack stack, LootRarity rarity, float level, LivingEntity user, AbstractArrow arrow) {
if (user.level.random.nextFloat() <= getTrueLevel(rarity, level)) {
if (!user.level.isClientSide) {
ArrowItem arrowitem = (ArrowItem) Items.SPECTRAL_ARROW;
AbstractArrow spectralArrow = arrowitem.createArrow(user.level, ItemStack.EMPTY, user);
spectralArrow.shoot(user.getXRot(), user.getYRot(), 0.0F, 2.0F, 1.0F);
this.cloneMotion(arrow, spectralArrow);
spectralArrow.setCritArrow(arrow.isCritArrow());
spectralArrow.setBaseDamage(arrow.getBaseDamage());
spectralArrow.setKnockback(arrow.knockback);
spectralArrow.setRemainingFireTicks(arrow.getRemainingFireTicks());
spectralArrow.pickup = Pickup.CREATIVE_ONLY;
spectralArrow.getPersistentData().putBoolean("apoth.attrib.done", true);
arrow.level.addFreshEntity(spectralArrow);
}
}
}
private void cloneMotion(AbstractArrow src, AbstractArrow dest) {
dest.setDeltaMovement(src.getDeltaMovement().scale(1));
dest.setYRot(src.getYRot());
dest.setXRot(src.getXRot());
dest.yRotO = dest.yRotO;
dest.xRotO = dest.xRotO;
}
private float getTrueLevel(LootRarity rarity, float level) {
return this.values.get(rarity).get(level);
}
public static SpectralShotAffix read(JsonObject obj) {
var values = AffixHelper.readValues(GsonHelper.getAsJsonObject(obj, "values"));
return new SpectralShotAffix(values);
}
public JsonObject write() {
return new JsonObject();
}
public void write(FriendlyByteBuf buf) {
buf.writeMap(this.values, (b, key) -> b.writeUtf(key.id()), (b, func) -> func.write(b));
}
public static SpectralShotAffix read(FriendlyByteBuf buf) {
Map<LootRarity, StepFunction> values = buf.readMap(b -> LootRarity.byId(b.readUtf()), b -> StepFunction.read(b));
return new SpectralShotAffix(values);
}
}