aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/enderio/conduit/gas/GasConduit.java
blob: bac24cabfdd6fbc83d725c7189b43064039006e4 (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
package miscutil.enderio.conduit.gas;

import java.util.HashMap;
import java.util.Map;

import mekanism.api.gas.Gas;
import mekanism.api.gas.GasStack;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.EnderIO;
import crazypants.enderio.conduit.AbstractConduitNetwork;
import crazypants.enderio.conduit.ConnectionMode;
import crazypants.enderio.conduit.IConduit;
import crazypants.enderio.conduit.geom.CollidableComponent;
import crazypants.enderio.config.Config;
import crazypants.render.IconUtil;
import crazypants.util.BlockCoord;

public class GasConduit
  extends AbstractGasTankConduit
{
  public static final int CONDUIT_VOLUME = 1000;
  public static final String ICON_KEY = "enderio:gasConduit";
  public static final String ICON_CORE_KEY = "enderio:gasConduitCore";
  public static final String ICON_EXTRACT_KEY = "enderio:gasConduitInput";
  public static final String ICON_INSERT_KEY = "enderio:gasConduitOutput";
  public static final String ICON_EMPTY_EDGE = "enderio:gasConduitEdge";
  static final Map<String, IIcon> ICONS = new HashMap();
  private GasConduitNetwork network;
  
  @SideOnly(Side.CLIENT)
  public static void initIcons()
  {
    IconUtil.addIconProvider(new IconUtil.IIconProvider()
    {
      public void registerIcons(IIconRegister register)
      {
        GasConduit.ICONS.put("enderio:gasConduit", register.registerIcon("enderio:gasConduit"));
        GasConduit.ICONS.put("enderio:gasConduitCore", register.registerIcon("enderio:gasConduitCore"));
        GasConduit.ICONS.put("enderio:gasConduitInput", register.registerIcon("enderio:gasConduitInput"));
        GasConduit.ICONS.put("enderio:gasConduitOutput", register.registerIcon("enderio:gasConduitOutput"));
        GasConduit.ICONS.put("enderio:gasConduitEdge", register.registerIcon("enderio:gasConduitEdge"));
      }
      
      public int getTextureType()
      {
        return 0;
      }
    });
  }
  
  private long ticksSinceFailedExtract = 0L;
  public static final int MAX_EXTRACT_PER_TICK = Config.gasConduitExtractRate;
  public static final int MAX_IO_PER_TICK = Config.gasConduitMaxIoRate;
  
  public GasConduit()
  {
    updateTank();
  }
  
  public void updateEntity(World world)
  {
    super.updateEntity(world);
    if (world.isRemote) {
      return;
    }
    doExtract();
    if (this.stateDirty)
    {
      getBundle().dirty();
      this.stateDirty = false;
    }
  }
  
  private void doExtract()
  {
    BlockCoord loc = getLocation();
    if (!hasConnectionMode(ConnectionMode.INPUT)) {
      return;
    }
    if (this.network == null) {
      return;
    }
    this.ticksSinceFailedExtract += 1L;
    if ((this.ticksSinceFailedExtract > 25L) && (this.ticksSinceFailedExtract % 10L != 0L)) {
      return;
    }
    Gas f = this.tank.getGas() == null ? null : this.tank.getGas().getGas();
    for (ForgeDirection dir : this.externalConnections) {
      if ((autoExtractForDir(dir)) && 
        (this.network.extractFrom(this, dir, MAX_EXTRACT_PER_TICK))) {
        this.ticksSinceFailedExtract = 0L;
      }
    }
  }
  
  protected void updateTank()
  {
    this.tank.setCapacity(1000);
    if (this.network != null) {
      this.network.updateConduitVolumes();
    }
  }
  
  public ItemStack createItem()
  {
    return new ItemStack(EnderIO.itemGasConduit);
  }
  
  public AbstractConduitNetwork<?, ?> getNetwork()
  {
    return this.network;
  }
  
  public boolean setNetwork(AbstractConduitNetwork<?, ?> network)
  {
    if (network == null)
    {
      this.network = null;
      return true;
    }
    if (!(network instanceof GasConduitNetwork)) {
      return false;
    }
    GasConduitNetwork n = (GasConduitNetwork)network;
    if (this.tank.getGas() == null) {
      this.tank.setGas(n.getGasType() == null ? null : n.getGasType().copy());
    } else if (n.getGasType() == null) {
      n.setGasType(this.tank.getGas());
    } else if (!this.tank.getGas().isGasEqual(n.getGasType())) {
      return false;
    }
    this.network = n;
    return true;
  }
  
  public boolean canConnectToConduit(ForgeDirection direction, IConduit con)
  {
    if (!super.canConnectToConduit(direction, con)) {
      return false;
    }
    if (!(con instanceof GasConduit)) {
      return false;
    }
    if ((getGasType() != null) && (((GasConduit)con).getGasType() == null)) {
      return false;
    }
    return GasConduitNetwork.areGassCompatable(getGasType(), ((GasConduit)con).getGasType());
  }
  
  public void setConnectionMode(ForgeDirection dir, ConnectionMode mode)
  {
    super.setConnectionMode(dir, mode);
    refreshInputs(dir);
  }
  
  private void refreshInputs(ForgeDirection dir)
  {
    if (this.network == null) {
      return;
    }
    GasOutput lo = new GasOutput(getLocation().getLocation(dir), dir.getOpposite());
    this.network.removeInput(lo);
    if ((getConnectionMode(dir).acceptsOutput()) && (containsExternalConnection(dir))) {
      this.network.addInput(lo);
    }
  }
  
  public void externalConnectionAdded(ForgeDirection fromDirection)
  {
    super.externalConnectionAdded(fromDirection);
    refreshInputs(fromDirection);
  }
  
  public void externalConnectionRemoved(ForgeDirection fromDirection)
  {
    super.externalConnectionRemoved(fromDirection);
    refreshInputs(fromDirection);
  }
  
  public IIcon getTextureForState(CollidableComponent component)
  {
    if (component.dir == ForgeDirection.UNKNOWN) {
      return (IIcon)ICONS.get("enderio:gasConduitCore");
    }
    return (IIcon)ICONS.get("enderio:gasConduit");
  }
  
  public IIcon getTextureForInputMode()
  {
    return (IIcon)ICONS.get("enderio:gasConduitInput");
  }
  
  public IIcon getTextureForOutputMode()
  {
    return (IIcon)ICONS.get("enderio:gasConduitOutput");
  }
  
  public IIcon getNotSetEdgeTexture()
  {
    return (IIcon)ICONS.get("enderio:gasConduitEdge");
  }
  
  public IIcon getTransmitionTextureForState(CollidableComponent component)
  {
    if ((isActive()) && (this.tank.containsValidGas())) {
      return this.tank.getGas().getGas().getIcon();
    }
    return null;
  }
  
  public int receiveGas(ForgeDirection from, GasStack resource)
  {
    if ((this.network == null) || (!getConnectionMode(from).acceptsInput())) {
      return 0;
    }
    return this.network.fill(from, resource, true);
  }
  
  public GasStack drawGas(ForgeDirection from, int maxDrain)
  {
    if ((this.network == null) || (!getConnectionMode(from).acceptsOutput())) {
      return null;
    }
    return this.network.drain(from, maxDrain, true);
  }
  
  public boolean canReceiveGas(ForgeDirection from, Gas gas)
  {
    if (this.network == null) {
      return false;
    }
    return (getConnectionMode(from).acceptsInput()) && (GasConduitNetwork.areGassCompatable(getGasType(), new GasStack(gas, 0)));
  }
  
  public boolean canDrawGas(ForgeDirection from, Gas gas)
  {
    if (this.network == null) {
      return false;
    }
    return (getConnectionMode(from).acceptsOutput()) && (GasConduitNetwork.areGassCompatable(getGasType(), new GasStack(gas, 0)));
  }
  
  protected boolean canJoinNeighbour(IGasConduit n)
  {
    return n instanceof GasConduit;
  }
  
  public AbstractGasTankConduitNetwork<? extends AbstractGasTankConduit> getTankNetwork()
  {
    return this.network;
  }
}