LordRelentless

Untitled

Nov 6th, 2022
228
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | Gaming | 0 0
  1. import com.google.common.io.ByteStreams;
  2. import net.minecraft.nbt.CompressedStreamTools;
  3. import net.minecraft.nbt.NBTTagCompound;
  4. import net.minecraft.util.ResourceLocation;
  5. import net.minecraftforge.fml.common.FMLCommonHandler;
  6.  
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.util.zip.GZIPInputStream;
  12.  
  13. public class GLBLoader {
  14.     public static NBTTagCompound loadGLBFile(File file) throws IOException {
  15.         if (!file.exists()) {
  16.             throw new IOException("File does not exist");
  17.         }
  18.         if (!file.isFile()) {
  19.             throw new IOException("File is not a valid file");
  20.         }
  21.         if (!file.canRead()) {
  22.             throw new IOException("File cannot be read");
  23.         }
  24.         String fileName = file.getName();
  25.         String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
  26.         if (!fileExtension.equalsIgnoreCase("glb")) {
  27.             throw new IOException("File is not a valid .glb file");
  28.         }
  29.         InputStream inputStream = new FileInputStream(file);
  30.         byte[] magicNumber = new byte[4];
  31.         inputStream.read(magicNumber);
  32.         if (!magicNumber[0] == 'g' || !magicNumber[1] == 'l' || !magicNumber[2] == 'b') {
  33.             throw new IOException("File is not a valid .glb file");
  34.         }
  35.         int version = inputStream.read();
  36.         if (version != 2) {
  37.             throw new IOException("Unsupported .glb file version");
  38.         }
  39.         int totalLength = readInt32(inputStream);
  40.         int modelLength = readInt32(inputStream);
  41.         //Skip model data for now
  42.         inputStream.skip(modelLength);
  43.         int sceneLength = readInt32(inputStream);
  44.         byte[] sceneData = new byte[sceneLength];
  45.         inputStream.read(sceneData);
  46.         InputStream sceneInputStream = new ByteArrayInputStream(sceneData);
  47.         NBTTagCompound nbtTagCompound = CompressedStreamTools.read(new GZIPInputStream(sceneInputStream), new            
  48.  NBTSizeTracker(Integer.MAX_VALUE));
  49.         inputStream.close();
  50.         sceneInputStream.close();
  51.         return nbtTagCompound;
  52.     }
  53. }
Tags: minecraft .glb
Advertisement
Comments
  • LordRelentless
    3 years
    # text 0.30 KB | 0 0
    1. Any help in implementing this or rewriting it so I can load .glb files into Minecraft would be appreciated. I'm currently using MCreator and I'm hoping to implement this code into MCreator. Their reliance on .obj and .mtl files instead of an integrated .glb (or similar 3D object file) is rather frustrating.
Add Comment
Please, Sign In to add comment