下面列出了怎么用net.minecraftforge.common.util.BlockSnapshot的API类实例代码及写法,或者点击链接到github查看源代码。
public CraftBlockState(BlockSnapshot blocksnapshot)
{
this.world = blocksnapshot.world.getWorld();
this.x = blocksnapshot.x;
this.y = blocksnapshot.y;
this.z = blocksnapshot.z;
this.type = net.minecraft.block.Block.getIdFromBlock(blocksnapshot.replacedBlock);
this.light = (byte) blocksnapshot.replacedBlock.getLightValue();
this.chunk = (CraftChunk) this.world.getBlockAt(this.x, this.y, this.z).getChunk();
this.flag = 3;
TileEntity te = this.world.getHandle().getTileEntity(this.x, this.y, this.z);
if (te != null)
{
this.nbt = new NBTTagCompound();
te.writeToNBT(this.nbt);
}
else
{
this.nbt = null;
}
this.createData((byte) blocksnapshot.meta);
}
public boolean place(IBlockState state, EnumFacing direction, EnumHand hand) {
if (!world.isBlockLoaded(blockPos)) return false;
if (spawnProtection) {
if (!world.isBlockModifiable(player, blockPos)) return false;
}
final BlockSnapshot snapshot = BlockSnapshot.getBlockSnapshot(world, blockPos);
if (!world.setBlockState(blockPos, state, blockPlaceFlags)) return false;
if (ForgeEventFactory.onPlayerBlockPlace(player, snapshot, direction, hand).isCanceled()) {
world.restoringBlockSnapshots = true;
snapshot.restore(true, false);
world.restoringBlockSnapshots = false;
return false;
}
return true;
}
public boolean generateTree(Location loc, TreeType type, BlockChangeDelegate delegate) {
world.captureTreeGeneration = true;
world.captureBlockSnapshots = true;
boolean grownTree = generateTree(loc, type);
world.captureBlockSnapshots = false;
world.captureTreeGeneration = false;
if (grownTree) { // Copy block data to delegate
for (BlockSnapshot blocksnapshot : this.world.capturedBlockSnapshots) {
BlockPos position = blocksnapshot.getPos();
int x = position.getX();
int y = position.getY();
int z = position.getZ();
net.minecraft.block.state.IBlockState oldBlock = world.getBlockState(position);
int typeId = net.minecraft.block.Block.getIdFromBlock(blocksnapshot.getReplacedBlock().getBlock());
int data = blocksnapshot.getMeta();
int flag = blocksnapshot.getFlag();
delegate.setTypeIdAndData(x, y, z, typeId, data);
IBlockState newBlock = world.getBlockState(position);
world.markAndNotifyBlock(position, null, oldBlock, newBlock, flag);
}
world.capturedBlockSnapshots.clear();
return true;
} else {
world.capturedBlockSnapshots.clear();
return false;
}
}
public CraftBlockState(BlockSnapshot blocksnapshot) {
this.world = blocksnapshot.getWorld().getWorld();
this.x = blocksnapshot.getPos().getX();
this.y = blocksnapshot.getPos().getY();
this.z = blocksnapshot.getPos().getZ();
this.type = net.minecraft.block.Block.getIdFromBlock(blocksnapshot.getReplacedBlock().getBlock());
this.chunk = (CraftChunk) this.world.getBlockAt(this.x, this.y, this.z).getChunk();
this.flag = 3;
this.nbt = blocksnapshot.getNbt();
this.createData((byte) blocksnapshot.getMeta());
}
/**
* Places the specified block into the world at the specified position if it is possible to do so without violating permission restrictions.
*
* @return <tt>true</tt> if the specified block was successfully placed into the world
*/
public static boolean placeBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull EntityPlayerMP player) {
if (!world.isBlockLoaded(pos)) return false;
if (!hasEditPermission(pos, player)) return false;
BlockEvent.PlaceEvent event = new BlockEvent.PlaceEvent(BlockSnapshot.getBlockSnapshot(world, pos), Blocks.AIR.getDefaultState(), player, player.getActiveHand());
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled()) return false;
world.setBlockState(pos, state);
world.notifyBlockUpdate(pos, state, state, 3);
return true;
}
public boolean generateTree(Location loc, TreeType type, BlockChangeDelegate delegate) {
net.minecraft.world.gen.feature.WorldGenerator gen;
switch (type) {
case BIG_TREE:
gen = new net.minecraft.world.gen.feature.WorldGenBigTree(true);
break;
case BIRCH:
gen = new net.minecraft.world.gen.feature.WorldGenForest(true, false);
break;
case REDWOOD:
gen = new net.minecraft.world.gen.feature.WorldGenTaiga2(true);
break;
case TALL_REDWOOD:
gen = new net.minecraft.world.gen.feature.WorldGenTaiga1();
break;
case JUNGLE:
gen = new net.minecraft.world.gen.feature.WorldGenMegaJungle(true, 10, 20, 3, 3); // Magic values as in BlockSapling
break;
case SMALL_JUNGLE:
gen = new net.minecraft.world.gen.feature.WorldGenTrees(true, 4 + rand.nextInt(7), 3, 3, false);
break;
case JUNGLE_BUSH:
gen = new net.minecraft.world.gen.feature.WorldGenShrub(3, 0);
break;
case RED_MUSHROOM:
gen = new net.minecraft.world.gen.feature.WorldGenBigMushroom(1);
break;
case BROWN_MUSHROOM:
gen = new net.minecraft.world.gen.feature.WorldGenBigMushroom(0);
break;
case SWAMP:
gen = new net.minecraft.world.gen.feature.WorldGenSwamp();
break;
case ACACIA:
gen = new net.minecraft.world.gen.feature.WorldGenSavannaTree(true);
break;
case DARK_OAK:
gen = new net.minecraft.world.gen.feature.WorldGenCanopyTree(true);
break;
case MEGA_REDWOOD:
gen = new net.minecraft.world.gen.feature.WorldGenMegaPineTree(true, rand.nextBoolean());
break;
case TALL_BIRCH:
gen = new net.minecraft.world.gen.feature.WorldGenForest(true, true);
break;
case TREE:
default:
gen = new net.minecraft.world.gen.feature.WorldGenTrees(true);
break;
}
world.captureTreeGeneration = true;
world.captureBlockSnapshots = true;
boolean grownTree = gen.generate(world, rand, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
world.captureBlockSnapshots = false;
world.captureTreeGeneration = false;
if (grownTree) { // Copy block data to delegate
for (BlockSnapshot blocksnapshot : world.capturedBlockSnapshots) {
int x = blocksnapshot.x;
int y = blocksnapshot.y;
int z = blocksnapshot.z;
net.minecraft.block.Block oldBlock = world.getBlock(x, y, z);
int newId = net.minecraft.block.Block.getIdFromBlock(blocksnapshot.replacedBlock);
int data = blocksnapshot.meta;
int flag = blocksnapshot.flag;
delegate.setTypeIdAndData(x, y, z, newId, data);
net.minecraft.block.Block newBlock = world.getBlock(x, y, z);
world.markAndNotifyBlock(x, y, z, null, oldBlock, newBlock, flag);
}
world.capturedBlockSnapshots.clear();
return true;
}
else {
world.capturedBlockSnapshots.clear();
return false;
}
}