/
PixieHandler.java
96 lines (81 loc) · 3.92 KB
/
PixieHandler.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
/*
* This class is distributed as part of the Botania Mod.
* Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*/
package vazkii.botania.common.handler;
import net.minecraft.Util;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ServerLevelAccessor;
import vazkii.botania.common.entity.EntityPixie;
import vazkii.botania.common.helper.PlayerHelper;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.equipment.armor.elementium.ItemElementiumHelm;
import java.util.EnumMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;
import static vazkii.botania.common.lib.ResourceLocationHelper.prefix;
public final class PixieHandler {
private PixieHandler() {}
public static final Attribute PIXIE_SPAWN_CHANCE = new RangedAttribute("attribute.name.botania.pixieSpawnChance", 0, 0, 1);
private static final Map<EquipmentSlot, UUID> DEFAULT_MODIFIER_UUIDS = Util.make(new EnumMap<>(EquipmentSlot.class), m -> {
m.put(EquipmentSlot.HEAD, UUID.fromString("3c1f559c-9ec4-412d-ada0-dbf3e714088e"));
m.put(EquipmentSlot.CHEST, UUID.fromString("9631121c-16f0-4ed4-ba0a-0e7a063cb71c"));
m.put(EquipmentSlot.LEGS, UUID.fromString("a87117a1-ac15-4b17-9fd5-e98d5fe31ff1"));
m.put(EquipmentSlot.FEET, UUID.fromString("ff67d38a-c5be-4a00-90ed-76bb12c45523"));
m.put(EquipmentSlot.MAINHAND, UUID.fromString("995829fa-94c0-41bd-b046-0468c509a488"));
m.put(EquipmentSlot.OFFHAND, UUID.fromString("34f62de8-f652-4fe7-899f-a8fc938c4940"));
});
private static final MobEffect[] potions = {
MobEffects.BLINDNESS,
MobEffects.WITHER,
MobEffects.MOVEMENT_SLOWDOWN,
MobEffects.WEAKNESS
};
public static void registerAttribute(BiConsumer<Attribute, ResourceLocation> r) {
r.accept(PIXIE_SPAWN_CHANCE, prefix("pixie_spawn_chance"));
}
public static AttributeModifier makeModifier(EquipmentSlot slot, String name, double amount) {
return new AttributeModifier(DEFAULT_MODIFIER_UUIDS.get(slot), name, amount, AttributeModifier.Operation.ADDITION);
}
public static void onDamageTaken(Player player, DamageSource source) {
if (!player.level.isClientSide && source.getEntity() instanceof LivingEntity livingSource) {
// Sometimes the player doesn't have the attribute, not sure why.
// Could be badly-written mixins on Fabric.
double chance = player.getAttributes().hasAttribute(PIXIE_SPAWN_CHANCE)
? player.getAttributeValue(PIXIE_SPAWN_CHANCE) : 0;
ItemStack sword = PlayerHelper.getFirstHeldItem(player, s -> s.is(ModItems.elementiumSword));
if (Math.random() < chance) {
EntityPixie pixie = new EntityPixie(player.level);
pixie.setPos(player.getX(), player.getY() + 2, player.getZ());
if (((ItemElementiumHelm) ModItems.elementiumHelm).hasArmorSet(player)) {
pixie.setApplyPotionEffect(new MobEffectInstance(potions[player.level.random.nextInt(potions.length)], 40, 0));
}
float dmg = 4;
if (!sword.isEmpty()) {
dmg += 2;
}
pixie.setProps(livingSource, player, 0, dmg);
pixie.finalizeSpawn((ServerLevelAccessor) player.level, player.level.getCurrentDifficultyAt(pixie.blockPosition()),
MobSpawnType.EVENT, null, null);
player.level.addFreshEntity(pixie);
}
}
}
}