Search in sources :

Example 1 with IRSComponentBlock

use of com.minecolonies.api.blocks.interfaces.IRSComponentBlock in project minecolonies by Minecolonies.

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 2 with IRSComponentBlock

use of com.minecolonies.api.blocks.interfaces.IRSComponentBlock 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)

Aggregations

AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 IRSComponentBlock (com.minecolonies.api.blocks.interfaces.IRSComponentBlock)2 OpenSuggestionWindowMessage (com.minecolonies.coremod.network.messages.client.OpenSuggestionWindowMessage)2 ClientPlayerEntity (net.minecraft.client.entity.player.ClientPlayerEntity)2 ClientWorld (net.minecraft.client.world.ClientWorld)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 BlockItem (net.minecraft.item.BlockItem)2 ItemStack (net.minecraft.item.ItemStack)2 BlockPos (net.minecraft.util.math.BlockPos)2 World (net.minecraft.world.World)2 ServerWorld (net.minecraft.world.server.ServerWorld)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2