From fdff19b454e064af14aceb3bd4e9eacfc2a5dcec Mon Sep 17 00:00:00 2001 From: Namikon Date: Thu, 25 Jun 2015 11:01:16 +0200 Subject: [PATCH] Added support for Potion-Effects of fluid that has been placed in the world. PotionEffects can be stacked, like IC2 radiation does --- build.gradle | 2 +- .../yamcore/fluids/ModSimpleBaseFluid.java | 112 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index f1e3106..968d455 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { apply plugin: 'forge' -version = "1.7.10-0.5.1" +version = "1.7.10-0.5.2" group= "eu.usrv.YAMCore" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "YAMCore" diff --git a/src/main/java/eu/usrv/yamcore/fluids/ModSimpleBaseFluid.java b/src/main/java/eu/usrv/yamcore/fluids/ModSimpleBaseFluid.java index 27b7b5e..5850b7c 100644 --- a/src/main/java/eu/usrv/yamcore/fluids/ModSimpleBaseFluid.java +++ b/src/main/java/eu/usrv/yamcore/fluids/ModSimpleBaseFluid.java @@ -1,21 +1,40 @@ package eu.usrv.yamcore.fluids; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.potion.PotionEffect; import net.minecraft.util.IIcon; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fluids.BlockFluidClassic; import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import eu.usrv.yamcore.YAMCore; public class ModSimpleBaseFluid extends BlockFluidClassic { public ModSimpleBaseFluid(Fluid fluid, Material material) { super(fluid, material); _mRegisterBucket = true; + _mPotionEffects = new ArrayList(); + _mStackingPotionEffects = new ArrayList(); + _mEffectsOnlyToPlayers = false; + _mRand = new Random(System.currentTimeMillis()); } + private List _mPotionEffects; + private List _mStackingPotionEffects; + private boolean _mEffectsOnlyToPlayers; + private Random _mRand; + @SideOnly(Side.CLIENT) protected IIcon stillIcon; @SideOnly(Side.CLIENT) @@ -37,6 +56,35 @@ public class ModSimpleBaseFluid extends BlockFluidClassic { return _mRegisterBucket; } + /** + * Add pEffect to the list of potion effects that will not stack over time + * @param pEffect + */ + public void addPotionEffect(PotionEffect pEffect) + { + _mPotionEffects.add(pEffect); + } + + /** + * Add pEffect to the list of potion effects that will stack over time as long + * as the entity remains in the fluid + * @param pEffect + */ + public void addStackingPotionEffect(PotionEffect pEffect) + { + _mStackingPotionEffects.add(pEffect); + } + + /** + * If set to true, the potion effects will only be applied to Players. + * If you don't set anything here, ALL entities will get the potion effects + * @param pFlag + */ + public void setApplyEffectsToPlayersOnly(boolean pFlag) + { + _mEffectsOnlyToPlayers = pFlag; + } + /** * Set the textures used to indicate still and liquid/flowing state * @param pModID ModID String, used to locate the resources @@ -101,4 +149,68 @@ public class ModSimpleBaseFluid extends BlockFluidClassic { { return _mCreativeTab; } + + @Override + public FluidStack drain(World pWorld, int pX, int pY, int pZ, boolean pDoDrain) + { + pWorld.setBlockToAir(pX, pY, pZ); + return new FluidStack(this.getFluid(), 1000); + } + + @Override + public boolean canDrain(World pWorld, int pX, int pY, int pZ) { + return true; + } + + /** + * Is triggered when an entity steps into this fluid + * @param pWorld + * @param pX + * @param pY + * @param pZ + * @param pEntity + */ + @Override + public void onEntityCollidedWithBlock(World pWorld, int pX, int pY, int pZ, Entity pEntity) { + super.onEntityCollidedWithBlock(pWorld, pX, pY, pZ, pEntity); + + try + { + if (!(pEntity instanceof EntityLivingBase)) + return; + + if (!(pEntity instanceof EntityPlayer) && _mEffectsOnlyToPlayers) + return; + + EntityLivingBase tElivBase = (EntityLivingBase)pEntity; + + for (PotionEffect tEffect : _mPotionEffects) + tElivBase.addPotionEffect(tEffect); + + if (_mRand.nextInt(10) == 0) + { + for (PotionEffect tStackingEffect : _mStackingPotionEffects) + { + int tDuration = tStackingEffect.getDuration(); + for (Object actEffObj : tElivBase.getActivePotionEffects()) + { + PotionEffect pae = (PotionEffect)actEffObj; + + if (pae.getPotionID() == tStackingEffect.getPotionID()) + { + tDuration += pae.getDuration(); + break; + } + } + + tElivBase.addPotionEffect(new PotionEffect(tStackingEffect.getPotionID(), tDuration, tStackingEffect.getAmplifier())); + } + } + } + catch(Exception e) + { + e.printStackTrace(); + YAMCore.instance.getLogger().error(String.format("Apply potioneffect failed for fluidblock %s", this.getFluid().getUnlocalizedName())); + } + } }