de.pewpewproject.lasertag.item.LasertagVestItem   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
B predicate(AnimationEvent) 0 28 6
A getFactory() 0 3 1
A registerControllers(AnimationData) 0 3 1
A LasertagVestItem(ArmorMaterial,Settings) 0 2 1
1
package de.pewpewproject.lasertag.item;
2
3
import net.minecraft.entity.EquipmentSlot;
4
import net.minecraft.entity.LivingEntity;
5
import net.minecraft.entity.decoration.ArmorStandEntity;
6
import net.minecraft.item.ArmorItem;
7
import net.minecraft.item.ArmorMaterial;
8
import net.minecraft.item.Item;
9
import software.bernie.geckolib3.core.IAnimatable;
10
import software.bernie.geckolib3.core.PlayState;
11
import software.bernie.geckolib3.core.builder.AnimationBuilder;
12
import software.bernie.geckolib3.core.builder.ILoopType;
13
import software.bernie.geckolib3.core.controller.AnimationController;
14
import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
15
import software.bernie.geckolib3.core.manager.AnimationData;
16
import software.bernie.geckolib3.core.manager.AnimationFactory;
17
import software.bernie.geckolib3.util.GeckoLibUtil;
18
19
import java.util.ArrayList;
20
import java.util.List;
21
22
/**
23
 * Class to implement the custom behavior of the lasertag vest
24
 *
25
 * @author Étienne Muser
26
 */
27
public class LasertagVestItem extends ArmorItem implements IAnimatable {
28
29
    private final AnimationFactory factory = GeckoLibUtil.createFactory(this);
30
31
    public LasertagVestItem(ArmorMaterial armorMaterial, Settings settings) {
32
        super(armorMaterial, EquipmentSlot.CHEST, settings);
33
    }
34
35
    // Predicate runs every frame
36
    private <P extends IAnimatable> PlayState predicate(AnimationEvent<P> event) {
37
        // This is all the extradata this event carries. The livingentity is the entity
38
        // that's wearing the armor. The itemstack and equipmentslottype are self
39
        // explanatory.
40
        LivingEntity livingEntity = event.getExtraDataOfType(LivingEntity.class).get(0);
41
42
        // Always loop the animation but later on in this method we'll decide whether or
43
        // not to actually play it
44
        event.getController().setAnimation(new AnimationBuilder().addAnimation("idle", ILoopType.EDefaultLoopTypes.LOOP));
45
46
        // If the living entity is an armorstand just play the animation nonstop
47
        if (livingEntity instanceof ArmorStandEntity) {
48
            return PlayState.CONTINUE;
49
        }
50
51
        // elements 2 to 6 are the armor so we take the sublist. Armorlist now only
52
        // contains the 4 armor slots
53
        List<Item> armorList = new ArrayList<>(4);
54
        for (EquipmentSlot slot : EquipmentSlot.values()) {
55
            if (slot.getType() == EquipmentSlot.Type.ARMOR && livingEntity.getEquippedStack(slot) != null) {
56
                    armorList.add(livingEntity.getEquippedStack(slot).getItem());
57
            }
58
        }
59
60
        // Make sure the player is wearing all the armor. If they are, continue playing
61
        // the animation, otherwise stop
62
        boolean isWearingAll = armorList.contains(Items.LASERTAG_VEST);
63
        return isWearingAll ? PlayState.CONTINUE : PlayState.STOP;
64
    }
65
66
    // All you need to do here is add your animation controllers to the
67
    // AnimationData
68
    @Override
69
    public void registerControllers(AnimationData data) {
70
        data.addAnimationController(new AnimationController<LasertagVestItem>(this, "controller", 20, this::predicate));
71
    }
72
73
    @Override
74
    public AnimationFactory getFactory() {
75
        return this.factory;
76
    }
77
}
78