/** * Performs an upgrade to a block from an entity. */ public static bool DoBlockUpgrade(Vector3i blockPos, Entity entity, bool requireItems = true) { // Check world is instantiated before trying to proceed. if (GameManager.Instance.World == null) { return(false); } World world = GameManager.Instance.World; // Get the block upgrade path from blocks at this coordinate. Block blockToUpgrade = CoordinateHelper.GetBlockAtCoordinate(world, blockPos); Block upgradeResult; ItemStack upgradeItems = GetBlockUpgradeItems(blockToUpgrade, out upgradeResult); // Check that the block actually has an upgrade result before continuing. if (upgradeResult == null) { return(false); } // If we don't require items, do the straight upgrade and we're done. if (!requireItems) { world.SetBlockRPC(blockPos, Block.GetBlockValue(upgradeResult.GetBlockName())); return(true); } // If items are required, need to get inventory for the entity. Inventory inventory; if (entity is EntityPlayerLocal) { inventory = ((EntityPlayerLocal)entity).inventory; } else if (entity is EntityNPC) { inventory = ((EntityNPC)entity).inventory; } else if (entity is EntityVehicle) { inventory = ((EntityVehicle)entity).inventory; } else { Log.Out("No inventory found for entity."); return(false); } bool removed = false; InventoryHelper.RemoveItemsInInventory(inventory.GetSlots(), upgradeItems, out removed); return(removed); }
/** * Performs a downgrade to a block from an entity. */ public static bool DoBlockDowngrade(Vector3i blockPos) { // Check world is instantiated before trying to proceed. if (GameManager.Instance.World == null) { return(false); } World world = GameManager.Instance.World; // Get the block upgrade path from blocks at this coordinate. Block blockToDowngrade = CoordinateHelper.GetBlockAtCoordinate(world, blockPos); Block downgradeResult = GetDowngradeBlock(blockToDowngrade); // Check that the block actually has an upgrade result before continuing. if (downgradeResult == null) { return(false); } world.SetBlockRPC(blockPos, Block.GetBlockValue(downgradeResult.GetBlockName())); return(true); }