@Override public SpellResult perform(CastContext context) { Block target = context.getTargetBlock(); if (requireSapling && target.getType() != Material.SAPLING) { return SpellResult.NO_TARGET; } if (!context.hasBuildPermission(target)) { return SpellResult.INSUFFICIENT_PERMISSION; } World world = context.getWorld(); Location treeLoc = new Location(world, target.getX(), target.getY() + 1, target.getZ(), 0, 0); Random random = context.getRandom(); TreeType useType = null; if (treeType != null) { useType = treeType; } else if (biomeMap != null) { Biome biome = treeLoc.getWorld().getBiome(treeLoc.getBlockX(), treeLoc.getBlockZ()); List<TreeType> types = biomeMap.get(biome); if (types != null) { useType = types.get(random.nextInt(types.size())); } } if (useType == null) { useType = TreeType.values()[random.nextInt(TreeType.values().length)]; } UndoList restoreOnFail = new UndoList(context.getMage(), context.getSpell().getName()); Block treeBlock = treeLoc.getBlock(); if (!context.isDestructible(treeBlock)) { return SpellResult.NO_TARGET; } restoreOnFail.add(treeBlock); treeLoc.getBlock().setType(Material.AIR); boolean result = world.generateTree(treeLoc, useType); if (!result) { UndoList undoList = new UndoList(context.getMage(), context.getSpell().getName()); for (int z = -2; z <= 2; z++) { for (int x = -2; x <= 2; x++) { Block clearBlock = treeBlock.getRelative(x, 0, z); Block lowerBlock = clearBlock.getRelative(BlockFace.DOWN); if (context.isDestructible(clearBlock) && lowerBlock.getType() != target.getType()) { undoList.add(lowerBlock); lowerBlock.setType(target.getType()); } if (x == 0 && z == 0) continue; if (!context.isDestructible(clearBlock)) continue; restoreOnFail.add(clearBlock); clearBlock.setType(Material.AIR); } } result = world.generateTree(treeLoc, useType); context.addWork(100); undoList.undo(true); } if (result) { context.addWork(500); } else { context.addWork(100); restoreOnFail.undo(true); } return result ? SpellResult.CAST : SpellResult.FAIL; }