/
ElementalMatrix.java
344 lines (301 loc) · 8.16 KB
/
ElementalMatrix.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package fr.Jodge.elementalLibrary.data.matrix;
import io.netty.buffer.ByteBuf;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.item.ItemStack;
import com.google.gson.JsonObject;
import fr.Jodge.elementalLibrary.data.ItemHelper;
import fr.Jodge.elementalLibrary.data.element.Element;
import fr.Jodge.elementalLibrary.data.interfaces.IElementalWritable;
import fr.Jodge.elementalLibrary.log.JLog;
/**
* What is that ?
*
* Matrix is a simple one line tab that give the '+' (or '-') give to an element.
* They are 2 kind of matrix in each living entity :
* - resistMatrix
* - atkMatrix
*
* resist matrix is applied when the entity is hit.
* It's a multiplier of damage taken.
* That is to say that a monster whit 0.5 will receive only 50% of total damage.
* More high was this value, more high will be the damage.
*
*
* atk matrix is applied when and entity hit.
* Its also a multiplier but this time about damage given
* That is to say that an entity whit 1.5 will deal 50% more damage.
* More this value is around 0, less will be the damage.
*
*
* @author Jodge
* @version 1.0.0
*/
public abstract class ElementalMatrix implements IElementalWritable
{
/** number of element is unknown or can change during process, so we use a list */
protected Map<Element, Float> matrix;
/**
* The default constructor will give a basic 1.0 matrix to every entity
*/
public ElementalMatrix()
{
this(0.0F);
}
public ElementalMatrix(float base)
{
this.matrix = new HashMap<Element, Float>();
completeArray(base);
}
public ElementalMatrix(Map<Element, Float> matrix)
{
this();
for(Element element : Element.getAllElement())
{
if(matrix.containsKey(element))
{
this.matrix.put(element, matrix.get(element));
}
}
}
/**
* private method that finish to initialize array.
* It's suppose to be call whenever you create a new matrix.
*/
protected ElementalMatrix completeArray()
{
return completeArray(1.0F);
}
protected ElementalMatrix completeArray(float defaultValue)
{
// for each key in Element
for(Element element : Element.getAllElement())
{
if(!matrix.containsKey(element))
{
matrix.put(element, defaultValue);
}
}
return this;
}
public Map<Element, Float> getMatrix()
{
return this.matrix;
}
/**
* (matrix is supposed already full, so their are not add statement available)
* wrong tentative will also be add, but their will never be used
*
* @param index <i>int</i> index you want to change. you can also put and Element instead
* @param value <i>float</i> value that you want to add to this matrix
* @return itself
*/
public ElementalMatrix set(int index, float value)
{
return set(Element.findById(index), value);
}
public ElementalMatrix set(Element index, float value)
{
matrix.put(index, value);
return this;
}
public float get(int index)
{
return get(Element.findById(index));
}
public float get(Element index)
{
return get(index, 0.0F);
}
public float get(Element index, float elseValue)
{
if(matrix.containsKey(index))
{
return matrix.get(index);
}
else
{
JLog.warning("Element @" + index.hashCode() + ": " + index + " is not references in Matrix : " + this.matrix);
return elseValue;
}
}
@Override
public String toString()
{
String text = "";
for(Entry<Element, Float> entry : matrix.entrySet())
{
text += entry.getKey();
text += ":";
text += entry.getValue();
text += ";";
}
return text;
}
@Override
public void fromJsonObject(JsonObject j)
{
for(Element element : Element.getAllElement())
{
String key = element.getName();
if(j.has(key))
this.set(element, j.get(key).getAsFloat());
}
completeArray();
}
@Override
public JsonObject toJsonObject()
{
JsonObject j = new JsonObject();
for(Element element : Element.getAllElement())
{
j.addProperty(element.getName(), matrix.get(element));
}
return j;
}
@Override
public void toByte(ByteBuf buf)
{
/* Value write in buffer :
* Int => number of item (length)
* ----------------------------------
* Int => index for next value
* Float => value
* ----------------------------------
* repeat 'length' time
*/
int length = matrix.size();
buf.writeInt(length);
for(Entry<Element, Float> value : matrix.entrySet())
{
buf.writeInt(value.getKey().getId());
buf.writeFloat(value.getValue());
}
}
@Override
public void fromByte(ByteBuf buf)
{
int length = buf.readInt();
for(int i = 0; i < length; i++)
{
int key = buf.readInt();
float value = buf.readFloat();
this.matrix.put(Element.findById(key), value);
}
}
public static boolean isCorrectMatrix(Map<Element, Float> testMatrix)
{
for(Element element : testMatrix.keySet())
{
if(!Element.checkElement(element))
{
return false;
}
}
return true;
}
// CONDITION ----------------------------------------------------------------------------------------
/**
*
* @param target <i>Entity</i> Entity we want to determine if it's a FUCKING ZOMBIE !!!
* @return <i>boolean</i> true if creatureAtrribute is "undead"
*/
public static boolean isUndead(Entity target)
{
if(target instanceof EntityLivingBase)
if(((EntityLivingBase)target).getCreatureAttribute() == EnumCreatureAttribute.UNDEAD)
return true;
return false;
}
// UPTDATE ----------------------------------------------------------------------------------------
/**
*
* @param entity <i>Entity</i>
* @param oldValue <i>float</i>
* @return
*/
public float getDamageFromEntity(EntityLivingBase entity, float oldValue)
{
return oldValue;
// TODO Improve calculation base for better mod scaled result
/*
float baseDamage = 0.0F;
if(entity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.ATTACK_DAMAGE) != null)
{
baseDamage = (float)entity.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue() + oldValue - 1.0F;
}
else
{
JLog.warning("No attribut Damage available, Old value will be used ");
return oldValue;
}
return baseDamage;*/
}
/**
*
* @param target
* @param clazz
*/
protected void updateEntity(Entity target, Class<? extends ElementalMatrix> clazz)
{
boolean atLeastOne = false;
for(Element element : Element.getAllElement())
{
// Warning : Float is an object, so it can be null.
Float value = element.getDefaultValue(clazz, target.getClass(), matrix.get(element));
if(value != null)
{
// if we have a value, we put it.
matrix.put(element, value);
if(element.isActive())
atLeastOne = true;
}
}
if(!atLeastOne)
{
matrix.put(Element.findById(0), 1.0F);
}
}
/**
*
* @param stack <i>ItemStack</i> or <i>String</i> item to update matrixName is used here
* @param clazz <i>Class<? extends ElementalMatrix></i> class needed (use to have the good list of default value. Often, it's this.getClass()
* @param mustHaveOne <i>boolean</i> (optional) set this to false to prevent update to give at least 1.0F to element whit id 0.
*/
protected void updateItem(ItemStack stack, Class<? extends ElementalMatrix> clazz)
{
updateItem(ItemHelper.getMatrixName(stack), clazz);
}
protected void updateItem(ItemStack stack, Class<? extends ElementalMatrix> clazz, boolean mustHaveOne)
{
updateItem(ItemHelper.getMatrixName(stack), clazz, mustHaveOne);
}
protected void updateItem(String itemName, Class<? extends ElementalMatrix> clazz)
{
updateItem(itemName, clazz, true);
}
protected void updateItem(String itemName, Class<? extends ElementalMatrix> clazz, boolean mustHaveOne)
{
boolean atLeastOne = false;
for(Element element : Element.getAllElement())
{
// Warning : Float is an object, so it can be null.
Float value = element.getDefaultValue(clazz, itemName, matrix.get(element));
if(value != null)
{
// if we have a value, we put it.
matrix.put(element, value);
if(element.isActive())
atLeastOne = true;
}
}
if(mustHaveOne && !atLeastOne)
{
matrix.put(Element.findById(0), 1.0F);
}
}
}