Search in sources :

Example 1 with ColonyViewRemoveMessage

use of com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveMessage 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(), 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);
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ColonyViewRemoveMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveMessage) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) World(net.minecraft.world.World) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with ColonyViewRemoveMessage

use of com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveMessage 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);
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) ColonyViewRemoveMessage(com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveMessage) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) World(net.minecraft.world.World) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 ColonyViewRemoveMessage (com.minecolonies.coremod.network.messages.client.colony.ColonyViewRemoveMessage)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 BlockPos (net.minecraft.util.math.BlockPos)2 World (net.minecraft.world.World)2