2016-02-25 08:54:18 -05:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
|
|
|
import WayofTime.bloodmagic.api.soul.DemonWillHolder;
|
|
|
|
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
|
|
|
|
import WayofTime.bloodmagic.api.soul.IDemonWillConduit;
|
2017-08-15 18:14:28 -07:00
|
|
|
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
|
2017-08-15 21:30:48 -07:00
|
|
|
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
|
2016-09-07 17:46:06 -07:00
|
|
|
import WayofTime.bloodmagic.tile.base.TileTicking;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-02-25 08:54:18 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class TileDemonCrystallizer extends TileTicking implements IDemonWillConduit {
|
2016-02-26 22:11:28 -05:00
|
|
|
public static final int maxWill = 100;
|
|
|
|
public static final double drainRate = 1;
|
2016-08-10 17:24:36 -04:00
|
|
|
public static final double willToFormCrystal = 99;
|
2016-02-26 22:11:28 -05:00
|
|
|
public static final double totalFormationTime = 1000;
|
2017-08-15 21:30:48 -07:00
|
|
|
//The whole purpose of this block is to grow a crystal initially. The acceleration and crystal growing is up to the crystal itself afterwards.
|
|
|
|
public DemonWillHolder holder = new DemonWillHolder();
|
2016-02-26 22:11:28 -05:00
|
|
|
public double internalCounter = 0;
|
2016-02-25 08:54:18 -05:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public TileDemonCrystallizer() {
|
2016-02-25 08:54:18 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void onUpdate() {
|
|
|
|
if (getWorld().isRemote) {
|
2016-02-26 22:11:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockPos offsetPos = pos.offset(EnumFacing.UP);
|
2016-12-12 19:56:36 -08:00
|
|
|
if (getWorld().isAirBlock(offsetPos)) //Room for a crystal to grow
|
2016-02-26 22:11:28 -05:00
|
|
|
{
|
2016-12-12 19:56:36 -08:00
|
|
|
EnumDemonWillType highestType = WorldDemonWillHandler.getHighestDemonWillType(getWorld(), pos);
|
|
|
|
double amount = WorldDemonWillHandler.getCurrentWill(getWorld(), pos, highestType);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (amount >= willToFormCrystal) {
|
2016-02-26 22:11:28 -05:00
|
|
|
internalCounter += getCrystalFormationRate(amount);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (internalCounter >= totalFormationTime) {
|
|
|
|
if (WorldDemonWillHandler.drainWill(getWorld(), getPos(), highestType, willToFormCrystal, false) >= willToFormCrystal) {
|
|
|
|
if (highestType == EnumDemonWillType.DEFAULT && formRandomSpecialCrystal(offsetPos) || formCrystal(highestType, offsetPos)) {
|
2016-12-12 19:56:36 -08:00
|
|
|
WorldDemonWillHandler.drainWill(getWorld(), getPos(), highestType, willToFormCrystal, true);
|
2016-02-26 22:11:28 -05:00
|
|
|
internalCounter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean formCrystal(EnumDemonWillType type, BlockPos position) {
|
2017-08-14 20:53:42 -07:00
|
|
|
getWorld().setBlockState(position, RegistrarBloodMagicBlocks.DEMON_CRYSTAL.getStateFromMeta(type.ordinal()));
|
2016-12-12 19:56:36 -08:00
|
|
|
TileEntity tile = getWorld().getTileEntity(position);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (tile instanceof TileDemonCrystal) {
|
2016-02-26 22:11:28 -05:00
|
|
|
((TileDemonCrystal) tile).setPlacement(EnumFacing.UP);
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-25 08:54:18 -05:00
|
|
|
|
2016-02-26 22:11:28 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean formRandomSpecialCrystal(BlockPos position) {
|
|
|
|
if (getWorld().rand.nextDouble() > 0.1) {
|
2016-02-26 22:11:28 -05:00
|
|
|
return formCrystal(EnumDemonWillType.DEFAULT, position);
|
|
|
|
}
|
2016-12-12 19:56:36 -08:00
|
|
|
EnumDemonWillType crystalType = EnumDemonWillType.values()[getWorld().rand.nextInt(EnumDemonWillType.values().length - 1) + 1];
|
2016-02-26 22:11:28 -05:00
|
|
|
return formCrystal(crystalType, position);
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public double getCrystalFormationRate(double currentWill) {
|
2016-02-26 22:11:28 -05:00
|
|
|
return 1;
|
2016-02-25 08:54:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void deserialize(NBTTagCompound tag) {
|
2016-02-25 08:54:18 -05:00
|
|
|
holder.readFromNBT(tag, "Will");
|
2016-02-26 22:11:28 -05:00
|
|
|
internalCounter = tag.getDouble("internalCounter");
|
2016-02-25 08:54:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public NBTTagCompound serialize(NBTTagCompound tag) {
|
2016-02-25 08:54:18 -05:00
|
|
|
holder.writeToNBT(tag, "Will");
|
2016-02-26 22:11:28 -05:00
|
|
|
tag.setDouble("internalCounter", internalCounter);
|
2016-05-19 17:43:33 -07:00
|
|
|
return tag;
|
2016-02-25 08:54:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IDemonWillConduit
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public int getWeight() {
|
2016-02-25 08:54:18 -05:00
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public double fillDemonWill(EnumDemonWillType type, double amount, boolean doFill) {
|
|
|
|
if (amount <= 0) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!canFill(type)) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!doFill) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return Math.min(maxWill - holder.getWill(type), amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return holder.addWill(type, amount, maxWill);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public double drainDemonWill(EnumDemonWillType type, double amount, boolean doDrain) {
|
2016-02-25 08:54:18 -05:00
|
|
|
double drained = amount;
|
|
|
|
double current = holder.getWill(type);
|
2017-08-15 21:30:48 -07:00
|
|
|
if (current < drained) {
|
2016-02-25 08:54:18 -05:00
|
|
|
drained = current;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (doDrain) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return holder.drainWill(type, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return drained;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean canFill(EnumDemonWillType type) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean canDrain(EnumDemonWillType type) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public double getCurrentWill(EnumDemonWillType type) {
|
2016-02-25 08:54:18 -05:00
|
|
|
return holder.getWill(type);
|
|
|
|
}
|
|
|
|
}
|