Search in sources :

Example 1 with BlockHutTownHall

use of com.minecolonies.coremod.blocks.huts.BlockHutTownHall 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 2 with BlockHutTownHall

use of com.minecolonies.coremod.blocks.huts.BlockHutTownHall in project minecolonies by ldtteam.

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)

Aggregations

AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 IColony (com.minecolonies.api.colony.IColony)2 IBuilderUndestroyable (com.minecolonies.api.entity.ai.citizen.builder.IBuilderUndestroyable)2 BlockHutTownHall (com.minecolonies.coremod.blocks.huts.BlockHutTownHall)2 Block (net.minecraft.block.Block)2 ItemStack (net.minecraft.item.ItemStack)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)2