LB-- / MCModify

WIP Java/C++ library for dealing with Minecraft files.
The Unlicense
21 stars 10 forks source link

TileEntity and Region.WriteChunk(x,z,c) #1

Open ducttapecrown opened 11 years ago

ducttapecrown commented 11 years ago

I couldn't get WriteChunk to work with TileEntities. I would add a TileEntity, WriteChunk, ReadChunk, and TileEntities would be empty. I'm also not really sure how TileEntities and BlockIDs work. I might have missed something?

On a side note, the library is really nice and simple to use. I'm making a level generator, NetHack style.

LB-- commented 11 years ago

Could you give a sample of the code you're using to do that? You can format code with syntax highlighting on GitHub like this:

```java
code;
code;
code;


Glad you like the library :) I still need to update it for the latest version of Minecraft - the latest commit is only some halfwork.
ducttapecrown commented 11 years ago
import java.io.FileInputStream;
import java.io.FileOutputStream;
import NBT.Tag;
import NBT.Serialization.NBTable;
import NBT.Minecraft.Map;
import NBT.Minecraft.IDs;
import NBT.Minecraft.Mob;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import NBT.FormatException;
import NBT.Minecraft.Level;
import NBT.Minecraft.Inventory;
import static NBT.Minecraft.IDs.*;
import NBT.Minecraft.Region;
import NBT.Minecraft.Chunk;
import NBT.Minecraft.TileEntity;

public class toMinecraft
{
    public static void main( String args[] ) throws Throwable
    {
        toMinecraft run = new toMinecraft();

        try
        {
            Region region = new Region( new File( "TestWorld/region/r.0.0.mca" ) );
            Chunk chunk = region.ReadChunk( 0, 0 );

            /*int y = 0;
            for( int x=0; x<16; x++ )
                for( int z=0; z<16; z++ )
                    chunk.BlockID( x, y, z, IDs.DiamondOre );

            chunk.BlockID( 0, 2, 0, IDs.Chest );
            chunk.BlockID( 0, 3, 0, IDs.Chest );*/

            /*System.out.println( "\n"+ chunk.Empty() +"\n"+ chunk.TileEntities().size() );
*/
            //chunk.TileEntities().add( run.newChest() );
            //chunk.Entities().add( new Mob.Zombie( 1, 10, 1 )) ; 

            chunk.BlockID( 0, 4, 0, IDs.Chest );

            for( TileEntity te : chunk.TileEntities() )
            {
                System.out.println( te.ToNBT(null) );
            }

            chunk = new Chunk( chunk.ToNBT(null) );

            region.WriteChunk( 0, 0, chunk );

            chunk = region.ReadChunk( 0, 0 );

            System.out.println( chunk.TileEntities().size()+" "+chunk.BlockID(0,4,0) );

            for( TileEntity te : chunk.TileEntities() )
                System.out.println( te.ToNBT(null) );

            //System.out.println( region.ReadChunk(0,0).TileEntities().size() );

        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
    }

    public TileEntity.Chest newChest() throws FormatException
    {
        Inventory.Item item = new Inventory.Item( 267, 0, 1 );
        Tag.List Items = new Tag.List( "Items", Tag.List.Type.COMPOUND, item.ToNBT(null, (byte)0 ) ); 
        Tag.Compound chest = new Tag.Compound( "Chest", Items, new Tag.String("id", "Chest"), new Tag.Int("x", 0), new Tag.Int("y", 4), new Tag.Int("z", 0) );
        return new TileEntity.Chest( chest ); 
    }

}
LB-- commented 11 years ago

Setting the block ID to that of a chest does not automatically create the tile entity for you, you still have to create the correct tile entity yourself and add it to the chunk.

chunk.BlockID( 0, 4, 0, IDs.Chest );
chunk.TileEntities().add(new TileEntity.Chest(new Tag.Compound(null, new Tag.List("Items", Tag.Type.COMPOUND))));

In the future I will be adding default constructors so everything doesn't have to be constructed from NBT.

ducttapecrown commented 11 years ago

Oh, right, sorry. I thought it was still in there. I was doing

chunk.TileEntities().add( newChest() );

and then region.WriteChunk(). Doing an add() to TileEntities and then using WriteChunk results in no TileEntities.

On Tue, Jun 18, 2013 at 5:14 PM, LB-- notifications@github.com wrote:

Setting the block ID to that of a chest does not automatically create the tile entity for you, you still have to create the correct tile entity yourself and add it to the chunk.

— Reply to this email directly or view it on GitHubhttps://github.com/LB--/MCModify/issues/1#issuecomment-19622340 .

LB-- commented 11 years ago

Strange, it works for me. I'll try and figure out why that might happen.

ducttapecrown commented 11 years ago
public class toMinecraft
{
    public static void main( String args[] ) throws Throwable
    {
        toMinecraft run = new toMinecraft();

        try
        {
            Region region = new Region( new File( "TestWorld/region/r.0.0.mca" ) );

            Chunk chunk = region.ReadChunk( 0, 0 );

            chunk.TileEntities().add( run.newChest() ); 

            System.out.println( chunk.TileEntities().size() ); //output here

            chunk = new Chunk( chunk.ToNBT(null) );

            region.WriteChunk( 0, 0, chunk );

            chunk = region.ReadChunk( 0, 0 );

            System.out.println( chunk.TileEntities().size() ); //output here

        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
    }

    public TileEntity.Chest newChest() throws FormatException
    {
        Inventory.Item item = new Inventory.Item( 267, 0, 1 );
        Tag.List Items = new Tag.List( "Items", Tag.List.Type.COMPOUND, item.ToNBT(null, (byte)0 ) ); 
        Tag.Compound chest = new Tag.Compound( "Chest", Items, new Tag.String("id", "Chest"), new Tag.Int("x", 0), new Tag.Int("y", 4), new Tag.Int("z", 0) );
        return new TileEntity.Chest( chest ); 
    }

}

Alright, this code should be clearer to read. I just lazily copy pasted stuff in earlier. I get the output

1
0

I'm running OSX 10.8.4 and Java 1.7.0_21-b12. That probably doesn't change anything, though, as minecraft files are pretty standard across machines, right?

PanierAvide commented 10 years ago

Hello, A part of the problem may come from Chunk.java, line 959

tileticklist = new Tag.List("TileEntities", Tag.Type.COMPOUND);

It might not be "TileTicks" instead ?

LB-- commented 10 years ago

@PanierAvide thanks! That was a copy-paste error.

@ninjabattyshogun can you see if 9837959a71928ae294283eeef6c7f1012e6dfa27 fixes the issue for you?