View difference between Paste ID: YXyKR6yX and v5mnYfGH
SHOW: | | - or go back to the newest paste.
1
package me.dpohvar.varscript.utils.minecraft;
2
3
import net.minecraft.server.NBTTagCompound;
4
import org.bukkit.craftbukkit.inventory.CraftItemStack;
5
import org.bukkit.inventory.ItemStack;
6
7
import java.nio.ByteBuffer;
8
9
/**
10
 * Created by IntelliJ IDEA.
11
 * User: DPOH-VAR
12
 * Date: 10.08.12
13
 * Time: 22:35
14
 * To change this template use File | Settings | File Templates.
15
 */
16
public class ColorEdit{
17
    public final ItemStack item;
18
    private NBTTagCompound tagData;
19
20
    public ColorEdit(ItemStack s){
21
        this.item = s;
22
        net.minecraft.server.ItemStack i = ((CraftItemStack) s).getHandle();
23
        if (i.tag == null) {
24
            tagData = new NBTTagCompound();
25
            i.tag = tagData;
26
        } else {
27
            tagData = i.tag;
28
        }
29
    }
30
31
    public int getColor(){
32
        if (!tagData.hasKey("display")) {
33
            return -1;
34
        }
35
        NBTTagCompound t = tagData.getCompound("display");
36
        if (!t.hasKey("color")) {
37
            return -1;
38
        }
39
        return tagData.getCompound("display").getInt("color");
40
    }
41
42
    public ColorEdit setColor(int color){
43
        if(color==-1){
44
            removeColor();
45
            return this;
46
        }
47
        if(tagData.hasKey("display")){
48
            NBTTagCompound d = tagData.getCompound("display");
49
            d.setInt("color", color);
50
        } else {
51
            NBTTagCompound d = new NBTTagCompound();
52
            d.setInt("color", color);
53
            tagData.setCompound("display",d);
54
        }
55
        return this;
56
    }
57
58
    public void removeColor(){
59
        tagData.getCompound("display").remove("color");
60
    }
61
62
    private byte[] getColors(){
63
        int color = getColor();
64
        if (color==-1) return new byte[4];
65
        return ByteBuffer.allocate(4).putInt(color).array();
66
    }
67
68
    private ColorEdit setColors(byte[] colors){
69
        setColor(ByteBuffer.wrap(colors).getInt());
70
        return this;
71
    }
72
73
    private ColorEdit setColorById(byte value,int id){
74
        byte[] colors = getColors();
75
        colors[id]=value;
76
        setColors(colors);
77
        return this;
78
    }
79
80
    private int getColorById(int id){
81
        int color = getColor();
82
        if(color==-1) return -1;
83
        return ByteBuffer.allocate(4).putInt(color).array()[id];
84
    }
85
86-
    private ColorEdit setRGB(byte red,byte green,byte blue){
86+
    public ColorEdit setRGB(byte red,byte green,byte blue){
87
        byte[] colors = getColors();
88
        colors[1]=red;
89
        colors[2]=green;
90
        colors[3]=blue;
91
        setColors(colors);
92
        return this;
93
    }
94
    public ColorEdit setRed(byte red){return setColorById(red,1);}
95
    public ColorEdit setGreen(byte green){return setColorById(green,2);}
96
    public ColorEdit setBlue(byte blue){return setColorById(blue,3);}
97
    @Deprecated public ColorEdit setAlpha(byte alpha){return setColorById(alpha,0);}
98
    public int getRed(){return getColorById(1);}
99
    public int getGreen(){return getColorById(2);}
100
    public int getBlue(){return getColorById(3);}
101
    @Deprecated public int getAlpha(){return getColorById(0);}
102
}