use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class ItemScrollGuardHelp method onItemUseSuccess.
@Override
protected ItemStack onItemUseSuccess(final ItemStack itemStack, final World world, final ServerPlayerEntity player) {
final IColony colony = getColony(itemStack);
final BlockPos buildingPos = BlockPosUtil.read(itemStack.getTag(), TAG_BUILDING_POS);
final IBuilding building = colony.getBuildingManager().getBuilding(buildingPos);
if (!(building instanceof AbstractBuildingGuards)) {
LanguageHandler.sendPlayerMessage(player, "minecolonies.scroll.noguardbuilding");
return itemStack;
}
itemStack.shrink(1);
final List<ICitizenData> guards = new ArrayList<>(building.getAllAssignedCitizen());
if (world.random.nextInt(10) == 0 || colony.getWorld() != world) {
// Fail
final LlamaEntity entity = EntityType.LLAMA.create(world);
entity.setPos(player.getX(), player.getY(), player.getZ());
world.addFreshEntity(entity);
player.displayClientMessage(new TranslationTextComponent("minecolonies.scroll.failed" + (world.random.nextInt(FAIL_RESPONSES_TOTAL) + 1)).setStyle(Style.EMPTY.withColor(TextFormatting.GOLD)), true);
SoundUtils.playSoundForPlayer(player, SoundEvents.EVOKER_CAST_SPELL, 0.5f, 1.0f);
return itemStack;
} else {
for (final ICitizenData citizenData : guards) {
final AbstractJobGuard job = citizenData.getJob(AbstractJobGuard.class);
if (job != null && job.getWorkerAI() != null && !((AbstractEntityAIGuard) job.getWorkerAI()).hasTool()) {
continue;
}
if (citizenData.getEntity().isPresent()) {
if (citizenData.getEntity().get().getCitizenDiseaseHandler().isSick()) {
continue;
}
citizenData.getEntity().get().remove();
}
colony.getCitizenManager().spawnOrCreateCivilian(citizenData, world, player.blockPosition(), true);
citizenData.setNextRespawnPosition(buildingPos);
((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.FOLLOW);
((AbstractBuildingGuards) building).setPlayerToFollow(player);
final FollowModeSetting grouping = ((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.FOLLOW_MODE);
if (grouping.getValue().equals(FollowModeSetting.LOOSE)) {
grouping.trigger();
}
citizenData.setSaturation(100);
colony.getPackageManager().addCloseSubscriber(player);
if (job != null && job.getWorkerAI() != null) {
final long spawnTime = world.getGameTime() + TICKS_SECOND * 900;
// Timed despawn
job.getWorkerAI().registerTarget(new AIOneTimeEventTarget(() -> {
if (world.getGameTime() - spawnTime > 0) {
((AbstractBuildingGuards) building).getSetting(AbstractBuildingGuards.GUARD_TASK).set(GuardTaskSetting.PATROL);
citizenData.getEntity().ifPresent(Entity::remove);
colony.getPackageManager().removeCloseSubscriber(player);
return true;
}
return false;
}, AIWorkerState.DECIDE));
}
}
SoundUtils.playSoundForPlayer(player, SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 0.3f, 1.0f);
}
return itemStack;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class BuildingManager method removeBuilding.
@Override
public void removeBuilding(@NotNull final IBuilding building, final Set<ServerPlayerEntity> subscribers) {
if (buildings.containsKey(building.getID())) {
final ImmutableMap.Builder<BlockPos, IBuilding> builder = new ImmutableMap.Builder<>();
for (final IBuilding tbuilding : buildings.values()) {
if (tbuilding != building) {
builder.put(tbuilding.getID(), tbuilding);
}
}
buildings = builder.build();
for (final ServerPlayerEntity player : subscribers) {
Network.getNetwork().sendToPlayer(new ColonyViewRemoveBuildingMessage(colony, building.getID()), player);
}
Log.getLogger().info(String.format("Colony %d - removed AbstractBuilding %s of type %s", colony.getID(), building.getID(), building.getSchematicName()));
}
if (building instanceof BuildingTownHall) {
townHall = null;
} else if (building instanceof BuildingWareHouse) {
wareHouses.remove(building);
} else if (building instanceof BuildingMysticalSite) {
mysticalSites.remove(building);
}
// Allow Citizens to fix up any data that wasn't fixed up by the AbstractBuilding's own onDestroyed
for (@NotNull final ICitizenData citizen : colony.getCitizenManager().getCitizens()) {
citizen.onRemoveBuilding(building);
building.cancelAllRequestsOfCitizen(citizen);
}
colony.getRequestManager().onProviderRemovedFromColony(building);
colony.getRequestManager().onRequesterRemovedFromColony(building.getRequester());
colony.getCitizenManager().calculateMaxCitizens();
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class CitizenManager method resurrectCivilianData.
@Override
public ICitizenData resurrectCivilianData(@NotNull final CompoundNBT compoundNBT, final boolean resetId, @NotNull final World world, final BlockPos spawnPos) {
// That's needed to prevent bugs when calling IDs that are not used.
for (int i = 1; i <= this.getCurrentCitizenCount() + 1; i++) {
if (this.getCivilian(i) == null) {
topCitizenId = i;
break;
}
}
if (resetId) {
compoundNBT.putInt(TAG_ID, topCitizenId);
}
final ICitizenData citizenData = deserializeCitizen(compoundNBT);
citizenData.onResurrect();
citizens.put(citizenData.getId(), citizenData);
spawnOrCreateCitizen(citizenData, world, spawnPos);
return citizenData;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class CitizenManager method deserializeCitizen.
/**
* Creates a citizen data from NBT
*
* @param compound NBT
* @return citizen data
*/
private ICitizenData deserializeCitizen(@NotNull final CompoundNBT compound) {
final ICitizenData data = ICitizenDataManager.getInstance().createFromNBT(compound, colony);
topCitizenId = Math.max(topCitizenId, data.getId());
return data;
}
use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.
the class CitizenManager method createAndRegisterCivilianData.
@Override
public ICitizenData createAndRegisterCivilianData() {
// That's needed to prevent bugs when calling IDs that are not used.
for (int i = 1; i <= this.getCurrentCitizenCount() + 1; i++) {
if (this.getCivilian(i) == null) {
topCitizenId = i;
break;
}
}
final CitizenData citizenData = new CitizenData(topCitizenId, colony);
citizenData.initForNewCivilian();
citizens.put(citizenData.getId(), citizenData);
return citizenData;
}
Aggregations