use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class AbstractBuilding method removeWorkOrder.
/**
* Remove the work order for the building.
* <p>
* Remove either the upgrade or repair work order
*/
@Override
public void removeWorkOrder() {
for (@NotNull final WorkOrderBuilding o : colony.getWorkManager().getWorkOrdersOfType(WorkOrderBuilding.class)) {
if (o.getLocation().equals(getID())) {
colony.getWorkManager().removeWorkOrder(o.getID());
markDirty();
final BlockPos buildingPos = o.getClaimedBy();
final IBuilding building = colony.getBuildingManager().getBuilding(buildingPos);
if (building != null) {
for (final AbstractAssignedCitizenModule module : building.getModules(AbstractAssignedCitizenModule.class)) {
for (final ICitizenData citizen : module.getAssignedCitizen()) {
building.cancelAllRequestsOfCitizen(citizen);
}
}
}
return;
}
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class ColonyManager method deleteColony.
/**
* Delete a colony and purge all buildings and citizens.
*
* @param iColony the colony to destroy.
* @param canDestroy if the building outlines should be destroyed as well.
*/
private void deleteColony(@Nullable final IColony iColony, final boolean canDestroy) {
if (!(iColony instanceof Colony)) {
return;
}
final Colony colony = (Colony) iColony;
final int id = colony.getID();
final World world = colony.getWorld();
if (world == null) {
Log.getLogger().warn("Deleting Colony " + id + " errored: World is Null");
return;
}
try {
ChunkDataHelper.claimColonyChunks(world, false, id, colony.getCenter());
Log.getLogger().info("Removing citizens for " + id);
for (final ICitizenData citizenData : new ArrayList<>(colony.getCitizenManager().getCitizens())) {
Log.getLogger().info("Kill Citizen " + citizenData.getName());
citizenData.getEntity().ifPresent(entityCitizen -> entityCitizen.die(CONSOLE_DAMAGE_SOURCE));
}
Log.getLogger().info("Removing buildings for " + id);
for (final IBuilding building : new ArrayList<>(colony.getBuildingManager().getBuildings().values())) {
try {
final BlockPos location = building.getPosition();
Log.getLogger().info("Delete Building at " + location);
if (canDestroy) {
building.deconstruct();
}
building.destroy();
if (world.getBlockState(location).getBlock() instanceof AbstractBlockHut) {
Log.getLogger().info("Found Block, deleting " + world.getBlockState(location).getBlock());
world.removeBlock(location, false);
}
} catch (final Exception ex) {
Log.getLogger().warn("Something went wrong deleting a building while deleting the colony!", ex);
}
}
try {
MinecraftForge.EVENT_BUS.unregister(colony.getEventHandler());
} catch (final NullPointerException e) {
Log.getLogger().warn("Can't unregister the event handler twice");
}
Log.getLogger().info("Deleting colony: " + colony.getID());
final IColonyManagerCapability cap = world.getCapability(COLONY_MANAGER_CAP, null).resolve().orElse(null);
if (cap == null) {
Log.getLogger().warn(MISSING_WORLD_CAP_MESSAGE);
return;
}
cap.deleteColony(id);
BackUpHelper.markColonyDeleted(colony.getID(), colony.getDimension());
colony.getImportantMessageEntityPlayers().forEach(player -> Network.getNetwork().sendToPlayer(new ColonyViewRemoveMessage(colony.getID(), colony.getDimension()), (ServerPlayerEntity) player));
Log.getLogger().info("Successfully deleted colony: " + id);
} catch (final RuntimeException e) {
Log.getLogger().warn("Deleting Colony " + id + " errored:", e);
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class BuildingResourcesModule method updateAvailableResources.
/**
* Update the available resources.
* <p>
* which are needed for the build and in the structureBuilder's chest or inventory
*/
private void updateAvailableResources() {
final Set<ICitizenData> set = building.getAllAssignedCitizen();
final ICitizenData data = set.isEmpty() ? null : set.iterator().next();
if (data == null) {
return;
}
data.getEntity().ifPresent(structureBuilder -> {
final InventoryCitizen structureBuilderInventory = data.getInventory();
if (structureBuilderInventory == null) {
return;
}
for (@NotNull final Map.Entry<String, BuildingBuilderResource> entry : neededResources.entrySet()) {
final BuildingBuilderResource resource = entry.getValue();
resource.setAvailable(0);
if (structureBuilderInventory != null) {
resource.addAvailable(InventoryUtils.getItemCountInItemHandler(structureBuilderInventory, stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
}
if (building.getTileEntity() != null) {
resource.addAvailable(InventoryUtils.getItemCountInItemHandler(building.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
}
if (data.getJob() instanceof IJobWithExternalWorkStations) {
for (final IBuilding station : ((IJobWithExternalWorkStations) data.getJob()).getWorkStations()) {
resource.addAvailable(InventoryUtils.getItemCountInItemHandler(station.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
}
}
}
});
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by Minecolonies.
the class ItemScrollGuardHelp method useOn.
@Override
@NotNull
public ActionResultType useOn(ItemUseContext ctx) {
final ActionResultType result = super.useOn(ctx);
if (ctx.getLevel().isClientSide) {
return result;
}
final TileEntity te = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
if (te instanceof TileEntityColonyBuilding && ctx.getPlayer() != null) {
final IBuilding building = ((TileEntityColonyBuilding) te).getColony().getBuildingManager().getBuilding(ctx.getClickedPos());
if (!(building instanceof AbstractBuildingGuards)) {
MessageUtils.format(TOOL_GUARD_SCROLL_NO_GUARD_BUILDING).sendTo(ctx.getPlayer());
}
}
return result;
}
use of com.minecolonies.api.colony.buildings.IBuilding 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)) {
MessageUtils.format(TOOL_GUARD_SCROLL_NO_GUARD_BUILDING).sendTo(player);
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;
}
Aggregations