2016-02-25 21:19:57 +00:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.network.NetworkManager;
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.BlockPos;
|
2016-02-26 03:00:02 +00:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-02-25 21:19:57 +00:00
|
|
|
import net.minecraft.util.ITickable;
|
|
|
|
import net.minecraft.util.MathHelper;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import WayofTime.bloodmagic.api.soul.DemonWillHolder;
|
|
|
|
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
|
|
|
|
import WayofTime.bloodmagic.api.soul.IDemonWillConduit;
|
2016-02-27 03:11:28 +00:00
|
|
|
import WayofTime.bloodmagic.demonAura.WorldDemonWillHandler;
|
2016-02-25 21:19:57 +00:00
|
|
|
|
|
|
|
public class TileDemonCrystal extends TileEntity implements ITickable, IDemonWillConduit
|
|
|
|
{
|
|
|
|
public DemonWillHolder holder = new DemonWillHolder();
|
|
|
|
public final int maxWill = 100;
|
|
|
|
public final double drainRate = 1;
|
2016-02-27 03:11:28 +00:00
|
|
|
public static final double sameWillConversionRate = 5;
|
|
|
|
public static final double defaultWillConversionRate = 50;
|
2016-02-27 21:36:56 +00:00
|
|
|
public static final double timeDelayForWrongWill = 0.6;
|
2016-02-27 03:11:28 +00:00
|
|
|
|
|
|
|
public double progressToNextCrystal = 0;
|
|
|
|
public int internalCounter = 0;
|
2016-02-25 21:19:57 +00:00
|
|
|
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
public int crystalCount = 1;
|
2016-02-26 03:00:02 +00:00
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
public EnumFacing placement = EnumFacing.UP; //Side that this crystal is placed on.
|
2016-02-25 21:19:57 +00:00
|
|
|
|
|
|
|
public TileDemonCrystal()
|
|
|
|
{
|
|
|
|
this.crystalCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update()
|
|
|
|
{
|
|
|
|
if (worldObj.isRemote)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:11:28 +00:00
|
|
|
internalCounter++;
|
|
|
|
|
|
|
|
if (internalCounter % 20 == 0 && crystalCount < 7)
|
2016-02-25 21:19:57 +00:00
|
|
|
{
|
2016-02-27 03:11:28 +00:00
|
|
|
EnumDemonWillType type = EnumDemonWillType.values()[this.getBlockMetadata()];
|
|
|
|
|
|
|
|
double value = WorldDemonWillHandler.getCurrentWill(worldObj, pos, type);
|
|
|
|
if (type != EnumDemonWillType.DEFAULT)
|
|
|
|
{
|
|
|
|
if (value >= 100)
|
|
|
|
{
|
|
|
|
double nextProgress = getCrystalGrowthPerSecond(value);
|
|
|
|
progressToNextCrystal += WorldDemonWillHandler.drainWill(worldObj, getPos(), type, nextProgress * sameWillConversionRate, true) / sameWillConversionRate;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
value = WorldDemonWillHandler.getCurrentWill(worldObj, pos, EnumDemonWillType.DEFAULT);
|
|
|
|
if (value > 0.5)
|
|
|
|
{
|
2016-02-27 21:36:56 +00:00
|
|
|
double nextProgress = getCrystalGrowthPerSecond(value) * timeDelayForWrongWill;
|
2016-02-27 03:11:28 +00:00
|
|
|
progressToNextCrystal += WorldDemonWillHandler.drainWill(worldObj, getPos(), EnumDemonWillType.DEFAULT, nextProgress * defaultWillConversionRate, true) / defaultWillConversionRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (value > 0.5)
|
|
|
|
{
|
|
|
|
double nextProgress = getCrystalGrowthPerSecond(value);
|
|
|
|
progressToNextCrystal += WorldDemonWillHandler.drainWill(worldObj, getPos(), type, nextProgress * sameWillConversionRate, true) / sameWillConversionRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progressToNextCrystal >= 1)
|
|
|
|
{
|
|
|
|
progressToNextCrystal--;
|
|
|
|
crystalCount++;
|
|
|
|
worldObj.markBlockForUpdate(pos);
|
|
|
|
}
|
2016-02-25 21:19:57 +00:00
|
|
|
}
|
2016-02-27 03:11:28 +00:00
|
|
|
|
|
|
|
// if (worldObj.getWorldTime() % 200 == 0)
|
|
|
|
// {
|
|
|
|
// crystalCount = Math.min(crystalCount + 1, 7);
|
|
|
|
// worldObj.markBlockForUpdate(pos);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getCrystalGrowthPerSecond(double will)
|
|
|
|
{
|
2016-02-27 21:36:56 +00:00
|
|
|
return 1.0 / 800 * Math.sqrt(will / 200);
|
2016-02-25 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getCrystalCountForRender()
|
|
|
|
{
|
|
|
|
return MathHelper.clamp_int(crystalCount - 1, 0, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
super.readFromNBT(tag);
|
|
|
|
|
|
|
|
holder.readFromNBT(tag, "Will");
|
|
|
|
crystalCount = tag.getInteger("crystalCount");
|
2016-02-26 03:00:02 +00:00
|
|
|
placement = EnumFacing.getFront(tag.getInteger("placement"));
|
2016-02-27 03:11:28 +00:00
|
|
|
progressToNextCrystal = tag.getDouble("progress");
|
2016-02-25 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
super.writeToNBT(tag);
|
|
|
|
|
|
|
|
holder.writeToNBT(tag, "Will");
|
|
|
|
tag.setInteger("crystalCount", crystalCount);
|
2016-02-26 03:00:02 +00:00
|
|
|
tag.setInteger("placement", placement.getIndex());
|
2016-02-27 03:11:28 +00:00
|
|
|
tag.setDouble("progress", progressToNextCrystal);
|
2016-02-25 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IDemonWillConduit
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWeight()
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double fillDemonWill(EnumDemonWillType type, double amount, boolean doFill)
|
|
|
|
{
|
|
|
|
if (amount <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canFill(type))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!doFill)
|
|
|
|
{
|
|
|
|
return Math.min(maxWill - holder.getWill(type), amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return holder.addWill(type, amount, maxWill);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double drainDemonWill(EnumDemonWillType type, double amount, boolean doDrain)
|
|
|
|
{
|
|
|
|
double drained = amount;
|
|
|
|
double current = holder.getWill(type);
|
|
|
|
if (current < drained)
|
|
|
|
{
|
|
|
|
drained = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doDrain)
|
|
|
|
{
|
|
|
|
return holder.drainWill(type, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return drained;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canFill(EnumDemonWillType type)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canDrain(EnumDemonWillType type)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getCurrentWill(EnumDemonWillType type)
|
|
|
|
{
|
|
|
|
return holder.getWill(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
|
|
|
|
{
|
|
|
|
return oldState.getBlock() != newState.getBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Packet getDescriptionPacket()
|
|
|
|
{
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
writeToNBT(nbt);
|
|
|
|
return new S35PacketUpdateTileEntity(getPos(), -999, nbt);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
|
|
|
|
{
|
|
|
|
super.onDataPacket(net, pkt);
|
|
|
|
readFromNBT(pkt.getNbtCompound());
|
|
|
|
worldObj.markBlockRangeForRenderUpdate(getPos(), getPos());
|
|
|
|
}
|
|
|
|
}
|