Search in sources :

Example 36 with Direction

use of net.minecraft.util.Direction in project BluePower by Qmunity.

the class TileBlockBreaker method redstoneChanged.

@Override
protected void redstoneChanged(boolean newValue) {
    super.redstoneChanged(newValue);
    if (!level.isClientSide && newValue) {
        Direction direction = getFacingDirection();
        BlockState breakState = level.getBlockState(worldPosition.relative(direction));
        if (!canBreakBlock(breakState.getBlock(), level, breakState, worldPosition.relative(direction)))
            return;
        List<ItemStack> breakStacks = breakState.getBlock().getDrops(breakState, (ServerWorld) level, worldPosition.relative(direction), this);
        // destroyBlock
        level.destroyBlock(worldPosition.relative(direction), false);
        addItemsToOutputBuffer(breakStacks);
    }
}
Also used : BlockState(net.minecraft.block.BlockState) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction)

Example 37 with Direction

use of net.minecraft.util.Direction 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 + "!");
    Direction faceDir = getFacingDirection();
    int dx = faceDir.getStepX();
    int dy = faceDir.getStepY();
    int dz = faceDir.getStepZ();
    int x = worldPosition.getX() + dx;
    int y = worldPosition.getY() + dy;
    int z = worldPosition.getZ() + dz;
    player.setPos(x + 0.5, y + 0.5 - player.getEyeHeight(), z + 0.5);
    player.xRot = faceDir.getStepY() * -90;
    switch(faceDir) {
        case NORTH:
            player.yRot = 180;
            break;
        case SOUTH:
            player.yRot = 0;
            break;
        case WEST:
            player.yRot = 90;
            break;
        case EAST:
            player.yRot = -90;
    }
    try {
        PlayerInteractEvent event = new PlayerInteractEvent.RightClickEmpty(player, Hand.MAIN_HAND);
        if (event.isCanceled())
            return false;
        Block block = level.getBlockState(new BlockPos(x, y, z)).getBlock();
        List<LivingEntity> detectedEntities = level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1));
        Entity entity = detectedEntities.isEmpty() ? null : detectedEntities.get(level.random.nextInt(detectedEntities.size()));
        if (entity != null) {
            for (int i = 0; i < useItems; i++) {
                player.inventory.selected = i;
                ItemStack stack = player.getMainHandItem();
                if (canDeployItem(stack) && stack.getItem().interactLivingEntity(stack, player, (LivingEntity) entity, Hand.MAIN_HAND).shouldSwing())
                    return true;
                if (entity instanceof AnimalEntity && ((AnimalEntity) entity).mobInteract(player, Hand.MAIN_HAND).shouldSwing())
                    return true;
            }
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            if (canDeployItem(stack) && stack.getItem().onItemUseFirst(stack, new ItemUseContext(player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false))) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            if (!level.isEmptyBlock(new BlockPos(x, y, z)) && block.use(level.getBlockState(new BlockPos(x, y, z)), level, new BlockPos(x, y, z), player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false)) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            boolean isGoingToShift = false;
            if (!stack.isEmpty()) {
                if (stack.getItem() == Items.SUGAR_CANE || stack.getItem() == Items.REDSTONE) {
                    isGoingToShift = true;
                }
            }
            int useX = isGoingToShift ? worldPosition.getX() : x;
            int useY = isGoingToShift ? worldPosition.getY() : y;
            int useZ = isGoingToShift ? worldPosition.getZ() : z;
            if (canDeployItem(stack) && stack.getItem().useOn(new ItemUseContext(player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false))) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            if (canDeployItem(stack)) {
                ItemStack copy = stack.copy();
                // TODO Check this
                player.setItemInHand(Hand.MAIN_HAND, stack.getItem().use(level, player, Hand.MAIN_HAND).getObject());
                if (!copy.sameItem(stack))
                    return true;
            }
        }
        return false;
    } catch (Throwable e) {
        BluePower.log.error("Deployer crashed! Stacktrace: ");
        e.printStackTrace();
        return true;
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) LivingEntity(net.minecraft.entity.LivingEntity) ItemEntity(net.minecraft.entity.item.ItemEntity) ItemUseContext(net.minecraft.item.ItemUseContext) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) Direction(net.minecraft.util.Direction) LivingEntity(net.minecraft.entity.LivingEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 38 with Direction

use of net.minecraft.util.Direction in project BluePower by Qmunity.

the class TileBattery method tick.

@Override
public void tick() {
    if (!level.isClientSide) {
        storage.resetCurrent();
        // Balance power of attached blulectric blocks.
        for (Direction facing : Direction.values()) {
            TileEntity tile = level.getBlockEntity(worldPosition.relative(facing));
            if (tile != null)
                tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, facing.getOpposite()).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
        }
        double energy = storage.getEnergy();
        int batteryLevel = (int) ((energy / MAX_ENERGY) * 6);
        BlockState state = getBlockState();
        if (state.getValue(BlockBattery.LEVEL) != batteryLevel) {
            this.level.setBlockAndUpdate(worldPosition, state.setValue(BlockBattery.LEVEL, batteryLevel));
            markForRenderUpdate();
            setChanged();
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IPowerBase(com.bluepowermod.api.power.IPowerBase) CompoundNBT(net.minecraft.nbt.CompoundNBT) CapabilityBlutricity(com.bluepowermod.api.power.CapabilityBlutricity) EnergyHelper(com.bluepowermod.helper.EnergyHelper) TileMachineBase(com.bluepowermod.tile.TileMachineBase) Direction(net.minecraft.util.Direction) NetworkManager(net.minecraft.network.NetworkManager) Capability(net.minecraftforge.common.capabilities.Capability) LazyOptional(net.minecraftforge.common.util.LazyOptional) BlutricityStorage(com.bluepowermod.api.power.BlutricityStorage) SUpdateTileEntityPacket(net.minecraft.network.play.server.SUpdateTileEntityPacket) BlockBattery(com.bluepowermod.block.power.BlockBattery) BPTileEntityType(com.bluepowermod.tile.BPTileEntityType) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) INBT(net.minecraft.nbt.INBT) Nullable(javax.annotation.Nullable) BlockState(net.minecraft.block.BlockState) Direction(net.minecraft.util.Direction)

Example 39 with Direction

use of net.minecraft.util.Direction in project BluePower by Qmunity.

the class TileBlulectricCable method getCapability.

@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    List<Direction> directions = new ArrayList<>(BlockBlulectricCable.FACING.getPossibleValues());
    if (level != null) {
        BlockState state = getBlockState();
        if (state.getBlock() instanceof BlockBlulectricCable) {
            // Remove upward connections
            directions.remove(state.getValue(BlockBlulectricCable.FACING));
            // Make sure the cable is on the same side of the block
            directions.removeIf(d -> level.getBlockState(worldPosition.relative(d)).getBlock() instanceof BlockBlulectricCable && level.getBlockState(worldPosition.relative(d)).getValue(BlockBlulectricCable.FACING) != state.getValue(BlockBlulectricCable.FACING));
        }
    }
    if (cap == CapabilityBlutricity.BLUTRICITY_CAPABILITY && (side == null || directions.contains(side))) {
        if (blutricityCap == null)
            blutricityCap = LazyOptional.of(() -> storage);
        return blutricityCap.cast();
    }
    return LazyOptional.empty();
}
Also used : BlockState(net.minecraft.block.BlockState) BlockBlulectricCable(com.bluepowermod.block.power.BlockBlulectricCable) ArrayList(java.util.ArrayList) Direction(net.minecraft.util.Direction) Nonnull(javax.annotation.Nonnull)

Example 40 with Direction

use of net.minecraft.util.Direction in project BluePower by Qmunity.

the class TileBlulectricCable method tick.

@Override
public void tick() {
    storage.resetCurrent();
    if (level != null && !level.isClientSide) {
        BlockState state = getBlockState();
        if (state.getBlock() instanceof BlockBlulectricCable) {
            List<Direction> directions = new ArrayList<>(BlockBlulectricCable.FACING.getPossibleValues());
            // Check the side has capability
            directions.removeIf(d -> !getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, d).isPresent());
            // Balance power of attached blulectric blocks.
            for (Direction facing : directions) {
                Block fBlock = level.getBlockState(worldPosition.relative(facing)).getBlock();
                if (fBlock != Blocks.AIR && fBlock != Blocks.WATER) {
                    TileEntity tile = level.getBlockEntity(worldPosition.relative(facing));
                    if (tile != null)
                        tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, facing.getOpposite()).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
                } else {
                    TileEntity tile = level.getBlockEntity(worldPosition.relative(facing).relative(state.getValue(BlockBlulectricCable.FACING).getOpposite()));
                    if (tile != null)
                        tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, state.getValue(BlockBlulectricCable.FACING)).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BlockBlulectricCable(com.bluepowermod.block.power.BlockBlulectricCable) IPowerBase(com.bluepowermod.api.power.IPowerBase) CompoundNBT(net.minecraft.nbt.CompoundNBT) CapabilityBlutricity(com.bluepowermod.api.power.CapabilityBlutricity) EnergyHelper(com.bluepowermod.helper.EnergyHelper) TileMachineBase(com.bluepowermod.tile.TileMachineBase) Direction(net.minecraft.util.Direction) NetworkManager(net.minecraft.network.NetworkManager) Blocks(net.minecraft.block.Blocks) Capability(net.minecraftforge.common.capabilities.Capability) LazyOptional(net.minecraftforge.common.util.LazyOptional) ArrayList(java.util.ArrayList) List(java.util.List) BlutricityStorage(com.bluepowermod.api.power.BlutricityStorage) Block(net.minecraft.block.Block) SUpdateTileEntityPacket(net.minecraft.network.play.server.SUpdateTileEntityPacket) BPTileEntityType(com.bluepowermod.tile.BPTileEntityType) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) INBT(net.minecraft.nbt.INBT) Nullable(javax.annotation.Nullable) BlockState(net.minecraft.block.BlockState) BlockBlulectricCable(com.bluepowermod.block.power.BlockBlulectricCable) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) Direction(net.minecraft.util.Direction)

Aggregations

Direction (net.minecraft.util.Direction)50 BlockState (net.minecraft.block.BlockState)18 TileEntity (net.minecraft.tileentity.TileEntity)17 BlockPos (net.minecraft.util.math.BlockPos)15 ItemStack (net.minecraft.item.ItemStack)12 Block (net.minecraft.block.Block)10 Nonnull (javax.annotation.Nonnull)8 Nullable (javax.annotation.Nullable)8 CompoundNBT (net.minecraft.nbt.CompoundNBT)6 IPowerBase (com.bluepowermod.api.power.IPowerBase)5 PlayerEntity (net.minecraft.entity.player.PlayerEntity)5 World (net.minecraft.world.World)5 Capability (net.minecraftforge.common.capabilities.Capability)5 BlutricityStorage (com.bluepowermod.api.power.BlutricityStorage)4 CapabilityBlutricity (com.bluepowermod.api.power.CapabilityBlutricity)4 EnergyHelper (com.bluepowermod.helper.EnergyHelper)4 BPTileEntityType (com.bluepowermod.tile.BPTileEntityType)4 TileMachineBase (com.bluepowermod.tile.TileMachineBase)4 INBT (net.minecraft.nbt.INBT)4 LazyOptional (net.minecraftforge.common.util.LazyOptional)4