use of com.minecolonies.api.util.Tuple in project minecolonies by ldtteam.
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 = getOwnBuilding().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(getOwnBuilding(), 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 ldtteam.
the class EventHandler method onEntityConverted.
/**
* Gets called when a Hoglin, Pig, Piglin, Villager, or ZombieVillager gets converted to something else.
*
* @param event the event to handle.
*/
@SubscribeEvent
public static void onEntityConverted(@NotNull final LivingConversionEvent.Pre event) {
LivingEntity entity = event.getEntityLiving();
if (entity instanceof ZombieVillagerEntity && event.getOutcome() == EntityType.VILLAGER) {
final World world = entity.getCommandSenderWorld();
final IColony colony = IColonyManager.getInstance().getIColony(world, entity.blockPosition());
if (colony != null && colony.hasBuilding("tavern", 1, false)) {
event.setCanceled(true);
if (ForgeEventFactory.canLivingConvert(entity, ModEntities.VISITOR, null)) {
IVisitorData visitorData = (IVisitorData) colony.getVisitorManager().createAndRegisterCivilianData();
BlockPos tavernPos = colony.getBuildingManager().getRandomBuilding(b -> !b.getModules(TavernBuildingModule.class).isEmpty());
IBuilding tavern = colony.getBuildingManager().getBuilding(tavernPos);
visitorData.setHomeBuilding(tavern);
visitorData.setBedPos(tavernPos);
tavern.getModules(TavernBuildingModule.class).forEach(mod -> mod.getExternalCitizens().add(visitorData.getId()));
int recruitLevel = world.random.nextInt(10 * tavern.getBuildingLevel()) + 15;
List<com.minecolonies.api.util.Tuple<Item, Integer>> recruitCosts = IColonyManager.getInstance().getCompatibilityManager().getRecruitmentCostsWeights();
visitorData.getCitizenSkillHandler().init(recruitLevel);
colony.getVisitorManager().spawnOrCreateCivilian(visitorData, world, entity.blockPosition(), false);
colony.getEventDescriptionManager().addEventDescription(new VisitorSpawnedEvent(entity.blockPosition(), visitorData.getName()));
if (visitorData.getEntity().isPresent()) {
AbstractEntityCitizen visitorEntity = visitorData.getEntity().get();
for (EquipmentSlotType slotType : EquipmentSlotType.values()) {
ItemStack itemstack = entity.getItemBySlot(slotType);
if (slotType.getType() == EquipmentSlotType.Group.ARMOR && !itemstack.isEmpty()) {
visitorEntity.setItemSlot(slotType, itemstack);
}
}
}
if (!entity.isSilent()) {
world.levelEvent((PlayerEntity) null, 1027, entity.blockPosition(), 0);
}
entity.remove();
Tuple<Item, Integer> cost = recruitCosts.get(world.random.nextInt(recruitCosts.size()));
visitorData.setRecruitCosts(new ItemStack(cost.getA(), (int) (recruitLevel * 3.0 / cost.getB())));
visitorData.triggerInteraction(new RecruitmentInteraction(new TranslationTextComponent("com.minecolonies.coremod.gui.chat.recruitstorycured", visitorData.getName().split(" ")[0]), ChatPriority.IMPORTANT));
}
}
}
}
use of com.minecolonies.api.util.Tuple in project minecolonies by ldtteam.
the class CitizenDataView method deserialize.
@Override
public void deserialize(@NotNull final PacketBuffer buf) {
name = buf.readUtf(32767);
female = buf.readBoolean();
entityId = buf.readInt();
paused = buf.readBoolean();
isChild = buf.readBoolean();
homeBuilding = buf.readBoolean() ? buf.readBlockPos() : null;
workBuilding = buf.readBoolean() ? buf.readBlockPos() : null;
// Attributes
health = buf.readFloat();
maxHealth = buf.readFloat();
saturation = buf.readDouble();
happiness = buf.readDouble();
citizenSkillHandler.read(buf.readNbt());
job = buf.readUtf(32767);
colonyId = buf.readInt();
final CompoundNBT compound = buf.readNbt();
inventory = new InventoryCitizen(this.name, true);
final ListNBT ListNBT = compound.getList("inventory", 10);
this.inventory.read(ListNBT);
this.inventory.setHeldItem(Hand.MAIN_HAND, compound.getInt(TAG_HELD_ITEM_SLOT));
this.inventory.setHeldItem(Hand.OFF_HAND, compound.getInt(TAG_OFFHAND_HELD_ITEM_SLOT));
position = buf.readBlockPos();
citizenChatOptions.clear();
final int size = buf.readInt();
for (int i = 0; i < size; i++) {
final CompoundNBT compoundNBT = buf.readNbt();
final ServerCitizenInteraction handler = (ServerCitizenInteraction) MinecoloniesAPIProxy.getInstance().getInteractionResponseHandlerDataManager().createFrom(this, compoundNBT);
citizenChatOptions.put(handler.getInquiry(), handler);
}
sortedInteractions = citizenChatOptions.values().stream().sorted(Comparator.comparingInt(e -> e.getPriority().getPriority())).collect(Collectors.toList());
citizenHappinessHandler.read(buf.readNbt());
int statusindex = buf.readInt();
statusIcon = statusindex >= 0 ? VisibleCitizenStatus.getForId(statusindex) : null;
if (buf.readBoolean()) {
final IColonyView colonyView = IColonyManager.getInstance().getColonyView(colonyId, Minecraft.getInstance().level.dimension());
jobView = IJobDataManager.getInstance().createViewFrom(colonyView, this, buf);
} else {
jobView = null;
}
children.clear();
siblings.clear();
partner = buf.readInt();
final int siblingsSize = buf.readInt();
for (int i = 0; i < siblingsSize; i++) {
siblings.add(buf.readInt());
}
final int childrenSize = buf.readInt();
for (int i = 0; i < childrenSize; i++) {
children.add(buf.readInt());
}
final String parentA = buf.readUtf();
final String parentB = buf.readUtf();
parents = new Tuple<>(parentA, parentB);
}
use of com.minecolonies.api.util.Tuple in project minecolonies by ldtteam.
the class WindowInfoPage method createAndSetStatistics.
/**
* Creates several statistics and sets them in the building GUI.
*/
private void createAndSetStatistics() {
final DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
final String roundedHappiness = df.format(building.getColony().getOverallHappiness());
findPaneOfTypeByID(HAPPINESS_LABEL, Text.class).setText(roundedHappiness);
final int citizensSize = building.getColony().getCitizens().size();
final int citizensCap;
if (MinecoloniesAPIProxy.getInstance().getGlobalResearchTree().hasResearchEffect(CITIZEN_CAP)) {
citizensCap = (int) (Math.min(MineColonies.getConfig().getServer().maxCitizenPerColony.get(), 25 + this.building.getColony().getResearchManager().getResearchEffects().getEffectStrength(CITIZEN_CAP)));
} else {
citizensCap = MineColonies.getConfig().getServer().maxCitizenPerColony.get();
}
final Text totalCitizenLabel = findPaneOfTypeByID(TOTAL_CITIZENS_LABEL, Text.class);
totalCitizenLabel.setText(LanguageHandler.format(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_TOTALCITIZENS_COUNT, citizensSize, Math.max(citizensSize, building.getColony().getCitizenCountLimit())));
List<IFormattableTextComponent> hoverText = new ArrayList<>();
if (citizensSize < (citizensCap * 0.9) && citizensSize < (building.getColony().getCitizenCountLimit() * 0.9)) {
totalCitizenLabel.setColors(DARKGREEN);
} else if (citizensSize < citizensCap) {
hoverText.add(new TranslationTextComponent("com.minecolonies.coremod.gui.townhall.population.totalcitizens.houselimited", this.building.getColony().getName()));
totalCitizenLabel.setColors(ORANGE);
} else {
if (citizensCap < MineColonies.getConfig().getServer().maxCitizenPerColony.get()) {
hoverText.add(new TranslationTextComponent("com.minecolonies.coremod.gui.townhall.population.totalcitizens.researchlimited", this.building.getColony().getName()));
} else {
hoverText.add(new TranslationTextComponent("com.minecolonies.coremod.gui.townhall.population.totalcitizens.configlimited", this.building.getColony().getName()));
}
totalCitizenLabel.setText(LanguageHandler.format(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_TOTALCITIZENS_COUNT, citizensSize, citizensCap));
totalCitizenLabel.setColors(RED);
}
PaneBuilders.tooltipBuilder().hoverPane(totalCitizenLabel).build().setText(hoverText);
int children = 0;
final Map<String, Tuple<Integer, Integer>> jobMaxCountMap = new HashMap<>();
for (@NotNull final IBuildingView building : building.getColony().getBuildings()) {
if (building instanceof AbstractBuildingView) {
for (final WorkerBuildingModuleView module : building.getModuleViews(WorkerBuildingModuleView.class)) {
int max = module.getMaxInhabitants();
int workers = module.getAssignedCitizens().size();
final String jobName = module.getJobDisplayName().toLowerCase(Locale.ENGLISH);
final Tuple<Integer, Integer> tuple = jobMaxCountMap.getOrDefault(jobName, new Tuple<>(0, 0));
jobMaxCountMap.put(jobName, new Tuple<>(tuple.getA() + workers, tuple.getB() + max));
}
}
}
// calculate number of children
int unemployedCount = 0;
for (ICitizenDataView iCitizenDataView : building.getColony().getCitizens().values()) {
if (iCitizenDataView.isChild()) {
children++;
} else if (iCitizenDataView.getJobView() == null) {
unemployedCount++;
}
}
final String numberOfUnemployed = LanguageHandler.format(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_UNEMPLOYED, unemployedCount);
final String numberOfKids = LanguageHandler.format(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_CHILDS, children);
final ScrollingList list = findPaneOfTypeByID("citizen-stats", ScrollingList.class);
if (list == null) {
return;
}
final int maxJobs = jobMaxCountMap.size();
final List<Map.Entry<String, Tuple<Integer, Integer>>> theList = new ArrayList<>(jobMaxCountMap.entrySet());
list.setDataProvider(new ScrollingList.DataProvider() {
@Override
public int getElementCount() {
return maxJobs + 2;
}
@Override
public void updateElement(final int index, @NotNull final Pane rowPane) {
final Text label = rowPane.findPaneOfTypeByID(CITIZENS_AMOUNT_LABEL, Text.class);
if (index < theList.size()) {
final Map.Entry<String, Tuple<Integer, Integer>> entry = theList.get(index);
final String job = LanguageHandler.format(entry.getKey());
final String numberOfWorkers = LanguageHandler.format(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_EACH, job, entry.getValue().getA(), entry.getValue().getB());
label.setText(numberOfWorkers);
} else {
if (index == maxJobs + 1) {
label.setText(numberOfUnemployed);
} else {
label.setText(numberOfKids);
}
}
}
});
}
use of com.minecolonies.api.util.Tuple in project minecolonies by Minecolonies.
the class WindowInfoPage method createAndSetStatistics.
/**
* Creates several statistics and sets them in the building GUI.
*/
private void createAndSetStatistics() {
final DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
final String roundedHappiness = df.format(building.getColony().getOverallHappiness());
findPaneOfTypeByID(HAPPINESS_LABEL, Text.class).setText(roundedHappiness);
final int citizensSize = building.getColony().getCitizens().size();
final int citizensCap;
if (MinecoloniesAPIProxy.getInstance().getGlobalResearchTree().hasResearchEffect(CITIZEN_CAP)) {
citizensCap = (int) (Math.min(MineColonies.getConfig().getServer().maxCitizenPerColony.get(), 25 + this.building.getColony().getResearchManager().getResearchEffects().getEffectStrength(CITIZEN_CAP)));
} else {
citizensCap = MineColonies.getConfig().getServer().maxCitizenPerColony.get();
}
final Text totalCitizenLabel = findPaneOfTypeByID(TOTAL_CITIZENS_LABEL, Text.class);
totalCitizenLabel.setText(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_TOTALCITIZENS_COUNT, citizensSize, Math.max(citizensSize, building.getColony().getCitizenCountLimit())));
List<IFormattableTextComponent> hoverText = new ArrayList<>();
if (citizensSize < (citizensCap * 0.9) && citizensSize < (building.getColony().getCitizenCountLimit() * 0.9)) {
totalCitizenLabel.setColors(DARKGREEN);
} else if (citizensSize < citizensCap) {
hoverText.add(new TranslationTextComponent(WARNING_POPULATION_NEEDS_HOUSING, this.building.getColony().getName()));
totalCitizenLabel.setColors(ORANGE);
} else {
if (citizensCap < MineColonies.getConfig().getServer().maxCitizenPerColony.get()) {
hoverText.add(new TranslationTextComponent(WARNING_POPULATION_RESEARCH_LIMITED, this.building.getColony().getName()));
} else {
hoverText.add(new TranslationTextComponent(WARNING_POPULATION_CONFIG_LIMITED, this.building.getColony().getName()));
}
totalCitizenLabel.setText(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_TOTALCITIZENS_COUNT, citizensSize, citizensCap));
totalCitizenLabel.setColors(RED);
}
PaneBuilders.tooltipBuilder().hoverPane(totalCitizenLabel).build().setText(hoverText);
int children = 0;
final Map<String, Tuple<Integer, Integer>> jobMaxCountMap = new HashMap<>();
for (@NotNull final IBuildingView building : building.getColony().getBuildings()) {
if (building instanceof AbstractBuildingView) {
for (final WorkerBuildingModuleView module : building.getModuleViews(WorkerBuildingModuleView.class)) {
int max = module.getMaxInhabitants();
int workers = module.getAssignedCitizens().size();
final String jobName = module.getJobDisplayName().toLowerCase(Locale.ENGLISH);
final Tuple<Integer, Integer> tuple = jobMaxCountMap.getOrDefault(jobName, new Tuple<>(0, 0));
jobMaxCountMap.put(jobName, new Tuple<>(tuple.getA() + workers, tuple.getB() + max));
}
}
}
// calculate number of children
int unemployedCount = 0;
for (ICitizenDataView iCitizenDataView : building.getColony().getCitizens().values()) {
if (iCitizenDataView.isChild()) {
children++;
} else if (iCitizenDataView.getJobView() == null) {
unemployedCount++;
}
}
final ITextComponent numberOfUnemployed = new TranslationTextComponent(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_UNEMPLOYED, unemployedCount);
final ITextComponent numberOfKids = new TranslationTextComponent(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_CHILDS, children);
final ScrollingList list = findPaneOfTypeByID("citizen-stats", ScrollingList.class);
if (list == null) {
return;
}
final int maxJobs = jobMaxCountMap.size();
final List<Map.Entry<String, Tuple<Integer, Integer>>> theList = new ArrayList<>(jobMaxCountMap.entrySet());
theList.sort(Map.Entry.comparingByKey());
list.setDataProvider(new ScrollingList.DataProvider() {
@Override
public int getElementCount() {
return maxJobs + 2;
}
@Override
public void updateElement(final int index, @NotNull final Pane rowPane) {
final Text label = rowPane.findPaneOfTypeByID(CITIZENS_AMOUNT_LABEL, Text.class);
if (index < theList.size()) {
final Map.Entry<String, Tuple<Integer, Integer>> entry = theList.get(index);
final ITextComponent job = new TranslationTextComponent(entry.getKey());
final ITextComponent numberOfWorkers = new TranslationTextComponent(COM_MINECOLONIES_COREMOD_GUI_TOWNHALL_POPULATION_EACH, job, entry.getValue().getA(), entry.getValue().getB());
label.setText(numberOfWorkers);
} else {
if (index == maxJobs + 1) {
label.setText(numberOfUnemployed);
} else {
label.setText(numberOfKids);
}
}
}
});
}
Aggregations