Minecraft - Java - Extinguish fires in a chunk


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3.  
  4. /**
  5.  *
  6.  * @author jkerr88
  7.  *
  8.  * You need the code from here:
  9.  *
  10.  * http://www.minecraftwiki.net/wiki/NBT_class
  11.  *
  12.  * saved as Tag.java in the same folder as this one. This file should be saved as ReportPlayerLocation.java
  13.  *
  14.  */
  15. public class ExtinguishFiresInChunk implements Runnable{
  16.  
  17. static ExtinguishFiresInChunk iThis;
  18. static String isCurrentFile;
  19.  
  20. public static void main(String argv[]){
  21.  
  22. iThis = new ExtinguishFiresInChunk();
  23. Thread t = new Thread(iThis);
  24. t.start();
  25.  
  26. } // main
  27.  
  28. public void run() {
  29.  
  30. String sWorldFolder = "C:\\Documents and Settings\\Jeff\\Application Data\\.minecraft\\saves\\World1";
  31.  
  32. int xPos = 4;
  33. int zPos = -6;
  34.  
  35. String sInputFile = getChunkFilename(sWorldFolder,xPos,zPos);
  36.  
  37. //System.out.println(sInputFile);
  38.  
  39. try{
  40.  
  41. FileInputStream fis = new FileInputStream(sInputFile);
  42. Tag datFileTag = Tag.readFrom(fis);
  43. fis.close();
  44.  
  45. editBlocksInTag(datFileTag);
  46.  
  47. FileOutputStream fos = new FileOutputStream(sInputFile);
  48. datFileTag.writeTo(fos);
  49. fos.close();
  50.  
  51. }catch(Exception e){
  52. System.out.println(e);
  53. return;
  54. }
  55.  
  56. System.out.println("Done!");
  57.  
  58. }
  59.  
  60. private String getChunkFilename(String p_sWorldFolder, int p_xPos, int p_zPos){
  61.  
  62. //get mod 64 of x and z
  63. int nModX;
  64. int nModZ;
  65.  
  66. if(p_xPos<0){
  67. nModX = (p_xPos + 64) % 64;
  68. }else{
  69. nModX = p_xPos % 64;
  70. }
  71.  
  72. if(p_zPos<0){
  73. nModZ = (p_zPos + 64) % 64;
  74. }else{
  75. nModZ = p_zPos % 64;
  76. }
  77.  
  78. System.out.println("x:" + nModX + " " + getBase36(nModX));
  79. System.out.println("z:" + nModZ + " " + getBase36(nModZ));
  80.  
  81. String sReturn = p_sWorldFolder;
  82. if(!sReturn.endsWith("\\")){
  83. sReturn += "\\";
  84. }
  85.  
  86. sReturn += getBase36(nModX) + "\\" + getBase36(nModZ) + "\\c." + getBase36(p_xPos) + "." + getBase36(p_zPos) + ".dat";
  87.  
  88. return sReturn;
  89.  
  90. }
  91.  
  92. private static String getBase36(int decimalNumber){
  93.  
  94. boolean bNegative = false;
  95. if(decimalNumber<0){
  96. bNegative = true;
  97. decimalNumber = decimalNumber*-1;
  98. }
  99.  
  100. String tempVal = decimalNumber == 0 ? "0" : "";
  101. int mod = 0;
  102.  
  103. String baseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  104.  
  105. while( decimalNumber != 0 ) {
  106. mod = decimalNumber % 36;
  107. tempVal = baseDigits.substring( mod, mod + 1 ) + tempVal;
  108. decimalNumber = decimalNumber / 36;
  109. }
  110.  
  111. if(bNegative){
  112. tempVal = "-" + tempVal;
  113. }
  114. return tempVal;
  115. }
  116.  
  117.  
  118. private byte[] editBlocksInTag(Tag p_tag){
  119.  
  120. Tag.Type type = p_tag.getType();
  121. if (type == Tag.Type.TAG_End)
  122. return null;
  123.  
  124. String sName = p_tag.getName();
  125.  
  126. if (type == Tag.Type.TAG_Byte_Array) {
  127. if(sName.equals("Blocks")){
  128. Object o = p_tag.getValue();
  129. byte[] b = (byte[]) o;
  130.  
  131. //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
  132.  
  133. //Extinguish any fires in the chunk (id 51) by replacing with air (id 0)
  134.  
  135. int nCount =0;
  136. for(int n=0;n<b.length;n++){
  137. if(b[n]==51){//fire
  138. b[n] = 0; //air
  139. nCount++;
  140. }
  141. }
  142.  
  143. System.out.println(nCount + " fires extinguished!");
  144.  
  145. return b;
  146. }
  147. } else if (type == Tag.Type.TAG_List) {
  148. Tag[] subtags = (Tag[]) p_tag.getValue();
  149. for (Tag st : subtags) {
  150. editBlocksInTag(st);
  151. }
  152.  
  153. } else if (type == Tag.Type.TAG_Compound) {
  154. Tag[] subtags = (Tag[]) p_tag.getValue();
  155. for (Tag st : subtags) {
  156. editBlocksInTag(st);
  157. }
  158.  
  159. } else {
  160.  
  161. }
  162.  
  163. return null;
  164. }
  165.  
  166. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.