2016-02-25 16:19:57 -05:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
import WayofTime.bloodmagic.api.soul.DemonWillHolder;
|
|
|
|
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
|
|
|
|
import WayofTime.bloodmagic.block.BlockDemonCrystal;
|
|
|
|
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
|
2016-09-07 17:46:06 -07:00
|
|
|
import WayofTime.bloodmagic.tile.base.TileTicking;
|
2016-02-25 16:19:57 -05:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2016-02-27 19:34:42 -05:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2016-02-25 16:19:57 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-02-25 22:00:02 -05:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2016-02-25 16:19:57 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class TileDemonCrystal extends TileTicking {
|
2016-03-24 14:11:05 -04:00
|
|
|
public static final double sameWillConversionRate = 50;
|
|
|
|
public static final double defaultWillConversionRate = 100;
|
2016-02-27 16:36:56 -05:00
|
|
|
public static final double timeDelayForWrongWill = 0.6;
|
2017-08-15 21:30:48 -07:00
|
|
|
public final int maxWill = 100;
|
|
|
|
public final double drainRate = 1;
|
|
|
|
public DemonWillHolder holder = new DemonWillHolder();
|
2016-02-26 22:11:28 -05:00
|
|
|
public double progressToNextCrystal = 0;
|
|
|
|
public int internalCounter = 0;
|
2016-02-25 16:19:57 -05:00
|
|
|
public int crystalCount = 1;
|
2016-02-25 22:00:02 -05:00
|
|
|
public EnumFacing placement = EnumFacing.UP; //Side that this crystal is placed on.
|
2016-02-25 16:19:57 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public TileDemonCrystal() {
|
2016-02-25 16:19:57 -05:00
|
|
|
this.crystalCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void onUpdate() {
|
|
|
|
if (getWorld().isRemote) {
|
2016-02-25 16:19:57 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:11:28 -05:00
|
|
|
internalCounter++;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (internalCounter % 20 == 0 && crystalCount < 7) {
|
2016-02-26 22:11:28 -05:00
|
|
|
EnumDemonWillType type = EnumDemonWillType.values()[this.getBlockMetadata()];
|
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
double value = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, type);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (type != EnumDemonWillType.DEFAULT) {
|
|
|
|
if (value >= 100) {
|
2016-02-26 22:11:28 -05:00
|
|
|
double nextProgress = getCrystalGrowthPerSecond(value);
|
2016-12-12 19:56:36 -08:00
|
|
|
progressToNextCrystal += WorldDemonWillHandler.drainWill(getWorld(), getPos(), type, nextProgress * sameWillConversionRate, true) / sameWillConversionRate;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-12-12 19:56:36 -08:00
|
|
|
value = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, EnumDemonWillType.DEFAULT);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (value > 0.5) {
|
2016-02-27 16:36:56 -05:00
|
|
|
double nextProgress = getCrystalGrowthPerSecond(value) * timeDelayForWrongWill;
|
2016-12-12 19:56:36 -08:00
|
|
|
progressToNextCrystal += WorldDemonWillHandler.drainWill(getWorld(), getPos(), EnumDemonWillType.DEFAULT, nextProgress * defaultWillConversionRate, true) / defaultWillConversionRate;
|
2016-02-26 22:11:28 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
|
|
|
if (value > 0.5) {
|
2016-02-26 22:11:28 -05:00
|
|
|
double nextProgress = getCrystalGrowthPerSecond(value);
|
2016-12-12 19:56:36 -08:00
|
|
|
progressToNextCrystal += WorldDemonWillHandler.drainWill(getWorld(), getPos(), type, nextProgress * sameWillConversionRate, true) / sameWillConversionRate;
|
2016-02-26 22:11:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:11:05 -04:00
|
|
|
checkAndGrowCrystal();
|
2016-02-25 16:19:57 -05:00
|
|
|
}
|
2016-02-26 22:11:28 -05:00
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
// if (getWorld().getWorldTime() % 200 == 0)
|
2016-02-26 22:11:28 -05:00
|
|
|
// {
|
|
|
|
// crystalCount = Math.min(crystalCount + 1, 7);
|
2016-12-12 19:56:36 -08:00
|
|
|
// getWorld().markBlockForUpdate(pos);
|
2016-02-26 22:11:28 -05:00
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:11:05 -04:00
|
|
|
/**
|
|
|
|
* Encourages the crystal to grow by a large percentage by telling it to
|
|
|
|
* drain will from the aura.
|
2017-08-15 21:30:48 -07:00
|
|
|
*
|
|
|
|
* @param willDrain The amount of drain that is needed for the crystal to grow
|
|
|
|
* successfully for the desired amount. Can be more than the base
|
|
|
|
* amount.
|
2016-03-24 14:11:05 -04:00
|
|
|
* @param progressPercentage
|
|
|
|
* @return percentage actually grown.
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public double growCrystalWithWillAmount(double willDrain, double progressPercentage) {
|
|
|
|
if (crystalCount >= 7) {
|
2016-03-24 14:11:05 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
IBlockState state = getWorld().getBlockState(pos);
|
2016-06-07 10:15:40 -04:00
|
|
|
int meta = this.getBlockType().getMetaFromState(state);
|
|
|
|
EnumDemonWillType type = EnumDemonWillType.values()[meta];
|
2016-03-24 14:11:05 -04:00
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
double value = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, type);
|
2016-03-24 14:11:05 -04:00
|
|
|
double percentDrain = willDrain <= 0 ? 1 : Math.min(1, value / willDrain);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (percentDrain <= 0) {
|
2016-03-24 14:11:05 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verification that you can actually drain the will from this chunk, for future proofing.
|
2016-12-12 19:56:36 -08:00
|
|
|
WorldDemonWillHandler.drainWill(getWorld(), pos, type, percentDrain * willDrain, true);
|
2016-03-24 14:11:05 -04:00
|
|
|
progressToNextCrystal += percentDrain * progressPercentage;
|
|
|
|
|
|
|
|
checkAndGrowCrystal();
|
|
|
|
|
|
|
|
return percentDrain * progressPercentage;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public void checkAndGrowCrystal() {
|
|
|
|
if (progressToNextCrystal >= 1) {
|
2016-03-24 14:11:05 -04:00
|
|
|
progressToNextCrystal--;
|
|
|
|
crystalCount++;
|
2016-12-12 19:56:36 -08:00
|
|
|
IBlockState thisState = getWorld().getBlockState(pos);
|
|
|
|
getWorld().notifyBlockUpdate(pos, thisState, thisState, 3);
|
2016-03-24 14:11:05 -04:00
|
|
|
markDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public double getMaxWillForCrystal() {
|
2016-03-24 14:11:05 -04:00
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean dropSingleCrystal() {
|
|
|
|
if (!getWorld().isRemote && crystalCount > 1) {
|
2016-12-12 19:56:36 -08:00
|
|
|
IBlockState state = getWorld().getBlockState(pos);
|
2016-02-27 19:34:42 -05:00
|
|
|
EnumDemonWillType type = state.getValue(BlockDemonCrystal.TYPE);
|
|
|
|
ItemStack stack = BlockDemonCrystal.getItemStackDropped(type, 1);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (stack != null) {
|
2016-02-27 19:34:42 -05:00
|
|
|
crystalCount--;
|
2016-12-12 19:56:36 -08:00
|
|
|
getWorld().spawnEntity(new EntityItem(getWorld(), pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, stack));
|
2016-02-27 19:34:42 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public double getCrystalGrowthPerSecond(double will) {
|
2016-02-27 16:36:56 -05:00
|
|
|
return 1.0 / 800 * Math.sqrt(will / 200);
|
2016-02-25 16:19:57 -05:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public int getCrystalCountForRender() {
|
2016-12-12 19:56:36 -08:00
|
|
|
return MathHelper.clamp(crystalCount - 1, 0, 6);
|
2016-02-25 16:19:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void deserialize(NBTTagCompound tag) {
|
2016-02-25 16:19:57 -05:00
|
|
|
holder.readFromNBT(tag, "Will");
|
|
|
|
crystalCount = tag.getInteger("crystalCount");
|
2016-02-25 22:00:02 -05:00
|
|
|
placement = EnumFacing.getFront(tag.getInteger("placement"));
|
2016-02-26 22:11:28 -05:00
|
|
|
progressToNextCrystal = tag.getDouble("progress");
|
2016-02-25 16:19:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public NBTTagCompound serialize(NBTTagCompound tag) {
|
2016-02-25 16:19:57 -05:00
|
|
|
holder.writeToNBT(tag, "Will");
|
|
|
|
tag.setInteger("crystalCount", crystalCount);
|
2016-02-25 22:00:02 -05:00
|
|
|
tag.setInteger("placement", placement.getIndex());
|
2016-02-26 22:11:28 -05:00
|
|
|
tag.setDouble("progress", progressToNextCrystal);
|
2016-05-19 17:43:33 -07:00
|
|
|
return tag;
|
2016-02-25 16:19:57 -05:00
|
|
|
}
|
2017-08-14 20:53:42 -07:00
|
|
|
|
|
|
|
public int getCrystalCount() {
|
|
|
|
return crystalCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCrystalCount(int crystalCount) {
|
|
|
|
this.crystalCount = crystalCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public EnumFacing getPlacement() {
|
|
|
|
return placement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPlacement(EnumFacing placement) {
|
|
|
|
this.placement = placement;
|
|
|
|
}
|
2016-02-25 16:19:57 -05:00
|
|
|
}
|