2016-02-21 20:10:56 -05:00
|
|
|
package WayofTime.bloodmagic.demonAura;
|
|
|
|
|
|
|
|
import WayofTime.bloodmagic.api.BloodMagicAPI;
|
|
|
|
import WayofTime.bloodmagic.api.soul.DemonWillHolder;
|
|
|
|
import WayofTime.bloodmagic.api.soul.EnumDemonWillType;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
import javax.annotation.Nullable;
|
2016-03-17 13:00:44 -07:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2016-02-21 20:10:56 -05:00
|
|
|
|
|
|
|
public class WorldDemonWillHandler
|
|
|
|
{
|
|
|
|
static ConcurrentHashMap<Integer, WillWorld> containedWills = new ConcurrentHashMap<Integer, WillWorld>();
|
|
|
|
public static ConcurrentHashMap<Integer, CopyOnWriteArrayList<PosXY>> dirtyChunks = new ConcurrentHashMap<Integer, CopyOnWriteArrayList<PosXY>>();
|
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
@Nullable
|
2016-07-10 15:27:26 -04:00
|
|
|
public static DemonWillHolder getWillHolder(int dim, int x, int y)
|
|
|
|
{
|
|
|
|
WillChunk chunk = getWillChunk(dim, x, y);
|
|
|
|
if (chunk != null)
|
|
|
|
{
|
|
|
|
return chunk.getCurrentWill();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-12 16:57:22 -04:00
|
|
|
public static DemonWillHolder getWillHolder(World world, BlockPos pos)
|
|
|
|
{
|
|
|
|
return getWillHolder(world.provider.getDimension(), pos.getX() >> 4, pos.getZ() >> 4);
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:11:28 -05:00
|
|
|
public static WillWorld getWillWorld(int dim)
|
2016-02-21 20:10:56 -05:00
|
|
|
{
|
|
|
|
return containedWills.get(dim);
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
@Nullable
|
2016-02-21 20:10:56 -05:00
|
|
|
public static WillChunk getWillChunk(int dim, int x, int y)
|
|
|
|
{
|
2016-03-14 20:16:31 -04:00
|
|
|
if (!containedWills.containsKey(dim))
|
2016-02-21 20:10:56 -05:00
|
|
|
{
|
2016-03-14 20:16:31 -04:00
|
|
|
addWillWorld(dim);
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:16:31 -04:00
|
|
|
return (containedWills.get(dim)).getWillChunkAt(x, y);
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void addWillWorld(int dim)
|
|
|
|
{
|
|
|
|
if (!containedWills.containsKey(dim))
|
|
|
|
{
|
|
|
|
containedWills.put(dim, new WillWorld(dim));
|
2017-08-14 20:53:42 -07:00
|
|
|
BloodMagicAPI.logger.info("Creating demon will cache for world " + dim);
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void removeWillWorld(int dim)
|
|
|
|
{
|
|
|
|
containedWills.remove(dim);
|
2017-08-14 20:53:42 -07:00
|
|
|
BloodMagicAPI.logger.info("Removing demon will cache for world " + dim);
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void addWillChunk(int dim, Chunk chunk, short base, DemonWillHolder currentWill)
|
|
|
|
{
|
|
|
|
WillWorld aw = containedWills.get(dim);
|
|
|
|
if (aw == null)
|
|
|
|
{
|
|
|
|
aw = new WillWorld(dim);
|
|
|
|
}
|
2017-08-14 20:53:42 -07:00
|
|
|
aw.getWillChunks().put(new PosXY(chunk.x, chunk.z), new WillChunk(chunk, base, currentWill));
|
2016-02-21 20:10:56 -05:00
|
|
|
|
|
|
|
containedWills.put(dim, aw);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void removeWillChunk(int dim, int x, int y)
|
|
|
|
{
|
|
|
|
WillWorld aw = containedWills.get(dim);
|
|
|
|
if (aw != null)
|
|
|
|
{
|
|
|
|
WillChunk chunk = aw.getWillChunks().remove(new PosXY(x, y));
|
|
|
|
if (chunk != null)
|
|
|
|
{
|
|
|
|
markChunkAsDirty(chunk, dim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:11:28 -05:00
|
|
|
public static EnumDemonWillType getHighestDemonWillType(World world, BlockPos pos)
|
|
|
|
{
|
|
|
|
double currentMax = 0;
|
|
|
|
EnumDemonWillType currentHighest = EnumDemonWillType.DEFAULT;
|
|
|
|
|
|
|
|
WillChunk willChunk = getWillChunk(world, pos);
|
|
|
|
|
|
|
|
DemonWillHolder currentWill = willChunk.getCurrentWill();
|
|
|
|
for (EnumDemonWillType type : EnumDemonWillType.values())
|
|
|
|
{
|
|
|
|
if (currentWill.getWill(type) > currentMax)
|
|
|
|
{
|
|
|
|
currentMax = currentWill.getWill(type);
|
|
|
|
currentHighest = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentHighest;
|
|
|
|
}
|
|
|
|
|
2016-02-21 20:10:56 -05:00
|
|
|
public static double drainWill(World world, BlockPos pos, EnumDemonWillType type, double amount, boolean doDrain)
|
|
|
|
{
|
|
|
|
WillChunk willChunk = getWillChunk(world, pos);
|
|
|
|
|
|
|
|
DemonWillHolder currentWill = willChunk.getCurrentWill();
|
|
|
|
double drain = Math.min(currentWill.getWill(type), amount);
|
|
|
|
if (!doDrain)
|
|
|
|
{
|
|
|
|
return drain;
|
|
|
|
}
|
|
|
|
|
|
|
|
drain = currentWill.drainWill(type, drain);
|
2016-03-17 13:00:44 -07:00
|
|
|
markChunkAsDirty(willChunk, world.provider.getDimension());
|
2016-02-21 20:10:56 -05:00
|
|
|
|
|
|
|
return drain;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double fillWillToMaximum(World world, BlockPos pos, EnumDemonWillType type, double amount, double max, boolean doFill)
|
|
|
|
{
|
|
|
|
WillChunk willChunk = getWillChunk(world, pos);
|
|
|
|
|
|
|
|
DemonWillHolder currentWill = willChunk.getCurrentWill();
|
|
|
|
double fill = Math.min(amount, max - currentWill.getWill(type));
|
2016-09-11 10:02:06 -04:00
|
|
|
if (!doFill || fill <= 0)
|
2016-02-21 20:10:56 -05:00
|
|
|
{
|
2016-09-11 10:02:06 -04:00
|
|
|
return fill > 0 ? fill : 0;
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fill = currentWill.addWill(type, amount, max);
|
2016-03-17 13:00:44 -07:00
|
|
|
markChunkAsDirty(willChunk, world.provider.getDimension());
|
2016-02-21 20:10:56 -05:00
|
|
|
|
|
|
|
return fill;
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:40:08 -05:00
|
|
|
public static double fillWill(World world, BlockPos pos, EnumDemonWillType type, double amount, boolean doFill)
|
|
|
|
{
|
|
|
|
WillChunk willChunk = getWillChunk(world, pos);
|
|
|
|
|
|
|
|
DemonWillHolder currentWill = willChunk.getCurrentWill();
|
|
|
|
if (!doFill)
|
|
|
|
{
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentWill.addWill(type, amount);
|
2016-03-17 13:00:44 -07:00
|
|
|
markChunkAsDirty(willChunk, world.provider.getDimension());
|
2016-02-23 13:40:08 -05:00
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2016-02-21 20:10:56 -05:00
|
|
|
public static WillChunk getWillChunk(World world, BlockPos pos)
|
|
|
|
{
|
2016-03-17 13:00:44 -07:00
|
|
|
WillChunk willChunk = getWillChunk(world.provider.getDimension(), pos.getX() >> 4, pos.getZ() >> 4);
|
2016-03-14 20:16:31 -04:00
|
|
|
if (willChunk == null)
|
|
|
|
{
|
|
|
|
Chunk chunk = world.getChunkFromBlockCoords(pos);
|
|
|
|
generateWill(chunk);
|
|
|
|
|
2016-03-17 13:00:44 -07:00
|
|
|
willChunk = getWillChunk(world.provider.getDimension(), pos.getX() >> 4, pos.getZ() >> 4);
|
2016-03-14 20:16:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return willChunk;
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:40:08 -05:00
|
|
|
public static double getCurrentWill(World world, BlockPos pos, EnumDemonWillType type)
|
|
|
|
{
|
|
|
|
WillChunk willChunk = getWillChunk(world, pos);
|
|
|
|
|
2016-12-20 16:42:39 -08:00
|
|
|
if (willChunk == null)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:40:08 -05:00
|
|
|
DemonWillHolder currentWill = willChunk.getCurrentWill();
|
|
|
|
return currentWill.getWill(type);
|
|
|
|
}
|
|
|
|
|
2016-02-21 20:10:56 -05:00
|
|
|
private static void markChunkAsDirty(WillChunk chunk, int dim)
|
|
|
|
{
|
|
|
|
if (chunk.isModified())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PosXY pos = new PosXY(chunk.loc.x, chunk.loc.y);
|
|
|
|
if (!dirtyChunks.containsKey(dim))
|
|
|
|
{
|
|
|
|
dirtyChunks.put(dim, new CopyOnWriteArrayList<PosXY>());
|
|
|
|
}
|
|
|
|
CopyOnWriteArrayList<PosXY> dc = dirtyChunks.get(dim);
|
|
|
|
if (!dc.contains(pos))
|
|
|
|
{
|
|
|
|
dc.add(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void generateWill(Chunk chunk)
|
|
|
|
{
|
2016-03-17 13:00:44 -07:00
|
|
|
addWillChunk(chunk.getWorld().provider.getDimension(), chunk, (short) 1, new DemonWillHolder());
|
2016-02-21 20:10:56 -05:00
|
|
|
}
|
|
|
|
}
|