Search in sources :

Example 1 with AbstractBlockHut

use of com.minecolonies.api.blocks.AbstractBlockHut 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 AbstractBlockHut

use of com.minecolonies.api.blocks.AbstractBlockHut in project minecolonies by Minecolonies.

the class BuildToolPasteMessage method handleHut.

/**
 * Handles the placement of huts.
 *
 * @param world    World the hut is being placed into.
 * @param player   Who placed the hut.
 * @param sn       The name of the structure.
 * @param rotation The number of times the structure should be rotated.
 * @param buildPos The location the hut is being placed.
 * @param mirror   Whether or not the strcture is mirrored.
 * @param state    The state of the hut.
 * @param complete If complete or not.
 */
private static void handleHut(@NotNull final World world, @NotNull final PlayerEntity player, final StructureName sn, final int rotation, @NotNull final BlockPos buildPos, final boolean mirror, final BlockState state, final boolean complete) {
    final IColony tempColony = IColonyManager.getInstance().getClosestColony(world, buildPos);
    if (!complete && tempColony != null && !tempColony.getPermissions().hasPermission(player, Action.MANAGE_HUTS) && IColonyManager.getInstance().isFarEnoughFromColonies(world, buildPos)) {
        return;
    }
    final String hut = sn.getSection();
    final ItemStack stack = BuildingUtils.getItemStackForHutFromInventory(player.inventory, hut);
    final Block block = stack.getItem() instanceof BlockItem ? ((BlockItem) stack.getItem()).getBlock() : null;
    if (block != null && EventHandler.onBlockHutPlaced(world, player, block, buildPos)) {
        world.destroyBlock(buildPos, true);
        world.setBlockAndUpdate(buildPos, state);
        if (!complete) {
            ((AbstractBlockHut<?>) block).onBlockPlacedByBuildTool(world, buildPos, world.getBlockState(buildPos), player, null, mirror, sn.getStyle());
            setupBuilding(world, player, sn, rotation, buildPos, mirror);
        }
    }
}
Also used : IColony(com.minecolonies.api.colony.IColony) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) BlockItem(net.minecraft.item.BlockItem) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut)

Example 3 with AbstractBlockHut

use of com.minecolonies.api.blocks.AbstractBlockHut in project minecolonies by Minecolonies.

the class BuildToolPlaceMessage method handleHut.

/**
 * Handles the placement of huts.
 *
 * @param world    World the hut is being placed into.
 * @param player   Who placed the hut.
 * @param sn       The name of the structure.
 * @param rotation The number of times the structure should be rotated.
 * @param buildPos The location the hut is being placed.
 * @param mirror   Whether or not the strcture is mirrored.
 * @param state    the state.
 */
private static void handleHut(@NotNull final World world, @NotNull final PlayerEntity player, final StructureName sn, final int rotation, @NotNull final BlockPos buildPos, final boolean mirror, final BlockState state) {
    final Block blockAtPos = world.getBlockState(buildPos).getBlock();
    if (blockAtPos instanceof IBuilderUndestroyable || ModTags.indestructible.contains(blockAtPos)) {
        LanguageHandler.sendPlayerMessage(player, INDESTRUCTIBLE_BLOCK_AT_POS);
        SoundUtils.playErrorSound(player, buildPos);
        return;
    }
    final Block block = state.getBlock();
    final ItemStack tempStack = new ItemStack(block, 1);
    final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(player.inventory), s -> ItemStackUtils.compareItemStacksIgnoreStackSize(tempStack, s, false, false));
    if (slot < 0) {
        return;
    }
    final ItemStack stack = player.inventory.getItem(slot);
    final IColony tempColony = IColonyManager.getInstance().getClosestColony(world, buildPos);
    if (tempColony != null && (!tempColony.getPermissions().hasPermission(player, Action.MANAGE_HUTS) && !(block instanceof BlockHutTownHall && IColonyManager.getInstance().isFarEnoughFromColonies(world, buildPos)))) {
        return;
    }
    final CompoundNBT compound = stack.getTag();
    if (tempColony != null && compound != null && compound.contains(TAG_COLONY_ID) && tempColony.getID() != compound.getInt(TAG_COLONY_ID)) {
        LanguageHandler.sendPlayerMessage(player, WRONG_COLONY, compound.getInt(TAG_COLONY_ID));
        return;
    }
    if (block != null && player.inventory.contains(new ItemStack(block))) {
        if (EventHandler.onBlockHutPlaced(world, player, block, buildPos)) {
            if (tempColony != null) {
                AdvancementUtils.TriggerAdvancementPlayersForColony(tempColony, playerMP -> AdvancementTriggers.PLACE_STRUCTURE.trigger(playerMP, sn));
            } else {
                AdvancementTriggers.PLACE_STRUCTURE.trigger((ServerPlayerEntity) player, sn);
            }
            world.destroyBlock(buildPos, true);
            world.setBlockAndUpdate(buildPos, state);
            ((AbstractBlockHut<?>) block).onBlockPlacedByBuildTool(world, buildPos, world.getBlockState(buildPos), player, null, mirror, sn.getStyle());
            boolean complete = false;
            int level = 0;
            if (compound != null) {
                if (compound.getAllKeys().contains(TAG_OTHER_LEVEL)) {
                    level = compound.getInt(TAG_OTHER_LEVEL);
                }
                if (compound.getAllKeys().contains(TAG_PASTEABLE)) {
                    String schematic = sn.toString();
                    schematic = schematic.substring(0, schematic.length() - 1);
                    schematic += level;
                    CreativeBuildingStructureHandler.loadAndPlaceStructureWithRotation(player.level, schematic, buildPos, BlockPosUtil.getRotationFromRotations(rotation), mirror ? Mirror.FRONT_BACK : Mirror.NONE, true, (ServerPlayerEntity) player);
                    complete = true;
                }
            }
            InventoryUtils.reduceStackInItemHandler(new InvWrapper(player.inventory), stack, 1);
            setupBuilding(world, player, sn, rotation, buildPos, mirror, level, complete);
        }
    } else {
        LanguageHandler.sendPlayerMessage(player, NO_HUT_IN_INVENTORY);
    }
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Block(net.minecraft.block.Block) IColony(com.minecolonies.api.colony.IColony) BlockHutTownHall(com.minecolonies.coremod.blocks.huts.BlockHutTownHall) ItemStack(net.minecraft.item.ItemStack) IBuilderUndestroyable(com.minecolonies.api.entity.ai.citizen.builder.IBuilderUndestroyable) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut)

Example 4 with AbstractBlockHut

use of com.minecolonies.api.blocks.AbstractBlockHut in project minecolonies by Minecolonies.

the class AbstractSchematicProvider method getRotation.

@Override
public int getRotation() {
    if (cachedRotation != -1) {
        return cachedRotation;
    }
    final StructureName structureName = new StructureName(Structures.SCHEMATICS_PREFIX, style, this.getSchematicName() + Math.max(1, buildingLevel));
    try {
        final LoadOnlyStructureHandler structure = new LoadOnlyStructureHandler(colony.getWorld(), getPosition(), structureName.toString(), new PlacementSettings(), true);
        final Blueprint blueprint = structure.getBluePrint();
        if (blueprint != null) {
            final BlockState structureState = structure.getBluePrint().getBlockInfoAsMap().get(structure.getBluePrint().getPrimaryBlockOffset()).getState();
            if (structureState != null) {
                if (!(structureState.getBlock() instanceof AbstractBlockHut) || !(colony.getWorld().getBlockState(this.location).getBlock() instanceof AbstractBlockHut)) {
                    Log.getLogger().error(String.format("Schematic %s doesn't have a correct Primary Offset", structureName.toString()));
                    return 0;
                }
                final int structureRotation = structureState.getValue(AbstractBlockHut.FACING).get2DDataValue();
                final int worldRotation = colony.getWorld().getBlockState(this.location).getValue(AbstractBlockHut.FACING).get2DDataValue();
                if (structureRotation <= worldRotation) {
                    cachedRotation = worldRotation - structureRotation;
                } else {
                    cachedRotation = 4 + worldRotation - structureRotation;
                }
                return cachedRotation;
            }
        }
    } catch (Exception e) {
        Log.getLogger().error(String.format("Failed to get rotation for %s: ", structureName.toString()), e);
        return 0;
    }
    return 0;
}
Also used : BlockState(net.minecraft.block.BlockState) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) StructureName(com.ldtteam.structurize.management.StructureName) LoadOnlyStructureHandler(com.minecolonies.api.util.LoadOnlyStructureHandler) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint)

Example 5 with AbstractBlockHut

use of com.minecolonies.api.blocks.AbstractBlockHut in project minecolonies by Minecolonies.

the class ClientEventHandler method onItemTooltipEvent.

/**
 * Fires when an item tooltip is requested, generally from inventory, JEI, or when minecraft is first populating the recipe book.
 * @param event An ItemTooltipEvent
 */
@SubscribeEvent
public static void onItemTooltipEvent(final ItemTooltipEvent event) {
    // Vanilla recipe books populate tooltips once before the player exists on remote clients, some other cases.
    if (event.getPlayer() == null) {
        return;
    }
    IColony colony = IMinecoloniesAPI.getInstance().getColonyManager().getIColony(event.getPlayer().level, event.getPlayer().blockPosition());
    if (colony == null) {
        colony = IMinecoloniesAPI.getInstance().getColonyManager().getIColonyByOwner(event.getPlayer().level, event.getPlayer());
    }
    handleCrafterRecipeTooltips(colony, event.getToolTip(), event.getItemStack().getItem());
    if (event.getItemStack().getItem() instanceof BlockItem) {
        final BlockItem blockItem = (BlockItem) event.getItemStack().getItem();
        if (blockItem.getBlock() instanceof AbstractBlockHut) {
            handleHutBlockResearchUnlocks(colony, event.getToolTip(), blockItem.getBlock());
        }
    }
}
Also used : IColony(com.minecolonies.api.colony.IColony) BlockItem(net.minecraft.item.BlockItem) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)22 ItemStack (net.minecraft.item.ItemStack)8 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)8 IColony (com.minecolonies.api.colony.IColony)6 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)6 Block (net.minecraft.block.Block)6 BlockItem (net.minecraft.item.BlockItem)6 AbstractTileEntityColonyBuilding (com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding)4 TileEntityColonyBuilding (com.minecolonies.api.tileentities.TileEntityColonyBuilding)4 BlockMinecoloniesRack (com.minecolonies.coremod.blocks.BlockMinecoloniesRack)4 AirBlock (net.minecraft.block.AirBlock)4 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)4 TileEntity (net.minecraft.tileentity.TileEntity)4 BlockPos (net.minecraft.util.math.BlockPos)4 World (net.minecraft.world.World)4 Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)2 ItemScanTool (com.ldtteam.structurize.items.ItemScanTool)2 StructureName (com.ldtteam.structurize.management.StructureName)2 PlacementSettings (com.ldtteam.structurize.util.PlacementSettings)2 IRSComponentBlock (com.minecolonies.api.blocks.interfaces.IRSComponentBlock)2