Skip to content

Commit

Permalink
Cauldrons now try and perform recipes when dropping an item in
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jul 2, 2018
1 parent 93dc9d7 commit 1559f24
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 66 deletions.
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -101,10 +102,10 @@ public void onEntityCollidedWithBlock(World world, BlockPos pos, IBlockState sta

// ensure the entity is touching the fluid inside
int level = getLevel(state);
float f = pos.getY() + ((Config.enableBiggerCauldron ? 2.5F : 5.5F) + 3 * level) / 16.0F;
if (level > 0 && entity.getEntityBoundingBox().minY <= f) {
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);
int newLevel = ((TileCauldron)te).onEntityCollide(entity, level, state);
// if the level changed, update it
if(level != newLevel) {
this.setWaterLevel(world, pos, state, newLevel);
Expand Down Expand Up @@ -151,16 +152,47 @@ public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPo
if(te instanceof TileCauldron) {
state = state.withProperty(CONTENTS, ((TileCauldron)te).getContentType());
}

// add boiling
state = state.withProperty(BOILING, InspirationsRegistry.isCauldronFire(world.getBlockState(pos.down())));
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.getActualState(world, pos).getValue(BOILING)) {
if(!state.getValue(BOILING)) {
return;
}

Expand All @@ -178,23 +210,6 @@ public void randomDisplayTick(IBlockState state, World world, BlockPos pos, Rand
}
}

@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
}

/* 4 bottle support */
@Override
Expand Down Expand Up @@ -224,11 +239,12 @@ public int getComparatorInputOverride(IBlockState state, World worldIn, BlockPos
*/
@Override
public IBlockState getStateFromMeta(int meta) {
IBlockState state = this.getDefaultState().withProperty(LEVEL, MathHelper.clamp(meta, 0, 3));
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, 0, 4));
state = state.withProperty(LEVELS, MathHelper.clamp(meta & 7, 0, 4));
}
state = state.withProperty(BOILING, (meta & 8) > 0);
return state;
}

Expand All @@ -237,7 +253,7 @@ public IBlockState getStateFromMeta(int meta) {
*/
@Override
public int getMetaFromState(IBlockState state) {
return getLevel(state);
return getLevel(state) | (state.getValue(BOILING) ? 8 : 0);
}

public static enum CauldronContents implements IStringSerializable {
Expand Down
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -110,16 +111,19 @@ public static boolean interact(World world, BlockPos pos, IBlockState blockState
// 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
boolean boiling = InspirationsRegistry.isCauldronFire(world.getBlockState(pos.down()));
int level = InspirationsRecipes.cauldron.getLevel(blockState);

// grab recipe
Expand Down Expand Up @@ -181,56 +185,109 @@ public static boolean interact(World world, BlockPos pos, IBlockState blockState
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) {
// whether a level should be consumed
switch(this.getContentType()) {
case FLUID:
// estinguish fire
if(this.isWater()) {
if(entity.isBurning()) {
entity.extinguish();
level = level - 1;
}
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 {
// set fire if the liquid is hot
Fluid fluid = state.getFluid();
if(fluid.getTemperature() > 450 && !entity.isImmuneToFire()) {
entity.attackEntityFrom(DamageSource.LAVA, 4.0F);
entity.setFire(15);
break;
}
entityItem.setItem(transform);
entityTags.setInteger(TAG_CAULDRON_CRAFTED, transform.getCount());
}
// 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);

// update the state
if(!state.matches(newState)) {
state = newState;
world.notifyBlockUpdate(pos, currentState, currentState, 2);
}
break;
case POTION:
// 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));
} 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;
}
level = level - 1;
}
}
break;
break;
}
}
return level;
}
Expand Down

0 comments on commit 1559f24

Please sign in to comment.