aboutsummaryrefslogtreecommitdiff
path: root/main/java/gregtech/common/items/behaviors/Behaviour_Sonictron.java
blob: 781f85c80f5f67834e4eb9ff7e4850300862bcc7 (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
package gregtech.common.items.behaviors;

import gregtech.api.GregTech_API;
import gregtech.api.enums.GT_Values;
import gregtech.api.interfaces.IItemBehaviour;
import gregtech.api.interfaces.internal.IGT_Mod;
import gregtech.api.items.GT_MetaBase_Item;
import gregtech.api.util.GT_Utility;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

public class Behaviour_Sonictron
  extends Behaviour_None
{
  public static final IItemBehaviour<GT_MetaBase_Item> INSTANCE = new Behaviour_Sonictron();
  
  public boolean onItemUseFirst(GT_MetaBase_Item aItem, ItemStack aStack, EntityPlayer aPlayer, World aWorld, int aX, int aY, int aZ, int aSide, float hitX, float hitY, float hitZ)
  {
    if ((!aWorld.isRemote) && (aWorld.getBlock(aX, aY, aZ) == GregTech_API.sBlockMachines) && (aWorld.getBlockMetadata(aX, aY, aZ) == 6)) {}
    setCurrentIndex(aStack, -1);
    return false;
  }
  
  public ItemStack onItemRightClick(GT_MetaBase_Item aItem, ItemStack aStack, World aWorld, EntityPlayer aPlayer)
  {
    setCurrentIndex(aStack, 0);
    return aStack;
  }
  
  public void onUpdate(GT_MetaBase_Item aItem, ItemStack aStack, World aWorld, Entity aPlayer, int aTimer, boolean aIsInHand)
  {
    int tTickTimer = getTickTimer(aStack);
    int tCurrentIndex = getCurrentIndex(aStack);
    if ((tTickTimer++ % 2 == 0) && (tCurrentIndex > -1))
    {
      ItemStack[] tInventory = getNBTInventory(aStack);
      GT_Values.GT.doSonictronSound(tInventory[tCurrentIndex], aPlayer.worldObj, aPlayer.posX, aPlayer.posY, aPlayer.posZ);
      tCurrentIndex++;
      if (tCurrentIndex > 63) {
        tCurrentIndex = -1;
      }
    }
    setTickTimer(aStack, tTickTimer);
    setCurrentIndex(aStack, tCurrentIndex);
  }
  
  public static int getCurrentIndex(ItemStack aStack)
  {
    NBTTagCompound tNBTTagCompound = aStack.getTagCompound();
    if (tNBTTagCompound == null) {
      tNBTTagCompound = new NBTTagCompound();
    }
    return tNBTTagCompound.getInteger("mCurrentIndex");
  }
  
  public static int getTickTimer(ItemStack aStack)
  {
    NBTTagCompound tNBTTagCompound = aStack.getTagCompound();
    if (tNBTTagCompound == null) {
      tNBTTagCompound = new NBTTagCompound();
    }
    return tNBTTagCompound.getInteger("mTickTimer");
  }
  
  public static NBTTagCompound setCurrentIndex(ItemStack aStack, int aIndex)
  {
    NBTTagCompound tNBTTagCompound = aStack.getTagCompound();
    if (tNBTTagCompound == null) {
      tNBTTagCompound = new NBTTagCompound();
    }
    tNBTTagCompound.setInteger("mCurrentIndex", aIndex);
    return tNBTTagCompound;
  }
  
  public static NBTTagCompound setTickTimer(ItemStack aStack, int aTime)
  {
    NBTTagCompound tNBTTagCompound = aStack.getTagCompound();
    if (tNBTTagCompound == null) {
      tNBTTagCompound = new NBTTagCompound();
    }
    tNBTTagCompound.setInteger("mTickTimer", aTime);
    return tNBTTagCompound;
  }
  
  public static ItemStack[] getNBTInventory(ItemStack aStack)
  {
    ItemStack[] tInventory = new ItemStack[64];
    NBTTagCompound tNBT = aStack.getTagCompound();
    if (tNBT == null) {
      return tInventory;
    }
    NBTTagList tNBT_ItemList = tNBT.getTagList("Inventory", 10);
    for (int i = 0; i < tNBT_ItemList.tagCount(); i++)
    {
      NBTTagCompound tag = tNBT_ItemList.getCompoundTagAt(i);
      byte slot = tag.getByte("Slot");
      if ((slot >= 0) && (slot < tInventory.length)) {
        tInventory[slot] = GT_Utility.loadItem(tag);
      }
    }
    return tInventory;
  }
  
  public static NBTTagCompound setNBTInventory(ItemStack aStack, ItemStack[] aInventory)
  {
    NBTTagCompound tNBT = aStack.getTagCompound();
    if (tNBT == null) {
      tNBT = new NBTTagCompound();
    }
    NBTTagList tNBT_ItemList = new NBTTagList();
    for (int i = 0; i < aInventory.length; i++)
    {
      ItemStack stack = aInventory[i];
      if (stack != null)
      {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setByte("Slot", (byte)i);
        stack.writeToNBT(tag);
        tNBT_ItemList.appendTag(tag);
      }
    }
    tNBT.setTag("Inventory", tNBT_ItemList);
    aStack.setTagCompound(tNBT);
    return tNBT;
  }
  
  public static void copyInventory(ItemStack[] aInventory, ItemStack[] aNewContent, int aIndexlength)
  {
    for (int i = 0; i < aIndexlength; i++) {
      if (aNewContent[i] == null) {
        aInventory[i] = null;
      } else {
        aInventory[i] = GT_Utility.copy(new Object[] { aNewContent[i] });
      }
    }
  }
}