Return to Snippet

Revision: 39905
at January 22, 2011 11:18 by jkerr88


Initial Code
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 
 * @author jkerr88
 *
 * You need the code from here:
 *
 *  http://www.minecraftwiki.net/wiki/NBT_class
 *  
 * saved as Tag.java in the same folder as this one.  This file should be saved as ReportPlayerLocation.java
 * 
 */
public class ExtinguishFiresInChunk implements Runnable{
   
   static ExtinguishFiresInChunk iThis;
   static String isCurrentFile;
   
   public static void main(String argv[]){
      
      iThis = new ExtinguishFiresInChunk();
      Thread t = new Thread(iThis);    
      t.start();
   
   } // main
   
   public void run() {     
      
      String sWorldFolder = "C:\\Documents and Settings\\Jeff\\Application Data\\.minecraft\\saves\\World1";
      
      int xPos = 4;
      int zPos = -6;
      
      String sInputFile = getChunkFilename(sWorldFolder,xPos,zPos);
         
      //System.out.println(sInputFile);

      try{
         
         FileInputStream fis = new FileInputStream(sInputFile);
         Tag datFileTag = Tag.readFrom(fis);
         fis.close();
         
         editBlocksInTag(datFileTag);
         
         FileOutputStream fos = new FileOutputStream(sInputFile);
         datFileTag.writeTo(fos);
         fos.close();
         
      }catch(Exception e){
         System.out.println(e);
         return;   
      }
      
      System.out.println("Done!");
   
   } 
 
   private String getChunkFilename(String p_sWorldFolder, int p_xPos, int p_zPos){
      
      //get mod 64 of x and z
      int nModX;
      int nModZ;
      
      if(p_xPos<0){
         nModX = (p_xPos + 64) % 64;
      }else{
         nModX = p_xPos % 64;
      }
      
      if(p_zPos<0){
         nModZ = (p_zPos + 64) % 64;
      }else{
         nModZ = p_zPos % 64;
      }
      
      System.out.println("x:" + nModX + " " + getBase36(nModX));
      System.out.println("z:" + nModZ + " " + getBase36(nModZ));
      
      String sReturn = p_sWorldFolder;
      if(!sReturn.endsWith("\\")){
         sReturn += "\\";
      }
      
      sReturn += getBase36(nModX) + "\\" + getBase36(nModZ) + "\\c." + getBase36(p_xPos) + "." + getBase36(p_zPos) + ".dat";
      
      return sReturn;
      
   }

   private static String getBase36(int decimalNumber){
      
      boolean bNegative = false;
      if(decimalNumber<0){
         bNegative = true;
         decimalNumber = decimalNumber*-1;
      }
      
      String tempVal = decimalNumber == 0 ? "0" : "";  
      int mod = 0;  
      
      String baseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  
      
      while( decimalNumber != 0 ) {  
          mod = decimalNumber % 36;  
          tempVal = baseDigits.substring( mod, mod + 1 ) + tempVal;  
          decimalNumber = decimalNumber / 36;  
      }  

      if(bNegative){
         tempVal = "-" + tempVal;
      }
      return tempVal;  
   }  
   
   
   private byte[] editBlocksInTag(Tag p_tag){
      
      Tag.Type type = p_tag.getType();
      if (type == Tag.Type.TAG_End)
         return null;
      
      String sName = p_tag.getName();
      
      if (type == Tag.Type.TAG_Byte_Array) {
         if(sName.equals("Blocks")){
            Object o = p_tag.getValue();
            byte[] b = (byte[]) o;
            
            //b is a byte array of the blocks in this chunk - Changes can be made and the passed Tag is written back out by the caller
            
            //Extinguish any fires in the chunk (id 51) by replacing with air (id 0)
            
            int nCount =0;
            for(int n=0;n<b.length;n++){
               if(b[n]==51){//fire
                  b[n] = 0; //air
                  nCount++;
               }
            }
            
            System.out.println(nCount + " fires extinguished!");
         
            return b;
         }
      } else if (type == Tag.Type.TAG_List) {
         Tag[] subtags = (Tag[]) p_tag.getValue();
         for (Tag st : subtags) {
            editBlocksInTag(st);
         }
         
      } else if (type == Tag.Type.TAG_Compound) {
         Tag[] subtags = (Tag[]) p_tag.getValue();
         for (Tag st : subtags) {
            editBlocksInTag(st);
         }
         
      } else {
         
      }
      
      return null;
   }
   
}

Initial URL


Initial Description


Initial Title
Minecraft - Java - Extinguish fires in a chunk

Initial Tags


Initial Language
Java