Minecraft Wiki

除另有声明,转载时均必须注明出处若簡繁轉換出錯,請以遊戲內為準请勇于扩充与修正内容有兴趣逛逛我们的微博沟通交流,欢迎到社区专页需要协助,请在告示板留言

了解更多

Minecraft Wiki
Advertisement

要编译并使用此类,请让你的类路径(classpath)包含LevelEditor.java文件和minecraft-server.jar文件的路径。

javac -classpath .:minecraft-server.jar LevelEditor.java
java -classpath .:minecraft-server.jar LevelEditor

java -classpath .:minecraft-server.jar LevelEditor new_server_level.dat


import java.io.*;
import java.text.DateFormat;
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;

public class LevelEditor {
	private com.mojang.minecraft.level.Level level = null;

	LevelEditor() {
	}

	// 从名为filename的文件中加载
	public void load(String filename) {
		FileInputStream fis = null;
		GZIPInputStream gzis = null;
		ObjectInputStream in = null;
		DataInputStream inputstream = null;
		try {
			fis = new FileInputStream(filename);
			gzis = new GZIPInputStream(fis);
			inputstream = new DataInputStream(gzis);
			if((inputstream.readInt()) != 0x271bb788) {
				return;
			}
			if((inputstream.readByte()) > 2) {
				System.out.println("Error: Level version > 2, this is unexpected!");
				return;
			}
			in = new ObjectInputStream(gzis);
			level = (com.mojang.minecraft.level.Level)in.readObject();
			inputstream.close();
			in.close();
			System.out.println("Loading level "+filename+" successful");
		} catch(IOException ex) {
			ex.printStackTrace();
		} catch(ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		level.initTransient();
	}

	// 从名为filename的文件中保存
	public void save(String filename) {
		FileOutputStream fos = null;
		GZIPOutputStream gzos = null;
		ObjectOutputStream out = null;
		DataOutputStream outputstream = null;
		try {
			fos = new FileOutputStream(filename);
			gzos = new GZIPOutputStream(fos);
			outputstream = new DataOutputStream(gzos);
			outputstream.writeInt(0x271bb788);
			outputstream.writeByte(2);
			out = new ObjectOutputStream(gzos);
			out.writeObject(level);
			outputstream.close();
			out.close();
			System.out.println("Saving level "+filename+" successful");
		} catch(IOException ex) {
			ex.printStackTrace();
		}
	}

	// 打印所有除了方块数据之外关于地图的信息
	public void printInfo() {
		if (level == null) {
			return;
		}
		System.out.println("Level info:");
		System.out.println("name: "+level.name);
		System.out.println("creator: "+level.creator);
		System.out.println("createTime: "+(DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL).format(level.createTime)));
		System.out.println("width: "+level.width);
		System.out.println("height: "+level.height);
		System.out.println("depth: "+level.depth);
		System.out.println("spawnpoint: ["+level.xSpawn+","+level.ySpawn+","+level.zSpawn+"]");
		System.out.println("spawn rotation: "+level.rotSpawn);
	}

	// 安全使用的方法,返回值让你知道是否有任何改变
	public boolean setTile(int x, int y, int z, int t) {
		if (
			x >=0 && x < level.width &&
			y >=0 && y < level.depth &&
			z >=0 && z < level.height &&
			t >= 0 && t <= 37
		) {
			if (t == 8 || t == 10) {
				level.setTile(x,y,z,t);
			} else if (t >= 0 && t <= 18) {
				level.setTileNoUpdate(x,y,z,t);
			}
			return true;
		}
		return false;
	}

	// 从方块数组索引中获取地图坐标
	public int[] getCoords(int index) {
		int x = index % level.width;
		index = (index-x) / level.width;
		int z = index % level.height;
		int y = (index-z) / level.height;
		return new int[] {x, y, z};
	}

	public void clearBlocks() {
		for (int i=0; i<level.blocks.length; i++) {
			level.blocks[i] = 0;
		}
	}

	public void floor(int y, int type) {
		for (int i=0; i<level.width; i++) {
		for (int j=0; j<level.height; j++) {
			setTile(i,y,j,type);
		}
		}
	}

	public void wallX(int x1, int x2, int z, int y, int height, int type) {
		for (int i=x1; i<=x2; i++) {
		for (int j=y; j<y+height; j++) {
			if (!setTile(i,j,z,type)) {
				System.out.println("Warning: a tile got ignored while building a wallX: ["+i+","+j+","+z+"]");
			}
		}
		}
	}

	public void wallZ(int x, int z1, int z2, int y, int height, int type) {
		for (int i=z1; i<=z2; i++) {
		for (int j=y; j<y+height; j++) {
			if (!setTile(x,j,i,type)) {
				System.out.println("Warning: a tile got ignored while building a wallZ: ["+x+","+j+","+i+"]");
			}
		}
		}
	}

	// 从‘from’到‘to’替换所有方块类型,返回已更改的方块数
	public int substitute(byte from, byte to) {
		int count=0;
		for (int i=0; i<level.blocks.length; i++) {
			if (level.blocks[i] == from) {
				level.blocks[i] = to;
				count++;
			}
		}
		return count;
	}

	public void setSize(int x, int y, int z) {
		level.setData(x, y, z, new byte[x*y*z]);
	}

	public static void main(String [] args) {
		LevelEditor le = new LevelEditor();
		String filename = "server_level.dat";
		if(args.length > 0) {
			filename = args[0];
			le.load(filename);
			if (le.level == null) {
				System.out.println("Loading level "+filename+" failed");
				return;
			}
		} else {
			le.level = new com.mojang.minecraft.level.Level();
		}
		// 在这里做一些各种的编辑

		// 设置自定义尺寸:256宽,128高,512长
		le.setSize(256, 128, 512);

		// 首先,让我们清空一下这个字段
		le.clearBlocks();

		// 添加一堆墙来填充地图的下半部分
		for (int i=0; i<le.level.width; i++) {
			le.wallX(0,le.level.height-1,i,0,(int)le.level.getWaterLevel()-1,3);
		}
		// 铺下地板
		le.floor((int)le.level.getWaterLevel()-1,2);

		// 让地图为我们找到一个出生位置
		le.level.findSpawn();

		// 留下我们的指纹
		le.level.creator = "Minecrafter";
		le.level.name = "A Custom World";
		le.level.createTime = System.currentTimeMillis();

		le.save(filename);
		le.printInfo();
	}
}

你还可以使用此类来帮助获取不同方块类型的正确字节

   public class Blocks 
   {
  	public static final byte air 				= (byte)0;
  	public static final byte rock 				= (byte)1;
  	public static final byte grass 				= (byte)2;
  	public static final byte dirt 				= (byte)3;
  	public static final byte stone 				= (byte)4;
  	public static final byte wood 				= (byte)5;
  	public static final byte shrub 				= (byte)6;
  	public static final byte blackrock 			= (byte)7;
  	public static final byte water 				= (byte)8;
  	public static final byte waterstill 			= (byte)9;
  	public static final byte lava 				= (byte)10;
  	public static final byte lavastill 			= (byte)11;
  	public static final byte sand 				= (byte)12;
  	public static final byte gravel 			= (byte)13;
  	public static final byte goldrock 			= (byte)14;
  	public static final byte ironrock 			= (byte)15;
  	public static final byte coal 				= (byte)16;
  	public static final byte trunk 				= (byte)17;
  	public static final byte leaf 				= (byte)18;
  	public static final byte sponge 			= (byte)19;
  	public static final byte glass 				= (byte)20;
  	public static final byte red 				= (byte)21;
  	public static final byte orange 			= (byte)22;
  	public static final byte yellow 			= (byte)23;
  	public static final byte lightgreen 			= (byte)24;
  	public static final byte green 				= (byte)25;
  	public static final byte aquagreen 			= (byte)26;
  	public static final byte cyan 				= (byte)27;
  	public static final byte lightblue 			= (byte)28;
  	public static final byte blue 				= (byte)29;
  	public static final byte purple 			= (byte)30;
  	public static final byte lightpurple 			= (byte)31;
  	public static final byte pink 				= (byte)32;
  	public static final byte darkpink 			= (byte)33;
  	public static final byte darkgrey 			= (byte)34;
  	public static final byte lightgrey 			= (byte)35;
  	public static final byte white 				= (byte)36;
  	public static final byte yellowflower 			= (byte)37;
  	public static final byte redflower 			= (byte)38;
  	public static final byte mushroom 			= (byte)39;
  	public static final byte redmushroom 			= (byte)40;
  	public static final byte goldsolid 			= (byte)41;
  	public static final byte iron 				= (byte)42;
  	public static final byte staircasefull 			= (byte)43;
  	public static final byte staircasestep 			= (byte)44;
  	public static final byte brick 				= (byte)45;
  	public static final byte tnt 				= (byte)46;
  	public static final byte bookcase 			= (byte)47;
  	public static final byte stonevine 			= (byte)48;
  	public static final byte obsidian 			= (byte)49;
   }

语言

Advertisement