summaryrefslogtreecommitdiff
path: root/ihl/collector/GlassBoxTileEntity.java
blob: a05e283b349e429ed04a45b601500a579bc334e1 (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
package ihl.collector;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ic2.core.ContainerBase;
import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.block.TileEntityInventory;
import ic2.core.block.invslot.InvSlot;
import ihl.utils.IHLUtils;

public class GlassBoxTileEntity extends TileEntityInventory implements IHasGui {

    public final InvSlot invSlot;
	public boolean isGuiScreenOpened=false;

	public GlassBoxTileEntity()
	{
        super();
        this.invSlot = new InvSlot(this, "inventory", 0, InvSlot.Access.IO, 16);
	}
	
	@Override
	public ItemStack getWrenchDrop(EntityPlayer entityPlayer) 
	{
		for(int i=0;i<this.invSlot.size();i++)
		{
			if(this.invSlot.get(i)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.invSlot.get(i)));
		}
		return IHLUtils.getThisModItemStack("glassBoxBlock");
	}
	
	@Override
    public List<String> getNetworkedFields()
    {
		List<String> fields = super.getNetworkedFields();
		fields.add("invSlot");
		return fields;
    }
	
    public boolean enableUpdateEntity()
    {
        return IC2.platform.isSimulating();
    }
	
    @Override
	public void updateEntityServer()
    {
    		boolean needInvUpdate=false;
    			List<EntityItem> eItemList = new ArrayList<EntityItem>();
    			for(int i=0;i<this.invSlot.size();i++)
    			{
    				if(this.invSlot.get(i)==null)
    				{
    	        		if(eItemList.isEmpty())
    	        		{
    	        			eItemList = this.getEItemsList();
    	            		if(eItemList.isEmpty())
    	            		{
    	            			break;
    	            		}
    	            		else
    	            		{
    	            			EntityItem entityItem = eItemList.remove(0);
        	        			ItemStack eitem = entityItem.getEntityItem();
    	        				entityItem.setDead();
        	        			if(eitem!=null)
        	        			{
        	        				this.invSlot.put(i, eitem);
        	        				needInvUpdate=true;
        	        			}
        	            		if(eItemList.isEmpty())
        	            		{
        	            			break;
        	            		}
    	            		}
    	        		}
    	        		else
    	        		{
    	        			EntityItem entityItem = eItemList.remove(0);
    	        			ItemStack eitem = entityItem.getEntityItem();
	        				entityItem.setDead();
    	        			if(eitem!=null)
    	        			{
    	        				this.invSlot.put(i, eitem);
    	        				needInvUpdate=true;
    	        			}
    	            		if(eItemList.isEmpty())
    	            		{
    	            			break;
    	            		}
    	        		}
    				}
    				else
    				{
    		            EntityPlayer player = this.worldObj.getClosestPlayer(this.xCoord+0.5D, this.yCoord+0.5D, this.zCoord+0.5D, 0.5D);
    		            if(player!=null && player instanceof EntityPlayerMP)
    		            {
  		            			if(player.inventory.getFirstEmptyStack()>=0 && player.inventory.addItemStackToInventory(this.invSlot.get(i)))
  		            			{
    		            				this.invSlot.put(i, null);
    		            				needInvUpdate=true;
   		            			}
    		            }
    				}
    			}
    		if(needInvUpdate)
    		{
    			IC2.network.get().updateTileEntityField(this, "invSlot");
    		}
    }
    
    @Override
	public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);
    }

    @Override
	public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
    }
	
	@Override
	@SideOnly(Side.CLIENT)
	public GuiScreen getGui(EntityPlayer arg0, boolean arg1) 
	{
		return new GlassBoxGui(new GlassBoxContainer(arg0, this));
	}

	@Override
	public ContainerBase<?> getGuiContainer(EntityPlayer arg0) {
		return new GlassBoxContainer(arg0, this);
	}

	@Override
	public void onGuiClosed(EntityPlayer arg0) 	
	{
		this.isGuiScreenOpened=false;
	}

	@Override
	public String getInventoryName() {
		return "glass_box";
	}
	
    public boolean getGui(EntityPlayer player)
    {
    		this.isGuiScreenOpened = this instanceof IHasGui ? (IC2.platform.isSimulating() ? IC2.platform.launchGui(player, this) : true) : false;
			return this.isGuiScreenOpened;
    }
    
    @SuppressWarnings("unchecked")
	protected List<EntityItem> getEItemsList()
    {
        double range = 0.2D;
        AxisAlignedBB searchArea = AxisAlignedBB.getBoundingBox(this.xCoord,this.yCoord,this.zCoord,this.xCoord+1.0D,this.yCoord+1.0D+range,this.zCoord+1.0D);
        List<EntityItem> eItemsList = this.worldObj.getEntitiesWithinAABB(EntityItem.class, searchArea);
    	return eItemsList;
    }
    
    @Override
    public void setInventorySlotContents(int var1, ItemStack var2)
    {
    	super.setInventorySlotContents(var1, var2);
    	IC2.network.get().updateTileEntityField(this, "invSlot");
    }
    
    public void dropContents()
    {
		for(int i=0;i<this.invSlot.size();i++)
		{
			if(this.invSlot.get(i)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.invSlot.get(i)));
		}
    }
    
	@Override
	public short getFacing()
	{
		return 3;
	}
}