/
BlockEnhancedCauldron.java
294 lines (249 loc) · 9.13 KB
/
BlockEnhancedCauldron.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
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
package knightminer.inspirations.recipes.block;
import java.util.Locale;
import java.util.Random;
import javax.annotation.Nonnull;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.library.util.TextureBlockUtil;
import knightminer.inspirations.recipes.client.BoilingParticle;
import knightminer.inspirations.recipes.tileentity.TileCauldron;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCauldron;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.ParticleManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.property.PropertyString;
public class BlockEnhancedCauldron extends BlockCauldron implements ITileEntityProvider {
public static final PropertyEnum<CauldronContents> CONTENTS = PropertyEnum.create("contents", CauldronContents.class);
public static final PropertyInteger LEVELS = PropertyInteger.create("levels", 0, 4);
public static final PropertyBool BOILING = PropertyBool.create("boiling");
public static final PropertyString TEXTURE = TextureBlockUtil.TEXTURE_PROP;
public BlockEnhancedCauldron() {
IBlockState state = this.blockState.getBaseState()
.withProperty(LEVEL, 0)
.withProperty(BOILING, false)
.withProperty(CONTENTS, CauldronContents.FLUID);
if(Config.enableBiggerCauldron) {
state = state.withProperty(LEVELS, 0);
}
this.setDefaultState(state);
this.setHardness(2.0F);
this.setUnlocalizedName("cauldron");
this.hasTileEntity = true;
}
/* TE behavior */
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileCauldron();
}
@Override
public void fillWithRain(World world, BlockPos pos) {
TileEntity te = world.getTileEntity(pos);
// do not fill unless the current contents are water
if(te instanceof TileCauldron && !((TileCauldron) te).isWater()) {
return;
}
// allow disabling the random 1/20 chance
if((Config.fasterCauldronRain || world.rand.nextInt(20) == 0)
&& world.getBiomeProvider().getTemperatureAtHeight(world.getBiome(pos).getTemperature(pos), pos.getY()) >= 0.15F) {
IBlockState state = world.getBlockState(pos);
int level = getLevel(state);
if(level < (Config.enableBiggerCauldron ? 4 : 3)) {
setWaterLevel(world, pos, state, level+1);
}
}
}
/**
* Called When an Entity Collided with the Block
*/
@Override
public void onEntityCollidedWithBlock(World world, BlockPos pos, IBlockState state, Entity entity) {
TileEntity te = world.getTileEntity(pos);
// do not estinguish unless the current contents are water
if(!(te instanceof TileCauldron)) {
return;
}
if(world.isRemote) {
return;
}
// ensure the entity is touching the fluid inside
int level = getLevel(state);
float f = pos.getY() + ((Config.enableBiggerCauldron ? 2.5F : 5.5F) + 3 * Math.max(level, 1)) / 16.0F;
if (entity.getEntityBoundingBox().minY <= f) {
// if so, have the TE handle it
int newLevel = ((TileCauldron)te).onEntityCollide(entity, level, state);
// if the level changed, update it
if(level != newLevel) {
this.setWaterLevel(world, pos, state, newLevel);
}
}
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
int level = getLevel(state);
if(!Config.dropCauldronContents || level == 0) {
return;
}
TileEntity te = world.getTileEntity(pos);
if(te instanceof TileCauldron) {
((TileCauldron)te).onBreak(pos, level);
}
super.breakBlock(world, pos, state);
}
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
this.onBlockHarvested(world, pos, state, player);
world.setBlockState(pos, Blocks.AIR.getDefaultState(), world.isRemote ? 11 : 3);
return world.getBlockState(pos).getBlock() != Blocks.CAULDRON;
}
/* Content texture */
@Override
protected ExtendedBlockState createBlockState() {
if(Config.enableBiggerCauldron) {
return new ExtendedBlockState(this, new IProperty[]{LEVEL, LEVELS, CONTENTS, BOILING}, new IUnlistedProperty[]{TEXTURE});
}
return new ExtendedBlockState(this, new IProperty[]{LEVEL, CONTENTS, BOILING}, new IUnlistedProperty[]{TEXTURE});
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) {
TileEntity te = world.getTileEntity(pos);
if(te instanceof TileCauldron) {
state = state.withProperty(CONTENTS, ((TileCauldron)te).getContentType());
}
return state;
}
@Nonnull
@Override
public IBlockState getExtendedState(@Nonnull IBlockState state, IBlockAccess world, BlockPos pos) {
IExtendedBlockState extendedState = (IExtendedBlockState) state;
TileEntity te = world.getTileEntity(pos);
if(te instanceof TileCauldron) {
return ((TileCauldron)te).writeExtendedBlockState(extendedState);
}
return super.getExtendedState(state, world, pos);
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
return true; // all moved to the cauldron registry
}
/* boiling */
@Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block blockIn, BlockPos fromPos) {
setBoiling(world, pos, state);
}
@Override
public void onBlockAdded(World world, BlockPos pos, IBlockState state) {
setBoiling(world, pos, state);
}
private static void setBoiling(World world, BlockPos pos, IBlockState state) {
world.setBlockState(pos, state.withProperty(BOILING, InspirationsRegistry.isCauldronFire(world.getBlockState(pos.down()))));
}
@SideOnly(Side.CLIENT)
@Override
public void randomDisplayTick(IBlockState state, World world, BlockPos pos, Random rand) {
if(!state.getValue(BOILING)) {
return;
}
int level = getLevel(state);
if(level == 0) {
return;
}
ParticleManager manager = Minecraft.getMinecraft().effectRenderer;
for(int i = 0; i < 2; i++) {
double x = pos.getX() + 0.1875D + (rand.nextFloat() * 0.625D);
double y = pos.getY() + (Config.enableBiggerCauldron ? 0.1875 : 0.375D) + (level * 0.1875D);
double z = pos.getZ() + 0.1875D + (rand.nextFloat() * 0.625D);
manager.addEffect(new BoilingParticle(world, x, y, z, 0, 0, 0));
}
}
/* 4 bottle support */
@Override
public void setWaterLevel(World worldIn, BlockPos pos, IBlockState state, int level) {
// if 4, set 4 prop
if(Config.enableBiggerCauldron) {
state = state.withProperty(LEVELS, MathHelper.clamp(level, 0, 4));
}
worldIn.setBlockState(pos, state.withProperty(LEVEL, MathHelper.clamp(level, 0, 3)), 2);
worldIn.updateComparatorOutputLevel(pos, this);
}
public static int getCauldronLevel(IBlockState state) {
Block block = state.getBlock();
if(state.getBlock() instanceof BlockEnhancedCauldron) {
return ((BlockEnhancedCauldron)block).getLevel(state);
}
return state.getValue(LEVEL);
}
public int getLevel(IBlockState state) {
if(Config.enableBiggerCauldron) {
return state.getValue(LEVELS);
}
return state.getValue(LEVEL);
}
@Override
public int getComparatorInputOverride(IBlockState state, World worldIn, BlockPos pos) {
return getLevel(state);
}
/**
* Convert the given metadata into a BlockState for this Block
*/
@Override
public IBlockState getStateFromMeta(int meta) {
IBlockState state = this.getDefaultState().withProperty(LEVEL, MathHelper.clamp(meta & 7, 0, 3));
// if 4, set 4 prop
if(Config.enableBiggerCauldron) {
state = state.withProperty(LEVELS, MathHelper.clamp(meta & 7, 0, 4));
}
state = state.withProperty(BOILING, (meta & 8) > 0);
return state;
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state) {
return getLevel(state) | (state.getValue(BOILING) ? 8 : 0);
}
public static enum CauldronContents implements IStringSerializable {
FLUID,
DYE,
POTION;
private int meta;
CauldronContents() {
this.meta = ordinal();
}
@Override
public String getName() {
return name().toLowerCase(Locale.US);
}
public int getMeta() {
return meta;
}
public static CauldronContents fromMeta(int meta) {
if(meta > values().length) {
meta = 0;
}
return values()[meta];
}
}
}