use of com.minecolonies.api.util.Tuple in project minecolonies by Minecolonies.
the class AbstractEntityAIRequestSmelter method checkForItems.
@Override
protected IAIState checkForItems(@NotNull final IRecipeStorage storage) {
if (storage.getIntermediate() != Blocks.FURNACE) {
return super.checkForItems(storage);
}
final List<ItemStorage> input = storage.getCleanedInput();
final int countInFurnaces = getExtendedCount(storage.getPrimaryOutput());
int outputInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, storage.getPrimaryOutput()));
for (final ItemStorage inputStorage : input) {
final Predicate<ItemStack> predicate = stack -> !ItemStackUtils.isEmpty(stack) && ItemStackUtils.compareItemStacksIgnoreStackSize(stack, inputStorage.getItemStack());
int inputInFurnace = getExtendedCount(inputStorage.getItemStack());
int inputInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), predicate);
if (countInFurnaces + inputInFurnace + inputInInv + outputInInv < inputStorage.getAmount() * job.getMaxCraftingCount()) {
if (InventoryUtils.hasItemInProvider(building, predicate)) {
needsCurrently = new Tuple<>(predicate, inputStorage.getAmount() * (job.getMaxCraftingCount() - countInFurnaces - inputInFurnace));
return GATHERING_REQUIRED_MATERIALS;
}
}
// if we don't have enough at all, cancel
int countOfInput = inputInInv + InventoryUtils.getCountFromBuilding(building, predicate) + countInFurnaces + inputInFurnace + outputInInv;
if (countOfInput < inputStorage.getAmount() * job.getMaxCraftingCount()) {
job.finishRequest(false);
resetValues();
}
}
return CRAFT;
}
use of com.minecolonies.api.util.Tuple in project minecolonies by Minecolonies.
the class AbstractEntityAIRequestSmelter method fillUpFurnace.
/**
* Smelt the smeltable after the required items are in the inv.
*
* @return the next state to go to.
*/
private IAIState fillUpFurnace() {
final FurnaceUserModule module = building.getFirstModuleOccurance(FurnaceUserModule.class);
if (module.getFurnaces().isEmpty()) {
if (worker.getCitizenData() != null) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(BAKER_HAS_NO_FURNACES_MESSAGE), ChatPriority.BLOCKING));
}
setDelay(STANDARD_DELAY);
return START_WORKING;
}
if (walkTo == null || world.getBlockState(walkTo).getBlock() != Blocks.FURNACE) {
walkTo = null;
setDelay(STANDARD_DELAY);
return START_WORKING;
}
final int burningCount = countOfBurningFurnaces();
final TileEntity entity = world.getBlockEntity(walkTo);
if (entity instanceof FurnaceTileEntity && currentRecipeStorage != null) {
final FurnaceTileEntity furnace = (FurnaceTileEntity) entity;
final int maxFurnaces = getMaxUsableFurnaces();
final Predicate<ItemStack> smeltable = stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(currentRecipeStorage.getCleanedInput().get(0).getItemStack(), stack);
final int smeltableInFurnaces = getExtendedCount(currentRecipeStorage.getCleanedInput().get(0).getItemStack());
final int resultInFurnaces = getExtendedCount(currentRecipeStorage.getPrimaryOutput());
final int resultInCitizenInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentRecipeStorage.getPrimaryOutput()));
final int targetCount = currentRequest.getRequest().getCount() - smeltableInFurnaces - resultInFurnaces - resultInCitizenInv;
if (targetCount <= 0) {
return START_WORKING;
}
final int amountOfSmeltableInBuilding = InventoryUtils.getCountFromBuilding(building, smeltable);
final int amountOfSmeltableInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), smeltable);
if (worker.getItemInHand(Hand.MAIN_HAND).isEmpty()) {
worker.setItemInHand(Hand.MAIN_HAND, currentRecipeStorage.getCleanedInput().get(0).getItemStack().copy());
}
if (amountOfSmeltableInInv > 0) {
if (hasFuelInFurnaceAndNoSmeltable(furnace) || hasNeitherFuelNorSmeltAble(furnace)) {
int toTransfer = 0;
if (burningCount < maxFurnaces) {
final int availableFurnaces = maxFurnaces - burningCount;
if (targetCount > STACKSIZE * availableFurnaces) {
toTransfer = STACKSIZE;
} else {
// We need to split stacks and spread them across furnaces for best performance
// We will front-load the remainder
toTransfer = Math.min((targetCount / availableFurnaces) + (targetCount % availableFurnaces), STACKSIZE);
}
}
if (toTransfer > 0) {
if (walkToBlock(walkTo)) {
return getState();
}
worker.getCitizenItemHandler().hitBlockWithToolInHand(walkTo);
InventoryUtils.transferXInItemHandlerIntoSlotInItemHandler(worker.getInventoryCitizen(), smeltable, toTransfer, new InvWrapper(furnace), SMELTABLE_SLOT);
}
}
} else if (amountOfSmeltableInBuilding >= targetCount - amountOfSmeltableInInv && currentRecipeStorage.getIntermediate() == Blocks.FURNACE) {
needsCurrently = new Tuple<>(smeltable, targetCount);
return GATHERING_REQUIRED_MATERIALS;
} else {
// This is a safety net for the AI getting way out of sync with it's tracking. It shouldn't happen.
job.finishRequest(false);
resetValues();
walkTo = null;
return IDLE;
}
} else if (!(world.getBlockState(walkTo).getBlock() instanceof FurnaceBlock)) {
module.removeFromFurnaces(walkTo);
}
walkTo = null;
setDelay(STANDARD_DELAY);
return START_WORKING;
}
use of com.minecolonies.api.util.Tuple in project minecolonies by Minecolonies.
the class AbstractEntityAIUsesFurnace method startWorking.
/**
* Central method of the furnace user, he decides about what to do next from here. First check if any of the workers has important tasks to handle first. If not check if there
* is an oven with an item which has to be retrieved. If not check if fuel and smeltable are available and request if necessary and get into inventory. Then check if able to
* smelt already.
*
* @return the next state to go to.
*/
public IAIState startWorking() {
if (walkToBuilding()) {
return getState();
}
final FurnaceUserModule furnaceModule = building.getFirstModuleOccurance(FurnaceUserModule.class);
final ItemListModule itemListModule = building.getModuleMatching(ItemListModule.class, m -> m.getId().equals(FUEL_LIST));
worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING);
if (itemListModule.getList().isEmpty()) {
if (worker.getCitizenData() != null) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(FURNACE_USER_NO_FUEL), ChatPriority.BLOCKING));
}
return getState();
}
if (furnaceModule.getFurnaces().isEmpty()) {
if (worker.getCitizenData() != null) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(BAKER_HAS_NO_FURNACES_MESSAGE), ChatPriority.BLOCKING));
}
return getState();
}
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_STATUS_DECIDING));
final IAIState nextState = checkForImportantJobs();
if (nextState != START_WORKING) {
return nextState;
}
final BlockPos posOfUsedFuelOven = getPositionOfOvenToRetrieveFuelFrom();
if (posOfUsedFuelOven != null) {
walkTo = posOfUsedFuelOven;
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.retrieving"));
return RETRIEVING_USED_FUEL_FROM_FURNACE;
}
final BlockPos posOfOven = getPositionOfOvenToRetrieveFrom();
if (posOfOven != null) {
walkTo = posOfOven;
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.retrieving"));
return RETRIEVING_END_PRODUCT_FROM_FURNACE;
}
final int amountOfSmeltableInBuilding = InventoryUtils.getCountFromBuilding(building, this::isSmeltable);
final int amountOfSmeltableInInv = InventoryUtils.getItemCountInItemHandler((worker.getInventoryCitizen()), this::isSmeltable);
final int amountOfFuelInBuilding = InventoryUtils.getCountFromBuilding(building, itemListModule.getList());
final int amountOfFuelInInv = InventoryUtils.getItemCountInItemHandler((worker.getInventoryCitizen()), stack -> itemListModule.isItemInList(new ItemStorage(stack)));
if (amountOfSmeltableInBuilding + amountOfSmeltableInInv <= 0 && !reachedMaxToKeep()) {
requestSmeltable();
}
if (amountOfFuelInBuilding + amountOfFuelInInv <= 0 && !building.hasWorkerOpenRequestsFiltered(worker.getCitizenData().getId(), req -> req.getShortDisplayString().getSiblings().contains(new TranslationTextComponent(RequestSystemTranslationConstants.REQUESTS_TYPE_BURNABLE)))) {
worker.getCitizenData().createRequestAsync(new StackList(getAllowedFuel(), RequestSystemTranslationConstants.REQUESTS_TYPE_BURNABLE, STACKSIZE * furnaceModule.getFurnaces().size(), 1));
}
if (amountOfSmeltableInBuilding > 0 && amountOfSmeltableInInv == 0) {
needsCurrently = new Tuple<>(this::isSmeltable, STACKSIZE);
return GATHERING_REQUIRED_MATERIALS;
} else if (amountOfFuelInBuilding > 0 && amountOfFuelInInv == 0) {
needsCurrently = new Tuple<>(stack -> itemListModule.isItemInList(new ItemStorage(stack)), STACKSIZE);
return GATHERING_REQUIRED_MATERIALS;
}
return checkIfAbleToSmelt(amountOfFuelInBuilding + amountOfFuelInInv, amountOfSmeltableInBuilding + amountOfSmeltableInInv);
}
use of com.minecolonies.api.util.Tuple in project minecolonies by Minecolonies.
the class ResearchListener method parseRemoveResearches.
/**
* Parses out a researches map for elements containing Remove properties, and applies those removals to the researchMap
*
* @param object A Map containing the resource location of each json file, and the element within that json file.
* @return A Tuple containing resource locations of Researches (A) and Branches (B) to remove from the global research tree.
*/
private Tuple<Collection<ResourceLocation>, Collection<ResourceLocation>> parseRemoveResearches(final Map<ResourceLocation, JsonElement> object) {
Collection<ResourceLocation> removeResearches = new HashSet<>();
Collection<ResourceLocation> removeBranches = new HashSet<>();
for (final Map.Entry<ResourceLocation, JsonElement> entry : object.entrySet()) {
final JsonObject researchJson = entry.getValue().getAsJsonObject();
if (researchJson.has(RESEARCH_REMOVE_PROP)) {
// Removing an entire branch, and all research on that branch.
if (researchJson.has(RESEARCH_BRANCH_NAME_PROP) || researchJson.has(RESEARCH_BASE_TIME_PROP)) {
// Accept arrays, if the data pack makers wants to remove multiple branches.
if (researchJson.get(RESEARCH_REMOVE_PROP).isJsonArray()) {
for (final JsonElement remove : researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonArray()) {
if (remove.isJsonPrimitive() && remove.getAsJsonPrimitive().isString()) {
removeBranches.add(new ResourceLocation(remove.getAsString()));
}
}
} else // The json for such a removal can have an arbitrary filename, and the remove property points to the specific json to remove.
if (researchJson.get(RESEARCH_REMOVE_PROP).isJsonPrimitive() && researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonPrimitive().isString()) {
removeBranches.add(new ResourceLocation(researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonPrimitive().getAsString()));
} else // Lastly, accept just boolean true, for the simple case of removing this particular branch and all component researches.
if (researchJson.get(RESEARCH_REMOVE_PROP).isJsonPrimitive() && researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonPrimitive().isBoolean() && researchJson.get(RESEARCH_REMOVE_PROP).getAsBoolean()) {
removeBranches.add(entry.getKey());
}
} else // The json for such a removal can have an arbitrary filename, and the remove property points to the specific json to remove.
if (researchJson.get(RESEARCH_REMOVE_PROP).isJsonArray()) {
for (final JsonElement remove : researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonArray()) {
if (remove.isJsonPrimitive() && remove.getAsJsonPrimitive().isString()) {
removeResearches.add(new ResourceLocation(remove.getAsString()));
}
}
} else // Removing individual researches by name.
if (researchJson.get(RESEARCH_REMOVE_PROP).isJsonPrimitive() && researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonPrimitive().isString()) {
removeResearches.add(new ResourceLocation(researchJson.get(RESEARCH_REMOVE_PROP).getAsString()));
} else // Removes with a boolean true, but are not branch removes.
if (researchJson.get(RESEARCH_REMOVE_PROP).isJsonPrimitive() && researchJson.get(RESEARCH_REMOVE_PROP).getAsJsonPrimitive().isBoolean() && researchJson.get(RESEARCH_REMOVE_PROP).getAsBoolean()) {
removeResearches.add(entry.getKey());
} else // Files which declare remove, but are malformed should be reported to help diagnose the error.
{
Log.getLogger().error(entry.getKey() + " is a research remove, but does not contain all required fields. Research Removes must have remove:boolean and id:string.");
}
}
}
return new Tuple<>(removeResearches, removeBranches);
}
use of com.minecolonies.api.util.Tuple in project minecolonies by Minecolonies.
the class TavernBuildingModule method spawnVisitor.
/**
* Spawns a recruitable visitor citizen.
*/
private void spawnVisitor() {
IVisitorData newCitizen = (IVisitorData) building.getColony().getVisitorManager().createAndRegisterCivilianData();
externalCitizens.add(newCitizen.getId());
newCitizen.setBedPos(building.getPosition());
newCitizen.setHomeBuilding(building);
int recruitLevel = building.getColony().getWorld().random.nextInt(10 * building.getBuildingLevel()) + 15;
List<com.minecolonies.api.util.Tuple<Item, Integer>> recruitCosts = IColonyManager.getInstance().getCompatibilityManager().getRecruitmentCostsWeights();
if (newCitizen.getName().contains("Ray")) {
newCitizen.setRecruitCosts(new ItemStack(Items.BAKED_POTATO, 64));
}
newCitizen.getCitizenSkillHandler().init(recruitLevel);
BlockPos spawnPos = BlockPosUtil.findSpawnPosAround(building.getColony().getWorld(), building.getPosition());
if (spawnPos == null) {
spawnPos = building.getPosition();
}
Tuple<Item, Integer> cost = recruitCosts.get(building.getColony().getWorld().random.nextInt(recruitCosts.size()));
ItemStack boots = ItemStack.EMPTY;
if (recruitLevel > LEATHER_SKILL_LEVEL) {
// Leather
boots = new ItemStack(Items.LEATHER_BOOTS);
}
if (recruitLevel > GOLD_SKILL_LEVEL) {
// Gold
boots = new ItemStack(Items.GOLDEN_BOOTS);
}
if (recruitLevel > IRON_SKILL_LEVEL) {
if (cost.getB() <= 2) {
cost = recruitCosts.get(building.getColony().getWorld().random.nextInt(recruitCosts.size()));
}
// Iron
boots = new ItemStack(Items.IRON_BOOTS);
}
if (recruitLevel > DIAMOND_SKILL_LEVEL) {
if (cost.getB() <= 3) {
cost = recruitCosts.get(building.getColony().getWorld().random.nextInt(recruitCosts.size()));
}
// Diamond
boots = new ItemStack(Items.DIAMOND_BOOTS);
}
newCitizen.setRecruitCosts(new ItemStack(cost.getA(), (int) (recruitLevel * 3.0 / cost.getB())));
if (!CustomVisitorListener.chanceCustomVisitors(newCitizen)) {
newCitizen.triggerInteraction(new RecruitmentInteraction(new TranslationTextComponent("com.minecolonies.coremod.gui.chat.recruitstory" + (building.getColony().getWorld().random.nextInt(MAX_STORY) + 1), newCitizen.getName().split(" ")[0]), ChatPriority.IMPORTANT));
}
building.getColony().getVisitorManager().spawnOrCreateCivilian(newCitizen, building.getColony().getWorld(), spawnPos, true);
if (newCitizen.getEntity().isPresent()) {
newCitizen.getEntity().get().setItemSlot(EquipmentSlotType.FEET, boots);
}
building.getColony().getEventDescriptionManager().addEventDescription(new VisitorSpawnedEvent(spawnPos, newCitizen.getName()));
}
Aggregations