Jump to content

otter

Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by otter

  1. I've solved it, but it seems awfully complicated for such as basic task.

     

    Here's my solution:

    @Override
      protected void dropBlockAsItem(World p_149642_1_, int p_149642_2_, int p_149642_3_,
          int p_149642_4_, ItemStack p_149642_5_) {
        System.out.println("dropBlockAsItem");
        // super.dropBlockAsItem(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_, p_149642_5_);
        // TODO: TEST
        try {
          TileEntityBaseScanner tile =
              TileEntityHelper.getTileEntityBaseScannerFromCoords(p_149642_1_, p_149642_2_,
                  p_149642_3_, p_149642_4_);
          ItemStack itemstack = new ItemStack(this, 1);
          itemstack.setTagCompound(tile.getNBTTagCompound());
          itemstack.setStackDisplayName("Test: "
              + tile.getNBTTagCompound().getInteger("test"));
          Entity item = new EntityItem(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_, itemstack);
          p_149642_1_.spawnEntityInWorld(item);
        } catch (Exception e) {
          System.out.println("NO entity dropped!");
          e.printStackTrace();
        }
      }
    
      @Override
      public void onBlockPlacedBy(World p_149689_1_, int p_149689_2_, int p_149689_3_, int p_149689_4_,
          EntityLivingBase p_149689_5_, ItemStack p_149689_6_) {
        if (p_149689_1_.isRemote)
          return;
    
        try {
          NBTTagCompound nbt = p_149689_6_.getTagCompound();
          if(nbt != null){
            TileEntityBaseScanner tile = new TileEntityBaseScanner();
            //tile.writeToNBT(nbt);
            tile.writeToParByNBT(nbt);
            System.out.println(tile.test);
            p_149689_1_.setTileEntity(p_149689_2_, p_149689_3_, p_149689_4_, tile);
          }
          else {
            System.out.println("No NBT");
          }
        } catch (Exception e) {
          System.out.println("NO entity dropped!");
          e.printStackTrace();
        }
    
      }
    
    // tile-class
      /**
       * Write values from NBT to this
       * bit of an awkward solution, but since we ain't got no pointers making this more flexible is a pain, so I wrote it static for the time being
       * @param par1: NBTTagCompound
       */
      public void writeToParByNBT(NBTTagCompound par1){
        this.test= (par1.hasKey("test")) ? par1.getInteger("test") : test;
      }

  2. Hello,

    I've successfully stored the blocks's ID w/in the TileEntity - thanks for that!

     

    I can't figure out how to store the NBT quite yet. Do you have an example?

     

    The breakBlock()-method doesn't seem to interact w/ the player or the ItemStack at all. If I override this, the only benefit would be that I'd be able to prevent removeTileEntity() from being called.

     

    So I tried overriding dropBlockAsItem() - but how on earth do I get the NBT from my TileEntity?

     

    	@Override
        public void breakBlock(World p_149749_1_, int p_149749_2_, int p_149749_3_, int p_149749_4_, Block p_149749_5_, int p_149749_6_)
        {
            super.breakBlock(p_149749_1_, p_149749_2_, p_149749_3_, p_149749_4_, p_149749_5_, p_149749_6_);
            //p_149749_1_.removeTileEntity(p_149749_2_, p_149749_3_, p_149749_4_);
        }
    
    @Override
    protected void dropBlockAsItem(World p_149642_1_, int p_149642_2_, int p_149642_3_, int p_149642_4_, ItemStack p_149642_5_){
    	TileEntity tile = TileEntityHelper.getTileEntityBaseScannerFromCoords(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_);
    	p_149642_5_.setTagCompound()); // 
    	super.dropBlockAsItem(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_, p_149642_5_);
    }

  3. Hello,

    sorry if this is trivial.

     

    I've a block which is supposed to store meta data, in my case information about a different block. I can't figure out how to store "a block w/in a block", though...

     

    My approach:

    public class TileEntityBaseScanner extends TileEntity {
    public Block bb = Blocks.diamond_ore;
    
    @Override
    public void writeToNBT(NBTTagCompound par1) {
    	super.writeToNBT(par1);
    	try {
    		par1.setByteArray("bb", TileEntityHelper.serialize(bb));
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    
    @Override
    public void readFromNBT(NBTTagCompound par1) {
    	super.readFromNBT(par1);
    	try {
    		this.bb = (Block) TileEntityHelper.deserialize(par1.getByteArray("bb"));
    	} catch (ClassNotFoundException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    }
    
    public class TileEntityHelper {
    public static byte[] serialize(Object obj) throws IOException {
    	ByteArrayOutputStream out = new ByteArrayOutputStream();
    	ObjectOutputStream os = new ObjectOutputStream(out);
    	os.writeObject(obj);
    	return out.toByteArray();
    }
    
    public static Object deserialize(byte[] data) throws IOException,
    		ClassNotFoundException {
    	ByteArrayInputStream in = new ByteArrayInputStream(data);
    	ObjectInputStream is = new ObjectInputStream(in);
    	return is.readObject();
    }
    }
    

    Unfortunately, the block class can't be serialized and there's no "NBTTagCompound.setBlock()"-method...

     

    Also, is there any way to store the TileEntity once a block is destroyed? i.e., the player destroys the block and he has the block including the metadata in his inventory?

  4. Hey,

    beginner here, a short question concerning 1.7.2 I couldn't figure out myself by reading the code or Google.

     

    How do I get the current chunk's borders?

     

    This is my current approach:

    	@Override
    public boolean onBlockActivated(World p_149727_1_, int p_149727_2_, int p_149727_3_, int p_149727_4_, EntityPlayer p_149727_5_, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_)
       {
    if(p_149727_1_.isRemote) return false;
    
    	int x = p_149727_2_;
    	int y = p_149727_3_;
    	int z = p_149727_4_;
    
    	Chunk currentChunk = p_149727_1_.getChunkFromChunkCoords(x,y);
    	ChunkCoordIntPair CCIP = currentChunk.getChunkCoordIntPair();
    	int x_border = CCIP.chunkXPos;
    	int z_border = CCIP.chunkZPos;
    	int y_border = currentChunk.heightMap[x_border << 4 | z_border]; // TODO: crash; taken from canBlockSeeTheSky
    //... 

    I did some testing and these coordinates definitely aren't the chunks borders but rather the current block's coords. Also, my y_border-method crashes the game via a ArrayIndexOutOfBoundsException (haven't looked into this too much, as soon as I see bit-shifting my mind crashes as well...).

     

    Thanks in advance.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.