/
TileCauldron.java
400 lines (352 loc) · 12.4 KB
/
TileCauldron.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package knightminer.inspirations.recipes.tileentity;
import java.util.List;
import javax.annotation.Nonnull;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.recipe.cauldron.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.ICauldronRecipe.CauldronState;
import knightminer.inspirations.recipes.InspirationsRecipes;
import knightminer.inspirations.recipes.block.BlockEnhancedCauldron;
import knightminer.inspirations.recipes.block.BlockEnhancedCauldron.CauldronContents;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAreaEffectCloud;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionType;
import net.minecraft.potion.PotionUtils;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.items.ItemHandlerHelper;
public class TileCauldron extends TileEntity {
public static final DamageSource DAMAGE_BOIL = new DamageSource(Util.prefix("boiling")).setDamageBypassesArmor();
private CauldronState state;
public TileCauldron() {
this.state = CauldronState.WATER;
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
return oldState.getBlock() != newSate.getBlock();
}
@Nonnull
public CauldronContents getContentType() {
if(state.getFluid() != null) {
return CauldronContents.FLUID;
}
if(state.getColor() > -1) {
return CauldronContents.DYE;
}
if(state.getPotion() != null) {
return CauldronContents.POTION;
}
return CauldronContents.FLUID;
}
/**
* Checks if this TE currently has water in it
* @return
*/
public boolean isWater() {
return state.isWater();
}
/**
* Gets the current cauldron state
* @return current state
*/
public CauldronState getState() {
return state;
}
/**
* Returns the current color for tinting
* @return block colors color
*/
public int getColor() {
switch(getContentType()) {
case DYE:
return state.getColor();
case POTION:
return PotionUtils.getPotionColor(state.getPotion());
}
Fluid fluid = state.getFluid();
if(fluid != null) {
return state.getFluid().getColor();
}
return -1;
}
/**
* Method to run cauldron interaction code. Used for both TileCauldron and simple cauldron
* @return True if successful, false for pass
*/
public static boolean interact(World world, BlockPos pos, IBlockState blockState, EntityPlayer player, EnumHand hand) {
// ensure we have a stack
ItemStack stack = player.getHeldItem(hand);
if(stack.isEmpty()) {
return false;
}
// grab the TE if extended
TileCauldron cauldron = null;
CauldronState state = CauldronState.WATER;
boolean boiling = false;
if(Config.enableExtendedCauldron) {
TileEntity te = world.getTileEntity(pos);
if(te instanceof TileCauldron) {
cauldron = (TileCauldron) te;
state = cauldron.state;
boiling = blockState.getValue(BlockEnhancedCauldron.BOILING);
}
} else {
boiling = InspirationsRegistry.isCauldronFire(world.getBlockState(pos.down()));
}
// other properties
int level = InspirationsRecipes.cauldron.getLevel(blockState);
// grab recipe
ICauldronRecipe recipe = InspirationsRegistry.getCauldronResult(stack, boiling, level, state);
if(recipe != null) {
// update properties based on the recipe
if(!world.isRemote) {
// grab state first since we may need to back out
CauldronState newState = recipe.getState(stack, boiling, level, state);
// if its not a TE, stop right here and disallow any recipes which do not return water
if(cauldron == null && !CauldronState.WATER.matches(newState)) {
return true;
}
// play sound
SoundEvent sound = recipe.getSound(stack, boiling, level, state);
if(sound != null) {
world.playSound((EntityPlayer)null, pos, sound, SoundCategory.BLOCKS, recipe.getVolume(sound), 1.0F);
}
// update level
int newLevel = recipe.getLevel(level);
if(newLevel != level) {
InspirationsRecipes.cauldron.setWaterLevel(world, pos, blockState, newLevel);
if(newLevel == 0) {
newState = CauldronState.WATER;
}
}
// update the state
if(cauldron != null && !state.matches(newState)) {
cauldron.state = newState;
world.notifyBlockUpdate(pos, blockState, blockState, 2);
}
// result
ItemStack result = recipe.getResult(stack, boiling, level, state);
// update held item
if(!player.capabilities.isCreativeMode) {
player.setHeldItem(hand, recipe.transformInput(stack, boiling, level, state));
}
// and give the new item to the player
if(!result.isEmpty()) {
ItemHandlerHelper.giveItemToPlayer(player, result, player.inventory.currentItem);
}
}
return true;
}
// if we have water, allow default actions to run, otherwise block
if(state.isWater()) {
// though skip default interactions for water bottles and water buckets if not pure water
Item item = stack.getItem();
return state != CauldronState.WATER && (item == Items.POTIONITEM || item == Items.WATER_BUCKET);
}
return true;
}
private static final String TAG_CAULDRON_CRAFTED = "cauldron_crafted";
/**
* Called when an entity collides with the cauldron
* @param entity Entity that collided
* @param level Cauldron level
* @return New cauldron level after the collision
*/
public int onEntityCollide(Entity entity, int level, IBlockState currentState) {
// if an entity item, try crafting with it
if(entity instanceof EntityItem) {
// skip items that we have already processed
EntityItem entityItem = (EntityItem)entity;
ItemStack stack = entityItem.getItem();
NBTTagCompound entityTags = entity.getEntityData();
// if its the same size as the item we tagged before, skip
if(entityTags.getInteger(TAG_CAULDRON_CRAFTED) == stack.getCount()) {
return level;
}
// try and find a recipe
boolean boiling = currentState.getValue(BlockEnhancedCauldron.BOILING);
ICauldronRecipe recipe = InspirationsRegistry.getCauldronResult(stack, boiling, level, state);
if(recipe != null) {
// update properties based on the recipe
CauldronState newState = recipe.getState(stack, boiling, level, state);
// update level
level = recipe.getLevel(level);
if(level == 0) {
newState = CauldronState.WATER;
}
// spawn the new item in the world
ItemStack result = recipe.getResult(stack, boiling, level, state);
if(!result.isEmpty()) {
EntityItem resultEntity = new EntityItem(world, entityItem.posX, entityItem.posY, entityItem.posZ, result);
resultEntity.getEntityData().setInteger(TAG_CAULDRON_CRAFTED, result.getCount());
world.spawnEntity(resultEntity);
}
// and kill the old one, or update its item
ItemStack transform = recipe.transformInput(stack, boiling, level, state);
if(transform.isEmpty()) {
entityItem.setDead();
} else {
entityItem.setItem(transform);
entityTags.setInteger(TAG_CAULDRON_CRAFTED, transform.getCount());
}
// update the state
if(!state.matches(newState)) {
state = newState;
world.notifyBlockUpdate(pos, currentState, currentState, 2);
}
} else {
entityTags.setInteger(TAG_CAULDRON_CRAFTED, stack.getCount());
}
// otherwise apply fluid special effects
} else if(level > 0) {
switch(this.getContentType()) {
case FLUID:
// water estinguishs fire
if(this.isWater()) {
if(entity.isBurning()) {
entity.extinguish();
level = level - 1;
}
} else {
// hot fluids set fire to the entity
Fluid fluid = state.getFluid();
if(fluid.getTemperature() > 450 && !entity.isImmuneToFire()) {
entity.attackEntityFrom(DamageSource.LAVA, 4.0F);
entity.setFire(15);
break;
}
}
// continue for boiling
case DYE:
// if the cauldron is boiling, boiling the entity
if (InspirationsRegistry.isCauldronFire(world.getBlockState(pos.down()))) {
entity.attackEntityFrom(DAMAGE_BOIL, 2.0F);
}
break;
case POTION:
// potions apply potion effects
if(entity instanceof EntityLivingBase) {
EntityLivingBase living = (EntityLivingBase) entity;
List<PotionEffect> effects = state.getPotion().getEffects();
// if any of the effects are not currently on the player, apply it and lower the level
if(effects.stream().anyMatch(effect -> !living.isPotionActive(effect.getPotion()))) {
for(PotionEffect effect : effects) {
if (effect.getPotion().isInstant()) {
effect.getPotion().affectEntity(living, living, living, effect.getAmplifier(), 1.0D);
} else {
living.addPotionEffect(new PotionEffect(effect));
}
}
level = level - 1;
}
}
break;
}
}
return level;
}
/**
* Called when the cauldron is broken
* @param pos Position of the cauldron
* @param level Cauldron level
*/
public void onBreak(BlockPos pos, int level) {
switch(getContentType()) {
case FLUID:
Block block = state.getFluid().getBlock();
if(block != null) {
// switch to flowing states
if(block == Blocks.WATER) {
block = Blocks.FLOWING_WATER;
} else if(block == Blocks.LAVA) {
block = Blocks.FLOWING_LAVA;
}
// height varies based on what is left. Will place a source if the cauldron is full
if(level == (Config.enableBiggerCauldron ? 4 : 3)) {
world.setBlockState(pos, block.getDefaultState());
}
}
break;
case POTION:
PotionType potion = state.getPotion();
EntityAreaEffectCloud cloud = new EntityAreaEffectCloud(this.world, pos.getX() + 0.5F, pos.getY() + 0.5F, pos.getZ() + 0.5F);
cloud.setRadius(0.5F * level + 0.5F);
cloud.setDuration(20*(level+1));
cloud.setRadiusOnUse(-0.5F);
cloud.setWaitTime(10);
cloud.setRadiusPerTick(-cloud.getRadius() / cloud.getDuration());
cloud.setPotion(potion);
for(PotionEffect effect : potion.getEffects()) {
cloud.addEffect(new PotionEffect(effect));
}
this.world.spawnEntity(cloud);
break;
}
}
public IBlockState writeExtendedBlockState(IExtendedBlockState state) {
// just pull the texture right from the fluid
if(this.state != CauldronState.WATER && getContentType() == CauldronContents.FLUID) {
Fluid fluid = this.state.getFluid();
if(fluid != null) {
state = state.withProperty(BlockEnhancedCauldron.TEXTURE, fluid.getStill().toString());
}
}
return state;
}
/* NBT */
public static final String TAG_STATE = "state";
@Nonnull
@Override
public NBTTagCompound getUpdateTag() {
// new tag instead of super since default implementation calls the super of writeToNBT
return writeToNBT(new NBTTagCompound());
}
@Nonnull
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tags) {
tags = super.writeToNBT(tags);
tags.setTag(TAG_STATE, state.writeToNBT());
return tags;
}
@Override
public void readFromNBT(NBTTagCompound tags) {
super.readFromNBT(tags);
this.state = CauldronState.fromNBT(tags.getCompoundTag(TAG_STATE));
}
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return new SPacketUpdateTileEntity(this.getPos(), this.getBlockMetadata(), state.writeToNBT());
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
CauldronState newState = CauldronState.fromNBT(pkt.getNbtCompound());
if(!this.state.matches(newState)) {
this.state = newState;
if(world.isRemote) {
Minecraft.getMinecraft().renderGlobal.notifyBlockUpdate(null, pos, null, null, 0);
}
}
}
}