use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
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(), colony.getDimension());
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 ldtteam.
the class ChunkDataHelper method claimChunksInRange.
/**
* Claim a number of chunks in a certain range around a position. Prevents the initial chunkradius from beeing unclaimed, unless forced.
*
* @param colony the colony to claim for
* @param add if claim or unclaim.
* @param range the range.
* @param center the center position to be claimed.
* @param force whether to ignore restrictions.
*/
public static void claimChunksInRange(final IColony colony, final boolean add, final int range, final BlockPos center, final boolean force) {
final World world = colony.getWorld();
final int colonyId = colony.getID();
final RegistryKey<World> dimension = colony.getDimension();
boolean areAllChunksAdded = true;
final IChunkmanagerCapability chunkManager = world.getCapability(CHUNK_STORAGE_UPDATE_CAP, null).resolve().orElse(null);
if (chunkManager == null) {
Log.getLogger().error(UNABLE_TO_FIND_WORLD_CAP_TEXT, new Exception());
return;
}
final int chunkColonyCenterX = colony.getCenter().getX() >> 4;
final int chunkColonyCenterZ = colony.getCenter().getZ() >> 4;
final BlockPos colonyCenterCompare = new BlockPos(colony.getCenter().getX(), 0, colony.getCenter().getZ());
final int chunkX = center.getX() >> 4;
final int chunkZ = center.getZ() >> 4;
final int initialColonySize = getConfig().getServer().initialColonySize.get();
final int maxColonySize = getConfig().getServer().maxColonySize.get();
for (int i = chunkX - range; i <= chunkX + range; i++) {
for (int j = chunkZ - range; j <= chunkZ + range; j++) {
// TODO: move out from for loops
if (!force && !add && (Math.abs(chunkColonyCenterX - i) <= initialColonySize + 1 && Math.abs(chunkColonyCenterZ - j) <= initialColonySize + 1)) {
Log.getLogger().debug("Unclaim of initial chunk prevented");
continue;
}
final BlockPos pos = new BlockPos(i * BLOCKS_PER_CHUNK, 0, j * BLOCKS_PER_CHUNK);
if (!force && maxColonySize != 0 && pos.distSqr(colonyCenterCompare) > Math.pow(maxColonySize * BLOCKS_PER_CHUNK, 2)) {
Log.getLogger().debug("Tried to claim chunk at pos X:" + pos.getX() + " Z:" + pos.getZ() + " too far away from the colony:" + colony.getID() + " center:" + colony.getCenter() + " max is config workingRangeTownHall ^2");
continue;
}
if (loadChunkAndAddData(world, pos, add, colonyId, center, chunkManager)) {
continue;
}
areAllChunksAdded = false;
@NotNull final ChunkLoadStorage newStorage = new ChunkLoadStorage(colonyId, ChunkPos.asLong(i, j), dimension.location(), center);
chunkManager.addChunkStorage(i, j, newStorage);
}
}
if (areAllChunksAdded && add && range > 0) {
final IBuilding building = colony.getBuildingManager().getBuilding(center);
sendPlayersMessage(colony.getImportantMessageEntityPlayers(), COLONY_SIZE_CHANGE, range, building.getSchematicName());
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class RecipeStorage method canFullFillRecipe.
@Override
public boolean canFullFillRecipe(final int qty, final Map<ItemStorage, Integer> existingRequirements, @NotNull final List<IItemHandler> citizen, @NotNull final IBuilding building) {
final List<ItemStorage> items = getCleanedInput();
for (final ItemStorage storage : items) {
final ItemStack stack = storage.getItemStack();
final int availableCount = InventoryUtils.getItemCountInItemHandlers(citizen, itemStack -> !ItemStackUtils.isEmpty(itemStack) && ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack, false, !storage.ignoreNBT())) + InventoryUtils.getCountFromBuilding(building, storage);
;
if (!canFulfillItemStorage(qty, existingRequirements, availableCount, storage)) {
return false;
}
}
return true;
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class AbstractJobGuard method initEntityValues.
@Override
public void initEntityValues(AbstractEntityCitizen citizen) {
super.initEntityValues(citizen);
final IBuilding workBuilding = citizen.getCitizenData().getWorkBuilding();
if (workBuilding instanceof AbstractBuildingGuards) {
AttributeModifierUtils.addHealthModifier(citizen, new AttributeModifier(GUARD_HEALTH_MOD_BUILDING_NAME, ((AbstractBuildingGuards) workBuilding).getBonusHealth(), AttributeModifier.Operation.ADDITION));
AttributeModifierUtils.addHealthModifier(citizen, new AttributeModifier(GUARD_HEALTH_MOD_CONFIG_NAME, MineColonies.getConfig().getServer().guardHealthMult.get() - 1.0, AttributeModifier.Operation.MULTIPLY_TOTAL));
}
}
use of com.minecolonies.api.colony.buildings.IBuilding in project minecolonies by ldtteam.
the class VisitorCitizen method hurt.
@Override
public boolean hurt(@NotNull final DamageSource damageSource, final float damage) {
if (!(damageSource.getEntity() instanceof EntityCitizen) && super.hurt(damageSource, damage)) {
if (damageSource.getEntity() instanceof LivingEntity && damage > 1.01f) {
final IBuilding home = getCitizenData().getHomeBuilding();
if (home.hasModule(TavernBuildingModule.class)) {
final TavernBuildingModule module = home.getFirstModuleOccurance(TavernBuildingModule.class);
for (final Integer id : module.getExternalCitizens()) {
ICitizenData data = citizenColonyHandler.getColony().getVisitorManager().getCivilian(id);
if (data != null && data.getEntity().isPresent() && data.getEntity().get().getLastHurtByMob() == null) {
data.getEntity().get().setLastHurtByMob((LivingEntity) damageSource.getEntity());
}
}
}
final Entity sourceEntity = damageSource.getEntity();
if (sourceEntity instanceof PlayerEntity) {
if (sourceEntity instanceof ServerPlayerEntity) {
return damage <= 1 || getCitizenColonyHandler().getColony().getPermissions().hasPermission((PlayerEntity) sourceEntity, Action.HURT_VISITOR);
} else {
final IColonyView colonyView = IColonyManager.getInstance().getColonyView(getCitizenColonyHandler().getColonyId(), level.dimension());
return damage <= 1 || colonyView == null || colonyView.getPermissions().hasPermission((PlayerEntity) sourceEntity, Action.HURT_VISITOR);
}
}
}
return true;
}
return false;
}
Aggregations