Search in sources :

Example 1 with ItemRedstone

use of net.minecraft.item.ItemRedstone in project BluePower by Qmunity.

the class TileDeployer method rightClick.

/**
     * Be sure to set up the fake player's hotbar with the right clicked items. starting with hotbar slot 0.
     * @param player
     * @param useItems this method will set the current selected slot of the fake player to 0, and move on to the next slot useItems - 1 times.
     *          So to use the first slot only, pass 1, to use the full hotbar, 9.
     * @return
     */
protected boolean rightClick(FakePlayer player, int useItems) {
    if (useItems > 9)
        throw new IllegalArgumentException("Hotbar is 9 items in width! You're trying " + useItems + "!");
    ForgeDirection faceDir = getFacingDirection();
    int dx = faceDir.offsetX;
    int dy = faceDir.offsetY;
    int dz = faceDir.offsetZ;
    int x = xCoord + dx;
    int y = yCoord + dy;
    int z = zCoord + dz;
    player.setPosition(x + 0.5, y + 0.5 - player.eyeHeight, z + 0.5);
    player.rotationPitch = faceDir.offsetY * -90;
    switch(faceDir) {
        case NORTH:
            player.rotationYaw = 180;
            break;
        case SOUTH:
            player.rotationYaw = 0;
            break;
        case WEST:
            player.rotationYaw = 90;
            break;
        case EAST:
            player.rotationYaw = -90;
    }
    try {
        PlayerInteractEvent event = ForgeEventFactory.onPlayerInteract(player, Action.RIGHT_CLICK_AIR, x, y, z, faceDir.ordinal(), worldObj);
        if (event.isCanceled())
            return false;
        Block block = worldObj.getBlock(x, y, z);
        List<EntityLivingBase> detectedEntities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(x, y, z, x + 1, y + 1, z + 1));
        Entity entity = detectedEntities.isEmpty() ? null : detectedEntities.get(worldObj.rand.nextInt(detectedEntities.size()));
        if (entity != null) {
            for (int i = 0; i < useItems; i++) {
                player.inventory.currentItem = i;
                ItemStack stack = player.getCurrentEquippedItem();
                if (canDeployItem(stack) && stack.getItem().itemInteractionForEntity(stack, player, (EntityLivingBase) entity))
                    return true;
                if (entity instanceof EntityAnimal && ((EntityAnimal) entity).interact(player))
                    return true;
            }
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.currentItem = i;
            ItemStack stack = player.getCurrentEquippedItem();
            if (canDeployItem(stack) && stack.getItem().onItemUseFirst(stack, player, worldObj, x, y, z, faceDir.ordinal(), dx, dy, dz))
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.currentItem = i;
            if (!worldObj.isAirBlock(x, y, x) && block.onBlockActivated(worldObj, x, y, z, player, faceDir.ordinal(), dx, dy, dz))
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.currentItem = i;
            ItemStack stack = player.getCurrentEquippedItem();
            boolean isGoingToShift = false;
            if (stack != null) {
                if (stack.getItem() instanceof ItemReed || stack.getItem() instanceof ItemRedstone) {
                    isGoingToShift = true;
                }
            }
            int useX = isGoingToShift ? xCoord : x;
            int useY = isGoingToShift ? yCoord : y;
            int useZ = isGoingToShift ? zCoord : z;
            if (canDeployItem(stack) && stack.getItem().onItemUse(stack, player, worldObj, useX, useY, useZ, faceDir.ordinal(), dx, dy, dz))
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.currentItem = i;
            ItemStack stack = player.getCurrentEquippedItem();
            if (canDeployItem(stack)) {
                ItemStack copy = stack.copy();
                player.setCurrentItemOrArmor(0, stack.getItem().onItemRightClick(stack, worldObj, player));
                if (!copy.isItemEqual(stack))
                    return true;
            }
        }
        return false;
    } catch (Throwable e) {
        BluePower.log.error("Deployer crashed! Stacktrace: ");
        e.printStackTrace();
        return true;
    }
}
Also used : Entity(net.minecraft.entity.Entity) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) ItemRedstone(net.minecraft.item.ItemRedstone) ItemReed(net.minecraft.item.ItemReed) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) EntityLivingBase(net.minecraft.entity.EntityLivingBase) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) EntityAnimal(net.minecraft.entity.passive.EntityAnimal)

Example 2 with ItemRedstone

use of net.minecraft.item.ItemRedstone in project PneumaticCraft by MineMaarten.

the class DroneAIBlockInteract method rightClick.

private boolean rightClick(ChunkPosition pos) {
    int xCoord = pos.chunkPosX;
    int yCoord = pos.chunkPosY;
    int zCoord = pos.chunkPosZ;
    ForgeDirection faceDir = ProgWidgetPlace.getDirForSides(((ISidedWidget) widget).getSides());
    EntityPlayer player = drone.getFakePlayer();
    World worldObj = drone.getWorld();
    int dx = faceDir.offsetX;
    int dy = faceDir.offsetY;
    int dz = faceDir.offsetZ;
    int x = xCoord;
    int y = yCoord;
    int z = zCoord;
    player.setPosition(x + 0.5, y + 0.5 - player.eyeHeight, z + 0.5);
    player.rotationPitch = faceDir.offsetY * -90;
    switch(faceDir) {
        case NORTH:
            player.rotationYaw = 180;
            break;
        case SOUTH:
            player.rotationYaw = 0;
            break;
        case WEST:
            player.rotationYaw = 90;
            break;
        case EAST:
            player.rotationYaw = -90;
    }
    try {
        PlayerInteractEvent event = ForgeEventFactory.onPlayerInteract(player, Action.RIGHT_CLICK_AIR, x, y, z, faceDir.ordinal(), worldObj);
        if (event.isCanceled())
            return false;
        Block block = worldObj.getBlock(x, y, z);
        ItemStack stack = player.getCurrentEquippedItem();
        if (stack != null && stack.getItem().onItemUseFirst(stack, player, worldObj, x, y, z, faceDir.ordinal(), dx, dy, dz))
            return false;
        if (!worldObj.isAirBlock(x, y, z) && block.onBlockActivated(worldObj, x, y, z, player, faceDir.ordinal(), dx, dy, dz))
            return false;
        if (stack != null) {
            boolean isGoingToShift = false;
            if (stack.getItem() instanceof ItemReed || stack.getItem() instanceof ItemRedstone) {
                isGoingToShift = true;
            }
            int useX = isGoingToShift ? xCoord : x;
            int useY = isGoingToShift ? yCoord : y;
            int useZ = isGoingToShift ? zCoord : z;
            if (stack.getItem().onItemUse(stack, player, worldObj, useX, useY, useZ, faceDir.ordinal(), dx, dy, dz))
                return false;
            ItemStack copy = stack.copy();
            player.setCurrentItemOrArmor(0, stack.getItem().onItemRightClick(stack, worldObj, player));
            if (!copy.isItemEqual(stack))
                return true;
        }
        return false;
    } catch (Throwable e) {
        Log.error("DroneAIBlockInteract crashed! Stacktrace: ");
        e.printStackTrace();
        return false;
    }
}
Also used : PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Block(net.minecraft.block.Block) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) ItemRedstone(net.minecraft.item.ItemRedstone) ItemReed(net.minecraft.item.ItemReed)

Aggregations

Block (net.minecraft.block.Block)2 ItemRedstone (net.minecraft.item.ItemRedstone)2 ItemReed (net.minecraft.item.ItemReed)2 ItemStack (net.minecraft.item.ItemStack)2 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)2 PlayerInteractEvent (net.minecraftforge.event.entity.player.PlayerInteractEvent)2 Entity (net.minecraft.entity.Entity)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityAnimal (net.minecraft.entity.passive.EntityAnimal)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 World (net.minecraft.world.World)1