-
Notifications
You must be signed in to change notification settings - Fork 755
/
HeraldryData.java
138 lines (106 loc) · 3.68 KB
/
HeraldryData.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package mods.battlegear2.api.heraldry;
import mods.battlegear2.utils.BattlegearUtils;
import java.io.*;
import java.util.Arrays;
public class HeraldryData {
public static final int RED = 1;
public static final int GREEN = 2;
public static final int BLUE = 3;
private byte pattern;
private short crest;
private byte crestPosition;
private int[] colours = new int[5];
private byte helm;
private byte banner;
private byte[] byteArray = null;
public HeraldryData(int pattern, int pattern_col_1, int pattern_col_2, int pattern_col_3, int crest, int crest_col_1, int crest_col_2, int crest_position, int helm, int banner) {
this.pattern = (byte) pattern;
this.crest = (short) crest;
colours = new int[]{pattern_col_1, pattern_col_2, pattern_col_3, crest_col_1, crest_col_2};
this.crestPosition = (byte) crest_position;
this.helm = (byte) helm;
this.banner = (byte) banner;
}
public HeraldryData(byte pattern, int pattern_col_1, int pattern_col_2, int pattern_col_3){
this(pattern, pattern_col_1, pattern_col_2, pattern_col_3, 0, 0, 0,0, 0, 0);
}
public HeraldryData(byte[] data){
DataInputStream input = null;
try{
input = new DataInputStream(new ByteArrayInputStream(data));
pattern = input.readByte();;
crest = input.readShort();
crestPosition = input.readByte();
colours= new int[5];
for(int i = 0; i < colours.length; i++){
colours[i] = input.readInt();
}
helm = input.readByte();
banner = input.readByte();
byteArray = Arrays.copyOf(data, data.length);
}catch (Exception e){
e.printStackTrace();
}finally {
BattlegearUtils.closeStream(input);
}
}
public static HeraldryData getDefault() {
return new HeraldryData(10, 0xFF000000, 0xFFFFFFFF, 0xFFFFFF00, 0, 0xFF000000, 0xFF000000, 0, 0, 0);
}
public byte[] getByteArray(){
if(byteArray != null){
return byteArray;
}
DataOutputStream output = null;
ByteArrayOutputStream bos = null;
try{
bos = new ByteArrayOutputStream();
output = new DataOutputStream(bos);
output.writeByte(pattern);
output.writeShort(crest);
output.writeByte(crestPosition);
for(int i = 0; i < 5; i++){
output.writeInt(colours[i]);
}
output.writeByte(helm);
output.writeByte(banner);
return bos.toByteArray();
} catch (Exception e){
e.printStackTrace();
} finally {
BattlegearUtils.closeStream(output);
}
return null;
}
public int getColourChanel(int colour, int chanelNo){
switch (chanelNo){
case RED:
return (colours[colour] >> 16) & 0xFF;
case GREEN:
return (colours[colour] >> 8) & 0xFF;
case BLUE:
return (colours[colour] >> 0) & 0xFF;
default:
return (colours[colour] >> 16) & 0xFF;
}
}
public byte getPattern() {
return pattern;
}
public int getColour(int i) {
return colours[i];
}
@Override
public String toString() {
return byteArrayToHex(getByteArray());
}
public static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder();
for(byte b: a)
sb.append(String.format("%02x", b&0xff));
return sb.toString();
}
public short getCrest() {
return crest;
}
}