Search in sources :

Example 1 with FastByteArrayOutputStream

use of it.unimi.dsi.fastutil.io.FastByteArrayOutputStream in project DynamicSurroundings by OreCruncher.

the class ConfigData method load.

public static ConfigData load() {
    final FastByteArrayOutputStream bits = new FastByteArrayOutputStream(16 * 1024);
    try (final OutputStreamWriter output = new OutputStreamWriter(new GZIPOutputStream(bits))) {
        // We are writing a Json array of objects so start with the open
        output.write("[");
        boolean prependComma = false;
        // Collect the locations where DS data is configured
        final List<IMyResourcePack> packs = ResourcePacks.findResourcePacks();
        final List<ModContainer> activeMods = Loader.instance().getActiveModList();
        // files from the dsurround jar.
        for (final ModContainer mod : activeMods) {
            final ResourceLocation rl = new ResourceLocation(ModInfo.MOD_ID, "data/" + mod.getModId().toLowerCase() + ".json");
            for (final IMyResourcePack p : packs) {
                if (p.resourceExists(rl))
                    prependComma = copy(p, rl, output, "[" + rl.toString() + "] from [" + p.getModName() + "]", prependComma);
            }
        }
        // Get config data from our JAR.
        final ResourceLocation rl = ResourcePacks.CONFIGURE_RESOURCE;
        for (final IMyResourcePack p : packs) {
            if (p.resourceExists(rl))
                prependComma = copy(p, rl, output, "[" + rl.toString() + "] from [" + p.getModName() + "]", prependComma);
        }
        // Built in toggle profiles for turning feature sets on/off
        final List<ProfileScript> resources = Profiles.getProfileStreams();
        for (final ProfileScript script : resources) {
            try (final InputStreamReader reader = new InputStreamReader(script.stream)) {
                prependComma = copy(reader, output, script.packName, prependComma);
            } catch (@Nonnull final Throwable t) {
                ModBase.log().error("Error reading profile script", t);
            }
        }
        // by players or pack makers.
        for (final String cfg : ModOptions.general.externalScriptFiles) {
            final File file = getFileReference(cfg);
            if (file.exists()) {
                ModBase.log().info("Loading external configuration script '%s'", cfg);
                try (final InputStream stream = new FileInputStream(file)) {
                    try (final InputStreamReader input = new InputStreamReader(stream)) {
                        prependComma = copy(input, output, cfg, prependComma);
                    } catch (final Throwable t) {
                        ModBase.log().error(String.format("Unable to load configuration script '%s'", cfg), t);
                    }
                } catch (final Throwable t) {
                    ModBase.log().error(String.format("Unable to load configuration script '%s'", cfg), t);
                }
            }
        }
        // The tap - need to close out the json array and flush
        // the stream to make sure.
        output.write("]");
        output.flush();
    } catch (@Nonnull final Throwable t) {
        ModBase.log().error("Something went horribly wrong", t);
    }
    // dump(bits.toByteArray());
    bits.trim();
    return new ConfigData(bits.array);
}
Also used : ModContainer(net.minecraftforge.fml.common.ModContainer) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FastByteArrayInputStream(it.unimi.dsi.fastutil.io.FastByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IMyResourcePack(org.orecruncher.dsurround.registry.config.packs.IMyResourcePack) FileInputStream(java.io.FileInputStream) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ResourceLocation(net.minecraft.util.ResourceLocation) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) ProfileScript(org.orecruncher.dsurround.registry.config.Profiles.ProfileScript)

Example 2 with FastByteArrayOutputStream

use of it.unimi.dsi.fastutil.io.FastByteArrayOutputStream in project meteor-client by MeteorDevelopment.

the class BookScreenMixin method onInit.

@Inject(method = "init", at = @At("TAIL"))
private void onInit(CallbackInfo info) {
    addDrawableChild(new ButtonWidget(4, 4, 120, 20, new LiteralText("Copy"), button -> {
        NbtList listTag = new NbtList();
        for (int i = 0; i < contents.getPageCount(); i++) listTag.add(NbtString.of(contents.getPage(i).getString()));
        NbtCompound tag = new NbtCompound();
        tag.put("pages", listTag);
        tag.putInt("currentPage", pageIndex);
        FastByteArrayOutputStream bytes = new FastByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bytes);
        try {
            NbtIo.write(tag, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        GLFW.glfwSetClipboardString(mc.getWindow().getHandle(), Base64.getEncoder().encodeToString(bytes.array));
    }));
    // Edit title & author
    ItemStack itemStack = mc.player.getMainHandStack();
    Hand hand = Hand.MAIN_HAND;
    if (itemStack.getItem() != Items.WRITTEN_BOOK) {
        itemStack = mc.player.getOffHandStack();
        hand = Hand.OFF_HAND;
    }
    if (itemStack.getItem() != Items.WRITTEN_BOOK)
        return;
    // Fuck you Java
    ItemStack book = itemStack;
    // Honestly
    Hand hand2 = hand;
    addDrawableChild(new ButtonWidget(4, 4 + 20 + 2, 120, 20, new LiteralText("Edit title & author"), button -> {
        mc.setScreen(new EditBookTitleAndAuthorScreen(GuiThemes.get(), book, hand2));
    }));
}
Also used : NbtIo(net.minecraft.nbt.NbtIo) LiteralText(net.minecraft.text.LiteralText) EditBookTitleAndAuthorScreen(meteordevelopment.meteorclient.gui.screens.EditBookTitleAndAuthorScreen) Inject(org.spongepowered.asm.mixin.injection.Inject) NbtList(net.minecraft.nbt.NbtList) ItemStack(net.minecraft.item.ItemStack) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) BookScreen(net.minecraft.client.gui.screen.ingame.BookScreen) DataOutputStream(java.io.DataOutputStream) Mixin(org.spongepowered.asm.mixin.Mixin) Hand(net.minecraft.util.Hand) At(org.spongepowered.asm.mixin.injection.At) MeteorClient.mc(meteordevelopment.meteorclient.MeteorClient.mc) GuiThemes(meteordevelopment.meteorclient.gui.GuiThemes) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) IOException(java.io.IOException) Items(net.minecraft.item.Items) GLFW(org.lwjgl.glfw.GLFW) NbtCompound(net.minecraft.nbt.NbtCompound) NbtString(net.minecraft.nbt.NbtString) Base64(java.util.Base64) Screen(net.minecraft.client.gui.screen.Screen) Shadow(org.spongepowered.asm.mixin.Shadow) Text(net.minecraft.text.Text) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) NbtCompound(net.minecraft.nbt.NbtCompound) DataOutputStream(java.io.DataOutputStream) EditBookTitleAndAuthorScreen(meteordevelopment.meteorclient.gui.screens.EditBookTitleAndAuthorScreen) NbtList(net.minecraft.nbt.NbtList) IOException(java.io.IOException) ItemStack(net.minecraft.item.ItemStack) Hand(net.minecraft.util.Hand) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) LiteralText(net.minecraft.text.LiteralText) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with FastByteArrayOutputStream

use of it.unimi.dsi.fastutil.io.FastByteArrayOutputStream in project Client by MatHax.

the class BookEditScreenMixin method onInit.

@Inject(method = "init", at = @At("TAIL"))
private void onInit(CallbackInfo info) {
    addDrawableChild(new ButtonWidget(4, 4, 120, 20, new LiteralText("Copy"), button -> {
        NbtList listTag = new NbtList();
        pages.stream().map(NbtString::of).forEach(listTag::add);
        NbtCompound tag = new NbtCompound();
        tag.put("pages", listTag);
        tag.putInt("currentPage", currentPage);
        FastByteArrayOutputStream bytes = new FastByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bytes);
        try {
            NbtIo.write(tag, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            GLFW.glfwSetClipboardString(mc.getWindow().getHandle(), Base64.getEncoder().encodeToString(bytes.array));
        } catch (OutOfMemoryError exception) {
            GLFW.glfwSetClipboardString(mc.getWindow().getHandle(), exception.toString());
        }
    }));
    addDrawableChild(new ButtonWidget(4, 4 + 20 + 2, 120, 20, new LiteralText("Paste"), button -> {
        String clipboard = GLFW.glfwGetClipboardString(mc.getWindow().getHandle());
        if (clipboard == null)
            return;
        byte[] bytes;
        try {
            bytes = Base64.getDecoder().decode(clipboard);
        } catch (IllegalArgumentException ignored) {
            return;
        }
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
        try {
            NbtCompound tag = NbtIo.read(in);
            NbtList listTag = tag.getList("pages", 8).copy();
            pages.clear();
            for (int i = 0; i < listTag.size(); ++i) {
                pages.add(listTag.getString(i));
            }
            if (pages.isEmpty())
                pages.add("");
            currentPage = tag.getInt("currentPage");
            dirty = true;
            updateButtons();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }));
}
Also used : NbtIo(net.minecraft.nbt.NbtIo) LiteralText(net.minecraft.text.LiteralText) DataInputStream(java.io.DataInputStream) Inject(org.spongepowered.asm.mixin.injection.Inject) NbtList(net.minecraft.nbt.NbtList) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) IOException(java.io.IOException) Final(org.spongepowered.asm.mixin.Final) GLFW(org.lwjgl.glfw.GLFW) MatHax.mc(mathax.client.MatHax.mc) NbtCompound(net.minecraft.nbt.NbtCompound) NbtString(net.minecraft.nbt.NbtString) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) Base64(java.util.Base64) List(java.util.List) Screen(net.minecraft.client.gui.screen.Screen) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) Mixin(org.spongepowered.asm.mixin.Mixin) Shadow(org.spongepowered.asm.mixin.Shadow) Text(net.minecraft.text.Text) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) BookEditScreen(net.minecraft.client.gui.screen.ingame.BookEditScreen) At(org.spongepowered.asm.mixin.injection.At) NbtCompound(net.minecraft.nbt.NbtCompound) DataOutputStream(java.io.DataOutputStream) NbtString(net.minecraft.nbt.NbtString) IOException(java.io.IOException) NbtString(net.minecraft.nbt.NbtString) DataInputStream(java.io.DataInputStream) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) NbtList(net.minecraft.nbt.NbtList) LiteralText(net.minecraft.text.LiteralText) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with FastByteArrayOutputStream

use of it.unimi.dsi.fastutil.io.FastByteArrayOutputStream in project Client by MatHax.

the class BookScreenMixin method onInit.

@Inject(method = "init", at = @At("TAIL"))
private void onInit(CallbackInfo info) {
    addDrawableChild(new ButtonWidget(4, 4, 120, 20, new LiteralText("Copy"), button -> {
        NbtList listTag = new NbtList();
        for (int i = 0; i < contents.getPageCount(); i++) listTag.add(NbtString.of(contents.getPage(i).getString()));
        NbtCompound tag = new NbtCompound();
        tag.put("pages", listTag);
        tag.putInt("currentPage", pageIndex);
        FastByteArrayOutputStream bytes = new FastByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bytes);
        try {
            NbtIo.write(tag, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        GLFW.glfwSetClipboardString(mc.getWindow().getHandle(), Base64.getEncoder().encodeToString(bytes.array));
    }));
    // Edit title & author
    ItemStack itemStack = mc.player.getMainHandStack();
    Hand hand = Hand.MAIN_HAND;
    if (itemStack.getItem() != Items.WRITTEN_BOOK) {
        itemStack = mc.player.getOffHandStack();
        hand = Hand.OFF_HAND;
    }
    if (itemStack.getItem() != Items.WRITTEN_BOOK)
        return;
    // Fuck you Java
    ItemStack book = itemStack;
    // Honestly
    Hand hand2 = hand;
    addDrawableChild(new ButtonWidget(4, 4 + 20 + 2, 120, 20, new LiteralText("Edit title & author"), button -> mc.setScreen(new EditBookTitleAndAuthorScreen(GuiThemes.get(), book, hand2))));
}
Also used : NbtIo(net.minecraft.nbt.NbtIo) LiteralText(net.minecraft.text.LiteralText) Inject(org.spongepowered.asm.mixin.injection.Inject) NbtList(net.minecraft.nbt.NbtList) ItemStack(net.minecraft.item.ItemStack) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) BookScreen(net.minecraft.client.gui.screen.ingame.BookScreen) DataOutputStream(java.io.DataOutputStream) Mixin(org.spongepowered.asm.mixin.Mixin) Hand(net.minecraft.util.Hand) At(org.spongepowered.asm.mixin.injection.At) GuiThemes(mathax.client.gui.GuiThemes) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) IOException(java.io.IOException) Items(net.minecraft.item.Items) GLFW(org.lwjgl.glfw.GLFW) MatHax.mc(mathax.client.MatHax.mc) NbtCompound(net.minecraft.nbt.NbtCompound) NbtString(net.minecraft.nbt.NbtString) Base64(java.util.Base64) Screen(net.minecraft.client.gui.screen.Screen) EditBookTitleAndAuthorScreen(mathax.client.gui.screens.EditBookTitleAndAuthorScreen) Shadow(org.spongepowered.asm.mixin.Shadow) Text(net.minecraft.text.Text) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) NbtCompound(net.minecraft.nbt.NbtCompound) DataOutputStream(java.io.DataOutputStream) EditBookTitleAndAuthorScreen(mathax.client.gui.screens.EditBookTitleAndAuthorScreen) NbtList(net.minecraft.nbt.NbtList) IOException(java.io.IOException) ItemStack(net.minecraft.item.ItemStack) Hand(net.minecraft.util.Hand) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) LiteralText(net.minecraft.text.LiteralText) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 5 with FastByteArrayOutputStream

use of it.unimi.dsi.fastutil.io.FastByteArrayOutputStream in project meteor-client by MeteorDevelopment.

the class BookEditScreenMixin method onInit.

@Inject(method = "init", at = @At("TAIL"))
private void onInit(CallbackInfo info) {
    addDrawableChild(new ButtonWidget(4, 4, 120, 20, new LiteralText("Copy"), button -> {
        NbtList listTag = new NbtList();
        pages.stream().map(NbtString::of).forEach(listTag::add);
        NbtCompound tag = new NbtCompound();
        tag.put("pages", listTag);
        tag.putInt("currentPage", currentPage);
        FastByteArrayOutputStream bytes = new FastByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bytes);
        try {
            NbtIo.write(tag, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            GLFW.glfwSetClipboardString(mc.getWindow().getHandle(), Base64.getEncoder().encodeToString(bytes.array));
        } catch (OutOfMemoryError exception) {
            GLFW.glfwSetClipboardString(mc.getWindow().getHandle(), exception.toString());
        }
    }));
    addDrawableChild(new ButtonWidget(4, 4 + 20 + 2, 120, 20, new LiteralText("Paste"), button -> {
        String clipboard = GLFW.glfwGetClipboardString(mc.getWindow().getHandle());
        if (clipboard == null)
            return;
        byte[] bytes;
        try {
            bytes = Base64.getDecoder().decode(clipboard);
        } catch (IllegalArgumentException ignored) {
            return;
        }
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
        try {
            NbtCompound tag = NbtIo.read(in);
            NbtList listTag = tag.getList("pages", 8).copy();
            pages.clear();
            for (int i = 0; i < listTag.size(); ++i) {
                pages.add(listTag.getString(i));
            }
            if (pages.isEmpty()) {
                pages.add("");
            }
            currentPage = tag.getInt("currentPage");
            dirty = true;
            updateButtons();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }));
}
Also used : NbtIo(net.minecraft.nbt.NbtIo) LiteralText(net.minecraft.text.LiteralText) DataInputStream(java.io.DataInputStream) Inject(org.spongepowered.asm.mixin.injection.Inject) NbtList(net.minecraft.nbt.NbtList) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) IOException(java.io.IOException) Final(org.spongepowered.asm.mixin.Final) GLFW(org.lwjgl.glfw.GLFW) NbtCompound(net.minecraft.nbt.NbtCompound) NbtString(net.minecraft.nbt.NbtString) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) Base64(java.util.Base64) List(java.util.List) Screen(net.minecraft.client.gui.screen.Screen) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) Mixin(org.spongepowered.asm.mixin.Mixin) Shadow(org.spongepowered.asm.mixin.Shadow) Text(net.minecraft.text.Text) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) BookEditScreen(net.minecraft.client.gui.screen.ingame.BookEditScreen) At(org.spongepowered.asm.mixin.injection.At) MeteorClient.mc(meteordevelopment.meteorclient.MeteorClient.mc) NbtCompound(net.minecraft.nbt.NbtCompound) DataOutputStream(java.io.DataOutputStream) NbtString(net.minecraft.nbt.NbtString) IOException(java.io.IOException) NbtString(net.minecraft.nbt.NbtString) DataInputStream(java.io.DataInputStream) ButtonWidget(net.minecraft.client.gui.widget.ButtonWidget) FastByteArrayOutputStream(it.unimi.dsi.fastutil.io.FastByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) NbtList(net.minecraft.nbt.NbtList) LiteralText(net.minecraft.text.LiteralText) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

FastByteArrayOutputStream (it.unimi.dsi.fastutil.io.FastByteArrayOutputStream)5 DataOutputStream (java.io.DataOutputStream)4 IOException (java.io.IOException)4 Base64 (java.util.Base64)4 Screen (net.minecraft.client.gui.screen.Screen)4 ButtonWidget (net.minecraft.client.gui.widget.ButtonWidget)4 NbtCompound (net.minecraft.nbt.NbtCompound)4 NbtIo (net.minecraft.nbt.NbtIo)4 NbtList (net.minecraft.nbt.NbtList)4 NbtString (net.minecraft.nbt.NbtString)4 LiteralText (net.minecraft.text.LiteralText)4 Text (net.minecraft.text.Text)4 GLFW (org.lwjgl.glfw.GLFW)4 Mixin (org.spongepowered.asm.mixin.Mixin)4 Shadow (org.spongepowered.asm.mixin.Shadow)4 At (org.spongepowered.asm.mixin.injection.At)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 CallbackInfo (org.spongepowered.asm.mixin.injection.callback.CallbackInfo)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2