Error with Loading Schematics

Discussion in 'Plugin Development' started by xWatermelon, Aug 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    xWatermelon

    I am trying to load a .schematic file using this method:
    Code:java
    1. public static Schematic loadScheamtic(File file){
    2. try{
    3. if(file.exists()){
    4. NBTInputStream nbtStream = new NBTInputStream(new FileInputStream(file));
    5. CompoundTag compound = (CompoundTag) nbtStream.readTag();
    6. Map<String, Tag> tags = compound.getValue();
    7. Short width = ((ShortTag) tags.get("Width")).getValue();
    8. Short height = ((ShortTag) tags.get("Height")).getValue();
    9. Short length = ((ShortTag) tags.get("Length")).getValue();
    10. String materials = ((StringTag) tags.get("Materials")).getValue();
    11. byte[] blocks = ((ByteArrayTag) tags.get("Blocks")).getValue();
    12. byte[] data = ((ByteArrayTag) tags.get("Data")).getValue();
    13.  
    14. nbtStream.close();
    15.  
    16. Schematic schematic = new Schematic(width, height, length, materials, blocks, data);
    17.  
    18. return schematic;
    19. }
    20. } catch(Exception e){
    21. WatermelonWars.getInstance().getLogger().severe("Failed to load the schematic: " + file.getAbsolutePath());
    22. e.printStackTrace();
    23. }
    24.  
    25. return null;
    26. }

    However, I am getting this error:
    Code:
    17:36:51 [SEVERE] [WatermelonWars] Failed to load the schematic: /Users/xWatermelon/Desktop/Testing-Server/plugins/WatermelonWars/maps/test1-schematic.schematic
    17:36:51 [SEVERE] java.io.EOFException
    17:36:51 [SEVERE]    at java.io.DataInputStream.readFully(DataInputStream.java:180)
    17:36:51 [SEVERE]    at java.io.DataInputStream.readFully(DataInputStream.java:152)
    17:36:51 [SEVERE]    at com.sk89q.jnbt.NBTInputStream.readTag(NBTInputStream.java:122)
    17:36:51 [SEVERE]    at com.sk89q.jnbt.NBTInputStream.readTag(NBTInputStream.java:103)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.schematic.SchematicUtils.loadScheamtic(SchematicUtils.java:35)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.Map.loadSchematic(Map.java:128)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.Map.<init>(Map.java:56)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.TeamMap.<init>(TeamMap.java:20)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.TeamMap.<init>(TeamMap.java:26)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.TDMMap.<init>(TDMMap.java:16)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.MapUtils.loadMaps(MapUtils.java:42)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.game.map.MapUtils.init(MapUtils.java:24)
    17:36:51 [SEVERE]    at me.xwatermelon.watermelonwars.WatermelonWars.onEnable(WatermelonWars.java:104)
    17:36:51 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    17:36:51 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
    17:36:51 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    17:36:51 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
    17:36:51 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
    17:36:51 [SEVERE]    at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:313)
    17:36:51 [SEVERE]    at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:290)
    17:36:51 [SEVERE]    at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:250)
    17:36:51 [SEVERE]    at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:151)
    17:36:51 [SEVERE]    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
    17:36:51 [SEVERE]    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Note: I am using WorldEdit's schematic API.
     
  2. Offline

    Weasel_Squeezer

    If you don't mind using worldEdit as a dependency, here is some code that will load, save, and paste schematics. I did not like to read through WorldEdit to create this so I'll save you the trouble if you decide to use WorldEdit
    btw, what Schematic class is that?

    Code:java
    1. public static void saveSchematic(Location max, Location min, String fileName) {
    2. File file = new File(fileName + ".schematic");
    3. if (!file.exists()) {
    4. try {
    5. file.createNewFile();
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. }
    9. }
    10.  
    11. Vector minVector = new Vector(min.getBlockX(), min.getBlockY(), min.getBlockZ());
    12. Vector maxVector = new Vector(max.getBlockX(), max.getBlockY(), max.getBlockZ());
    13.  
    14. CuboidClipboard clipboard = new CuboidClipboard(maxVector.subtract(minVector).add(new Vector(1, 1, 1)), minVector);
    15.  
    16. EditSession session = new EditSession(new BukkitWorld(min.getWorld()), 999999999);
    17. clipboard.copy(session);
    18. try {
    19. SchematicFormat.MCEDIT.save(clipboard, file);
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. } catch (DataException e) {
    23. e.printStackTrace();
    24. }
    25. }
    26.  
    27. public static void loadSchematic(String fileName, Location origin, boolean redundancy) {
    28. File file = new File(fileName + ".schematic");
    29. Vector pos = new Vector(origin.getBlockX(), origin.getBlockY(), origin.getBlockZ());
    30. EditSession session = new EditSession(new BukkitWorld(origin.getWorld()), 999999999);
    31. CuboidClipboard clipboard;
    32. try {
    33. clipboard = SchematicFormat.MCEDIT.load(file);
    34. clipboard.paste(session, pos, false, true);
    35. } catch (IOException e1) {
    36. e1.printStackTrace();
    37. } catch (DataException e1) {
    38. e1.printStackTrace();
    39. } catch (MaxChangedBlocksException e) {
    40. e.printStackTrace();
    41. }
    42. }
     
    xWatermelon likes this.
  3. Offline

    xWatermelon

    Weasel_Squeezer the schematic is a custom one I made. Do you happen to know how I can load it using WorldEdit's schematics?
     
  4. This is my method i use for WorldEdit. Not perfectly save at the moment, but it shows how to load schematics.
    Code:
    private CuboidClipboard getSchematicByFile(String fileName){
     
            File f = new File(schematicDir, fileName+".schematic");
            SchematicFormat format = SchematicFormat.getFormat(f);
            CuboidClipboard cubClip = null;
            try {
                cubClip = format.load(f);               
            } catch (IOException e) { // TODO:
                e.printStackTrace();
            } catch (DataException e) {
                e.printStackTrace();
            }       
            return cubClip;
        }
    And this clipboard you can paste into a EditSession for example, and so its pasted in the world.
     
    xWatermelon likes this.
  5. Offline

    xWatermelon

    Adversarius thanks! Do either of you know how I can retrieve a List of blocks from the schematic that got pasted?
     
Thread Status:
Not open for further replies.

Share This Page