use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
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.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class CitizenSkillHandler method addXpToSkill.
@Override
public void addXpToSkill(final Skill skill, final double xp, final ICitizenData data) {
final Tuple<Integer, Double> tuple = skillMap.getOrDefault(skill, new Tuple<>(0, 0.0D));
int level = tuple.getA();
final double currentXp = tuple.getB();
final IBuilding home = data.getHomeBuilding();
final double citizenHutLevel = home == null ? 0 : home.getBuildingLevel();
final double citizenHutMaxLevel = home == null ? MAX_BUILDING_LEVEL : home.getMaxBuildingLevel();
if (((citizenHutLevel < citizenHutMaxLevel || citizenHutMaxLevel < MAX_BUILDING_LEVEL) && (citizenHutLevel + 1) * 10 <= level) || level >= MAX_CITIZEN_LEVEL) {
return;
}
double xpToLevelUp = Math.min(Double.MAX_VALUE, currentXp + xp);
while (xpToLevelUp > 0) {
final double nextLevel = ExperienceUtils.getXPNeededForNextLevel(level);
if (nextLevel > xpToLevelUp) {
skillMap.put(skill, new Tuple<>(Math.min(MAX_CITIZEN_LEVEL, level), xpToLevelUp));
xpToLevelUp = 0;
} else {
xpToLevelUp = xpToLevelUp - nextLevel;
level++;
}
}
if (level > tuple.getA()) {
levelUp(data);
data.markDirty();
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class CitizenSleepHandler method notifyCitizenHandlersOfWakeUp.
private void notifyCitizenHandlersOfWakeUp() {
if (citizen.getCitizenColonyHandler().getWorkBuilding() != null) {
citizen.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.working"));
citizen.getCitizenColonyHandler().getWorkBuilding().onWakeUp();
}
if (citizen.getCitizenJobHandler().getColonyJob() != null) {
citizen.getCitizenJobHandler().getColonyJob().onWakeUp();
}
final IBuilding homeBuilding = citizen.getCitizenColonyHandler().getHomeBuilding();
if (homeBuilding != null) {
homeBuilding.onWakeUp();
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class EntityMercenaryAI method patrol.
/**
* Patrols the buildings and random points. Attempts to steal from buildings.
*
* @return true
*/
private boolean patrol() {
if (currentPatrolPos == null || entity.getProxy().walkToBlock(currentPatrolPos, 3, true)) {
if (currentPatrolPos != null && movingToBuilding) {
// Attempt to steal!
final IBuilding building = entity.getColony().getBuildingManager().getBuilding(currentPatrolPos);
if (building != null) {
final List<IItemHandler> handlers = new ArrayList<>(InventoryUtils.getItemHandlersFromProvider(building.getTileEntity()));
final IItemHandler handler = handlers.get(rand.nextInt(handlers.size()));
final ItemStack stack = handler.extractItem(rand.nextInt(handler.getSlots()), 5, false);
if (!ItemStackUtils.isEmpty(stack)) {
entity.swing(Hand.OFF_HAND);
MessageUtils.format(MESSAGE_INFO_COLONY_MERCENARY_STEAL_BUILDING, stack.getHoverName().getString()).sendTo(entity.getColony()).forAllPlayers();
}
}
}
if (rand.nextInt(4) == 0 && !patrolPoints.isEmpty()) {
movingToBuilding = true;
currentPatrolPos = patrolPoints.get(rand.nextInt(patrolPoints.size()));
} else {
movingToBuilding = false;
currentPatrolPos = BlockPosUtil.getRandomPosition(entity.getCommandSenderWorld(), entity.blockPosition(), entity.blockPosition(), 10, 27);
}
}
if (entity.blockPosition().equals(lastWorkerPos)) {
stuckTimer++;
} else {
stuckTimer = 0;
}
if (stuckTimer > MAX_STUCK_TIME) {
stuckTimer = 0;
currentPatrolPos = null;
entity.getNavigation().stop();
}
lastWorkerPos = entity.blockPosition();
return true;
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class ItemScepterGuard method handleItemUsage.
/**
* Handles the usage of the item.
*
* @param worldIn the world it is used in.
* @param pos the position.
* @param compound the compound.
* @param playerIn the player using it.
* @return if it has been successful.
*/
@NotNull
private static ActionResultType handleItemUsage(final World worldIn, final BlockPos pos, final CompoundNBT compound, final PlayerEntity playerIn) {
if (!compound.getAllKeys().contains(TAG_ID)) {
return ActionResultType.FAIL;
}
final IColony colony = IColonyManager.getInstance().getColonyByWorld(compound.getInt(TAG_ID), worldIn);
if (colony == null) {
return ActionResultType.FAIL;
}
final BlockPos guardTower = BlockPosUtil.read(compound, TAG_POS);
final IBuilding hut = colony.getBuildingManager().getBuilding(guardTower);
if (!(hut instanceof AbstractBuildingGuards)) {
return ActionResultType.FAIL;
}
final IGuardBuilding tower = (IGuardBuilding) hut;
if (BlockPosUtil.getDistance2D(pos, guardTower) > tower.getPatrolDistance()) {
MessageUtils.format(TOOL_GUARD_SCEPTER_TOWER_TOO_FAR).sendTo(playerIn);
return ActionResultType.FAIL;
}
if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_GUARD_TARGET, pos).sendTo(playerIn);
tower.setGuardPos(pos);
playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
} else {
if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
tower.resetPatrolTargets();
}
tower.addPatrolTargets(pos);
MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_PATROL_TARGET, pos).sendTo(playerIn);
}
BlockPosUtil.write(compound, TAG_LAST_POS, pos);
return ActionResultType.SUCCESS;
}
Aggregations