Search in sources :

Example 1 with ContainerBlock

use of net.minecraft.block.ContainerBlock in project minecolonies by Minecolonies.

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

use of net.minecraft.block.ContainerBlock in project Structurize by ldtteam.

the class ItemStackUtils method getItemStacksOfTileEntity.

/**
 * Get itemStack of tileEntityData. Retrieve the data from the tileEntity.
 *
 * @param compound the tileEntity stored in a compound.
 * @param state the block.
 * @return the list of itemstacks.
 */
public static List<ItemStack> getItemStacksOfTileEntity(final CompoundNBT compound, final BlockState state) {
    if (state.getBlock() instanceof ContainerBlock && compound.contains("Items")) {
        final NonNullList<ItemStack> items = NonNullList.create();
        ItemStackHelper.loadAllItems(compound, items);
        return items;
    }
    final TileEntity tileEntity = TileEntity.loadStatic(state, compound);
    if (tileEntity == null) {
        return Collections.emptyList();
    }
    final List<ItemStack> items = new ArrayList<>();
    for (final IItemHandler handler : getItemHandlersFromProvider(tileEntity)) {
        for (int slot = 0; slot < handler.getSlots(); slot++) {
            final ItemStack stack = handler.getStackInSlot(slot);
            if (!ItemStackUtils.isEmpty(stack)) {
                items.add(stack);
            }
        }
    }
    return items;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ContainerBlock(net.minecraft.block.ContainerBlock)

Example 3 with ContainerBlock

use of net.minecraft.block.ContainerBlock 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)

Aggregations

ContainerBlock (net.minecraft.block.ContainerBlock)3 ItemScanTool (com.ldtteam.structurize.items.ItemScanTool)2 AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 Permissions (com.minecolonies.coremod.colony.permissions.Permissions)2 AirBlock (net.minecraft.block.AirBlock)2 Block (net.minecraft.block.Block)2 ItemStack (net.minecraft.item.ItemStack)2 PotionItem (net.minecraft.item.PotionItem)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 IItemHandler (net.minecraftforge.items.IItemHandler)1