/
TileEntityWindGenerator.java
186 lines (156 loc) · 4.55 KB
/
TileEntityWindGenerator.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
package mekanism.generators.common.tile;
import io.netty.buffer.ByteBuf;
import mekanism.api.Coord4D;
import mekanism.common.base.IBoundingBlock;
import mekanism.common.base.TileNetworkList;
import mekanism.common.config.MekanismConfig;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.MekanismUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class TileEntityWindGenerator extends TileEntityGenerator implements IBoundingBlock
{
public static final float SPEED = 32F;
public static final float SPEED_SCALED = 256F/SPEED;
/** The angle the blades of this Wind Turbine are currently at. */
public double angle;
public float currentMultiplier;
public TileEntityWindGenerator()
{
super("wind", "WindGenerator", 200000, (MekanismConfig.current().generators.windGenerationMax.val())*2);
inventory = NonNullList.withSize(1, ItemStack.EMPTY);
}
@Override
public void onUpdate()
{
super.onUpdate();
if(!world.isRemote)
{
ChargeUtils.charge(0, this);
if(ticker % 20 == 0)
{
setActive((currentMultiplier = getMultiplier()) > 0);
}
if(getActive())
{
setEnergy(electricityStored + (MekanismConfig.current().generators.windGenerationMin.val()*currentMultiplier));
}
}
else {
if(getActive())
{
angle = (angle+(getPos().getY()+4F)/SPEED_SCALED) % 360;
}
}
}
@Override
public void handlePacketData(ByteBuf dataStream)
{
super.handlePacketData(dataStream);
if(world.isRemote)
{
currentMultiplier = dataStream.readFloat();
}
}
@Override
public TileNetworkList getNetworkedData(TileNetworkList data)
{
super.getNetworkedData(data);
data.add(currentMultiplier);
return data;
}
/** Determines the current output multiplier, taking sky visibility and height into account. **/
public float getMultiplier()
{
if (isInBlacklistedDimension())
{
return 0;
}
if(world.canSeeSky(getPos().add(0, 4, 0)))
{
final float minY = (float) MekanismConfig.current().generators.windGenerationMinY.val();
final float maxY = (float) MekanismConfig.current().generators.windGenerationMaxY.val();
final float minG = (float) MekanismConfig.current().generators.windGenerationMin.val();
final float maxG = (float) MekanismConfig.current().generators.windGenerationMax.val();
final float slope = (maxG - minG) / (maxY - minY);
final float intercept = minG - slope * minY;
final float clampedY = Math.min(maxY, Math.max(minY, (float)(getPos().getY()+4)));
final float toGen = slope * clampedY + intercept;
return toGen / minG;
}
else {
return 0;
}
}
public boolean isInBlacklistedDimension()
{
return MekanismConfig.current().generators.windGenerationBlacklist.val().contains(world.provider.getDimension());
}
@Override
@SideOnly(Side.CLIENT)
public float getVolume()
{
return 1.5F*super.getVolume();
}
private static final String[] methods = new String[] {"getEnergy", "getOutput", "getMaxEnergy", "getEnergyNeeded", "getMultiplier"};
@Override
public String[] getMethods()
{
return methods;
}
@Override
public Object[] invoke(int method, Object[] arguments) throws Exception
{
switch(method)
{
case 0:
return new Object[] {electricityStored};
case 1:
return new Object[] {output};
case 2:
return new Object[] {BASE_MAX_ENERGY};
case 3:
return new Object[] {(BASE_MAX_ENERGY -electricityStored)};
case 4:
return new Object[] {getMultiplier()};
default:
throw new NoSuchMethodException();
}
}
@Override
public boolean canOperate()
{
return electricityStored < BASE_MAX_ENERGY && getMultiplier() > 0 && MekanismUtils.canFunction(this);
}
@Override
public void onPlace()
{
Coord4D current = Coord4D.get(this);
MekanismUtils.makeBoundingBlock(world, getPos().offset(EnumFacing.UP, 1), current);
MekanismUtils.makeBoundingBlock(world, getPos().offset(EnumFacing.UP, 2), current);
MekanismUtils.makeBoundingBlock(world, getPos().offset(EnumFacing.UP, 3), current);
MekanismUtils.makeBoundingBlock(world, getPos().offset(EnumFacing.UP, 4), current);
}
@Override
public void onBreak()
{
world.setBlockToAir(getPos().add(0, 1, 0));
world.setBlockToAir(getPos().add(0, 2, 0));
world.setBlockToAir(getPos().add(0, 3, 0));
world.setBlockToAir(getPos().add(0, 4, 0));
world.setBlockToAir(getPos());
}
@Override
public boolean renderUpdate()
{
return false;
}
@Override
public boolean lightUpdate()
{
return false;
}
}