BloodMagic/src/main/java/WayofTime/bloodmagic/apibutnotreally/util/helper/PlayerSacrificeHelper.java

135 lines
4.8 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.apibutnotreally.util.helper;
2015-11-07 11:51:41 -05:00
import WayofTime.bloodmagic.ConfigHandler;
import WayofTime.bloodmagic.apibutnotreally.altar.IBloodAltar;
import WayofTime.bloodmagic.apibutnotreally.event.SacrificeKnifeUsedEvent;
2017-08-15 20:21:54 -07:00
import WayofTime.bloodmagic.core.RegistrarBloodMagic;
import net.minecraft.entity.EntityLivingBase;
2015-11-07 11:51:41 -05:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
2015-11-07 11:51:41 -05:00
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
2015-11-07 11:51:41 -05:00
2017-08-15 21:30:48 -07:00
public class PlayerSacrificeHelper {
public static float scalingOfSacrifice = 1f;
2015-11-07 11:51:41 -05:00
public static int soulFrayDuration = 400;
public static Potion soulFrayId;
2017-08-15 21:30:48 -07:00
public static double getPlayerIncense(EntityPlayer player) {
2015-11-07 11:51:41 -05:00
return IncenseHelper.getCurrentIncense(player);
}
2017-08-15 21:30:48 -07:00
public static void setPlayerIncense(EntityPlayer player, double amount) {
2015-11-07 11:51:41 -05:00
IncenseHelper.setCurrentIncense(player, amount);
}
2017-08-15 21:30:48 -07:00
public static boolean incrementIncense(EntityPlayer player, double min, double incenseAddition, double increment) {
double amount = getPlayerIncense(player);
2017-08-15 21:30:48 -07:00
if (amount < min || amount >= incenseAddition) {
2015-11-07 11:51:41 -05:00
return false;
}
amount = amount + Math.min(increment, incenseAddition - amount);
2015-11-07 11:51:41 -05:00
setPlayerIncense(player, amount);
// System.out.println("Amount of incense: " + amount + ", Increment: " +
// increment);
2015-11-07 11:51:41 -05:00
return true;
}
/**
2016-05-02 20:56:32 -04:00
* Sacrifices a player's health while the player is under the influence of
* incense
2017-08-15 21:30:48 -07:00
*
* @param player - The player sacrificing
* @return Whether or not the health sacrificing succeeded
*/
2017-08-15 21:30:48 -07:00
public static boolean sacrificePlayerHealth(EntityPlayer player) {
if (player.isPotionActive(soulFrayId)) {
2015-11-07 11:51:41 -05:00
return false;
}
double amount = getPlayerIncense(player);
2015-11-07 11:51:41 -05:00
2017-08-15 21:30:48 -07:00
if (amount >= 0) {
2015-11-07 11:51:41 -05:00
float health = player.getHealth();
float maxHealth = player.getMaxHealth();
2017-08-15 21:30:48 -07:00
if (health > maxHealth / 10.0) {
2015-11-07 11:51:41 -05:00
float sacrificedHealth = health - maxHealth / 10.0f;
int lpAdded = (int) (sacrificedHealth * ConfigHandler.values.sacrificialDaggerConversion * getModifier(amount));
2015-11-07 11:51:41 -05:00
SacrificeKnifeUsedEvent evt = new SacrificeKnifeUsedEvent(player, true, true, (int) sacrificedHealth, lpAdded);
if (MinecraftForge.EVENT_BUS.post(evt))
return false;
2017-08-15 21:30:48 -07:00
if (findAndFillAltar(player.getEntityWorld(), player, evt.lpAdded, false)) {
2015-11-07 11:51:41 -05:00
player.setHealth(maxHealth / 10.0f);
setPlayerIncense(player, 0);
2017-08-15 20:21:54 -07:00
player.addPotionEffect(new PotionEffect(RegistrarBloodMagic.SOUL_FRAY, soulFrayDuration));
2015-11-07 11:51:41 -05:00
return true;
}
}
}
return false;
}
2017-08-15 21:30:48 -07:00
public static double getModifier(double amount) {
2015-11-07 11:51:41 -05:00
return 1 + amount * scalingOfSacrifice;
}
/**
* Finds the nearest {@link IBloodAltar} and attempts to fill it
2017-08-15 21:30:48 -07:00
*
* @param world - The world
* @param sacrificingEntity - The entity having the sacrifice done on (can be
* {@link EntityPlayer} for self-sacrifice)
* @param amount - The amount of which the altar should be filled
* @param isSacrifice - Whether this is a Sacrifice or a Self-Sacrifice
* @return Whether the altar is found and (attempted) filled
*/
2017-08-15 21:30:48 -07:00
public static boolean findAndFillAltar(World world, EntityLivingBase sacrificingEntity, int amount, boolean isSacrifice) {
IBloodAltar altarEntity = getAltar(world, sacrificingEntity.getPosition());
2015-11-07 11:51:41 -05:00
if (altarEntity == null)
2015-11-07 11:51:41 -05:00
return false;
altarEntity.sacrificialDaggerCall(amount, isSacrifice);
2015-11-07 11:51:41 -05:00
altarEntity.startCycle();
return true;
}
/**
* Gets the nearest {@link IBloodAltar}
2017-08-15 21:30:48 -07:00
*
* @param world - The world
* @param blockPos - The position of where the check should be in (in a 2 block
* radius from this)
2016-05-02 20:56:32 -04:00
* @return The nearest altar, if no altar is found, then this will return
2017-08-15 21:30:48 -07:00
* null
*/
2017-08-15 21:30:48 -07:00
public static IBloodAltar getAltar(World world, BlockPos blockPos) {
2015-11-07 11:51:41 -05:00
TileEntity tileEntity;
2017-08-15 21:30:48 -07:00
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 1; y++) {
for (int z = -2; z <= 2; z++) {
tileEntity = world.getTileEntity(blockPos.add(x, y, z));
2015-11-07 11:51:41 -05:00
2017-08-15 21:30:48 -07:00
if (tileEntity instanceof IBloodAltar) {
2015-11-07 11:51:41 -05:00
return (IBloodAltar) tileEntity;
}
}
}
}
return null;
}
}