aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinItemStack.java
blob: c20ccebee16a394e8c84948bb63b16b3bc6d6ded (plain)
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
package io.github.moulberry.notenoughupdates.mixins;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.miscfeatures.ItemCooldowns;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin({ItemStack.class})
public class MixinItemStack {

    @Inject(method="hasEffect", at=@At("HEAD"), cancellable = true)
    public void hasEffect(CallbackInfoReturnable cir) {
        if(Utils.getHasEffectOverride()) {
            cir.setReturnValue(false);
        }
    }

    @Shadow
    private NBTTagCompound stackTagCompound;

    @Inject(method="getDisplayName",at=@At("HEAD"), cancellable=true)
    public void getDisplayName(CallbackInfoReturnable<String> returnable) {
        try {
            String customName = NotEnoughUpdates.INSTANCE.manager.itemRenameJson
                    .get(stackTagCompound.getCompoundTag("ExtraAttributes").getString("uuid")).getAsString();
            if(customName != null && !customName.equals("")) {
                String prefix = EnumChatFormatting.RESET.toString();
                if (stackTagCompound != null && stackTagCompound.hasKey("display", 10)) {
                    NBTTagCompound nbttagcompound = stackTagCompound.getCompoundTag("display");

                    if (nbttagcompound.hasKey("Name", 8)) {
                        String name = nbttagcompound.getString("Name");
                        char[] chars = name.toCharArray();

                        int i;
                        for(i=0; i<chars.length; i+=2) {
                            if(chars[i] != '\u00a7'){
                                break;
                            }
                        }

                        prefix = name.substring(0, i);
                    }
                }
                returnable.setReturnValue(prefix+customName);
            }
        } catch(Exception e) { }
    }


}