use of com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder in project minecolonies by Minecolonies.
the class AbstractEntityAIStructureWithWorkOrder method requestMaterialsState.
/**
* State for material requesting.
*/
private void requestMaterialsState() {
if (Constants.BUILDER_INF_RESOURECES || job.getWorkOrder().isRequested() || job.getWorkOrder() instanceof WorkOrderBuildRemoval) {
return;
}
final AbstractBuildingStructureBuilder buildingWorker = getOwnBuilding();
if (requestMaterials()) {
job.getWorkOrder().setRequested(true);
}
int newQuantity = buildingWorker.getNeededResources().values().stream().mapToInt(ItemStorage::getAmount).sum();
if (job.getWorkOrder().getAmountOfRes() == 0 || newQuantity > job.getWorkOrder().getAmountOfRes()) {
job.getWorkOrder().setAmountOfRes(newQuantity);
}
}
use of com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder in project minecolonies by Minecolonies.
the class AbstractEntityAIStructureWithWorkOrder method getTotalAmount.
/**
* Check how much of a certain stuck is actually required.
*
* @param stack the stack to check.
* @return the new stack with the correct amount.
*/
@Override
@Nullable
public ItemStack getTotalAmount(@Nullable final ItemStack stack) {
if (ItemStackUtils.isEmpty(stack)) {
return null;
}
final int hashCode = stack.hasTag() ? stack.getTag().hashCode() : 0;
final AbstractBuildingStructureBuilder buildingWorker = getOwnBuilding();
BuildingBuilderResource resource = buildingWorker.getNeededResources().get(stack.getDescriptionId() + "-" + hashCode);
if (resource == null) {
requestMaterials();
resource = buildingWorker.getNeededResources().get(stack.getDescriptionId() + "-" + hashCode);
}
if (resource == null) {
return stack;
}
final ItemStack resStack = new ItemStack(resource.getItem(), Math.min(STACKSIZE, resource.getAmount()));
resStack.setTag(resource.getItemStack().getTag());
return resStack;
}
use of com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder in project minecolonies by Minecolonies.
the class AbstractEntityAIStructureWithWorkOrder method requestMaterials.
@Override
public boolean requestMaterials() {
StructurePhasePlacementResult result;
final WorkerLoadOnlyStructureHandler structure = new WorkerLoadOnlyStructureHandler(world, structurePlacer.getB().getWorldPos(), structurePlacer.getB().getBluePrint(), new PlacementSettings(), true, this);
if (job.getWorkOrder().getIteratorType().isEmpty() && getOwnBuilding().hasModule(ISettingsModule.class) && getOwnBuilding().getSetting(BuildingBuilder.BUILDING_MODE) != null) {
job.getWorkOrder().setIteratorType(getOwnBuilding().getSetting(BuildingBuilder.BUILDING_MODE).getValue());
}
final StructurePlacer placer = new StructurePlacer(structure, job.getWorkOrder().getIteratorType());
if (requestProgress == null) {
final AbstractBuildingStructureBuilder buildingWorker = getOwnBuilding();
buildingWorker.resetNeededResources();
requestProgress = NULL_POS;
requestState = RequestStage.SOLID;
}
final RequestStage currState = requestState;
switch(currState) {
case SOLID:
result = placer.executeStructureStep(world, null, requestProgress, StructurePlacer.Operation.GET_RES_REQUIREMENTS, () -> placer.getIterator().increment(DONT_TOUCH_PREDICATE.or((info, pos, handler) -> !info.getBlockInfo().getState().getMaterial().isSolid() || isDecoItem(info.getBlockInfo().getState().getBlock()))), false);
requestProgress = result.getIteratorPos();
for (final ItemStack stack : result.getBlockResult().getRequiredItems()) {
getOwnBuilding().addNeededResource(stack, stack.getCount());
}
if (result.getBlockResult().getResult() == BlockPlacementResult.Result.FINISHED) {
requestState = RequestStage.DECO;
}
return false;
case DECO:
result = placer.executeStructureStep(world, null, requestProgress, StructurePlacer.Operation.GET_RES_REQUIREMENTS, () -> placer.getIterator().increment(DONT_TOUCH_PREDICATE.or((info, pos, handler) -> info.getBlockInfo().getState().getMaterial().isSolid() && !isDecoItem(info.getBlockInfo().getState().getBlock()))), false);
requestProgress = result.getIteratorPos();
for (final ItemStack stack : result.getBlockResult().getRequiredItems()) {
getOwnBuilding().addNeededResource(stack, stack.getCount());
}
if (result.getBlockResult().getResult() == BlockPlacementResult.Result.FINISHED) {
requestState = RequestStage.ENTITIES;
}
return false;
case ENTITIES:
result = placer.executeStructureStep(world, null, requestProgress, StructurePlacer.Operation.GET_RES_REQUIREMENTS, () -> placer.getIterator().increment(DONT_TOUCH_PREDICATE.or((info, pos, handler) -> info.getEntities().length == 0)), true);
requestProgress = result.getIteratorPos();
for (final ItemStack stack : result.getBlockResult().getRequiredItems()) {
getOwnBuilding().addNeededResource(stack, stack.getCount());
}
if (result.getBlockResult().getResult() == BlockPlacementResult.Result.FINISHED) {
requestState = RequestStage.SOLID;
requestProgress = null;
return true;
}
return false;
default:
return true;
}
}
use of com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder in project minecolonies by ldtteam.
the class AbstractEntityAIStructure method hasListOfResInInvOrRequest.
/**
* Check the placers inventory for the items in the itemList and remove it of the list if found.
*
* @param placer the placer.
* @param itemList the list to check.
* @param force if force insertion.
* @return true if need to request.
*/
public static <J extends AbstractJobStructure<?, J>, B extends AbstractBuildingStructureBuilder> ItemCheckResult hasListOfResInInvOrRequest(@NotNull final AbstractEntityAIStructure<J, B> placer, final List<ItemStack> itemList, final boolean force) {
final Map<ItemStorage, Integer> requestedMap = new HashMap<>();
for (final ItemStack stack : itemList) {
if (stack.getItem() instanceof BlockItem && isBlockFree(((BlockItem) stack.getItem()).getBlock())) {
continue;
}
ItemStorage tempStorage = new ItemStorage(stack.copy());
if (requestedMap.containsKey(tempStorage)) {
final int oldSize = requestedMap.get(tempStorage);
tempStorage.setAmount(tempStorage.getAmount() + oldSize);
}
requestedMap.put(tempStorage, tempStorage.getAmount());
}
for (final ItemStorage stack : requestedMap.keySet()) {
if (!InventoryUtils.hasItemInItemHandler(placer.getInventory(), stack1 -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack.getItemStack(), stack1)) && !placer.getOwnBuilding().hasResourceInBucket(stack.getItemStack())) {
return RECALC;
}
}
final List<ItemStack> foundStacks = InventoryUtils.filterItemHandler(placer.getWorker().getInventoryCitizen(), itemStack -> requestedMap.keySet().stream().anyMatch(storage -> ItemStackUtils.compareItemStacksIgnoreStackSize(storage.getItemStack(), itemStack)));
final Map<ItemStorage, Integer> localMap = new HashMap<>();
for (final ItemStack stack : foundStacks) {
ItemStorage tempStorage = new ItemStorage(stack.copy());
if (localMap.containsKey(tempStorage)) {
final int oldSize = localMap.get(tempStorage);
tempStorage.setAmount(tempStorage.getAmount() + oldSize);
}
localMap.put(tempStorage, tempStorage.getAmount());
}
if (force) {
for (final Map.Entry<ItemStorage, Integer> local : localMap.entrySet()) {
int req = requestedMap.getOrDefault(local.getKey(), 0);
if (req != 0) {
if (local.getValue() >= req) {
requestedMap.remove(local.getKey());
} else {
requestedMap.put(local.getKey(), req - local.getValue());
}
}
}
} else {
requestedMap.entrySet().removeIf(entry -> ItemStackUtils.isEmpty(entry.getKey().getItemStack()) || foundStacks.stream().anyMatch(target -> ItemStackUtils.compareItemStacksIgnoreStackSize(target, entry.getKey().getItemStack())));
}
for (final Map.Entry<ItemStorage, Integer> placedStack : requestedMap.entrySet()) {
final ItemStack stack = placedStack.getKey().getItemStack();
if (ItemStackUtils.isEmpty(stack)) {
return FAIL;
}
final ImmutableList<IRequest<? extends IDeliverable>> requests = placer.getOwnBuilding().getOpenRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(stack));
final ImmutableList<IRequest<? extends IDeliverable>> completedRequests = placer.getOwnBuilding().getCompletedRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(stack));
if (requests.isEmpty() && completedRequests.isEmpty()) {
final com.minecolonies.api.colony.requestsystem.requestable.Stack stackRequest = new Stack(stack, placer.getTotalAmount(stack).getCount(), 1);
placer.getWorker().getCitizenData().createRequest(stackRequest);
placer.registerBlockAsNeeded(stack);
return FAIL;
} else {
for (final IRequest<? extends IDeliverable> request : requests) {
if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
}
}
for (final IRequest<? extends IDeliverable> request : completedRequests) {
if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
}
}
}
return FAIL;
}
return SUCCESS;
}
use of com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder in project minecolonies by ldtteam.
the class AbstractEntityAIStructureWithWorkOrder method requestMaterialsState.
/**
* State for material requesting.
*/
private void requestMaterialsState() {
if (Constants.BUILDER_INF_RESOURECES || job.getWorkOrder().isRequested() || job.getWorkOrder() instanceof WorkOrderBuildRemoval) {
return;
}
final AbstractBuildingStructureBuilder buildingWorker = getOwnBuilding();
if (requestMaterials()) {
job.getWorkOrder().setRequested(true);
}
int newQuantity = buildingWorker.getNeededResources().values().stream().mapToInt(ItemStorage::getAmount).sum();
if (job.getWorkOrder().getAmountOfRes() == 0 || newQuantity > job.getWorkOrder().getAmountOfRes()) {
job.getWorkOrder().setAmountOfRes(newQuantity);
}
}
Aggregations