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;
}
Aggregations