use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingFarmer in project minecolonies by Minecolonies.
the class BuildingManager method sendFieldPackets.
/**
* Sends packages to update the fields.
*
* @param closeSubscribers the current event subscribers.
* @param newSubscribers the new event subscribers.
*/
private void sendFieldPackets(final Set<ServerPlayerEntity> closeSubscribers, final Set<ServerPlayerEntity> newSubscribers) {
if (isFieldsDirty || !newSubscribers.isEmpty()) {
final Set<ServerPlayerEntity> players = new HashSet<>();
if (isFieldsDirty) {
players.addAll(closeSubscribers);
}
players.addAll(newSubscribers);
for (final IBuilding building : buildings.values()) {
if (building instanceof BuildingFarmer) {
players.forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewBuildingViewMessage(building), player));
}
}
}
}
use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingFarmer in project minecolonies by ldtteam.
the class EntityAIWorkFarmer method workAtField.
/**
* This (re)initializes a field. Checks the block above to see if it is a plant, if so, breaks it. Then tills.
*
* @return the next state to go into.
*/
private IAIState workAtField() {
@Nullable final BuildingFarmer buildingFarmer = getOwnBuilding();
final FarmerFieldModule module = buildingFarmer.getFirstModuleOccurance(FarmerFieldModule.class);
if (checkForToolOrWeapon(ToolType.HOE) || module.getCurrentField() == null) {
return PREPARING;
}
worker.getCitizenData().setVisibleStatus(FARMING_ICON);
@Nullable final BlockPos field = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(field);
if (entity instanceof ScarecrowTileEntity) {
final ScarecrowTileEntity scarecrow = (ScarecrowTileEntity) entity;
if (workingOffset != null) {
if (scarecrow.getOwnerId() != worker.getCivilianID()) {
module.freeField(module.getCurrentField());
module.setCurrentField(null);
return getState();
}
final BlockPos position = field.below().south(workingOffset.getZ()).east(workingOffset.getX());
// Still moving to the block
if (walkToBlock(position.above())) {
return getState();
}
switch((AIWorkerState) getState()) {
case FARMER_HOE:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.hoeing"));
if (!hoeIfAble(position, scarecrow)) {
return getState();
}
break;
case FARMER_PLANT:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.planting"));
if (!tryToPlant(scarecrow, position)) {
return PREPARING;
}
break;
case FARMER_HARVEST:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.harvesting"));
if (!harvestIfAble(position)) {
return getState();
}
break;
default:
return PREPARING;
}
prevPos = position;
setDelay(getLevelDelay());
}
workingOffset = nextValidCell(scarecrow);
if (workingOffset == null) {
shouldDumpInventory = true;
scarecrow.nextState();
prevPos = null;
return IDLE;
}
} else {
return IDLE;
}
return getState();
}
use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingFarmer in project minecolonies by Minecolonies.
the class EntityAIWorkFarmer method workAtField.
/**
* This (re)initializes a field. Checks the block above to see if it is a plant, if so, breaks it. Then tills.
*
* @return the next state to go into.
*/
private IAIState workAtField() {
@Nullable final BuildingFarmer buildingFarmer = getOwnBuilding();
final FarmerFieldModule module = buildingFarmer.getFirstModuleOccurance(FarmerFieldModule.class);
if (checkForToolOrWeapon(ToolType.HOE) || module.getCurrentField() == null) {
return PREPARING;
}
worker.getCitizenData().setVisibleStatus(FARMING_ICON);
@Nullable final BlockPos field = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(field);
if (entity instanceof ScarecrowTileEntity) {
final ScarecrowTileEntity scarecrow = (ScarecrowTileEntity) entity;
if (workingOffset != null) {
if (scarecrow.getOwnerId() != worker.getCivilianID()) {
module.freeField(module.getCurrentField());
module.setCurrentField(null);
return getState();
}
final BlockPos position = field.below().south(workingOffset.getZ()).east(workingOffset.getX());
// Still moving to the block
if (walkToBlock(position.above())) {
return getState();
}
switch((AIWorkerState) getState()) {
case FARMER_HOE:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.hoeing"));
if (!hoeIfAble(position, scarecrow)) {
return getState();
}
break;
case FARMER_PLANT:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.planting"));
if (!tryToPlant(scarecrow, position)) {
return PREPARING;
}
break;
case FARMER_HARVEST:
worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.harvesting"));
if (!harvestIfAble(position)) {
return getState();
}
break;
default:
return PREPARING;
}
prevPos = position;
setDelay(getLevelDelay());
}
workingOffset = nextValidCell(scarecrow);
if (workingOffset == null) {
shouldDumpInventory = true;
scarecrow.nextState();
prevPos = null;
return IDLE;
}
} else {
return IDLE;
}
return getState();
}
use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingFarmer in project minecolonies by Minecolonies.
the class EntityAIWorkFarmer method prepareForFarming.
/**
* Prepares the farmer for farming. Also requests the tools and checks if the farmer has sufficient fields.
*
* @return the next IAIState
*/
@NotNull
private IAIState prepareForFarming() {
@Nullable final BuildingFarmer building = getOwnBuilding();
if (building == null || building.getBuildingLevel() < 1) {
return PREPARING;
}
if (!job.getTaskQueue().isEmpty() || getActionsDoneUntilDumping() <= job.getActionsDone()) {
return START_WORKING;
}
worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING);
final FarmerFieldModule module = building.getFirstModuleOccurance(FarmerFieldModule.class);
module.syncWithColony(world);
if (module.getFarmerFields().size() < getOwnBuilding().getBuildingLevel() && !module.assignManually()) {
searchAndAddFields();
}
if (module.getFarmerFields().size() == getOwnBuilding().getMaxBuildingLevel()) {
AdvancementUtils.TriggerAdvancementPlayersForColony(building.getColony(), AdvancementTriggers.MAX_FIELDS::trigger);
}
final int amountOfCompostInBuilding = InventoryUtils.getCountFromBuilding(getOwnBuilding(), this::isCompost);
final int amountOfCompostInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), this::isCompost);
if (amountOfCompostInBuilding + amountOfCompostInInv <= 0) {
if (getOwnBuilding().requestFertilizer() && !getOwnBuilding().hasWorkerOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(StackList.class))) {
final List<ItemStack> compostAbleItems = new ArrayList<>();
compostAbleItems.add(new ItemStack(ModItems.compost, 1));
compostAbleItems.add(new ItemStack(Items.BONE_MEAL, 1));
worker.getCitizenData().createRequestAsync(new StackList(compostAbleItems, FERTLIZER, STACKSIZE, 1));
}
} else if (amountOfCompostInInv <= 0 && amountOfCompostInBuilding > 0) {
needsCurrently = new Tuple<>(this::isCompost, STACKSIZE);
return GATHERING_REQUIRED_MATERIALS;
}
if (module.hasNoFields()) {
if (worker.getCitizenData() != null) {
worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(NO_FREE_FIELDS), ChatPriority.BLOCKING));
}
worker.getCitizenData().setIdleAtJob(true);
return PREPARING;
}
worker.getCitizenData().setIdleAtJob(false);
// If the farmer has no currentField and there is no field which needs work, check fields.
if (module.getCurrentField() == null && module.getFieldToWorkOn(world) == null) {
module.resetFields();
return IDLE;
}
@Nullable final BlockPos currentField = module.getCurrentField();
final TileEntity entity = world.getBlockEntity(currentField);
if (entity instanceof ScarecrowTileEntity && ((ScarecrowTileEntity) entity).needsWork()) {
if (((ScarecrowTileEntity) entity).getFieldStage() == ScarecrowFieldStage.PLANTED && checkIfShouldExecute((ScarecrowTileEntity) entity, this::shouldHarvest)) {
return FARMER_HARVEST;
} else if (((ScarecrowTileEntity) entity).getFieldStage() == ScarecrowFieldStage.HOED) {
return canGoPlanting((ScarecrowTileEntity) entity, building);
} else if (((ScarecrowTileEntity) entity).getFieldStage() == ScarecrowFieldStage.EMPTY && checkIfShouldExecute((ScarecrowTileEntity) entity, pos -> this.shouldHoe(pos, (ScarecrowTileEntity) entity))) {
return FARMER_HOE;
}
((ScarecrowTileEntity) entity).nextState();
} else {
module.setCurrentField(null);
}
return PREPARING;
}
use of com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingFarmer in project minecolonies by ldtteam.
the class BuildingManager method sendFieldPackets.
/**
* Sends packages to update the fields.
*
* @param closeSubscribers the current event subscribers.
* @param newSubscribers the new event subscribers.
*/
private void sendFieldPackets(final Set<ServerPlayerEntity> closeSubscribers, final Set<ServerPlayerEntity> newSubscribers) {
if (isFieldsDirty || !newSubscribers.isEmpty()) {
final Set<ServerPlayerEntity> players = new HashSet<>();
if (isFieldsDirty) {
players.addAll(closeSubscribers);
}
players.addAll(newSubscribers);
for (final IBuilding building : buildings.values()) {
if (building instanceof BuildingFarmer) {
players.forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewBuildingViewMessage(building), player));
}
}
}
}
Aggregations