refactor SchematicBlock, close #1949

This commit is contained in:
asiekierka 2014-10-19 13:31:47 +02:00
parent 260a57d600
commit ac2a969919

View file

@ -56,9 +56,7 @@ public class SchematicBlock extends SchematicBlockBase {
public void writeToWorld(IBuilderContext context, int x, int y, int z, LinkedList<ItemStack> stacks) {
super.writeToWorld(context, x, y, z, stacks);
// Meta needs to be specified twice, depending on the block behavior
context.world().setBlock(x, y, z, block, meta, 3);
context.world().setBlockMetadataWithNotify(x, y, z, meta, 3);
this.setBlockInWorld(context, x, y, z);
}
@Override
@ -80,37 +78,53 @@ public class SchematicBlock extends SchematicBlockBase {
public void writeToNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.writeToNBT(nbt, registry);
nbt.setInteger("blockId", registry.getIdForBlock(block));
nbt.setInteger("blockMeta", meta);
if (storedRequirements.length > 0) {
NBTTagList rq = new NBTTagList();
for (ItemStack stack : storedRequirements) {
NBTTagCompound sub = new NBTTagCompound();
stack.writeToNBT(sub);
registry.stackToRegistry(sub);
rq.appendTag(sub);
}
nbt.setTag("rq", rq);
}
writeBlockToNBT(nbt, registry);
writeRequirementsToNBT(nbt, registry);
}
@Override
public void readFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
super.readFromNBT(nbt, registry);
try {
block = registry.getBlockForId(nbt.getInteger("blockId"));
} catch (MappingNotFoundException e) {
defaultPermission = BuildingPermission.CREATIVE_ONLY;
return;
readBlockFromNBT(nbt, registry);
readRequirementsFromNBT(nbt, registry);
}
meta = nbt.getInteger("blockMeta");
@Override
public BuildingStage getBuildStage () {
if (block instanceof BlockFalling) {
return BuildingStage.SUPPORTED;
} else if (block instanceof BlockFluidBase || block instanceof BlockLiquid) {
return BuildingStage.EXPANDING;
} else if (block.isOpaqueCube()) {
return BuildingStage.STANDALONE;
} else {
return BuildingStage.SUPPORTED;
}
}
@Override
public BuildingPermission getBuildingPermission() {
return defaultPermission;
}
// Utility functions
protected void setBlockInWorld(IBuilderContext context, int x, int y, int z) {
// Meta needs to be specified twice, depending on the block behavior
context.world().setBlock(x, y, z, block, meta, 3);
context.world().setBlockMetadataWithNotify(x, y, z, meta, 3);
}
protected void readBlockFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
try {
block = registry.getBlockForId(nbt.getInteger("blockId"));
meta = nbt.getInteger("blockMeta");
} catch (MappingNotFoundException e) {
defaultPermission = BuildingPermission.CREATIVE_ONLY;
}
}
protected void readRequirementsFromNBT(NBTTagCompound nbt, MappingRegistry registry) {
if (nbt.hasKey("rq")) {
NBTTagList rq = nbt.getTagList("rq", Constants.NBT.TAG_COMPOUND);
@ -140,21 +154,23 @@ public class SchematicBlock extends SchematicBlockBase {
}
}
@Override
public BuildingStage getBuildStage () {
if (block instanceof BlockFalling) {
return BuildingStage.SUPPORTED;
} else if (block instanceof BlockFluidBase || block instanceof BlockLiquid) {
return BuildingStage.EXPANDING;
} else if (block.isOpaqueCube()) {
return BuildingStage.STANDALONE;
} else {
return BuildingStage.SUPPORTED;
}
protected void writeBlockToNBT(NBTTagCompound nbt, MappingRegistry registry) {
nbt.setInteger("blockId", registry.getIdForBlock(block));
nbt.setInteger("blockMeta", meta);
}
@Override
public BuildingPermission getBuildingPermission() {
return defaultPermission;
protected void writeRequirementsToNBT(NBTTagCompound nbt, MappingRegistry registry) {
if (storedRequirements.length > 0) {
NBTTagList rq = new NBTTagList();
for (ItemStack stack : storedRequirements) {
NBTTagCompound sub = new NBTTagCompound();
stack.writeToNBT(sub);
registry.stackToRegistry(sub);
rq.appendTag(sub);
}
nbt.setTag("rq", rq);
}
}
}