use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by ldtteam.
the class RecallCitizenMessage method onExecute.
@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final IBuilding building) {
final List<ICitizenData> citizens = new ArrayList<>(building.getAllAssignedCitizen());
for (int i = 0; i < building.getAllAssignedCitizen().size(); i++) {
Optional<AbstractEntityCitizen> optionalEntityCitizen = citizens.get(i).getEntity();
final ICitizenData citizenData = citizens.get(i);
if (!optionalEntityCitizen.isPresent()) {
if (citizenData != null) {
Log.getLogger().warn(String.format("Citizen #%d:%d has gone AWOL, respawning them!", colony.getID(), citizenData.getId()));
citizenData.setNextRespawnPosition(EntityUtils.getSpawnPoint(colony.getWorld(), building.getPosition()));
citizenData.updateEntityIfNecessary();
} else {
Log.getLogger().warn("Citizen is AWOL and citizenData is null!");
return;
}
} else if (optionalEntityCitizen.get().getTicksExisted() == 0) {
citizenData.getEntity().ifPresent(Entity::remove);
citizenData.updateEntityIfNecessary();
}
final BlockPos loc = building.getPosition();
if (optionalEntityCitizen.isPresent() && !TeleportHelper.teleportCitizen(optionalEntityCitizen.get(), colony.getWorld(), loc)) {
final PlayerEntity player = ctxIn.getSender();
if (player == null) {
return;
}
LanguageHandler.sendPlayerMessage(player, "com.minecolonies.coremod.workerhuts.recallFail");
}
}
}
use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by ldtteam.
the class BuildingCombatAcademy method getCombatPartner.
/**
* Get the citizen of the combat partner or null if not existing or available.
*
* @param citizen the citizen.
* @return the citizen or null.
*/
public AbstractEntityCitizen getCombatPartner(final AbstractEntityCitizen citizen) {
final ICitizenData data = citizen.getCitizenData();
if (data != null) {
final int citizenId;
if (trainingPartners.containsKey(data.getId())) {
citizenId = trainingPartners.get(data.getId());
} else if (trainingPartners.containsValue(data.getId())) {
citizenId = trainingPartners.inverse().get(data.getId());
} else {
return null;
}
final ICitizenData citizenData = getFirstModuleOccurance(WorkAtHomeBuildingModule.class).getAssignedCitizen().stream().filter(cit -> cit.getId() != data.getId()).filter(cit -> cit.getId() == citizenId).findFirst().orElse(null);
if (citizenData != null) {
return citizenData.getEntity().orElse(null);
}
}
return null;
}
use of com.minecolonies.api.entity.citizen.AbstractEntityCitizen in project minecolonies by ldtteam.
the class EntityAIWorkEnchanter method gatherAndDrain.
/**
* Gather experience from a worker. Go to the hut of the worker. Wait for the worker. Drain, and then return to work building.
*
* @return next state to go to.
*/
private IAIState gatherAndDrain() {
if (job.getPosToDrainFrom() == null) {
return IDLE;
}
if (walkToBlock(job.getPosToDrainFrom())) {
return getState();
}
final AbstractBuilding buildingWorker = getOwnBuilding().getColony().getBuildingManager().getBuilding(job.getPosToDrainFrom(), AbstractBuilding.class);
if (buildingWorker == null) {
resetDraining();
getOwnBuilding().getFirstModuleOccurance(EnchanterStationsModule.class).removeWorker(job.getPosToDrainFrom());
return IDLE;
}
if (citizenToGatherFrom == null) {
final List<AbstractEntityCitizen> workers = new ArrayList<>();
for (final Optional<AbstractEntityCitizen> citizen : getModuleForJob().getAssignedEntities()) {
citizen.ifPresent(workers::add);
}
final AbstractEntityCitizen citizen;
if (workers.size() > 1) {
citizen = workers.get(worker.getRandom().nextInt(workers.size()));
} else {
if (workers.isEmpty()) {
resetDraining();
return DECIDE;
}
citizen = workers.get(0);
}
citizenToGatherFrom = citizen.getCitizenData();
progressTicks = 0;
return getState();
}
if (!citizenToGatherFrom.getEntity().isPresent()) {
citizenToGatherFrom = null;
return getState();
}
if (progressTicks == 0) {
// If worker is too far away wait.
if (BlockPosUtil.getDistance2D(citizenToGatherFrom.getEntity().get().blockPosition(), worker.blockPosition()) > MIN_DISTANCE_TO_DRAIN) {
if (!job.incrementWaitingTicks()) {
resetDraining();
return DECIDE;
}
return getState();
}
}
progressTicks++;
if (progressTicks < MAX_PROGRESS_TICKS) {
final Vector3d start = worker.position().add(0, 2, 0);
final Vector3d goal = citizenToGatherFrom.getEntity().get().position().add(0, 2, 0);
Network.getNetwork().sendToTrackingEntity(new StreamParticleEffectMessage(start, goal, ParticleTypes.ENCHANT, progressTicks % MAX_PROGRESS_TICKS, MAX_PROGRESS_TICKS), worker);
Network.getNetwork().sendToTrackingEntity(new CircleParticleEffectMessage(start, ParticleTypes.HAPPY_VILLAGER, progressTicks), worker);
WorkerUtil.faceBlock(new BlockPos(goal), worker);
if (worker.getRandom().nextBoolean()) {
worker.swing(Hand.MAIN_HAND);
} else {
worker.swing(Hand.OFF_HAND);
}
return getState();
}
final int bookSlot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), Items.BOOK);
if (bookSlot != -1) {
final int size = citizenToGatherFrom.getInventory().getSlots();
final int attempts = (int) (getSecondarySkillLevel() / 5.0);
for (int i = 0; i < attempts; i++) {
int randomSlot = worker.getRandom().nextInt(size);
final ItemStack stack = citizenToGatherFrom.getInventory().getStackInSlot(randomSlot);
if (!stack.isEmpty() && stack.isEnchantable()) {
EnchantmentHelper.enchantItem(worker.getRandom(), stack, getSecondarySkillLevel() > 50 ? 2 : 1, false);
break;
}
}
worker.getInventoryCitizen().extractItem(bookSlot, 1, false);
worker.getCitizenData().getCitizenSkillHandler().incrementLevel(Skill.Mana, 1);
worker.getCitizenExperienceHandler().addExperience(XP_PER_DRAIN);
worker.getCitizenData().markDirty();
}
resetDraining();
return IDLE;
}
Aggregations