summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/util/JewelryNBT.java
blob: d76c84eb98990d265f66f96fc3badc9860d5aaf4 (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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package darkknight.jewelrycraft.util;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class JewelryNBT
{
    //TODO NBT Tag Adding
    /** 
     * 
     * @param item The item you want to add the NBT data on
     * @param metal The metal you want to add on the item
     */
    public static void addMetal(ItemStack item, ItemStack metal)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound ingotNBT = new NBTTagCompound();
        metal.writeToNBT(ingotNBT);
        itemStackData.setTag("ingot", ingotNBT);
    }

    /**
     * 
     * @param item The item you want to add the NBT data on
     * @param jewel The jewel you want to add on the item
     */
    public static void addJewel(ItemStack item, ItemStack jewel)
    {
        if(jewel != null)
        {
            NBTTagCompound itemStackData;
            if (item.hasTagCompound())
                itemStackData = item.getTagCompound();
            else
            {
                itemStackData = new NBTTagCompound();
                item.setTagCompound(itemStackData);
            }
            NBTTagCompound jewelNBT = new NBTTagCompound();
            jewel.writeToNBT(jewelNBT);
            itemStackData.setTag("jewel", jewelNBT);
        }
    }

    /**
     * 
     * @param item The item you want to add the NBT data on
     * @param modifier The modifier you want to add on the item
     */
    public static void addModifier(ItemStack item, ItemStack modifier)
    {
        if(modifier != null)
        {
            NBTTagCompound itemStackData;
            if (item.hasTagCompound())
                itemStackData = item.getTagCompound();
            else
            {
                itemStackData = new NBTTagCompound();
                item.setTagCompound(itemStackData);
            }
            NBTTagCompound modifierNBT = new NBTTagCompound();
            modifier.writeToNBT(modifierNBT);
            itemStackData.setTag("modifier", modifierNBT);
        }
    }

    public static void addEntity(ItemStack item, EntityLivingBase entity)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound entityNBT = new NBTTagCompound();
        entity.writeToNBT(entityNBT);
        itemStackData.setTag("entity", entityNBT);
    }

    public static void addEntityID(ItemStack item, EntityLivingBase entity)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound entityNBT = new NBTTagCompound();
        int id = EntityList.getEntityID(entity); 
        entityNBT.setInteger("entityID", id);
        itemStackData.setTag("entityID", entityNBT);
    }

    public static void addCoordonates(ItemStack item, double x, double y, double z)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound coords = new NBTTagCompound();
        coords.setDouble("x", x);
        coords.setDouble("y", y);
        coords.setDouble("z", z);
        itemStackData.setTag("x", coords);
        itemStackData.setTag("y", coords);
        itemStackData.setTag("z", coords);
    }

    public static void addTileEntityBlock(ItemStack item, World world, int x, int y, int z)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound tileNBT = new NBTTagCompound();
        NBTTagCompound block = new NBTTagCompound();
        world.getTileEntity(x, y, z).writeToNBT(tileNBT);
        itemStackData.setTag("tile", tileNBT);
        block.setInteger("blockID", Block.getIdFromBlock(world.getBlock(x, y, z)));
        block.setInteger("metadata", world.getBlockMetadata(x, y, z));
        block.setInteger("blockX", x);
        block.setInteger("blockY", y);
        block.setInteger("blockZ", z);
        itemStackData.setTag("metadata", block);
        itemStackData.setTag("blockID", block);
        itemStackData.setTag("blockX", block);
        itemStackData.setTag("blockY", block);
        itemStackData.setTag("blockZ", block);
    }

    public static void addBlock(ItemStack item, int block, int metadata)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound blockNBT = new NBTTagCompound();
        blockNBT.setInteger("blockID", block);
        itemStackData.setTag("blockID", blockNBT);
        blockNBT.setInteger("metadata", metadata);
        itemStackData.setTag("metadata", blockNBT);
    }

    public static void addBlockCoordonates(ItemStack item, int x, int y, int z)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound coords = new NBTTagCompound();
        coords.setInteger("blockX", x);
        coords.setInteger("blockY", y);
        coords.setInteger("blockZ", z);
        itemStackData.setTag("blockX", coords);
        itemStackData.setTag("blockY", coords);
        itemStackData.setTag("blockZ", coords);
    }

    public static void addCoordonatesAndDimension(ItemStack item, double x, double y, double z, int dim, String name)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound coords = new NBTTagCompound();
        coords.setDouble("x", x);
        coords.setDouble("y", y);
        coords.setDouble("z", z);
        coords.setInteger("dimension", dim);
        coords.setString("dimName", name);
        itemStackData.setTag("x", coords);
        itemStackData.setTag("y", coords);
        itemStackData.setTag("z", coords);
        itemStackData.setTag("dimension", coords);
        itemStackData.setTag("dimName", coords);
    }

    public static void addMode(ItemStack item, String modeN)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound mode = new NBTTagCompound();
        mode.setString("mode", modeN);
        itemStackData.setTag("mode", mode);
    }

    public static void addFakeEnchantment(ItemStack item)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        itemStackData.setTag("ench", new NBTTagList());
    }

    public static void addIngotColor(ItemStack item, int color)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound colors = new NBTTagCompound();
        colors.setInteger("ingotColor", color);
        itemStackData.setTag("ingotColor", colors);
    }

    public static void addJewelColor(ItemStack item, int color)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound colors = new NBTTagCompound();
        colors.setInteger("jewelColor", color);
        itemStackData.setTag("jewelColor", colors);
    }

    @SuppressWarnings("rawtypes")
    public static void addEntities(ItemStack item, List list)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        NBTTagCompound entityNBT = new NBTTagCompound();
        for(int i=0; i < list.size(); i++) ((EntityLivingBase) list.get(i)).writeToNBT(entityNBT);
        itemStackData.setTag("entities", entityNBT);
    }

    //TODO NBT Tag Removing    
    public static void removeNBT(ItemStack item, String tag)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        itemStackData.removeTag(tag);
    }

    public static void removeEntity(ItemStack item)
    {
        JewelryNBT.removeNBT(item, "entityID");
        JewelryNBT.removeNBT(item, "entity");
        JewelryNBT.removeNBT(item, "ench");
    }

    public static void removeBlock(ItemStack item)
    {
        JewelryNBT.removeNBT(item, "blockID");
        JewelryNBT.removeNBT(item, "metadata");
        JewelryNBT.removeNBT(item, "tile");
        JewelryNBT.removeNBT(item, "blockX");
        JewelryNBT.removeNBT(item, "blockY");
        JewelryNBT.removeNBT(item, "blockZ");
    }

    //TODO NTB Tag Checking
    public static boolean hasTag(ItemStack item, String tag)
    {
        NBTTagCompound itemStackData;
        if (item.hasTagCompound())
            itemStackData = item.getTagCompound();
        else
        {
            itemStackData = new NBTTagCompound();
            item.setTagCompound(itemStackData);
        }
        if(itemStackData.hasKey(tag)) return true;
        return false;
    }

    public static boolean isJewelX(ItemStack stack, ItemStack jewel)
    {
        if(jewel(stack) != null && jewel(stack).getItem() == jewel.getItem() && jewel(stack).getItemDamage() == jewel.getItemDamage()) return true;
        return false;
    }

    public static boolean isModifierX(ItemStack stack, ItemStack modifier)
    {
        if(modifier(stack) != null && modifier(stack).getItem() == modifier.getItem() && modifier(stack).getItemDamage() == modifier.getItemDamage()) return true;
        return false;
    }

    public static boolean isModifierEffectType(ItemStack stack)
    {
        if(modifier(stack) != null && (isModifierX(stack, new ItemStack(Items.blaze_powder)) || isModifierX(stack, new ItemStack(Items.sugar))
                || isModifierX(stack, new ItemStack(Items.iron_pickaxe)) || isModifierX(stack, new ItemStack(Items.feather))
                || isModifierX(stack, new ItemStack(Items.potionitem, 1, 8270)))) return true;
        return false;
    }

    public static boolean isIngotX(ItemStack stack, ItemStack ingot)
    {
        if(ingot(stack) != null && ingot(stack).getItem() == ingot.getItem() && ingot(stack).getItemDamage() == ingot.getItemDamage()) return true;
        return false;
    }

    public static boolean isModeX(ItemStack stack, String modeN)
    {
        if(modeName(stack) != null && modeName(stack).equals(modeN)) return true;
        return false;
    }

    public static boolean isEntityX(ItemStack stack, EntityPlayer player, EntityLivingBase entity)
    {
        if(entity != null && entity instanceof EntityLivingBase && entity(stack, player) != null && entity(stack, player).equals(entity)) return true;
        return false;
    }

    public static boolean isDimNameX(ItemStack stack, String dimName)
    {
        if(ingot(stack) != null && dimName(stack).equals(dimName)) return true;
        return false;
    }

    public static boolean isDimensionX(ItemStack stack, int dimension)
    {
        if(dimension(stack) != -2 && dimension(stack) == dimension) return true;
        return false;
    }

    //TODO Return components based on NBT

    public static ItemStack jewel(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("jewel"))
        {
            NBTTagCompound jewelNBT = (NBTTagCompound) stack.getTagCompound().getTag("jewel");
            ItemStack jewel = new ItemStack(Item.getItemById(0), 0, 0);
            jewel.readFromNBT(jewelNBT);
            return jewel;
        }
        return null;
    }

    public static ItemStack modifier(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("modifier"))
        {
            NBTTagCompound modifierNBT = (NBTTagCompound) stack.getTagCompound().getTag("modifier");
            ItemStack modifier = new ItemStack(Item.getItemById(0), 0, 0);
            modifier.readFromNBT(modifierNBT);
            return modifier;
        }
        return null;
    }

    public static ItemStack ingot(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("ingot"))
        {
            NBTTagCompound ingotNBT = (NBTTagCompound) stack.getTagCompound().getTag("ingot");
            ItemStack ingot = new ItemStack(Item.getItemById(0), 0, 0);
            ingot.readFromNBT(ingotNBT);
            return ingot;
        }
        return null;
    }

    public static EntityLivingBase entity(ItemStack stack, EntityPlayer player)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("entityID") && stack.getTagCompound().hasKey("entity"))
        {
            NBTTagCompound enID = (NBTTagCompound) stack.getTagCompound().getTag("entityID");
            NBTTagCompound en = (NBTTagCompound) stack.getTagCompound().getTag("entity");
            int entityID = 0;
            entityID = enID.getInteger("entityID");
            EntityLivingBase entity = (EntityLivingBase) EntityList.createEntityByID(entityID, player.worldObj);
            if(entity != null && entity instanceof EntityLivingBase)
            {
                entity.readFromNBT(en);
                return entity;
            }
            else return null;
        }
        return null;
    }

    public static TileEntity tileEntity(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("tile"))
        {
            NBTTagCompound tileNBT = (NBTTagCompound) stack.getTagCompound().getTag("tile");
            TileEntity tile = (TileEntity) TileEntity.createAndLoadEntity(tileNBT);
            if(tile != null && tile instanceof TileEntity)
            {
                tile.readFromNBT(tileNBT);
                return tile;
            }
            else return null;
        }
        return null;
    }

    public static String dimName(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("dimName"))
        {
            NBTTagCompound dim = (NBTTagCompound) stack.getTagCompound().getTag("dimName");
            String name = dim.getString("dimName");
            return name;
        }
        return null;
    }

    public static String modeName(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("mode"))
        {
            NBTTagCompound dim = (NBTTagCompound) stack.getTagCompound().getTag("mode");
            String name = dim.getString("mode");
            return name;
        }
        return null;
    }

    public static int dimension(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("dimension"))
        {
            NBTTagCompound dim = (NBTTagCompound) stack.getTagCompound().getTag("dimension");
            int dimension = dim.getInteger("dimension");
            return dimension;
        }
        return -2;
    }

    public static int blockCoordX(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("blockX"))
        {
            NBTTagCompound x = (NBTTagCompound) stack.getTagCompound().getTag("blockX");
            int posX = x.getInteger("blockX");
            return posX;
        }
        return -1;
    }

    public static int blockCoordY(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("blockY"))
        {
            NBTTagCompound y = (NBTTagCompound) stack.getTagCompound().getTag("blockY");
            int posY = y.getInteger("blockY");
            return posY;
        }
        return -1;
    }

    public static int blockCoordZ(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("blockZ"))
        {
            NBTTagCompound z = (NBTTagCompound) stack.getTagCompound().getTag("blockZ");
            int posZ = z.getInteger("blockZ");
            return posZ;
        }
        return -1;
    }

    public static int blockID(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("blockID"))
        {
            NBTTagCompound blockID = (NBTTagCompound) stack.getTagCompound().getTag("blockID");
            int blockId = blockID.getInteger("blockID");
            return blockId;
        }
        return -1;
    }

    public static int blockMetadata(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("metadata"))
        {
            NBTTagCompound metadataNBT = (NBTTagCompound) stack.getTagCompound().getTag("metadata");
            int metadata = metadataNBT.getInteger("metadata");
            return metadata;
        }
        return -1;
    }

    public static double playerPosX(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("x"))
        {
            NBTTagCompound x = (NBTTagCompound) stack.getTagCompound().getTag("x");
            double posX = x.getDouble("x");
            return posX;
        }
        return -1;
    }

    public static double playerPosY(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("y"))
        {
            NBTTagCompound y = (NBTTagCompound) stack.getTagCompound().getTag("y");
            double posY = y.getDouble("y");
            return posY;
        }
        return -1;
    }

    public static double playerPosZ(ItemStack stack)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("z"))
        {
            NBTTagCompound z = (NBTTagCompound) stack.getTagCompound().getTag("z");
            double posZ = z.getDouble("z");
            return posZ;
        }
        return -1;
    }

    public static int ingotColor(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("ingotColor"))
        {
            NBTTagCompound colors = (NBTTagCompound) stack.getTagCompound().getTag("ingotColor");
            int color = colors.getInteger("ingotColor");
            return color;
        }
        return 16777215;
    }

    public static int jewelColor(ItemStack stack)
    {
        if(stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.hasTagCompound() && stack.getTagCompound().hasKey("jewelColor"))
        {
            NBTTagCompound colors = (NBTTagCompound) stack.getTagCompound().getTag("jewelColor");
            int color = colors.getInteger("jewelColor");
            return color;
        }
        return 16777215;
    }

    @SuppressWarnings({ "rawtypes", "unchecked", "null" })
    public static List entities(ItemStack stack, EntityPlayer player)
    {
        if (stack != null && stack != new ItemStack(Item.getItemById(0), 0, 0) && stack.getTagCompound().hasKey("entities"))
        {
            NBTTagCompound enID = (NBTTagCompound) stack.getTagCompound().getTag("entitiesID");
            List list = null;
            int[] entityID;
            EntityLivingBase entity;
            entityID = enID.getIntArray("entitiesID");
            for(int i = 0; i < entityID.length; i++){
                entity = (EntityLivingBase) EntityList.createEntityByID(entityID[i], player.worldObj);
                list.add(entity);
            }
            return list;
        }
        return null;
    }
}