Search in sources :

Example 1 with Cancellable

use of com.chattriggers.ctjs.utils.Cancellable in project Hyperium by HyperiumClient.

the class AsyncScreenshotSaver method run.

/**
 * Threaded task that creates the image and uploads to Imgur if able to
 */
@Override
public void run() {
    // Create the colors and image
    processPixelValues(pixelValues, width, height);
    // Make an image to be posted
    BufferedImage image;
    // Create the screenshot file
    File screenshot = getTimestampedPNGFileForDirectory(screenshotDir);
    // Allow a developer to stop the screenshot process if they want to
    Cancellable cancellable = new Cancellable();
    // Screenshot is taken, so trigger all ChatTriggers modules using it
    TriggerType.SCREENSHOT_TAKEN.triggerAll(screenshot, cancellable);
    // Cancel the process if made to cancel
    if (cancellable.isCancelled())
        return;
    try {
        // Create the image
        if (OpenGlHelper.isFramebufferEnabled()) {
            image = new BufferedImage(frameBuffer.framebufferWidth, frameBuffer.framebufferHeight, 1);
            int texHeight;
            for (int heightSize = texHeight = frameBuffer.framebufferTextureHeight - frameBuffer.framebufferHeight; texHeight < frameBuffer.framebufferTextureHeight; ++texHeight) {
                for (int widthSize = 0; widthSize < frameBuffer.framebufferWidth; ++widthSize) {
                    // Set the image colors
                    image.setRGB(widthSize, texHeight - heightSize, pixelValues[texHeight * frameBuffer.framebufferTextureWidth + widthSize]);
                }
            }
        } else {
            // Fallback if the computer doesnt support Frame buffering
            image = new BufferedImage(width, height, 1);
            // Set the image colors
            image.setRGB(0, 0, width, height, pixelValues, 0, width);
        }
        // Write the image
        ImageIO.write(image, "png", screenshot);
        // If the user doesn't want the screenshot to be uploaded, run this
        if (!upload) {
            IChatComponent chatComponent = new ChatComponentText(ChatColor.RED + "[Hyperium] " + ChatColor.WHITE + "Captured to " + ChatColor.UNDERLINE + screenshot.getName());
            chatComponent.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, screenshot.getCanonicalPath()));
            Minecraft.getMinecraft().thePlayer.addChatMessage(chatComponent);
        } else {
            // Otherwise allow the Imgur uploading process to run
            new ImgurUploader("649f2fb48e59767", screenshot).run();
        }
    } catch (Exception e) {
        // If an issue is caught, tell the user it couldn't be saved and then give them the error
        Hyperium.LOGGER.warn("Couldn't save {} : {}", screenshot, e);
        Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentTranslation("screenshot.failure", e.getMessage()));
    }
}
Also used : Cancellable(com.chattriggers.ctjs.utils.Cancellable) ChatComponentTranslation(net.minecraft.util.ChatComponentTranslation) ClickEvent(net.minecraft.event.ClickEvent) IChatComponent(net.minecraft.util.IChatComponent) File(java.io.File) ChatComponentText(net.minecraft.util.ChatComponentText) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Cancellable (com.chattriggers.ctjs.utils.Cancellable)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 ClickEvent (net.minecraft.event.ClickEvent)1 ChatComponentText (net.minecraft.util.ChatComponentText)1 ChatComponentTranslation (net.minecraft.util.ChatComponentTranslation)1 IChatComponent (net.minecraft.util.IChatComponent)1