Search in sources :

Example 1 with CirclePart

use of com.favouriteless.enchanted.common.rites.util.CirclePart in project Enchanted by Favouriteless.

the class AbstractRite method differenceAt.

/**
 * Gets the number of extra requirement entities over the rite requirements at a given position
 * @param world
 * @param pos
 * @return No. of extra requirement entities, -1 if not valid.
 */
public int differenceAt(World world, BlockPos pos) {
    for (CirclePart circlePart : CIRCLES_REQUIRED.keySet()) {
        if (!circlePart.match(world, pos, CIRCLES_REQUIRED.get(circlePart))) {
            return -1;
        }
    }
    List<Entity> allEntities = world.getEntities(null, new AxisAlignedBB(pos.offset(-7, 0, -7), pos.offset(7, 1, 7)));
    HashMap<Item, Integer> items = new HashMap<>();
    HashMap<EntityType<?>, Integer> entities = new HashMap<>();
    for (Entity entity : allEntities) {
        // Get items/entities in area
        if (entity instanceof ItemEntity) {
            ItemEntity itemEntity = (ItemEntity) entity;
            ItemStack itemStack = itemEntity.getItem();
            if (!items.containsKey(itemStack.getItem())) {
                items.put(itemStack.getItem(), itemStack.getCount());
            } else {
                items.put(itemStack.getItem(), items.get(itemStack.getItem()) + itemStack.getCount());
            }
        } else {
            if (!entities.containsKey(entity.getType())) {
                entities.put(entity.getType(), 1);
            } else {
                entities.put(entity.getType(), entities.get(entity.getType()) + 1);
            }
        }
    }
    int diff = 0;
    if (!ITEMS_REQUIRED.isEmpty()) {
        for (Item item : ITEMS_REQUIRED.keySet()) {
            // Check if enough items
            if (!(items.containsKey(item) && items.get(item) >= ITEMS_REQUIRED.get(item)))
                return -1;
        }
        for (Item item : items.keySet()) {
            if (!ITEMS_REQUIRED.containsKey(item))
                diff += items.get(item);
        }
    }
    if (!ENTITIES_REQUIRED.isEmpty()) {
        for (EntityType<?> type : ENTITIES_REQUIRED.keySet()) {
            // Check if enough entities
            if (!(entities.containsKey(type) && entities.get(type) >= ENTITIES_REQUIRED.get(type)))
                return -1;
        }
        for (EntityType<?> type : entities.keySet()) {
            if (!ENTITIES_REQUIRED.containsKey(type))
                diff += entities.get(type);
        }
    }
    return diff;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ChalkGoldTileEntity(com.favouriteless.enchanted.common.tileentity.ChalkGoldTileEntity) AltarTileEntity(com.favouriteless.enchanted.common.tileentity.AltarTileEntity) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TileEntity(net.minecraft.tileentity.TileEntity) ItemEntity(net.minecraft.entity.item.ItemEntity) ItemEntity(net.minecraft.entity.item.ItemEntity) HashMap(java.util.HashMap) EntityType(net.minecraft.entity.EntityType) Item(net.minecraft.item.Item) CirclePart(com.favouriteless.enchanted.common.rites.util.CirclePart) ItemStack(net.minecraft.item.ItemStack)

Aggregations

CirclePart (com.favouriteless.enchanted.common.rites.util.CirclePart)1 AltarTileEntity (com.favouriteless.enchanted.common.tileentity.AltarTileEntity)1 ChalkGoldTileEntity (com.favouriteless.enchanted.common.tileentity.ChalkGoldTileEntity)1 HashMap (java.util.HashMap)1 Entity (net.minecraft.entity.Entity)1 EntityType (net.minecraft.entity.EntityType)1 ItemEntity (net.minecraft.entity.item.ItemEntity)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1