Search in sources :

Example 16 with AbstractBlockHut

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

the class AbstractBuildingContainer method registerBlockPosition.

@Override
@SuppressWarnings("squid:S1172")
public void registerBlockPosition(@NotNull final Block block, @NotNull final BlockPos pos, @NotNull final World world) {
    if (block instanceof AbstractBlockHut) {
        final TileEntity entity = world.getBlockEntity(pos);
        if (entity instanceof TileEntityColonyBuilding) {
            ((TileEntityColonyBuilding) entity).setStyle(this.getStyle());
            ((TileEntityColonyBuilding) entity).setMirror(isMirrored());
            final IBuilding building = colony.getBuildingManager().getBuilding(pos);
            if (building != null) {
                building.setStyle(this.getStyle());
                building.setParent(getID());
                addChild(pos);
            }
        }
    } else if (block instanceof BlockMinecoloniesRack) {
        addContainerPosition(pos);
        if (block instanceof BlockMinecoloniesRack) {
            final TileEntity entity = world.getBlockEntity(pos);
            if (entity instanceof TileEntityRack) {
                ((TileEntityRack) entity).setBuildingPos(this.getID());
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) BlockMinecoloniesRack(com.minecolonies.coremod.blocks.BlockMinecoloniesRack) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut)

Example 17 with AbstractBlockHut

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

the class ColonyPermissionEventHandler method on.

/**
 * PlayerInteractEvent handler.
 * <p>
 * Check, if a player right clicked a block. Deny if: - If the block is in colony - block is AbstractBlockHut - player has not permission
 *
 * @param event PlayerInteractEvent
 */
@SubscribeEvent
public void on(final PlayerInteractEvent event) {
    if (colony.isCoordInColony(event.getWorld(), event.getPos()) && !(event instanceof PlayerInteractEvent.EntityInteract || event instanceof PlayerInteractEvent.EntityInteractSpecific)) {
        final Block block = event.getWorld().getBlockState(event.getPos()).getBlock();
        // Huts
        if (event instanceof PlayerInteractEvent.RightClickBlock && block instanceof AbstractBlockHut && !colony.getPermissions().hasPermission(event.getPlayer(), Action.ACCESS_HUTS)) {
            cancelEvent(event, event.getPlayer(), colony, Action.ACCESS_HUTS, event.getPos());
            return;
        }
        final Permissions perms = colony.getPermissions();
        if (isFreeToInteractWith(block, event.getPos()) && perms.hasPermission(event.getPlayer(), Action.ACCESS_FREE_BLOCKS)) {
            return;
        }
        if (MineColonies.getConfig().getServer().enableColonyProtection.get()) {
            if (!perms.hasPermission(event.getPlayer(), Action.RIGHTCLICK_BLOCK) && !(block instanceof AirBlock)) {
                checkEventCancelation(Action.RIGHTCLICK_BLOCK, event.getPlayer(), event.getWorld(), event, event.getPos());
                return;
            }
            if (block instanceof ContainerBlock && !perms.hasPermission(event.getPlayer(), Action.OPEN_CONTAINER)) {
                cancelEvent(event, event.getPlayer(), colony, Action.OPEN_CONTAINER, event.getPos());
                return;
            }
            if (event.getWorld().getBlockEntity(event.getPos()) != null && !perms.hasPermission(event.getPlayer(), Action.RIGHTCLICK_ENTITY)) {
                checkEventCancelation(Action.RIGHTCLICK_ENTITY, event.getPlayer(), event.getWorld(), event, event.getPos());
                return;
            }
            final ItemStack stack = event.getItemStack();
            if (ItemStackUtils.isEmpty(stack) || stack.getItem().isEdible()) {
                return;
            }
            if (stack.getItem() instanceof PotionItem) {
                checkEventCancelation(Action.THROW_POTION, event.getPlayer(), event.getWorld(), event, event.getPos());
                return;
            }
            if (stack.getItem() instanceof ItemScanTool && !perms.hasPermission(event.getPlayer(), Action.USE_SCAN_TOOL)) {
                cancelEvent(event, event.getPlayer(), colony, Action.USE_SCAN_TOOL, event.getPos());
            }
        }
    }
}
Also used : AirBlock(net.minecraft.block.AirBlock) ItemScanTool(com.ldtteam.structurize.items.ItemScanTool) ContainerBlock(net.minecraft.block.ContainerBlock) PotionItem(net.minecraft.item.PotionItem) Permissions(com.minecolonies.coremod.colony.permissions.Permissions) AirBlock(net.minecraft.block.AirBlock) Block(net.minecraft.block.Block) ContainerBlock(net.minecraft.block.ContainerBlock) ItemStack(net.minecraft.item.ItemStack) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 18 with AbstractBlockHut

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

the class ColonyPermissionEventHandler method on.

/**
 * BlockEvent.BreakEvent handler.
 *
 * @param event BlockEvent.BreakEvent
 */
@SubscribeEvent
public void on(final BlockEvent.BreakEvent event) {
    final IWorld world = event.getWorld();
    if (!MineColonies.getConfig().getServer().enableColonyProtection.get() || world.isClientSide()) {
        return;
    }
    if (event.getState().getBlock() instanceof AbstractBlockHut) {
        @Nullable final IBuilding building = IColonyManager.getInstance().getBuilding(event.getPlayer().level, event.getPos());
        if (building == null) {
            return;
        }
        if (event.getState().getBlock() == ModBlocks.blockHutTownHall && !((BlockHutTownHall) event.getState().getBlock()).getValidBreak() && !event.getPlayer().isCreative()) {
            cancelEvent(event, event.getPlayer(), colony, Action.BREAK_HUTS, event.getPos());
            return;
        }
        if (!building.getColony().getPermissions().hasPermission(event.getPlayer(), Action.BREAK_HUTS)) {
            if (checkEventCancelation(Action.BREAK_HUTS, event.getPlayer(), event.getPlayer().getCommandSenderWorld(), event, event.getPos())) {
                return;
            }
        }
        building.destroy();
        if (MineColonies.getConfig().getServer().pvp_mode.get() && event.getState().getBlock() == ModBlocks.blockHutTownHall) {
            IColonyManager.getInstance().deleteColonyByWorld(building.getColony().getID(), false, event.getPlayer().level);
        }
    } else {
        checkEventCancelation(Action.BREAK_BLOCKS, event.getPlayer(), event.getPlayer().getCommandSenderWorld(), event, event.getPos());
    }
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IWorld(net.minecraft.world.IWorld) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) Nullable(org.jetbrains.annotations.Nullable) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 19 with AbstractBlockHut

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

the class EventHandler method onPlayerInteract.

/**
 * Event when a player right clicks a block, or right clicks with an item. Event gets cancelled when player has no permission. Event gets cancelled when the player has no
 * permission to place a hut, and tried it.
 *
 * @param event {@link PlayerInteractEvent.RightClickBlock}
 */
@SubscribeEvent
public static void onPlayerInteract(@NotNull final PlayerInteractEvent.RightClickBlock event) {
    final PlayerEntity player = event.getPlayer();
    final World world = event.getWorld();
    BlockPos bedBlockPos = event.getPos();
    // and uses that return value, but I didn't want to call it twice
    if (playerRightClickInteract(player, world, event.getPos()) && world.getBlockState(event.getPos()).getBlock() instanceof AbstractBlockHut) {
        final IColony colony = IColonyManager.getInstance().getIColony(world, event.getPos());
        if (colony != null && !colony.getPermissions().hasPermission(player, Action.ACCESS_HUTS)) {
            event.setCanceled(true);
        }
        return;
    } else if ("pmardle".equalsIgnoreCase(event.getPlayer().getName().getString()) && Block.byItem(event.getItemStack().getItem()) instanceof SilverfishBlock) {
        LanguageHandler.sendPlayerMessage(event.getPlayer(), "Stop that you twat!!!");
        event.setCanceled(true);
    }
    if (world.getBlockState(event.getPos()).getBlock().isBed(world.getBlockState(event.getPos()), world, event.getPos(), player)) {
        final IColony colony = IColonyManager.getInstance().getColonyByPosFromWorld(world, bedBlockPos);
        // Checks to see if player tries to sleep in a bed belonging to a Citizen, ancels the event, and Notifies Player that bed is occuppied
        if (colony != null && world.getBlockState(event.getPos()).hasProperty(BedBlock.PART)) {
            final List<ICitizenData> citizenList = colony.getCitizenManager().getCitizens();
            final BlockState potentialBed = world.getBlockState(event.getPos());
            if (potentialBed.getBlock() instanceof BedBlock && potentialBed.getValue(BedBlock.PART) == BedPart.FOOT) {
                bedBlockPos = bedBlockPos.relative(world.getBlockState(event.getPos()).getValue(BedBlock.FACING));
            }
            for (final ICitizenData citizen : citizenList) {
                if (citizen.getBedPos().equals(bedBlockPos) && citizen.isAsleep()) {
                    event.setCanceled(true);
                    LanguageHandler.sendPlayerMessage(player, "block.minecraft.bed.occupied");
                }
            }
        }
    }
    handleEventCancellation(event, player);
    if (!event.isCanceled() && event.getEntity() instanceof PlayerEntity && event.getItemStack().getItem() instanceof BlockItem) {
        final Block block = ((BlockItem) event.getItemStack().getItem()).getBlock().getBlock();
        if (block instanceof AbstractBlockHut && !(block instanceof IRSComponentBlock)) {
            final IColony colony = IColonyManager.getInstance().getIColony(world, event.getPos());
            if (colony != null && !colony.getPermissions().hasPermission(player, Action.ACCESS_HUTS)) {
                event.setCanceled(true);
                return;
            }
            if (MineColonies.getConfig().getServer().suggestBuildToolPlacement.get() && !(player.isCreative() && player.isShiftKeyDown())) {
                final ItemStack stack = event.getItemStack();
                if (!stack.isEmpty() && !world.isClientSide) {
                    Network.getNetwork().sendToPlayer(new OpenSuggestionWindowMessage(block.defaultBlockState().setValue(AbstractBlockHut.FACING, event.getPlayer().getDirection()), event.getPos().relative(event.getFace()), stack), (ServerPlayerEntity) player);
                }
                event.setCanceled(true);
            }
            return;
        }
    }
    if (!event.isCanceled() && event.getItemStack().getItem() == ModItems.buildTool.get()) {
        if (event.getWorld().isClientSide()) {
            if (event.getUseBlock() == Event.Result.DEFAULT && event.getFace() != null) {
                final IColonyView view = IColonyManager.getInstance().getClosestColonyView(event.getWorld(), event.getPos().relative(event.getFace()));
                if (view != null && Settings.instance.getStyle().isEmpty()) {
                    Settings.instance.setStyle(view.getStyle());
                }
                MineColonies.proxy.openBuildToolWindow(event.getPos().relative(event.getFace()));
            }
        }
        event.setCanceled(true);
    }
    if (!event.isCanceled() && event.getItemStack().getItem() == ModItems.shapeTool.get()) {
        if (event.getWorld().isClientSide()) {
            if (event.getUseBlock() == Event.Result.DEFAULT && event.getFace() != null) {
                MineColonies.proxy.openShapeToolWindow(event.getPos().relative(event.getFace()));
            }
        }
        event.setCanceled(true);
    }
}
Also used : World(net.minecraft.world.World) ClientWorld(net.minecraft.client.world.ClientWorld) ServerWorld(net.minecraft.world.server.ServerWorld) BlockItem(net.minecraft.item.BlockItem) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) OpenSuggestionWindowMessage(com.minecolonies.coremod.network.messages.client.OpenSuggestionWindowMessage) IRSComponentBlock(com.minecolonies.api.blocks.interfaces.IRSComponentBlock) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) IRSComponentBlock(com.minecolonies.api.blocks.interfaces.IRSComponentBlock) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 20 with AbstractBlockHut

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

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) IAltersBuildingFootprint(com.minecolonies.api.colony.buildings.modules.IAltersBuildingFootprint) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint)

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