use of com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable in project minecolonies by ldtteam.
the class EntityAIWorkDeliveryman method deliver.
/**
* Deliver the items to the hut. TODO: Current precondition: The dman's inventory may only consist of the requested itemstack.
*
* @return the next state.
*/
private IAIState deliver() {
final IRequest<? extends IDeliverymanRequestable> currentTask = job.getCurrentTask();
if (!(currentTask instanceof DeliveryRequest)) {
// Since prepareDelivery() was called earlier, go dumping first and then restart.
return DUMPING;
}
worker.getCitizenData().setVisibleStatus(DELIVERING);
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.delivering"));
final ILocation targetBuildingLocation = ((Delivery) currentTask.getRequest()).getTarget();
if (!targetBuildingLocation.isReachableFromLocation(worker.getLocation())) {
Log.getLogger().info(worker.getCitizenColonyHandler().getColony().getName() + ": " + worker.getName() + ": Can't inter dimension yet: ");
return START_WORKING;
}
if (!worker.isWorkerAtSiteWithMove(targetBuildingLocation.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
setDelay(WALK_DELAY);
return DELIVERY;
}
final TileEntity tileEntity = world.getBlockEntity(targetBuildingLocation.getInDimensionLocation());
if (!(tileEntity instanceof TileEntityColonyBuilding)) {
// TODO: Non-Colony deliveries are unsupported yet. Fix that at some point in time.
job.finishRequest(true);
return START_WORKING;
}
final IBuilding targetBuilding = ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding();
boolean success = true;
boolean extracted = false;
final IItemHandler workerInventory = worker.getInventoryCitizen();
for (int i = 0; i < workerInventory.getSlots(); i++) {
if (workerInventory.getStackInSlot(i).isEmpty()) {
continue;
}
final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
if (ItemStackUtils.isEmpty(stack)) {
continue;
}
extracted = true;
final ItemStack insertionResultStack;
// TODO: Please only push items into the target that were actually requested.
if (targetBuilding instanceof AbstractBuilding) {
insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, ((IBuilding) targetBuilding)::isItemStackInRequest);
} else {
// Buildings that are not inherently part of the request system, but just receive a delivery, cannot have their items replaced.
// Therefore, the keep-predicate always returns true.
insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, itemStack -> true);
}
if (!ItemStackUtils.isEmpty(insertionResultStack)) {
if (ItemStack.matches(insertionResultStack, stack) && worker.getCitizenData() != null) {
// The replaced stack is the same as the one we tried to put into the inventory.
// Meaning, replacing failed.
success = false;
if (targetBuilding.hasModule(WorkerBuildingModule.class)) {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, targetBuilding.getFirstModuleOccurance(WorkerBuildingModule.class).getFirstCitizen().getName()), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuilding.getID()));
} else {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new StringTextComponent(" :" + targetBuilding.getSchematicName())), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuildingLocation.getInDimensionLocation()));
}
}
// Insert the result back into the inventory so we do not lose it.
workerInventory.insertItem(i, insertionResultStack, false);
}
}
if (!extracted) {
// This can only happen if the dman's inventory was completely empty.
// Let the retry-system handle this case.
worker.decreaseSaturationForContinuousAction();
worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
job.finishRequest(false);
// No need to go dumping in this case.
return START_WORKING;
}
worker.getCitizenExperienceHandler().addExperience(1.5D);
worker.decreaseSaturationForContinuousAction();
worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
job.finishRequest(true);
return success ? START_WORKING : DUMPING;
}
use of com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable in project minecolonies by Minecolonies.
the class WindowHutCrafterTaskModule method onOpened.
@Override
public void onOpened() {
super.onOpened();
final List<IToken<?>> tasks = new ArrayList<>();
for (final WorkerBuildingModuleView moduleView : buildingView.getModuleViews(WorkerBuildingModuleView.class)) {
for (final int citizenId : moduleView.getAssignedCitizens()) {
ICitizenDataView citizen = buildingView.getColony().getCitizen(citizenId);
if (citizen != null) {
if (citizen.getJobView() instanceof CrafterJobView) {
tasks.addAll(((CrafterJobView) citizen.getJobView()).getDataStore().getQueue());
} else if (citizen.getJobView() instanceof DmanJobView) {
tasks.addAll(((DmanJobView) citizen.getJobView()).getDataStore().getQueue());
}
}
}
}
final ScrollingList deliveryList = findPaneOfTypeByID(LIST_TASKS, ScrollingList.class);
deliveryList.setDataProvider(new ScrollingList.DataProvider() {
@Override
public int getElementCount() {
tasks.removeIf(token -> buildingView.getColony().getRequestManager().getRequestForToken(token) == null);
return tasks.size();
}
@Override
public void updateElement(final int index, @NotNull final Pane rowPane) {
final IRequest<?> request = buildingView.getColony().getRequestManager().getRequestForToken(tasks.get(index));
final IRequest<?> parent = buildingView.getColony().getRequestManager().getRequestForToken(request.getParent());
if (parent != null) {
rowPane.findPaneOfTypeByID(REQUESTER, Text.class).setText(request.getRequester().getRequesterDisplayName(buildingView.getColony().getRequestManager(), request).getString() + " ->");
rowPane.findPaneOfTypeByID(PARENT, Text.class).setText(parent.getRequester().getRequesterDisplayName(buildingView.getColony().getRequestManager(), parent));
} else {
rowPane.findPaneOfTypeByID(REQUESTER, Text.class).setText(request.getRequester().getRequesterDisplayName(buildingView.getColony().getRequestManager(), request));
rowPane.findPaneOfTypeByID(PARENT, Text.class).clearText();
}
rowPane.findPaneOfTypeByID(REQUEST_SHORT_DETAIL, Text.class).setText(request.getShortDisplayString().getString().replace("§f", ""));
if (request.getRequest() instanceof IDeliverymanRequestable) {
rowPane.findPaneOfTypeByID(REQUEST_PRIORITY, Text.class).setText(LanguageHandler.format(COM_MINECOLONIES_COREMOD_ENTITY_DELIVERYMAN_PRIORITY) + ((IDeliverymanRequestable) (request.getRequest())).getPriority());
}
final Image logo = rowPane.findPaneOfTypeByID(DELIVERY_IMAGE, Image.class);
logo.setImage(request.getDisplayIcon());
}
});
}
use of com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable in project minecolonies by Minecolonies.
the class JobDeliveryman method addRequest.
/**
* Method used to add a request to the queue
*
* @param token The token of the requests to add.
*/
public void addRequest(@NotNull final IToken<?> token, final int insertionIndex) {
final IRequestManager requestManager = getColony().getRequestManager();
IRequest<? extends IDeliverymanRequestable> newRequest = (IRequest<? extends IDeliverymanRequestable>) (requestManager.getRequestForToken(token));
LinkedList<IToken<?>> taskQueue = getTaskQueueFromDataStore();
int offset = 0;
for (int i = insertionIndex; i < taskQueue.size(); i++) {
final IToken theToken = taskQueue.get(i);
final IRequest<? extends IDeliverymanRequestable> request = (IRequest<? extends IDeliverymanRequestable>) (requestManager.getRequestForToken(theToken));
if (request == null || request.getState() == RequestState.COMPLETED) {
taskQueue.remove(theToken);
i--;
offset--;
} else {
request.getRequest().incrementPriorityDueToAging();
}
}
getTaskQueueFromDataStore().add(Math.max(0, insertionIndex + offset), token);
if (newRequest instanceof StandardRequests.PickupRequest && newRequest.getRequest().getPriority() == getPlayerActionPriority(true)) {
getCitizen().getEntity().ifPresent(e -> e.getCitizenChatHandler().sendLocalizedChat(COM_MINECOLONIES_COREMOD_ENTITY_DELIVERYMAN_FORCEPICKUP));
;
}
}
use of com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable in project minecolonies by Minecolonies.
the class EntityAIWorkDeliveryman method deliver.
/**
* Deliver the items to the hut. TODO: Current precondition: The dman's inventory may only consist of the requested itemstack.
*
* @return the next state.
*/
private IAIState deliver() {
final IRequest<? extends IDeliverymanRequestable> currentTask = job.getCurrentTask();
if (!(currentTask instanceof DeliveryRequest)) {
// Since prepareDelivery() was called earlier, go dumping first and then restart.
return DUMPING;
}
worker.getCitizenData().setVisibleStatus(DELIVERING);
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.delivering"));
final ILocation targetBuildingLocation = ((Delivery) currentTask.getRequest()).getTarget();
if (!targetBuildingLocation.isReachableFromLocation(worker.getLocation())) {
Log.getLogger().info(worker.getCitizenColonyHandler().getColony().getName() + ": " + worker.getName() + ": Can't inter dimension yet: ");
return START_WORKING;
}
if (!worker.isWorkerAtSiteWithMove(targetBuildingLocation.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
setDelay(WALK_DELAY);
return DELIVERY;
}
final TileEntity tileEntity = world.getBlockEntity(targetBuildingLocation.getInDimensionLocation());
if (!(tileEntity instanceof TileEntityColonyBuilding)) {
// TODO: Non-Colony deliveries are unsupported yet. Fix that at some point in time.
job.finishRequest(true);
return START_WORKING;
}
final IBuilding targetBuilding = ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding();
boolean success = true;
boolean extracted = false;
final IItemHandler workerInventory = worker.getInventoryCitizen();
for (int i = 0; i < workerInventory.getSlots(); i++) {
if (workerInventory.getStackInSlot(i).isEmpty()) {
continue;
}
final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
if (ItemStackUtils.isEmpty(stack)) {
continue;
}
extracted = true;
final ItemStack insertionResultStack;
// TODO: Please only push items into the target that were actually requested.
if (targetBuilding instanceof AbstractBuilding) {
insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, ((IBuilding) targetBuilding)::isItemStackInRequest);
} else {
// Buildings that are not inherently part of the request system, but just receive a delivery, cannot have their items replaced.
// Therefore, the keep-predicate always returns true.
insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, itemStack -> true);
}
if (!ItemStackUtils.isEmpty(insertionResultStack)) {
if (ItemStack.matches(insertionResultStack, stack) && worker.getCitizenData() != null) {
// The replaced stack is the same as the one we tried to put into the inventory.
// Meaning, replacing failed.
success = false;
if (targetBuilding.hasModule(WorkerBuildingModule.class)) {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, targetBuilding.getFirstModuleOccurance(WorkerBuildingModule.class).getFirstCitizen().getName()), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuilding.getID()));
} else {
worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new StringTextComponent(" :" + targetBuilding.getSchematicName())), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuildingLocation.getInDimensionLocation()));
}
}
// Insert the result back into the inventory so we do not lose it.
workerInventory.insertItem(i, insertionResultStack, false);
}
}
if (!extracted) {
// This can only happen if the dman's inventory was completely empty.
// Let the retry-system handle this case.
worker.decreaseSaturationForContinuousAction();
worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
job.finishRequest(false);
// No need to go dumping in this case.
return START_WORKING;
}
worker.getCitizenExperienceHandler().addExperience(1.5D);
worker.decreaseSaturationForContinuousAction();
worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
job.finishRequest(true);
return success ? START_WORKING : DUMPING;
}
use of com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable in project minecolonies by ldtteam.
the class JobDeliveryman method addRequest.
/**
* Method used to add a request to the queue
*
* @param token The token of the requests to add.
*/
public void addRequest(@NotNull final IToken<?> token, final int insertionIndex) {
final IRequestManager requestManager = getColony().getRequestManager();
IRequest<? extends IDeliverymanRequestable> newRequest = (IRequest<? extends IDeliverymanRequestable>) (requestManager.getRequestForToken(token));
LinkedList<IToken<?>> taskQueue = getTaskQueueFromDataStore();
int offset = 0;
for (int i = insertionIndex; i < taskQueue.size(); i++) {
final IToken theToken = taskQueue.get(i);
final IRequest<? extends IDeliverymanRequestable> request = (IRequest<? extends IDeliverymanRequestable>) (requestManager.getRequestForToken(theToken));
if (request == null || request.getState() == RequestState.COMPLETED) {
taskQueue.remove(theToken);
i--;
offset--;
} else {
request.getRequest().incrementPriorityDueToAging();
}
}
getTaskQueueFromDataStore().add(Math.max(0, insertionIndex + offset), token);
if (newRequest instanceof StandardRequests.PickupRequest && newRequest.getRequest().getPriority() == getPlayerActionPriority(true)) {
getCitizen().getEntity().ifPresent(e -> e.getCitizenChatHandler().sendLocalizedChat(COM_MINECOLONIES_COREMOD_ENTITY_DELIVERYMAN_FORCEPICKUP));
;
}
}
Aggregations