- 318
- 12
Всем привет. Как работать со схематиками в моем моде? Вот мод WorldEdit для схематиков - http://minecraft.curseforge.com/mc-mods/225608-worldedit/files/2219060 .
Follow along with the video below to see how to install our site as a web app on your home screen.
Примечание: This feature may not be available in some browsers.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.util.vector.Matrix2f;
import scala.actors.threadpool.Arrays;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import com.google.common.collect.Lists;
import com.sun.istack.internal.logging.Logger;
/**
* @author nekosune, Agravaine
*/
public class Schematic{
private short width;
private short height;
private short length;
private Materials materials;
private byte[] rawBlocks;
private byte[] blockData;
private byte[] addBlockId;
private short[] blocks;
private List<Entity> entities = Lists.newArrayList();
private List<TileEntity> tileEntities = Lists.newArrayList();
/**
* Load schematic from file.
*
* @param nbtSchematic Schematic NBT.
* @param world The world.
*/
public void loadFromFile(File schematicFile, World world){
try{
InputStream stream = new FileInputStream(schematicFile);
NBTTagCompound schematicNBT = CompressedStreamTools.readCompressed(stream);
loadFromNBT(schematicNBT, world);
}catch(IOException e){
Logger.getLogger("Schematic Loader", Schematic.class).severe("Failed to load schematic file!", e);
}
}
/**
* Load schematic from NBT.
*
* @param nbtSchematic Schematic NBT.
* @param world The world.
*/
public void loadFromNBT(NBTTagCompound nbtSchematic, World world){
setHeight(nbtSchematic.getShort("Height"));
setWidth(nbtSchematic.getShort("Width"));
setLength(nbtSchematic.getShort("Length"));
setMaterials(Materials.valueOf(nbtSchematic.getString("Materials")));
setRawBlocks(nbtSchematic.getByteArray("Blocks"));
if(nbtSchematic.hasKey("AddBlocks")){
setAddBlockId(nbtSchematic.getByteArray("AddBlocks"));
}
blocks = new short[getRawBlocks().length];
if(getAddBlockId() == null){
setAddBlockId(new byte[0]);
}
boolean extra = nbtSchematic.hasKey("Add") || nbtSchematic.hasKey("AddBlocks");
byte extraBlocks[] = null;
byte extraBlocksNibble[] = null;
if(nbtSchematic.hasKey("AddBlocks")){
extraBlocksNibble = nbtSchematic.getByteArray("AddBlocks");
extraBlocks = new byte[extraBlocksNibble.length * 2];
for(int i = 0; i < extraBlocksNibble.length; i++){
extraBlocks[i * 2 + 0] = (byte) ((extraBlocksNibble[i] >> 4) & 0xF);
extraBlocks[i * 2 + 1] = (byte) (extraBlocksNibble[i] & 0xF);
}
}else if(nbtSchematic.hasKey("Add")){
extraBlocks = nbtSchematic.getByteArray("Add");
}
for(int i = 0; i < rawBlocks.length; i++){
blocks[i] = (short)(rawBlocks[i] & 0xFF);
if(extra){
blocks[i] |= (extraBlocks[i] & 0xFF) << 8;
}
}
setBlockData(nbtSchematic.getByteArray("Data"));
List<Entity> entitiesList = new LinkedList<Entity>();
NBTTagList nbtEntitiesList = nbtSchematic.getTagList("Entities", 10);
for(int i = 0; i < nbtEntitiesList.tagCount(); i++){
NBTTagCompound entityNBT = (NBTTagCompound)nbtEntitiesList.getCompoundTagAt(i);
Entity entity = EntityList.createEntityFromNBT(entityNBT, world);
entitiesList.add(entity);
}
setEntities(entitiesList);
List<TileEntity> tileEntitiesList = new LinkedList<TileEntity>();
NBTTagList nbtTileEntitiesList = nbtSchematic.getTagList("TileEntities", 10);
for(int i = 0; i < nbtTileEntitiesList.tagCount(); i++){
NBTTagCompound tileEntityNBT = (NBTTagCompound)nbtTileEntitiesList.getCompoundTagAt(i);
TileEntity tileEntity = TileEntity.createAndLoadEntity(tileEntityNBT);
System.out.println(tileEntity.getPos().toString());
tileEntitiesList.add(tileEntity);
}
setTileEntities(tileEntitiesList);
//System.out.println(width + ", " + height + ", " + length + ", " + materials.name() + ", " + blocks.length + ", " + entities.size() + ", " + tileEntities.size() + "!");
}
/**
* Build schematic structure.
*
* @param world The world.
* @param xPos X position.
* @param yPos Y position.
* @param zPos Z position.
* @param center Translate structure to center.
* @param ignoreAir Ignore air blocks.
*/
public void build(World world, int xPos, int yPos, int zPos, boolean center, boolean ignoreAir){
if(world.isRemote){
return;
}
for(int i = 0; i < width; ++i){
for(int j = 0; j < height; ++j){
for(int k = 0; k < length; ++k){
int index = j * width * length + k * width + i;
BlockPos pos = center ? new BlockPos(i + xPos - (width / 2), j + yPos, k + zPos - (length / 2)) : new BlockPos(i + xPos, j + yPos, k + zPos);
Block block = Block.getBlockById(blocks[index] & 0xFF);
if(ignoreAir){
if(block != Blocks.air){
world.setBlockState(pos, block.getStateFromMeta(blockData[index]));
}
}else{
world.setBlockState(pos, block.getStateFromMeta(blockData[index]));
}
/*if(block instanceof ITileEntityProvider){
for(TileEntity tile : tileEntities){
//System.out.println(pos);
world.setTileEntity(pos, tile);
}
}*/
}
}
}
for(TileEntity tile : tileEntities){
BlockPos tilePos = new BlockPos(xPos, yPos, zPos).add(tile.getPos());
System.out.println(tilePos);
world.removeTileEntity(tilePos);
world.setTileEntity(tilePos, tile);
}
for(Entity entity : entities){
entity.setPositionAndUpdate(xPos + entity.getPosition().getX(), xPos + entity.getPosition().getY(), xPos + entity.getPosition().getZ());
world.spawnEntityInWorld(entity);
}
}
/**
* @return the rawBlocks
*/
public byte[] getRawBlocks(){
return rawBlocks;
}
/**
* @param rawBlocks the schRawBlocks to set
*/
public void setRawBlocks(byte[] schRawBlocks){
rawBlocks = schRawBlocks;
}
/**
* @return the addBlockId
*/
public byte[] getAddBlockId(){
return addBlockId;
}
/**
* @param addBlockId the schAddBlockId to set
*/
public void setAddBlockId(byte[] schAddBlockId){
addBlockId = schAddBlockId;
}
/**
* @param block the schRawBlocks to set
*/
public void setBlocks(short[] schBlocks){
blocks = schBlocks;
}
/**
* @return the width
*/
public short getWidth(){
return width;
}
/**
* @param width the width to set
*/
public void setWidth(short schWidth){
width = schWidth;
}
/**
* @return the height
*/
public short getHeight(){
return height;
}
/**
* @param height the height to set
*/
public void setHeight(short schHeight){
height = schHeight;
}
/**
* @return the length
*/
public short getLength(){
return length;
}
/**
* @param length the length to set
*/
public void setLength(short len){
length = len;
}
/**
* @return the materials
*/
public Materials getMaterials(){
return materials;
}
/**
* @param materials the materials to set
*/
public void setMaterials(Materials mat){
materials = mat;
}
/**
* @return the blockData
*/
public byte[] getBlockData(){
return blockData;
}
/**
* @param blockData the blockData to set
*/
public void setBlockData(byte[] data){
blockData = data;
}
/**
* @return the entities
*/
public List<Entity> getEntities(){
return entities;
}
/**
* @param entities the entities to set
*/
public void setEntities(List<Entity> ent){
entities = ent;
}
/**
* @return the tileEntities
*/
public List<TileEntity> getTileEntities(){
return tileEntities;
}
/**
* @param tileEntities the tileEntities to set
*/
public void setTileEntities(List<TileEntity> tiles){
tileEntities = tiles;
}
/**
* @return the blocks
*/
public short[] getBlocks(){
return blocks;
}
public enum Materials{
Classic,
Alpha,
}
}