Java Code Examples for net.minecraft.nbt.CompressedStreamTools#readCompressed()

The following examples show how to use net.minecraft.nbt.CompressedStreamTools#readCompressed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateManager.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Template getBuiltinTemplate(World world, ResourceLocation templateId) {
    if (templateMap.containsKey(templateId)) {
        return templateMap.get(templateId);
    }
    Template template = new Template();
    String resourcePath = "/assets/" + templateId.getResourceDomain() + "/structures/" + templateId.getResourcePath() + ".nbt";
    InputStream inputStream = TemplateManager.class.getResourceAsStream(resourcePath);
    if (inputStream != null) {
        try {
            NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(inputStream);
            if (!nbttagcompound.hasKey("DataVersion", 99)) {
                nbttagcompound.setInteger("DataVersion", 500);
            }
            DataFixer dataFixer = world.getMinecraftServer().getDataFixer();
            template.read(dataFixer.process(FixTypes.STRUCTURE, nbttagcompound));
        } catch (IOException exception) {
            GTLog.logger.error("Failed to load builtin template {}", templateId, exception);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    } else {
        GTLog.logger.warn("Failed to find builtin structure with path {}", resourcePath);
    }
    templateMap.put(templateId, template);
    return template;
}
 
Example 2
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 6 votes vote down vote up
public static NBTTagCompound readNBT(File file) throws IOException {
    if (!file.exists()) {
        return null;
    }
    FileInputStream in = new FileInputStream(file);
    NBTTagCompound tag;
    try {
        tag = CompressedStreamTools.readCompressed(in);
    } catch (ZipException e) {
        if (e.getMessage().equals("Not in GZIP format")) {
            tag = CompressedStreamTools.read(file);
        } else {
            throw e;
        }
    }
    in.close();
    return tag;
}
 
Example 3
Source File: QCraftProxyCommon.java    From qcraft-mod with Apache License 2.0 6 votes vote down vote up
public static NBTTagCompound loadNBTFromPath( File file )
{
    try
    {
        if( file != null && file.exists() )
        {
            InputStream input = new BufferedInputStream( new FileInputStream( file ) );
            try
            {
                return CompressedStreamTools.readCompressed( input );
            }
            finally
            {
                input.close();
            }
        }
    }
    catch( IOException e )
    {
        QCraft.log( "Warning: failed to load QCraft entanglement info" );
    }
    return null;
}
 
Example 4
Source File: StructureUtil.java    From YUNoMakeGoodMap with Apache License 2.0 6 votes vote down vote up
private static Template loadTemplate(InputStream is)
{
    if (is == null)
        return null;
    try
    {
        NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
        Template template = new Template();
        template.read(nbt);
        return template;
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (is != null)
            IOUtils.closeQuietly(is);
    }
    return null;
}
 
Example 5
Source File: MaterialCache.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void readFromFile()
{
    File file = this.getCacheFile();

    if (file.exists() == false || file.canRead() == false)
    {
        return;
    }

    try
    {
        FileInputStream is = new FileInputStream(file);
        NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
        is.close();

        if (nbt != null)
        {
            this.readFromNBT(nbt);
            this.hasReadFromFile = true;
            this.dirty = false;
        }
    }
    catch (Exception e)
    {
        Litematica.logger.warn("Failed to read the material list cache from file '{}'", file.getAbsolutePath(), e);
    }
}
 
Example 6
Source File: MCRetentionManager.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public NBTTagCompound loadFile(File file) {
	try {
		if (file.exists()) {
			return CompressedStreamTools.readCompressed(new FileInputStream(file));
		} else {
			return new NBTTagCompound();
		}
	} catch (Exception e) {
		Game.logger().error("Failed to load {}!", file.getName());
		e.printStackTrace();
		return null;
	}
}
 
Example 7
Source File: MCRetentionManager.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public NBTTagCompound loadFile(File file) {
	try {
		if (file.exists()) {
			return CompressedStreamTools.readCompressed(new FileInputStream(file));
		} else {
			return new NBTTagCompound();
		}
	} catch (Exception e) {
		Game.logger().error("Failed to load {}!", file.getName(), e);
		e.printStackTrace();
		return null;
	}
}
 
Example 8
Source File: MCRetentionManager.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public NBTTagCompound loadFile(File file) {
	try {
		if (file.exists()) {
			return CompressedStreamTools.readCompressed(new FileInputStream(file));
		} else {
			return new NBTTagCompound();
		}
	} catch (Exception e) {
		Game.logger().error("Failed to load {}!", file.getName(), e);
		e.printStackTrace();
		return null;
	}
}
 
Example 9
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static NBTTagCompound readNBT(File file) throws IOException {
    if(!file.exists()) return null;
    FileInputStream in = new FileInputStream(file);
    NBTTagCompound tag;
    try {
        tag = CompressedStreamTools.readCompressed(in);
    } catch(ZipException e) {
        if(e.getMessage().equals("Not in GZIP format"))
            tag = CompressedStreamTools.read(file);
        else
            throw e;
    }
    in.close();
    return tag;
}
 
Example 10
Source File: TemplateManagerEU.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void readTemplateFromStream(String id, InputStream stream) throws IOException
{
    NBTTagCompound nbt = CompressedStreamTools.readCompressed(stream);

    if (nbt.hasKey("DataVersion", Constants.NBT.TAG_ANY_NUMERIC) == false)
    {
        nbt.setInteger("DataVersion", 500);
    }

    TemplateEnderUtilities template = new TemplateEnderUtilities();
    template.read(this.fixer.process(FixTypes.STRUCTURE, nbt));
    this.templates.put(id, template);
}
 
Example 11
Source File: TemplateManagerEU.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void readTemplateMetadataFromStream(String id, InputStream stream) throws IOException
{
    NBTTagCompound nbt = CompressedStreamTools.readCompressed(stream);
    TemplateMetadata templateMeta = new TemplateMetadata();
    templateMeta.read(nbt);
    this.templateMetas.put(id, templateMeta);
}
 
Example 12
Source File: IoUtil.java    From EnderZoo with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
public static NBTTagCompound readNBTTagCompound(ByteBuf dataIn) {
  try {
    short size = dataIn.readShort();
    if (size < 0) {
      return null;
    } else {
      byte[] buffer = new byte[size];
      dataIn.readBytes(buffer);
      return CompressedStreamTools.readCompressed(new ByteArrayInputStream(buffer));
    }
  } catch (IOException e) {
    FMLCommonHandler.instance().raiseException(e, "Custom Packet", true);
    return null;
  }
}
 
Example 13
Source File: BackpackManager.java    From SkyblockAddons with MIT License 4 votes vote down vote up
public static Backpack getFromItem(ItemStack stack) {
    if (stack == null) return null;
    SkyblockAddons main = SkyblockAddons.getInstance();
    String id = ItemUtils.getSkyBlockItemID(stack);
    if (id != null) {
        NBTTagCompound extraAttributes = stack.getTagCompound().getCompoundTag("ExtraAttributes");
        Matcher matcher = BACKPACK_ID_PATTERN.matcher(id);
        boolean matches = matcher.matches();

        boolean isCakeBag = main.getConfigValues().isEnabled(Feature.CAKE_BAG_PREVIEW) &&
                "NEW_YEAR_CAKE_BAG".equals(id) && EnumUtils.InventoryType.getCurrentInventoryType() != EnumUtils.InventoryType.BAKER;

        // If it's a backpack OR it's a cake bag and they have the setting enabled.
        if (matches || isCakeBag) {
            byte[] bytes = null;
            for (String key : extraAttributes.getKeySet()) {
                if (key.endsWith("backpack_data") || key.equals("new_year_cake_bag_data")) {
                    bytes = extraAttributes.getByteArray(key);
                    break;
                }
            }
            try {
                int length = 0;
                if (matches) {
                    String backpackType = matcher.group(1);
                    switch (backpackType) { // because sometimes the size of the tag is not updated (etc. when you upcraft it)
                        case "SMALL": length = 9; break;
                        case "MEDIUM": length = 18; break;
                        case "LARGE": length = 27; break;
                        case "GREATER": length = 36; break;
                    }
                }
                ItemStack[] items = new ItemStack[length];
                if (bytes != null) {
                    NBTTagCompound nbtTagCompound = CompressedStreamTools.readCompressed(new ByteArrayInputStream(bytes));
                    NBTTagList list = nbtTagCompound.getTagList("i", Constants.NBT.TAG_COMPOUND);
                    if (list.tagCount() > length) {
                        length = list.tagCount();
                        items = new ItemStack[length];
                    }
                    for (int i = 0; i < length; i++) {
                        NBTTagCompound item = list.getCompoundTagAt(i);
                        // This fixes an issue in Hypixel where enchanted potatoes have the wrong id (potato block instead of item).
                        short itemID = item.getShort("id");
                        if (itemID == 142) { // Potato Block -> Potato Item
                            item.setShort("id", (short) 392);
                        } else if (itemID == 141) { // Carrot Block -> Carrot Item
                            item.setShort("id", (short) 391);
                        }
                        ItemStack itemStack = ItemStack.loadItemStackFromNBT(item);
                        items[i] = itemStack;
                    }
                }
                BackpackColor color = BackpackColor.WHITE;
                if (extraAttributes.hasKey("backpack_color")) {
                    try {
                        color = BackpackColor.valueOf(extraAttributes.getString("backpack_color"));
                    } catch (IllegalArgumentException ignored) {}
                }
                return new Backpack(items, TextUtils.stripColor(stack.getDisplayName()), color);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    return null;
}
 
Example 14
Source File: SyncableInventory.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void readFromStream(DataInputStream stream) throws IOException {
	NBTTagCompound tag = CompressedStreamTools.readCompressed(stream);
	readFromNBT(tag);
}